Cod sursa(job #1521592)
Utilizator | Data | 10 noiembrie 2015 18:08:21 | |
---|---|---|---|
Problema | Problema rucsacului | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.51 kb |
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int n,G,w[5001],p[5001],pd[3][10001];
int main()
{
f>>n>>G;
for(int i=1;i<=n;i++)
f>>w[i]>>p[i];
int l=0;
for(int i=1;i<=n;i++,l=1-l)
for(int j=0;j<=G;j++)
{
pd[l][j]=pd[1-l][j];
if(w[i]<=j)
pd[l][j]=max(pd[l][j],pd[1-l][j-w[i]]+p[i]);
}
g<<max(pd[0][G],pd[1][G])<<'\n';
g.close();
return 0;
}