题目链接:https://vjudge.net/contest/126708#problem/G
这是一个01背包的模板题
AC代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int dp[100005]; 6 struct Node 7 { 8 int xishouliang; 9 int xingfuzhi; 10 } a[100005]; 11 int main() 12 { 13 int i, j, t, m; 14 while(~scanf("%d", &t)) 15 { 16 memset(dp,0,sizeof(dp)); 17 for (i = 1; i <= t; i++) 18 scanf("%d%d", &a[i].xingfuzhi, &a[i].xishouliang); 19 scanf("%d",&m); 20 for (i = 1; i <= t; i++) 21 { 22 for (j = a[i].xishouliang; j <= m; j++) 23 { 24 dp[j] = max(dp[j], dp[j - a[i].xishouliang] + a[i].xingfuzhi); 25 } 26 } 27 printf("%d ", dp[m]); 28 } 29 return 0; 30 }