Cod sursa(job #2532505)

Utilizator mariaghinescu22Ghinescu Maria mariaghinescu22 Data 27 ianuarie 2020 21:34:37
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#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;
}