Cod sursa(job #2577256)

Utilizator preda.andreiPreda Andrei preda.andrei Data 8 martie 2020 20:44:39
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <algorithm>
#include <fstream>
#include <vector>

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

    int n, capacity;
    fin >> n >> capacity;

    std::vector<int> profit(capacity + 1, -1);
    profit[0] = 0;

    for (int i = 0; i < n; i += 1) {
        int weight, value;
        fin >> weight >> value;

        for (int c = capacity - weight; c >= 0; c -= 1) {
            if (profit[c] >= 0) {
                profit[c + weight] = std::max(profit[c + weight],
                                              profit[c] + value);
            }
        }
    }

    fout << *max_element(profit.begin(), profit.end()) << "\n";
    return 0;
}