Cod sursa(job #2636693)

Utilizator sireanu_vladSireanu Vlad sireanu_vlad Data 19 iulie 2020 11:17:31
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int w[5010], p[5010], best[2][10010];


int main()
{
    int n, g;
    in >> n >> g;
    for(int i = 1; i <= n; i++)
    {
        in >> w[i] >> p[i];
    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= g; j++)
        {
            if(j >= w[i])
                best[i & 1][j] = max(best[(i&1)^1][j], best[(i&1)^1][j - w[i]] + p[i]);
            else
                best[i & 1][j] = best[(i&1)^1][j];


        }
    }

    out << best[n&1][g] << "\n";

    return 0;
}