#include<iostream> #include<vector> using namespace std; typedef struct{ double value; double money; }s; vector <s> v; bool cmp(s one,s two) { double x1=one.value/one.money; double x2=two.value/two.money; return x1>x2; } int main() { int m,n; s t; while(cin>>m>>n&&(m!=-1||n!=-1)) { v.clear(); for(int i=0;i<n;i++) cin>>t.value>>t.money,v.push_back(t); sort(v.begin(),v.end(),cmp); double sum=0; for(int i=0;i<n;i++) { if(m>=v[i].money) { sum+=v[i].value; m=m-v[i].money; } else { sum+=v[i].value/v[i].money*m;break; //cout<<v[i].value/v[i].money<<endl; } } printf("%.3f/n",sum); } system("pause"); return 0; } http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109