洛谷P2871 [USACO07DEC]手链Charm Bracelet

01背包,想了解背包的可以看看这个https://oi-wiki.org/dp/knapsack/

#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[3403] = { 0 };
    int value[3403] = { 0 };
    for (int i = 0; i < n; i++){
        cin >> cost[i] >> value[i];
    }
    int pos[12881] = { 0 };
    for (int i = 0; i < n; i++){
        for (int j = m; j >= cost[i]; j--){
            pos[j] = max(pos[j], pos[j - cost[i]] + value[i]);
        }
    }
    cout << pos[m] << endl;


    return 0;
}

 

posted @ 2020-02-09 19:48  Vetsama  阅读(87)  评论(0)    收藏  举报