#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f
using namespace std;
int f[100000],j[100000],x,n,m;
bool vis[100000];
double ans,maxx,b[100000];
int pattion(int p,int r,double a[])
{
int y,z,ex2;
double x,ex1;
x = a[p];
y = p-1;
z = r+1;
while(1)
{
y++;
while(a[y] < x)
y++;
z--;
while(a[z] > x)
z--;
if(y < z)
{
ex1 = a[y];
a[y] = a[z];
a[z] = ex1;
ex2 = f[y];
f[y] = f[z];
f[z] = ex2;
ex2 = j[y];
j[y] = j[z];
j[z] = ex2;
}
else return z;
}
}
void quicksort(int p,int r,double a[])
{
int q;
if(p < r)
{
q = pattion(p,r,a);
quicksort(p,q,a);
quicksort(q+1,r,a);
}
}
int main()
{
while(scanf("%d%d",&m,&n) && m != -1)
{
for(int i = 1 ; i <= n ; i++)
{
scanf("%d%d",&f[i],&j[i]);
b[i] = 1.0*f[i]/j[i];
}
quicksort(1,n,b);
ans = 0;
for(int i = n ; i >= 1 ; i--)
{
if(m >= j[i])
{
ans = ans + f[i];
m -= j[i];
}
else
{
ans += 1.0*m*f[i]/j[i];
break;
}
}
printf("%.3lf\n",ans);
}
}
部分背包问题,所以可以贪心处理,第一次超时了,后来用了快排先排序F[i]/J[i],从大到小找,酱紫就OK了