#include<stdio.h>voidSwap(int*,int*);voidQuickSort(int*,int*,int,int);//快速排序;intmain(){
int n, m, i, M =0, S =0;//M表示花费金额, S表示采购量;int p[5000], a[5000];scanf("%d%d",&n,&m);for(i =0; i < m; i++){
scanf("%d%d",&p[i],&a[i]);}QuickSort(p, a,0, m-1);for(i =0; i < m; i++){
S += a[i];//采购量不能大于需求量;if(S > n){
S -= a[i];break;}M += p[i]* a[i];}//将采购量和需求量的差值补齐;if(S < n)M += p[i]*(n - S);printf("%d", M);return0;}voidSwap(int*p,int*q){
int t;t =*p;*p =*q;*q = t;return;}voidQuickSort(int*p,int*a,int low,int high){
int i = low;int j = high;int key = p[low];if(low >= high)return;while(low < high){
while(low < high && key <= p[high])--high;if(key > p[high]){
Swap(&p[low],&p[high]);Swap(&a[low],&a[high]);++low;}while(low < high && key >= p[low])++low;if(key < p[low]){
Swap(&p[low],&p[high]);Swap(&a[low],&a[high]);--high;}}QuickSort(p, a, i, low-1);QuickSort(p, a, low+1, j);}