Cod sursa(job #1711017)

Utilizator tudormaximTudor Maxim tudormaxim Data 30 mai 2016 10:26:24
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int nmax = 5005;
int dp[nmax];

int main() {
    ios_base :: sync_with_stdio(false);
    int n, g, p, w, i, j, sol = 0;
    fin >> n >> g;
    for(i = 1; i <= n; i++) {
        fin >> w >> p;
        for(j = g - w; j >= 0; j--) {
            if(dp[j + w] < dp[j] + p) {
                dp[j + w] = dp[j] + p;
                if(dp[j + w] > sol) {
                    sol = dp[j + w];
                }
            }
        }
    }
    fout << sol;
    fin.close();
    fout.close();
    return 0;
}