Cod sursa(job #2156684)

Utilizator EclipseTepes Alexandru Eclipse Data 8 martie 2018 22:06:34
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#define dMAX 10001

using namespace std;

int n, capacity;
int weight, profit;
int a[dMAX];

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

int main()
{
    int i, j;
    fin >> n >> capacity;
    a[0] = 1;
    for (i = 1; i <= n; i++) {
        fin >> weight >> profit;
        for (j = capacity - weight; j >= 0; j--) {
            if (a[j] + profit > a[j + weight]) {
                a[j + weight] = a[j] + profit;
            }
        }
    }
    profit = 0;
    for (i = 0; i <= capacity; i++) {
        if (a[i] > profit) {
            profit = a[i];
        }
    }
    fout << profit - 1;
    return 0;
}