Cod sursa(job #2630864)

Utilizator sebimihMihalache Sebastian sebimih Data 27 iunie 2020 16:25:10
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

int n, gmax;

int main()
{
    fin >> n >> gmax;
    vector<int> greutate(n), profit(n);
    for (int i = 0; i < n; i++)
    {
        fin >> greutate[i] >> profit[i];
    }

    vector<vector<int>> dp(2, vector<int>(gmax + 1));

    int l = 0;
    for (int i = 1; i <= n; i++, l = 1 - l)
    {
        for (int j = 1; j <= gmax; j++)
        {
            dp[l][j] = dp[1 - l][j];

            if (j >= greutate[i - 1])
            {
                dp[l][j] = max(dp[l][j], dp[1 - l][j - greutate[i - 1]] + profit[i - 1]);
            }
        }
    }

    fout << dp[1 - l][gmax];
}