Cod sursa(job #2738373)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 5 aprilie 2021 19:21:37
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;

const string FILENAME = "strmatch";
ifstream fin(FILENAME + ".in");
ofstream fout(FILENAME + ".out");

#define pb push_back
#define mp make_pair

using ll = long long;

const int nmax = 5005;
int n, gmx;
int g[nmax], p[nmax];
int dp[nmax][10005];

int main()
{
    fin >> n >> gmx;
    for(int i = 1; i <= n; i++)
        fin >> g[i] >> p[i];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= gmx; j++)
            if(g[i] > j)
                dp[i][j] = dp[i - 1][j];
            else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - g[i]] + p[i]);
    fout << dp[n][gmx] << "\n";
    fin.close();
    fout.close();
	return 0;
}