Cod sursa(job #3228213)

Utilizator Cristi3956Pop Cristian Cristi3956 Data 6 mai 2024 19:08:55
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
long long n, G, gr[5001], val[5001], dp[10001];
int main()
{
    fin>>n>>G;
    for(int i = 1; i <= n; i++)
        cin>>gr[i]>>val[i];

    long long Pmax = 0; //profitul maxim
    for(int i = 1; i <= n; i++)
        for(int j = G - gr[i]; j >= 0; j--)
        {
            if(dp[j+gr[i]] < dp[j] + val[i])
            {
                dp[j+gr[i]] = dp[j] + val[i];
                if(dp[j+gr[i]] > Pmax)
                    Pmax = dp[j+gr[i]];
            }
        }
    fout << Pmax;
    return 0;
}