Cod sursa(job #3184532)

Utilizator mirceamaierean41Mircea Maierean mirceamaierean41 Data 16 decembrie 2023 10:39:08
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <string.h>

using namespace std;

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

const int oo = 0x3f3f3f3f;
const int GMAX = 1e4 + 2;

int dp[2][GMAX], n, w, p, g, res;

int main()
{
    fin >> n >> g;
    memset(dp, -oo, sizeof(dp));

    dp[0][0] = 0;

    for (int i = 1; i <= n; ++i)
    {
        fin >> w >> p;

        int crtIndex = i & 1, antIndex = crtIndex ^ 1;
        for (int j = 0; j <= g; ++j)
        {
            if (j - w >= 0)
                dp[crtIndex][j] = max(dp[antIndex][j], dp[antIndex][j - w] + p);
            else
                dp[crtIndex][j] = dp[antIndex][j];
            if (dp[crtIndex][j] > res)
                res = dp[crtIndex][j];
        }
    }

    fout << res << '\n';

    return 0;
}