Pagini recente » Cod sursa (job #1910204) | Cod sursa (job #86389) | Cod sursa (job #2961850) | Cod sursa (job #177110) | Cod sursa (job #2532505)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
const int N = 1002;
int n, total_amount;
struct energy {
int amount_of_energy, cost;
}v[N];
bool cmp(const energy& x, const energy& y) {
return (x.amount_of_energy * y.cost > x.cost* y.amount_of_energy);
}
int main() {
in >> n >> total_amount;
for (int i = 1; i <= n; i++)
in >> v[i].amount_of_energy >> v[i].cost;
sort(v + 1, v + n + 1, cmp);
int i = 0;
double sum = 0;
while (i <= n && total_amount > 0) {
if (v[i].amount_of_energy <= total_amount) {
total_amount -= v[i].amount_of_energy;
sum += v[i].cost;
}
i++;
}
if (total_amount == 0) out << sum;
else if (total_amount > 0) out << -1;
return 0;
}