Cod sursa(job #2279871)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 10 noiembrie 2018 10:02:37
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#define N 10010

using namespace std;

int sol[N];

int main()
{
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");
    int nr, maxWeight, price, weight, aux;
    fin >> nr >> maxWeight;
    for (int k=1; k<=nr; k++)
    {
        fin >> weight >> price;
        for (int i=maxWeight; i>=1; i--)
        {
            if (i - weight <= 0)
                aux = 0;
            else
                aux = i - weight;
            if (sol[aux] + price > sol[i] && (weight + aux <= i) )
                sol[i] = sol[aux] + price;
        }
    }
    fout << sol[maxWeight];
    return 0;
}