提交时间:2023-12-09 08:38:13
运行 ID: 114707
#include <iostream> #include <vector> using namespace std; int main() { int M, N; cin >> M >> N; vector<int> weight(N), value(N); for (int i = 0; i < N; i++) { cin >> weight[i] >> value[i]; } vector<int> dp(M + 1, 0); // dp[i]表示背包容量为i时的最大总价值 for (int i = 0; i < N; i++) { for (int j = weight[i]; j <= M; j++) { dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); } } cout << "max="<< dp[M] << endl; return 0; }