Cod sursa(job #2005866)

Utilizator basilescubasil basilescu basilescu Data 28 iulie 2017 13:39:03
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <iostream>

using namespace std;

const int G_max = 10001;
const int N_max = 5001;

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

int DP[G_max], W, P, N, G;

int main() {
    in >> N >> G;

    for(int i = 1; i < G; i++) {
        DP[i] = -2e9;
    }

    for(int k = 0; k < N; k++) {
        in >> W >> P;

        for(int h = G; h >= W; h--) {
            if(DP[h] < DP[h - W] + P) {
                DP[h] = DP[h - W] + P;
            }
        }
    }
    
    int current_max = 0;

    for(int k = 0; k <= G; k++) {
        if(DP[k] > current_max) {
            current_max = DP[k];
        }
    }

    out << current_max;

    return 0;
}