Pagini recente » Cod sursa (job #2809212) | Cod sursa (job #2513981) | Cod sursa (job #753104) | Cod sursa (job #2478413) | Cod sursa (job #2678994)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
struct per{
double e, cost, cost1;
}s[1002];
inline bool Cmp(per a, per b){
if(a.cost1 < b.cost1) return 1;
else if(a.cost1 == b.cost1){
if(a.e < b.e) return 1;
else if(a.e == b.e) return a.cost < b.cost;
}
return 0;
}
int main()
{
int w, g;
fin >> g >> w;
for(int i = 1; i <= g; i++){
fin >> s[i].e >> s[i].cost;
s[i].cost1 = double(s[i].cost / s[i].e);
}
sort(s + 1, s + g + 1, Cmp);
int suma = 0;
for(int i = 1; i <= g; i++){
if(suma + s[i].cost <= w)
suma += s[i].cost;
else{
while(suma <= w)
suma += s[i].cost1;
}
}
if(suma < w) fout << -1;
else fout << suma;
return 0;
}