Cod sursa(job #2449163)

Utilizator Ionut28Porumb Palincas Ionut Ionut28 Data 18 august 2019 13:57:07
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, g, dp[2][10005], w, p, L;
int main()
{
    fin >> n >> g;
    L = 1;
    for(int i = 1; i <= n; ++i, L = 1 - L)
    {
        fin >> w >> p;
        for(int j = 0; j <= g; ++j)
        {
            dp[L][j] = dp[1 - L][j];
            if(j >= w)
                dp[L][j] = max(dp[L][j], dp[1 - L][j - w] +  p);
        }
    }
    int sol = 0;
    L = 1 - L;
    for(int i = 1; i <= g; ++i)
        sol = max(sol, dp[L][i]);
    fout << sol;
    return 0;
}