![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/96141e8ed9fd9101cdf408caee57a655.png)
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y); memset(x,y,sizeof(x))
#define mv 2147483648+30
using namespace std;
ll gar(ll a,ll b){
return b==0?a:gar(b,a%b);
}
struct node{
int x,y;
}z[30030];
int main(){
ll m,n;
while(cin>>m>>n){
int dp[30030];mes(dp,0);
for(int i=0;i<n;i++){
cin>>z[i].x>>z[i].y;
}
int i,j;
for(i=0;i<n;i++){
for(j=m;j>z[i].x;j--){
dp[j]=max(dp[j],dp[j-z[i].x]+z[i].y*z[i].x);
}
}
cout<<dp[m]<<endl;
}
}