Cod sursa(job #1831733)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 18 decembrie 2016 17:17:43
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int nMax = 5005;
const int gMax = 10005;

int n, g;
int w[nMax];
int p[nMax];
int dp[2][gMax];

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

void rezolvare()
{
    int current = 1, last = 0;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= g; ++j)
        {
            if(j >= w[i])
                dp[current][j] = max(dp[last][j], dp[last][j - w[i]] + p[i]);
            else
                dp[current][j] = dp[last][j];
        }
        current ^= 1;
        last ^= 1;
    }
    out << dp[last][g];
}

int main()
{
    citire();
    rezolvare();
    return 0;
}