Cod sursa(job #1832232)
Utilizator | Vrabie Vladislav emity03 | Data | 19 decembrie 2016 17:22:05 |
---|---|---|---|
Problema | Problema rucsacului | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.55 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("rucsac.in");
ofstream cout("rucsac.out");
int N, Gr, rs;
int G[10005], P[5005];
int Dp[2][10005];
int main()
{
cin>>N>>Gr;
for(int i=1; i<=N; ++i)
cin>>G[i]>>P[i];
for(int i=1; i<=N; ++i,rs=1-rs)
for(int tot=0; tot<=Gr; ++tot)
{
Dp[1-rs][tot] = Dp[rs][tot];
if(G[i] <= tot) Dp[1-rs][tot] = max(Dp[1-rs][tot], Dp[rs][tot - G[i]] + P[i]);
}
cout<<Dp[rs][Gr];
return 0;
}