其实不用存着w和v的
贴出代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 1111;
int N;
int V;
int f[maxn][maxn];
int main()
{
while (scanf("%d%d", &V, &N) != EOF)
{
int v, w;
for (int i = 1; i <= N; i++)
{
scanf("%d%d", &v, &w);
for (int j = 0; j <= V; j++)
{
f[i][j] = (i == 1) ? 0 : f[i - 1][j];
if (j >= v)
{
f[i][j] = max(f[i][j], f[i - 1][j - v] + w);
}
}
}
printf("%d\n", f[N][V]);
}
system("pause");
return 0;
}