二进制分解成01背包
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
int dp[100005];
vector<int> v;
int N,cash;
int main()
{
while(~scanf("%d%d",&cash,&N))
{
memset(dp,0,sizeof(dp));
v.clear();
while(N--)
{
int vv,sum;
scanf("%d%d",&sum,&vv);
for(int i=1; i<=sum; i<<=1)
{
v.push_back(i*vv);
sum-=i;
}
if(sum)v.push_back(sum*vv);
}
for(int i=0;i<v.size();i++)
for(int j=cash;j>=v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
printf("%d\n",dp[cash]);
}
return 0;
}