From 266211ef37e3bb9db8d533154ea0169bd0219545 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 12 Aug 2021 15:54:21 +0800 Subject: [PATCH] =?UTF-8?q?2.=2001=E8=83=8C=E5=8C=85=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7078590/ --- AcWing/2/2.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/AcWing/2/2.cpp b/AcWing/2/2.cpp index 1ec0785d..b6db955f 100644 --- a/AcWing/2/2.cpp +++ b/AcWing/2/2.cpp @@ -2,7 +2,7 @@ using namespace std; -int n, m, v[1005], w[1005], f[1005][1005]; +int n, m, v[1005], w[1005], f[1005]; int main() { cin >> n >> m; @@ -10,10 +10,10 @@ int main() { cin >> v[i] >> w[i]; } for (int i = 1; i <= n; i++) { - for (int j = 0; j <= m; j++) { - f[i][j] = max(f[i - 1][j], j >= v[i] ? f[i - 1][j - v[i]] + w[i] : f[i - 1][j]); + for (int j = m; j >= v[i]; j--) { + f[j] = max(f[j], f[j - v[i]] + w[i]); } } - cout << f[n][m] << endl; + cout << f[m] << endl; return 0; }