洛谷P1060 开心的金明

题目一看就是标准的01背包,只需要在计算前把价格和重要度先计算了就好

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

int max(int a, int b){
    return a > b ? a : b;
}

int main()
{
    int n, m;
    cin >> n >> m;
    int cost[26] = { 0 };
    int value[26] = { 0 };
    for (int i = 0; i < m; i++){
        int t;
        cin >> cost[i] >> t;
        value[i] = t*cost[i];
    }
    int temp[30001] = { 0 };
    for (int i = 0; i < m; i++){
        for (int j = n; j - cost[i] >= 0; j--){
            temp[j] = max(temp[j], temp[j - cost[i]] + value[i]);
        }
    }
    cout << temp[n] << endl;



    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Vetsama/p/12288290.html