Cod sursa(job #2493045)

Utilizator anisca22Ana Baltaretu anisca22 Data 15 noiembrie 2019 20:57:50
Problema Problema rucsacului Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 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];
    for (int i = 1; i <= N; i++)
        for (int j = 0; j <= G; j++)
        {
            int row = i % 2;
            int antiRow = 1 - row;
            mxProfit[row][j] = max(mxProfit[antiRow][j], mxProfit[row][j - 1]);
            if (j >= weight[i])
                mxProfit[row][j] = max(mxProfit[row][j], mxProfit[antiRow][j - weight[i]] + profit[i]);
        }
    fout << mxProfit[N % 2][G];
    return 0;
}