Cod sursa(job #2627878)

Utilizator ArctopusKacso Peter-Gabor Arctopus Data 13 iunie 2020 00:30:26
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int glim = 10010;

int d[glim];

int main(){
    int n, g;
    fin >> n >> g;

    d[0] = 0;
    int best = 0;
    for( int i = 0; i < n; ++i ){
        int w, p;
        fin >> w >> p;
        for( int i = g; i >= w; --i ){
            if( d[i-w] + p > d[i] )
                d[i] = d[i-w] + p;
            
            if( d[i] > best ) best = d[i];
        }
    }

    fout << best << "\n";
}