Cod sursa(job #2493051)

Utilizator anisca22Ana Baltaretu anisca22 Data 15 noiembrie 2019 21:08:16
Problema Problema rucsacului Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <fstream>

using namespace std;

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

int N, G, weight, profit;
int mxProfit[5][10005];

int main()
{
    fin >> N >> G;
    for (int i = 1; i <= N; i++)
    {
        fin >> weight >> profit;
        for (int j = 0; j <= G; j++)
        {
            mxProfit[i % 2][j] = max(mxProfit[1 - i % 2][j], mxProfit[i % 2][j - 1]);
            if (j >= weight)
                mxProfit[i % 2][j] = max(mxProfit[i % 2][j], mxProfit[1 - i % 2][j - weight] + profit);
        }
    }

    fout << mxProfit[N % 2][G];
    return 0;
}