提交时间:2023-04-08 08:58:42
运行 ID: 73621
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; const int MAXN = 30 + 5; int v[MAXN] = {0}; int f[MAXN][MAXN]; int w, n; int main() { scanf("%d", &w); scanf(" %d", &n); for (int i = 1;i <= n;i++) scanf(" %d", &v[i]); for (int i = 1;i <= n;i++) { for (int j = 1;j <= w;j++) { if (j >= v[i]) f[i][j] = max(f[i - 1][j], f[i][j - v[i]] + v[i]); else f[i][j] = f[i - 1][j]; } } printf("%d", w - f[n][w]); return 0; }