Pagini recente » Cod sursa (job #2689528) | Cod sursa (job #817991) | Cod sursa (job #2594843) | Cod sursa (job #2172002) | Cod sursa (job #2854531)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
struct obiect{
int greutate;
int pret;
}obiecte[5001];
int n,W,aux;
int main()
{
f>>n>>W;
int profit[2][W+1];
for(int i=0;i<=W;i++){
profit[0][i]=0;
profit[1][i]=0;
}
for(int i=1;i<=n;i++){
f>>obiecte[i].greutate;
f>>obiecte[i].pret;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=W;j++){
if(j-obiecte[i].greutate<0){
profit[1][j]=profit[0][j];
}
else{
profit[0][j]=profit[1][j];
profit[1][j]=max(profit[0][j], profit[0][j-obiecte[i].greutate]+obiecte[i].pret);
}
}
for(int j=1;j<=W;j++){
profit[0][j]=profit[1][j];
}
}
/*
for(int j=1;j<=W;j++){
cout<<profit[0][j]<<" ";
}
cout<<'\n';
for(int j=1;j<=W;j++){
cout<<profit[1][j]<<" ";
}
*/
g<<profit[1][W];
return 0;
}