Cod sursa(job #2493042)

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