Cod sursa(job #3260407)

Utilizator DarkWoundsPaduraru Natalia Maria DarkWounds Data 2 decembrie 2024 11:14:23
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, g[5003], v[5003], G;
int dp[2][10003];

int main()
{
    int i, j, profitMax = 0, L0, L1;
    in >> n >> G;
    for (i = 1; i <= n; i++)
        in >> g[i] >> v[i];
    L0 = 0; L1 = 1;
    dp[L0][g[1]] = v[1];
    for (i = 2; i <= n; i++)
    {
        for (j = 1; j <= G; j++)
            if(g[i] <= j) dp[L1][j] = max(dp[L0][j], dp[L0][j-g[i]]+v[i]);
            else dp[L1][j] = dp[L0][j];
        swap(L0, L1);
    }
    for(j = 1; j <= G; j++)
        profitMax = max(profitMax,dp[L0][j]);
    out << profitMax << "\n";
    return 0;
}