Cod sursa(job #1531140)

Utilizator ArambasaVlad Arambasa Arambasa Data 21 noiembrie 2015 13:23:11
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb

#include <fstream>
using namespace std;

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

const int NMax = 5005, GMax = 10005;

int N,MG,P[NMax],G[NMax];
int DP[2][GMax];

int main()
{
    in>>N>>MG;
    for(int i = 1; i<=N; ++i)
    {
        in>>G[i]>>P[i];
    }
    for(int i = 1; i<=N; i++)
    {
        for(int j = 1; j<=MG; j++)
            {
                DP[1][j] = DP[0][j];
                if(G[i]<=j)//daca greutatea obiectului i<=j;
                    {
                        int aux=j-G[i];//fac un aux care e j-G[i]
                        DP[1][j] = max(DP[1][j],DP[0][aux] + P[i]);//Pun in DP[1][j] maximul dintre DP[1][j] si suma DP[0][aux] + P[i]
                    }
            }
        for(int j = 1; j<=MG; j++)
            DP[0][j] = DP[1][j];
    }
    out<<DP[1][MG]<<"\n";
    return 0;
}