Pagini recente » Cod sursa (job #1209696) | Cod sursa (job #2447993) | Cod sursa (job #2384704) | Cod sursa (job #2027316) | Cod sursa (job #3234629)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, gm;
int castig = 0;
struct obiect
{
int g, v;
} o[5001];
void citire()
{
fin >> n >> gm;
for(int i = 1; i <= n; i++)
fin >> o[i].g >> o[i].v;
}
void sortare()
{
sort(o + 1, o + n + 1, [](obiect a, obiect b)
{
return a.v * b.g > b.v * a.g;
});
}
int alegere()
{
for(int i = 1; i <= n && gm != 0; i++)
{
if(o[i].g <= gm)
{
gm -= o[i].g;
castig += o[i].v;
}
else
{
castig += (float)o[i].v / o[i].g * gm;
gm = 0;
}
}
return castig;
}
int main() {
citire();
sortare();
fout << alegere() << "\n";
fin.close();
fout.close();
return 0;
}