Cod sursa(job #1657802)

Utilizator preda.andreiPreda Andrei preda.andrei Data 20 martie 2016 20:09:54
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <cstdio>

using namespace std;

int profit[2][10001];

int main()
{
    FILE *fin = fopen("rucsac.in", "r");
    FILE *fout = fopen("rucsac.out", "w");

    int n, gMax, greutate, valoare;
    int pMax = 0, l1, l2;

    fscanf(fin, "%d%d", &n, &gMax);
    l1 = 0;
    for(int i = 1; i <= n; ++i){
        fscanf(fin, "%d%d", &greutate, &valoare);

        l1 = (l1 + 1) % 2;
        l2 = 1 - l1;
        for(int j = 0; j <= gMax; ++j){
            if(j >= greutate && profit[l2][j - greutate] + valoare > profit[l2][j])
                profit[l1][j] = profit[l2][j - greutate] + valoare;
            else profit[l1][j] = profit[l2][j];

            //fprintf(fout, "%d ", profit[l1][j]);

            if(i == n && profit[l1][j] > pMax)
                pMax = profit[l1][j];
        }
        //fprintf(fout, "\n");
    }

    fprintf(fout, "%d", pMax);
    return 0;
}