Pagini recente » Cod sursa (job #855899) | Cod sursa (job #955484) | Cod sursa (job #2338470) | Cod sursa (job #1353776) | Cod sursa (job #673854)
Cod sursa(job #673854)
#include<fstream>
#include<algorithm>
#define DIM 5000
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int N, G, castig_total;
struct obiect
{
int weight, profit;
}O[DIM];
bool comp(const obiect &unu, const obiect &doi)
{
return (unu.profit/unu.weight)>(doi.profit/doi.weight);
}
int main()
{
int i, local_profit, local_weight, gasit = 0;
in >> N >> G;
for(i = 1; i <= N; i++)
in >> O[i].weight >> O[i].profit;
sort(O+1, O+N+1, comp);
i = 1;
local_profit = 0;
local_weight = 0;
while( local_weight < G && i <= N && !gasit )
{
if( O[i].weight <= G - local_weight )
{
local_weight += O[i].weight;
local_profit += O[i].profit;
}
else {
castig_total = local_profit + (G-local_weight)*(O[i].profit/O[i].weight);
gasit = 1;
}
i++;
}
out << castig_total;
}