Cod sursa(job #2279896)

Utilizator k.bruenDan Cojocaru k.bruen Data 10 noiembrie 2018 10:18:25
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#include <vector>

using std::vector;

std::ifstream fin("rucsac.in");
std::ofstream fout("rucsac.out");

//void generator(int max, void f(int nr)) {
//    int a = 0;
//    int b = 1;
//    while (a < max) {
//        f(a);
//        int c = a + b;
//        a = b;
//        b = c;
//    }
//}

int main() {
//    generator(100, [](int nr) -> void {
//        fout << nr << '\n';
//    });
    
    int weight, price;
    int count, max_weight;
    fin >> count >> max_weight;
    
    vector<int> dp(max_weight + 1, 0);
    
    while (fin >> weight >> price) {
        vector<int> new_dp;
        for (int i = 0; i <= max_weight; i++) {
            if (i < weight) new_dp.push_back(dp[i]);
            else new_dp.push_back(dp[i] > dp[i - weight] + price ? dp[i] : dp[i - weight] + price);
        }
        dp = new_dp;
    }
    
    fout << dp[max_weight];
}