Cod sursa(job #3158053)

Utilizator LSSPLYAntochi Alessio LSSPLY Data 17 octombrie 2023 17:16:56
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 cin fin
#define cout fout
ifstream  fin("rucsac.in");
ofstream fout("rucsac.out");
int n, g, b[10001];
struct obj {
    int w, p;
}a[5001];
void solve() {
    cin >> n >> g;
    for(int i=1;i<=n;i++)
        cin >> a[i].w >> a[i].p;
    for(int i=1;i<=n;i++)
    {
        for(int j=g;j>0;j--)
        {
            if(a[i].w<=j)
                b[j]=max(b[j], b[j-a[i].w]+a[i].p);
        }
    }
    cout << b[g];
    return;
}

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    solve();
    return 0;
}