Pagini recente » Cod sursa (job #457440) | Cod sursa (job #461656) | Cod sursa (job #722568) | Statistici Meriem Voinica (Mirai906) | Cod sursa (job #1531140)
#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;
}