Cod sursa(job #2434294)
| Utilizator | Data | 1 iulie 2019 14:27:09 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.55 kb |
#include <fstream>
#define MAX 10005
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
long long n,g,W[MAX];
int main(){
long long i,j,x,y,maxx;
fin>>n>>g;
for(i=0;i<n;++i){
fin>>x>>y;//x-greutate, y-cost
for(j=g;j>0;--j)
if(W[j] && j+x<=g)
W[j+x]=max(W[j+x],W[j]+y);
if(x>g)
continue;
W[x]=max(W[x],W[0]+y);
}
maxx=0;
for(i=0;i<=g;++i){
maxx=max(maxx,W[i]);
}
fout<<maxx;
return 0;
}
