提交时间:2023-12-02 08:48:16

运行 ID: 113303

#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; const int MAXN = 2e5 + 1e2; int w[MAXN]; int v[MAXN]; int f[MAXN]; int m, n; int main() { scanf("%d %d", &m, &n); for (int i = 1;i <= n;i++) { scanf(" %d", &w[i]); v[i] = w[i]; } for (int i = 1;i <= n;i++) for (int j = m;j >= w[i];j--) if (f[j - w[i]] + v[i] > f[j]) f[j] = f[j - w[i]] + v[i]; printf("%d", m - f[m]); return 0; }