Pagini recente » Cod sursa (job #1655057) | Cod sursa (job #2275838) | Cod sursa (job #1467517) | Cod sursa (job #351553) | Cod sursa (job #2866994)
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
ifstream be("rucsac.in");
ofstream ki("rucsac.out");
int const nMax = 10001;
int n,g;
int w[nMax],p[nMax];
list<pair<int,int> > elem;
void rucsac()
{
int maxprofi = 0;
int actualWeight = 0;
int currentprofit;
int db = 0;
for(int i=1;i<=n;i++)
{
actualWeight = w[i];
currentprofit = p[i];
for(int j=i+1;j<=n ;j++)
{
actualWeight += w[j];
currentprofit += p[j];
if(actualWeight == g)
{
if(maxprofi < currentprofit)
{
maxprofi = currentprofit;
}
}
if(actualWeight > g)
{
actualWeight -= w[i+db];
currentprofit -= p[i+db];
}
db++;
}
}
cout<<maxprofi<<endl;
ki<<maxprofi<<endl;
}
int main()
{
be>>n>>g;
int a,b;
for(int i=1;i<=n;i++)
{
//be>>w[i]>>p[i];
be>>a>>b;
elem.push_back(make_pair(a,b));
}
elem.sort();
int db = 1;
for(list<pair<int,int> >::iterator it = elem.begin(); it != elem.end(); it++)
{
w[db] = it->first;
p[db++] = it->second;
}
rucsac();
}