i have applied knapsack algorithm on notes >>
it is working for all test cases …
here is my code >>
#include<iostream>
#include<algorithm>
#define max(a,b) (a>b)?a:b
using namespace std;
int main()
{
int t,non,amount;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&non,&amount);
int arr[non+1],i,w;
for(i=1;i<=non;i++)
{
scanf("%d",&arr[i]);
}
sort(arr,arr+non);
int V[non+1][amount+1];
for(w=0;w<=amount;w++)
V[0][w]=0;
for(w=0;w<=non;w++)
V[w][0]=0;
for(i=1;i<=non;i++)
for(w=1;w<=amount;w++)
{
if(arr[i]<=w) //weight of this item is less than the sub problem weight
{V[i][w]=max(V[i-1][w],arr[i]+V[i-1][w-arr[i]]);}
else
{V[i][w]=V[i-1][w];}
}
if(V[non][amount] == amount)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}