Pagini recente » Cod sursa (job #1723830) | Cod sursa (job #2631697) | Cod sursa (job #2937928) | Cod sursa (job #313892) | Cod sursa (job #3234087)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("rucsac.in");
ofstream cout("rucsac.out");
int n, g;
int v[5001];
int bestprof = -1;
struct obiect
{
int gr;
int v;
bool operator > (const obiect x) const
{
return ((this->v * x.gr) < (x.v*this->gr));
}
bool operator < (const obiect x) const
{
return ((this->v * x.gr) >(x.v * this->gr));
}
};
obiect rest[5001];
int ginit = 0;
int profinit = 0;
vector <obiect>o;
void bkt(int level)
{
bestprof = max(profinit, bestprof);
if (level > n || ginit==g )
{
;
}
else
{
if (profinit + min(rest[level].gr,g-ginit)*rest[level].v> bestprof && ginit+o[level].gr<=g)
{
ginit+=o[level].gr;
profinit+=o[level].v;
bkt(level + 1);
ginit -= o[level].gr;
profinit-=o[level].v;
}
if (profinit+rest[level+1].v*min(rest[level+1].gr,g-ginit)>bestprof)
{
bkt(level+1);
}
}
}
int main()
{
cin >> n >> g;
for (int i = 0; i < n; i++)
{
int gr, v;
cin >> gr >> v;
o.push_back({ gr,v});
}
sort(o.begin(),o.end());
rest[n].v=0;
rest[n].gr=0;
for (int i = n - 1; i >= 0; i--)
{
rest[i].v= rest[i + 1].v + o[i].v;
rest[i].gr=rest[i+2].gr+o[i].gr;
}
bkt(0);
cout << bestprof;
}