Cod sursa(job #2404720)

Utilizator MariusblockMoga Marius-Ioan Mariusblock Data 13 aprilie 2019 12:34:00
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.5 kb
#include <bits/stdc++.h>
using namespace std;

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

int v[10004];
int n,g;

int main()
{
    int auxp,auxg,i,j;
    fin>>n>>g;
    fin>>auxg>>auxp;
    if(n == 1 && auxg < g){
        fout<<auxp<<endl;
        return 0;
    }
    v[auxg] = auxp;
    for(i = 2; i <= n; i++){
        fin>>auxg>>auxp;
        for(j = g; j >= 1; j--){
            v[j] = max(v[j],v[j-auxg] + auxp);
        }
    }
    fout<<v[g]<<endl;
    return 0;
}