Cod sursa(job #2940471)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 15 noiembrie 2022 17:40:08
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>
using namespace std;
#define FMOD(x) if(x >= MOD) x-= MOD;

using ll = long long;
using ull = uint64_t;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using tl = tuple<ll, ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpl = vector<pl>;
using vti = vector<ti>;
using vtl = vector<tl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvpi = vector<vpi>;
using vvpil = vector<vpil>;
using vvpli = vector<vpli>;
using vvpl = vector<vpl>;
using vvti = vector<vti>;
using vvtl = vector<vtl>;

#if 1
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
#define cin fin
#define cout fout
#endif
const int nmx = 5e3 + 1;
const int gmx = 1e4 + 1;
int dp[nmx][gmx]; // i - ultimul element luat, j - suma curenta
int w[nmx],p[nmx];
int main()
{
    int n,g;
    cin >> n >> g;
    for(int i=0;i<n;i++)
        cin >> w[i] >> p[i];
    
    for(int i=0;i<n;i++)
        for(int j=0;j<g;j++)
        {
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            if(j+w[i] <= g)
            dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]], dp[i][j] + p[i]);
        }
    cout << dp[n][g];
}