/* ID: aznfy1 PROG: milk LANG: C++ */ #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #define clr(a,b); memset(a,b,sizeof(a)); using namespace std; struct node { int price; int have; }a[6000]; int need; int n; int money; bool cmp(const node& a,const node& b) { return a.price<b.price; } int main() { freopen("milk.in","r",stdin); freopen("milk.out","w",stdout); while(cin>>need>>n) { if(need==0&&n==0) {cout<<0<<endl;continue;} money=0; for(int i=0;i<n;i++) { cin>>a[i].price>>a[i].have; } sort(a,a+n,cmp); for(int i=0;i<n;i++) { if(a[i].have<=need) { need-=a[i].have; money+=a[i].have*a[i].price; } else { money+=a[i].price*need; break; } } cout<<money<<endl; } }
太水