# why is my approach wrong ?? knapsack problem

http://www.spoj.com/problems/PARTY/

i know this approach is not correct but why ???

``````//Data Structure includes
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<string>
#include<ctime>

//Other Includes
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cctype>

using namespace std;

#define s(n)                                    scanf("%d",&n)
#define sl(n)                                   scanf("%lld",&n)
#define sf(n)                                   scanf("%lf",&n)
#define ss(n)                                   scanf("%s",n)
#define p(n)                                    printf("%d\n",n)
#define pl(n)                                   printf("%lld\n",n)
#define maX(a,b)                                ((a)>(b)?(a):(b))
#define miN(a,b)                                ((a)<(b)?(a):(b))
#define abS(x)                                  ((x)<0?-(x):(x))
#define FOR(i,a,b)                              for(int i=a;i<b;i++)
#define mp                                      make_pair
#define FF                                      first
#define SS                                      second
#define pb                                      push_back
#define SZ(v)                                   ((int)(v.size()))
#define all(x)                                  x.begin(),x.end()
#define INF                                     (int)1e9
#define LINF                                    (long long)1e18
#define EPS                                     1e-9
#define MOD                                     ((1 << 30)-1))
#define MSZ                                     80

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<float,float> PFF;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;

{
register char c=0;
while (c<33) c=getchar_unlocked();
*a=0;
while (c>33)
{
*a=*a*10+c-'0';
c=getchar_unlocked();
}
}*/

int main()
{
#ifndef ONLINE_JUDGE
freopen ("input.txt", "r", stdin);
//freopen ("output.txt", "w", stdout);
#endif
while(1)
{
int amount,n,fee[101],fun[101],dp[600];
memset(dp,0,sizeof(dp));
s(amount);
s(n);
if(amount==0 && n==0)
break;
FOR(i,0,n)
{
s(fee[i]);
s(fun[i]);
}
FOR(i,1,amount+1)
{
FOR(j,0,n)
{
if(( fee[j]<=i )&& (dp[i]<(fun[j]+dp[i-fee[j]])))
dp[i]=fun[j]+dp[i-fee[j]];
}
}
int ans;
for(ans=amount;ans>=0;)
{
if(dp[amount]==dp[ans])
{
ans--;
}
else
break;
}
printf("%d %d\n",ans+1,dp[ans+1]);

}
return 0;
}
``````
//