#include<bits/stdc++.h>
#define pi acos (-1.0)
using namespace std;
int n,amount;
int dp[500][12000];
typedef int (*compfn)(const void*, const void*);
struct s
{
int tk,val;
}a[20000];
int compare(struct s *elem1, struct s *elem2)
{
if(elem1->tk>elem2->tk)
return 1;
else if(elem1->tk<elem2->tk)
return -1;
}
int func(int i,int w)
{
if(i==n) return 0;
if(dp[i][w]!=-1) return dp[i][w];
int profit1=0,profit2=0;
if((w+a[i].tk<=amount)||(w+a[i].tk>2000)&&(w+a[i].tk<=amount+200))
{
profit1=a[i].val+func(i+1,w+a[i].tk);
}
profit2=func(i+1,w);
dp[i][w]=max(profit1,profit2);
return dp[i][w];
}
int main()
{
int test,k,i;
memset(dp,-1,sizeof(dp));
while(cin>>amount>>n)
{
for(i=0;i<n;i++)
{
cin>>a[i].tk>>a[i].val;
}
qsort(a,n,sizeof(struct s),(compfn)compare);
cout<<func(0,0)<<endl;
memset(dp,-1,sizeof(dp));
}
return 0;
}
#define pi acos (-1.0)
using namespace std;
int n,amount;
int dp[500][12000];
typedef int (*compfn)(const void*, const void*);
struct s
{
int tk,val;
}a[20000];
int compare(struct s *elem1, struct s *elem2)
{
if(elem1->tk>elem2->tk)
return 1;
else if(elem1->tk<elem2->tk)
return -1;
}
int func(int i,int w)
{
if(i==n) return 0;
if(dp[i][w]!=-1) return dp[i][w];
int profit1=0,profit2=0;
if((w+a[i].tk<=amount)||(w+a[i].tk>2000)&&(w+a[i].tk<=amount+200))
{
profit1=a[i].val+func(i+1,w+a[i].tk);
}
profit2=func(i+1,w);
dp[i][w]=max(profit1,profit2);
return dp[i][w];
}
int main()
{
int test,k,i;
memset(dp,-1,sizeof(dp));
while(cin>>amount>>n)
{
for(i=0;i<n;i++)
{
cin>>a[i].tk>>a[i].val;
}
qsort(a,n,sizeof(struct s),(compfn)compare);
cout<<func(0,0)<<endl;
memset(dp,-1,sizeof(dp));
}
return 0;
}
No comments:
Post a Comment