洛谷 P1060 开心的金明(0/1背包)

题目链接:

https://www.luogu.com.cn/problem/P1060

思路:

背包裸题

代码:

#include<bits/stdc++.h>

using namespace std;

const int maxn = 3e4 + 5;
int n, m, v[maxn], p[maxn];
int dp[maxn];

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= m; ++i) {
    
    
		scanf("%d %d", &v[i], &p[i]);
	}
	for(int i = 1; i <= m; ++i)
	for(int j = n; j >= v[i]; --j) {
    
    
		dp[j] = max(dp[j], dp[j - v[i]] + v[i] * p[i]);
	}
	printf("%d", dp[n]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104674804