Cod sursa(job #801068)
Utilizator | Data | 23 octombrie 2012 12:28:38 | |
---|---|---|---|
Problema | Energii | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 0.49 kb |
#include <fstream>
using namespace std;
ifstream f("energii.in");
ofstream fout("energii.out");
int n,g,p[5005],w[5005],cost[2][10005],l;
int main()
{
f>>n>>g;
int i;
for(i=1;i<=n;i++)
f>>w[i]>>p[i];
for(i=1;i<=n;i++,l=1-l)
for(int c=0;c<=g;c++)
{
cost[1-l][c]=cost[l][c];
if(w[i]<=c)
cost[1-l][c]=max(cost[1-l][c],cost[l][c-w[i]]+p[i]);
}
fout<<cost[l][g]<<'\n';
f.close();
fout.close();
return 0;
}