Input
Output
Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
Sample Output
13.33331.500
#include<stdio.h> #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<iomanip> using namespace std; struct f{ int j; int f; }a[1001]; bool cmp(f a, f b){ return (double)a.j/a.f > (double)b.j/b.f; } int main() { int m, n, i; while(~scanf("%d%d", &m, &n)) { if(m==-1 && n==-1) break; for(i=0; i<n; i++) { scanf("%d%d", &a[i].j, &a[i].f); } sort(a, a+n, cmp); double sum = 0; for(i=0; i<n; i++) { if(a[i].f<m) { sum = sum + a[i].j; m = m - a[i].f; } else { sum = sum + (double)a[i].j/a[i].f*m; break; } } printf("%.3f\n", sum); } return 0; }