Cod sursa(job #2416838)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 28 aprilie 2019 12:36:45
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>

using namespace std;

#define w first
#define p second

pair<int, int> obj[5005];
int dp[10005];

int main()
{
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");
    int n, G;
    fin >> n >> G;
    for(int i = 1; i <= n; ++i)
        fin >> obj[i].w >> obj[i].p;
    for(int i = 1; i <= G; ++i) dp[i] = -1e9;
    int ans = 0;
    for(int i = 1; i <= n; ++i){
        for(int j = G; j >= obj[i].w; --j){
            dp[j] = max(dp[j], dp[j - obj[i].w] + obj[i].p);
            ans = max(ans, dp[j]);
        }
    }
    fout << ans;
    return 0;
}