Cod sursa(job #2299846)

Utilizator RaresLiscanLiscan Rares RaresLiscan Data 10 decembrie 2018 11:43:38
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

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

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

int main()
{
    int n, g;
    fin >> n >> g;
    V.push_back(pair <int, int> (0, 0));
    for (int i = 1; i <= n; i ++) {
        int a, b;
        fin >> a >> b;
        V.push_back(pair <int, int> (a, b));
    }
    dp[V[1].first] = V[1].second;
    for (int k = 2; k <= n; k ++) {
        for (int i = g; i > 0; i --) {
            if (i - V[k].first >= 0) dp[i] = max(dp[i], dp[i - V[k].first] + V[k].second);
        }
    }
    int maxim = 0;
    for (int i = 1; i <= g; i ++) {
        if (maxim < dp[i]) maxim = dp[i];
    }
    fout << maxim;
    return 0;
}