Cod sursa(job #2866792)

Utilizator tomaionutIDorando tomaionut Data 9 martie 2022 23:23:06
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, G, sol;
int g[5005], v[5005];
int dp[2][10005];
int main()
{
    int i, j, ind;
    fin >> n >> G;
    for (i = 1; i <= n; i++)
        fin >> g[i] >> v[i];

    ind = 1;
    for (i = 1; i <= n; i++, ind ^= 1)
    {
        for (j = G; j >= 0; j--)
            if (j >= g[i])
                dp[ind][j] = max(dp[ind ^ 1][j], dp[ind ^ 1][j - g[i]] + v[i]);
            else
                dp[ind][j] = dp[ind ^ 1][j];
    }
    ind ^= 1;
    fout << dp[ind][G] << "\n";

    return 0;
}