#include<iostream>
using
namespace
std;
int
main()
{
int
v,n;
cin>>v>>n;
int
w,c;
int
dp[1010];
for
(
int
i=1;i<=n;i++)
{
cin>>w>>c;
for
(
int
j=v;j>=w;j--)
{
dp[j]=max(dp[j],dp[j-w]+c);
}
}
cout<<dp[v]<<endl;
return
0;
}
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
using
namespace
std;
int
m,n,we[40],jz[40],dp[300],maxn;
int
main()
{
scanf
(
"%d%d"
,&m,&n);
for
(
int
i = 1;i <= n;i++)
scanf
(
"%d%d"
,&we[i],&jz[i]);
for
(
int
i = 1;i <= n;i++)
for
(
int
j = we[i];j <= m;j++)
dp[j] = max(dp[j],dp[j - we[i]] + jz[i]);
for
(
int
i = 1;i <= m;i++)
maxn = max(maxn,dp[i]);
printf
(
"max=%d"
,maxn);
return
0;
}
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using
namespace
std;
int
v,n,sz[30];
long
long
dp[11000];
void
swork()
{
for
(
int
i = 1;i <= v;i++)
{
for
(
int
j = 1;j <= n;j++)
{
if
(j - sz[i] >= 0)dp[j] = dp[j - sz[i]] + dp[j];
}
}
}
int
main()
{
scanf
(
"%d%d"
,&v,&n);
for
(
int
i = 1;i <= v;i++)
scanf
(
"%d"
,&sz[i]);
dp[0] = 1;
swork();
printf
(
"%lld\n"
,dp[n]);
return
0;
}
原文:https://www.cnblogs.com/Chri-K/p/13640290.html