题干
C++实现
cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
using namespace std;
int main() {
int c, n;
scanf("%d%d", &c, &n);
int p[101];
int v[101];
for (int i = 0; i < n; i++)
{
scanf("%d%d", &p[i], &v[i]);
}
//dp[x][y]:在总金额不超过x的情况下,考察0~y-1号商品的最大评分
int dp[1001][102];
for (int y = 0; y <= n; y++)
{
dp[0][y] = 0;
}
for (int x = 0; x <= c; x++)
{
dp[x][0] = 0;
}
for (int x = 1; x <= c; x++)
{
for (int y = 1; y <= n; y++) {
if (x - p[y - 1] < 0) {
dp[x][y] = dp[x][y - 1];
}
else {
dp[x][y] = max(dp[x][y - 1], dp[x - p[y - 1]][y - 1] + v[y - 1]);
}
}
}
printf("%d\n", dp[c][n]);
return 0;
}