Cod sursa(job #3209154)

Utilizator Sabin1133Padurariu Sabin Sabin1133 Data 2 martie 2024 07:07:22
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>

struct obj {
    int weight;
    int profit;
};

int main()
{
    int n, W;
    int max_profit = 0;
    std::vector<struct obj> objs;
    std::vector<int> total_profit;

    std::ifstream fin("rucsac.in");

    fin >> n >> W;

    objs.resize(n);
    dp.resize(W + 1, std::vector<int>(n + 1, 0));

    for (int w, p, i = 0; i < n; ++i) {
        fin >> w >> p;
        objs[i].weight = w;
        objs[i].profit = p;
    }

    fin.close();

    for (int i = 0; i < n; ++i)
        for (int w = W - objs[i].weight; w >= 0; --w)
            if (total_profit[w + objs[i].weight] < total_profit[w] + objs[i].profit) {
                total_profit[w + objs[i].weight] = total_profit[w] + objs[i].profit
                max_profit = std::max(max_profit, total_profit[w + objs[i].weight]);
            }
            

    std::ofstream fout("rucsac.out");

    fout << dp[W][n] << '\n';

    fout.close();

    return 0;
}