Pagini recente » Cod sursa (job #973285) | Cod sursa (job #667517) | Cod sursa (job #586428) | Cod sursa (job #1844016) | Cod sursa (job #1863231)
#include <iostream>
#include <fstream>
#define nmax 5005
using namespace std;
int v[nmax];
int n,w,pmax,i;
pair <int,int > per[nmax];
ifstream f("rucsac.in");
ofstream g("rucsac.out");
void sub(int k){
int i,cost,profit;
if (k>n){
cost=0;
profit=0;
for(i=1;i<=n;i++){
if (v[i]==1) {
cost=cost+per[i].first;
profit=profit+per[i].second;
}
if (cost<=w && profit > pmax) pmax=profit;
}
return;
}
v[k]=1;
sub(k+1);
v[k]=0;
sub(k+1);
}
int main()
{
int c,p;
f >> n >> w;
for(i=1;i<=n;i++){
f >> c >> p;
per[i]=make_pair(c,p);
}
sub(1);
g << pmax;
}