Cod sursa(job #2493049)

Utilizator anisca22Ana Baltaretu anisca22 Data 15 noiembrie 2019 21:04:22
Problema Problema rucsacului Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
#define NMAX 5005
#define GMAX 10005

using namespace std;

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

int N, G;
int mxProfit[5][GMAX];
int weight[NMAX], profit[NMAX];

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

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