Saving HDU
#include<iostream> using namespace std; struct bao{ int pi, mi; }a[100], b; void quick_sort(bao s[], int l, int r){ if(l < r){ int i=l, j=r, x=s[l].pi; b = s[l]; while(i < j){ while(i < j && s[j].pi >= x) j--; if(i < j) s[i++] = s[j]; while(i < j && s[i].pi < x) i++; if(i < j) s[j--] = s[i]; } s[i] = b; quick_sort(s, l, i-1); quick_sort(s, i+1, r); } } int main(){ int v, n; while(cin>>v, v){ cin>>n; int i, sum; for(i=0; i<n; i++) cin>>a[i].pi>>a[i].mi; quick_sort(a, 0, n-1); i=n-1, sum=0; while(v>0 && i>=0){ if(v-a[i].mi> 0){ sum += a[i].pi * a[i].mi; v -= a[i].mi; i--; } else { sum += v * a[i].pi ; break; } } cout<<sum<<endl; } return 0; }