Cod sursa(job #2971127)

Utilizator Ciorba21Tuduce Sergiu Ciorba21 Data 26 ianuarie 2023 18:28:05
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, Gmax;
int g[1005], v[1005], o[10001];


int main()
{
   // cin.tie(0);
   // ios::sync_with_stdio(0);
    fin >> n >> Gmax;
    for(int i=1; i<=n; i++)
        fin >> g[i] >> v[i];

    o[0] = 0;
    int sol = 0;

    for(int i=1; i<=n; i++)
        for(int j = Gmax - g[i]; j >= 0; j--)
            if(o[j+g[i]] < o[j] + v[i])
            {
                o[j + g[i]] = o[j] + v[i];
                if(o[j+g[i]] > sol) sol = o[j + g[i]];
            }
   fout << sol;
}