Cod sursa(job #2942003)

Utilizator LukyenDracea Lucian Lukyen Data 18 noiembrie 2022 17:58:17
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>
using namespace std;

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

int main()
{
    int n, wMax;

    fin >> n >> wMax;

    vector<pair<int, int>> vec(n + 1);
    for (int i = 1; i <= n; i++)
        fin >> vec[i].first >> vec[i].second;

    vector<vector<int>> best(2, vector<int>(wMax + 1, 0));
    for (int i = 1; i <= n; i++)
        for (int w = 1; w <= wMax; w++)
            if (w >= vec[i].first)
                best[i%2][w] = max(best[(i - 1) % 2][w], best[(i - 1) % 2][w - vec[i].first] + vec[i].second);
            else
                best[i%2][w] = best[(i - 1) % 2][w];

    fout << best[n % 2][wMax];

    return 0;
}