Cod sursa(job #2644117)

Utilizator Katherine456719Swan Katherine Katherine456719 Data 23 august 2020 15:24:03
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <bits/stdc++.h>

using namespace std;

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

int dp[10005];
vector< pair <int,int>> v;

int main()
{
    int n,g;
    f>>n>>g;
    for(int i=1; i<=n; i++)
    {
        int x,y;
        f>>x>>y;
        v.push_back({x,y});
    }
    for(int j = 0; j <= g; j++)
        dp[j]=0;
    for(int i=0; i<n; i++)
        for(int j=g; j>=v[i].first; --j)
            dp[j]=max(dp[j],dp[j-v[i].first]+v[i].second);
    fout<<dp[g];
    return 0;
}