题目1433:FatMouse
//简单的贪心策略
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
class House
{
public:
int weight;
int cost;
double ratio;
void init(int w, int c)
{
weight = w;
cost = c;
ratio = (double)w/(double)c;
}
bool operator < (House h)const
{
return ratio - h.ratio > 0.0001;
}
};
House wh[1001];
int main()
{
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
int m, n, w, c, i;
double res;
while (cin >> m && m!= -1)
{
cin >> n;
for (i=0; i<n; i++)
{
cin >> w >> c;
wh[i].init(w, c);
}
sort(wh, wh+n);
//计算
res = 0;
i=0;
while (m>0 && i<n)
{
if (m>wh[i].cost)
{
res += wh[i].weight;
m -= wh[i].cost;
}
else
{
res += (double)m/(double)wh[i].cost * (double)wh[i].weight;
m = 0;
break;
}
i++;
}
cout << setiosflags(ios::fixed) << setprecision(3);
cout << res << endl;
}
return 0;
}