题解 | #【模板】01背包#
【模板】01背包
https://www.nowcoder.com/practice/9bb79a902fb74ec9adde6e4e8fd1a5d1
01背包模板
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 5; int __t = 1, n, m; void solve() { cin >> n >> m; vector<int> w(n + 1), v(n + 1), dp(m + 1); for (int i = 1; i <= n; i++) cin >> w[i] >> v[i]; for (int i = 1; i <= n; i++) for (int j = m; j >= w[i]; j--) dp[j] = max(dp[j], dp[j - w[i]] + v[i]); cout << dp[m] << '\n'; return; } int32_t main() { #ifdef ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); #endif cin >> __t; while (__t--) solve(); return 0; }