Pagini recente » Cod sursa (job #1915868) | Cod sursa (job #2484155) | Cod sursa (job #1340876) | Cod sursa (job #2174631) | Cod sursa (job #1832232)
#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;
}