A burglar got into a matches warehouse and wants to steal as many matches as possible. In the warehouse there are m containers, in the i-th container there are ai matchboxes, and each matchbox contains bi matches. All the matchboxes are of the same size. The burglar's rucksack can hold n matchboxes exactly. Your task is to find out the maximum amount of matches that a burglar can carry away. He has no time to rearrange matches in the matchboxes, that's why he just chooses not more than n matchboxes so that the total amount of matches in them is maximal.

The first line of the input contains integer n (1 ≤ n ≤ 2·108) and integer m (1 ≤ m ≤ 20). The i + 1-th line contains a pair of numbers ai and bi (1 ≤ ai ≤ 108, 1 ≤ bi ≤ 10). All the input numbers are integer.

Output the only number — answer to the problem.

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define an "\n"

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll int n, m;
	cin >> n >> m;
	vector<pair<int, int>> vec;
	for(int i=0;i<m;i++) {
		int a, b;
		cin >> b >> a;
		vec.pb(make_pair(a, b));
	}
	ll int sum=0, a=0;
	sort(vec.begin(), vec.end());
	reverse(vec.begin(), vec.end());
	int i=0;
	while(true) {
		if(n>vec[i].second) {
			n-=vec[i].second;
			sum+=vec[i].first*vec[i].second;
		}else {
			sum+=n*vec[i].first;
			n=0;
		}
		i++;
		if(i==m) {
			break;
		}
	}
	cout << sum;
}

#include <iostream >
#include <string >
#include<vector>
#include<deque>
#include<algorithm>
#include<map>
#include<set>
#include<sstream>
#include <cstdlib>
#include <stdio.h>
#include <iomanip> // for //cout << fixed << setprecision();
using namespace std;
#define lli long long int
lli prime(lli n);
lli gcd(lli n, lli  m);
lli lcm(lli n, lli m);
lli fact(lli n);
int sumdigit(int n);
bool perfectSquare(lli n);
int power(int x, unsigned  int y);
void maxDistanceBetweenTwoPointsINarray(lli arr[], int n);
void  toMakeShiftOfArray(lli arr[], lli nuOfElements, lli nuOfShift);
#define fast ios_base::sync_with_stdio(false);   cin.tie(NULL)
//string s = "abcdefghijklmnopqrstuvwxyz";
//cout << fixed << setprecision(3);
//freopen("divisors.in", "r", stdin);
//string str2[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
set<lli>st;
lli arr[1000][1000];
//lli arr2[10000000];
#define mpint map<int,int>
#define si(a)   scanf("%d",&a)
int main() {
	fast;

	lli n; cin >> n;
	int m; cin >> m;
	vector <int>a(m);
	int f = 0;
	int arrr[100000];
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cin >> arr[i][j];
			if (j % 2 == 1)
				a[i] = arr[i][j];
			arrr[f] = arr[i][j];
			f++;
		}
	}

	sort(a.rbegin(), a.rend());
	//for (int i = 0; i < 2*m; i++)
		//cout << arrr[i] << " ";
	//cout << endl;
	//for (int i = 0; i < m; i++)
	//	cout << a[i] << " ";
//	cout << endl;
	lli ans = 0;
	for (int s = 0; s < m; s++)
	{
		//cout << n << endl;
		//cout << ans << endl;
		for (int i = 0; i < 2 * m; i++)
		{
			if (i % 2 == 1)
			{
				if (a[s] == arrr[i])
				{
					if (n == arrr[i - 1] && n > 0  )
					{
						ans += n * a[s];
						cout << ans << endl;
						return 0;
					}
					else if (n < arrr[i - 1] && n > 0 )
					{
						ans += (a[s] * (n));
						cout << ans << endl;
						return 0;
					}
					else
					{
						ans += (a[s] * ( arrr[i - 1]));
						n -= arrr[i - 1];
						arrr[i-1] = 0;
						arrr[i] = 0;
					}
				}
			}
		}
	}
	cout << ans << endl;
}

lli prime(lli n) {
	if (n % 2 == 0 && n != 2 || n<2)return false;
	else { for (lli i = 3; i*i <= n; i += 2) { if (n%i == 0) { return false; } } }
	return true;
}
lli gcd(lli a, lli  b) {//g
	if (b == 0)return a;
	else return gcd(b, a%b);
}
lli lcm(lli n, lli m) {
	return n*m / gcd(n, m);
}
lli fact(lli n) {
	if (n < 2)return 1;
	else return n * fact(n - 1);
}
int sumdigit(int n) {
	int sum = 0;
	while (n) { sum += (n % 10); n /= 10; }
	return sum;
}
bool perfectSquare(lli n)
{
	long double x = sqrt(n);
	if (x - floor(x) == 0)
		return true;
	else
		return false;
}
lli power(lli x, lli n){
	lli po = 1;
	for (int i = 1; i <= n; i++){
		po *= x;
		po %= 10;
	}
	return po;

	/*
	int power(int x, unsigned  int y)
	{
	if (y == 0)
	return 1;
	else if (y % 2 == 0)
	return power(x, y / 2) * power(x, y / 2);
	else
	return x * power(x, y / 2) * power(x, y / 2);
	}
	*/
}
void  toMakeShiftOfArray(lli arr[], lli nuOfElements, lli nuOfShift)
{
	lli n = nuOfElements;
	lli k = nuOfShift;
	if (k > n)
	{
		k = k%n;
		for (int i = n - k; i < n; i++)cout << arr[i] << " ";
		for (int i = 0; i < n - k; i++)cout << arr[i] << " ";

	}
	else
	{
		for (int i = abs(n - k); i < n; i++)cout << arr[i] << " ";
		for (int i = 0; i < abs(n - k); i++)cout << arr[i] << " ";
	}
	cout << endl;
	/*
	// another solution by using function
	fast;
	int n, d; cin >> n >> d; d = d%n;
	vector< unsigned long long int> a(n);
	for (int i = 0; i<n; i++)
	cin >> a[i];
	rotate(a.begin(), a.begin() + n - d, a.end());
	for (int i = 0; i<n; i++)
	{
	cout << a[i] << ' ';
	} cout << '\n';
	*/
}
void maxDistanceBetweenTwoPointsINarray(lli arr[], int n)
{
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (arr[i] != arr[0])
			ans = max(ans, i - 0);
		if (arr[i] != arr[n - 1])
			ans = max(ans, n - 1 - i);

	}
	cout << ans << endl;

}

#include <bits/stdc++.h>

using namespace std;

bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}

int main()
{
    int n,m,count=0;
    cin>>n>>m;
    vector <pair<int,int>> vec;
    for(int i=0 ; i<m ; i++){
        int a1 , a2;
        cin>>a1>>a2;
        vec.push_back( make_pair(a1,a2) );
    }

    sort(vec.begin(), vec.end() , sortbysec);

    for(int i=0 ;  i<m ; i++){
        if(n<(vec[i].first)){
            count +=  n * vec[i].second;
            break;

        }else{
            count += vec[i].first * vec[i].second;
            n = n - vec[i].first;
        }
    }

    cout<<count;

}

#include<bits/stdc++.h>
using namespace std;
int n,m;
struct node{
	long long a,b;
}e[225];
int cmp(node x,node y){
	return x.b>y.b;
}
int main(){
	cin>>n>>m;
	for(int i = 1;i<=m;i++)
		cin>>e[i].a>>e[i].b;
	sort(e+1,e+1+m,cmp);
	long long sum=0,ans=0;
	for(long long i = 1;i<=n;i++){
		sum+=e[i].a;
		ans=ans+e[i].a*e[i].b;
		if(sum>n){
			ans=ans-e[i].b*(sum-n);
			break;
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	int n, m, count = 0, maxi = 0;
	cin >> n >> m;
	int arr[11] = {0};
	for(int i= 0; i<m; i++){
		int a, b;
		cin >> a >> b;
		arr[b] = arr[b]+a;

	}
	// sort(arr, arr+m);
	// int l = sizeof(arr)/sizeof(arr[0]);
	for(int i=10; i>=1; i--){
		if(arr[i]<n){
			count = count + i*arr[i];
			n = n-arr[i];
		}
		else{
			count = count + n*i;
			break;
		}
		// c out<< count <<endl;
	}
	cout <<count;
}

#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;
bool sortbysec(const pair<int, int> &a,
	const pair<int, int> &b)
{
	return (a.second > b.second);
}

int main()
{
	int n, m, a, b,total=0;
	cin >> n >> m;
	vector<pair<int, int>>cont(m);
	for (int i = 0; i < m; ++i) {
		cin >> a >> b;
		pair<int, int>temp = { a,b };
		cont[i] = temp;
	}
	sort(cont.begin(), cont.end(), sortbysec);
	for (auto i : cont) {
		while (i.first > 0 && n > 0) {
			total += i.second;
			--n;
			--i.first;
		}
	}
	cout << total << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
//bool cmp(int a, int b)return a>b;
int main()
{
	pair<ll,ll>ar[25];
	ll int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		ll a,b;
		cin>>ar[i].second>>ar[i].first;
	}
	sort(ar,ar+m);
	ll cnt=0;
	ll ans=0;
	int i=m-1;
	while(cnt<n && i>=0)
	{

			if(ar[i].second+cnt<n)
			{

				ans+=(ar[i].second*ar[i].first);
				cnt+=ar[i].second;
				//cout<<cnt<<" "<<ans<<endl;
			}
			else
			{

				ans+=(n-cnt)*ar[i].first;
				n=cnt;

			}
			i--;
	}
	cout<<ans<<endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <list>
#include <set>
#include <climits>
#include <map>
#include <stack>
#include <queue>
#include <complex>
#include <cmath>
#include <sstream>
#include <deque>
#include <utility>
#include <bitset>
#include <numeric>
#include <ext/hash_set>
#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define oo (1<<30)
#define M 1001
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for (__typeof(s.begin()) it = s.begin(); it != s.end(); it++)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

int main() {
#ifndef ONLINE_JUDGE
	freopen("test.in", "rt", stdin);
	//	freopen("o.txt", "wt", stdout);
#endif
	int n, m;
	cin >> n >> m;
	vpi v(m);
	FOR (i ,0 , m)
		cin >> v[i].second >> v[i].first;
	int res = 0;
	sort(rall(v));
	FOR (i , 0 , m) {
		res += min(n, v[i].second) * v[i].first, n -= v[i].second;
		if (n <= 0)
			break;
	}
	cout << res << endl;
	return 0;
}

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <string>
#include <cstring>
#include <bits/stdc++.h>
#include <search.h>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include<stack>
#include <utility>
using namespace std;
int main()
{
vector < pair <long long int ,long long int > > v(20);
long long int n,m,all=0;
cin>>n>>m;
for(int i=0;i<m;++i){
    cin>>v[i].second>>v[i].first;
}
sort(v.begin(),v.end());
for(int i=19;i>=0 && n>0;--i){
    if(v[i].second>=n){
        n-v[i].second;
        all+=n*v[i].first;
        break;
    }
    else{
        all+=v[i].second*v[i].first;
        n-=v[i].second;
    }
}
cout<<all;
 return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>

struct container{long long boxes, matches;};

bool contSort(container A, container B){return A.matches < B.matches;}

int main(){

    long n, m; scanf("%ld %ld\n", &n, &m);
    std::vector<container> contVec(m);
    for(long k = 0; k < m; k++){scanf("%lld %lld", &contVec[k].boxes, &contVec[k].matches);}

    sort(contVec.begin(), contVec.end(), contSort);

    long capacity = n;
    long long output(0);
    for(long k = m - 1; k >= 0; k--){
        if(capacity > contVec[k].boxes){
            output += contVec[k].boxes * contVec[k].matches;
            capacity -= contVec[k].boxes;
        }
        else{
            output += capacity * contVec[k].matches;
            break;
        }
    }

    printf("%lld\n", output);

    return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 25

// a[N] is to store the number of matchboxes
// b[N] is to store the number of match in every matchbox
// c[N] is to store the i-th container
int a[N],b[N],c[N];
bool cmp(int i,int j)
{
    return b[i]>b[j];
}

int main()
{
    int n,m;
    while (cin>>n>>m)
    {
        for (int i=1;i<=m;i++)
        {
            c[i]=i;
            cin>>a[i]>>b[i];
        }
        sort(c+1,c+m+1,cmp);

        int ans=0;
        for (int i=1;i<=m;i++)
        {
            if (n>a[c[i]])
            {
                ans+=a[c[i]]*b[c[i]];
                n-=a[c[i]];
            }
            else
            {
                ans+=n*b[c[i]];
                break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

#include <iostream>
#include <list>
#include <string>
#include <vector>
#include<bits/stdc++.h>
using namespace std;
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}
int main()
{
    int n,m;
    cin >>n>>m;
    int a_i;
    int b_i;
    vector <pair <int,int> > vect;
    int count=0;
    for(int i=0;i<m;i++){
        cin >> a_i >> b_i;
        vect.push_back(make_pair(a_i,b_i));
    }

    sort(vect.begin(), vect.end(), sortbysecdesc);

    for(int y=0;y<m;y++){

        while(vect[y].first!=0&&n>=0){

        count += vect[y].second;

        vect[y].first-=1;
        n--;
        if(n==0){
            break;
        }
        }
        if(n==0){
            break;
        }
    }

    cout << count;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct container {
  long long int n_matchboxes, n_matches;
};

typedef struct container container;

bool containersort(container a, container b) {
  if (a.n_matches > b.n_matches) return true;
  return false;
}

int main(int argc, char *argv[]) {
  long long int max_matchboxes, n_containers;
  cin >> max_matchboxes >> n_containers;

  vector<container> containers;

  for (long long int i = 0; i < n_containers; i++) {
    long long int n_matchboxes, n_matches;
    cin >> n_matchboxes >> n_matches;

    container newCont = {n_matchboxes, n_matches};
    containers.push_back(newCont);
  }

  sort(containers.begin(), containers.end(), containersort);

  long long int max_matches = 0;
  long long int container_index = 0;
  while (max_matchboxes != 0 && container_index < containers.size()) {
    long long int can_carry = min(max_matchboxes, containers[container_index].n_matchboxes);
    max_matches += can_carry * containers[container_index].n_matches;
    max_matchboxes -= can_carry;
    container_index++;
  }

  cout << max_matches << endl;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int INF=21;
struct _node_k {
        int a,b;
        _node_k(){
                a=b=0;
        }
} a[INF];
__inline__ bool cmp(_node_k b,_node_k c) {
        return b.b>c.b;
}
int n,m;
long long sum;
signed main()
{
        scanf("%d %d",&n,&m);
        for (int i=1; i<=m; i++)
                scanf("%d %d",&a[i].a,&a[i].b);
        sort(a+1,a+1+m,cmp);
        for (int i=1,t=0; i<=m; i++) {
                for (int j=1; j<=a[i].a; j++) {
                        sum=sum+a[i].b;
                        t++;
                        if (t==n) break;
                }
                if (t==n) break;
        }
        printf("%lld\n",sum);
        return 0;
}

#include<bits/stdc++.h>

using namespace std;

int a[50000],b[50000];
long long ans=0;

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&a[i],&b[i]);
	for(int i=1;i<=m-1;i++)
		for(int j=i+1;j<=m;j++)
		{
			if(b[i]<b[j]){
				swap(a[i],a[j]);
				swap(b[i],b[j]);
			}
		}
	for(int i=1;;i++){
		if(a[i]<n){
			ans+=a[i]*b[i];
			n-=a[i];
		}
		else {
			ans+=n*b[i];
			break;
		}
	}
	cout<<ans<<endl;

	return 0;
}

#include<iostream>
using namespace std;
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<utility>
#include<assert.h>
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
#define fr(i ,n)  for (int i = 0; i < n; i++)
#define pb(x)  pushback(x)
#define fr(i,n)   for (ll i = 0; i < n; i++)
typedef long long ll;
#define adjList  vector<vector<int> >
#define all(a)  a.begin(),a.end()

ll n, m;
vector<pair<ll, ll> > a;

bool sortBySec(pair<int, int> x, pair<int, int>  y)
{
	return x.second > y.second;
}

int main()
{
	cin >> n >> m;
	a.resize(m);
	fr(i, m)
		cin >> a[i].first >> a[i].second;
	sort(all(a),sortBySec);
	ll ans=0;
	fr(i, m)
	{
		if (n == 0)
			break;
		if (a[i].first >= n)
		{
			ans += n * a[i].second;
			n -= n;
		}
		else
		{
			ans += a[i].first * a[i].second;
			n -= a[i].first;
		}
	}
	cout << ans;
	return 0;
}

#include <iostream>
using namespace std;
int main()
{
	long long n,m,sum=0;
	cin>>n>>m;
	int a[m];
	int b[m];
	for(int i=0; i<m;i++)
	{
		cin>>a[i];
		cin>>b[i];
	}
	for(int i=0; i<m-1; i++)
	 for(int j=0; j<m-i-1; j++)
	 if(b[j]<b[j+1])
	 {
	 	swap(b[j],b[j+1]);
	 	swap(a[j],a[j+1]);
	 }
	int k=0;
	int l=0;
    while(l<=n && k<m)
    {
    if (a[k]<=n-l)
    {
    	sum+=b[k]*a[k];
    	l+=a[k];
    	k++;
	}
    else
	{
	  sum+=b[k]*(n-l);
	  l=n+1;
    }
   }
    cout<<sum;
	return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
int main(){
    int val[30];
    int indx[15];
    int n,m;
    cin>>n>>m;
    int cm = m;
    int i =0;
    for(int s = 0;s<15;s++)indx[s]=0;
    for(int s = 0;s<30;s++)val[s]=0;

    while(cm--)
    {
        int a,b;
        cin>>a>>b;
        if(!(count(val,val+m-cm,b)))val[i]=b;
        indx[b]+=a;
        i++;
    }
    sort(val,val+m);
    int res = 0;
    for(int j=m-1;j>=0;j--){
        if(n-indx[val[j]]>=0){
            res+=val[j]*indx[val[j]];
            n-=indx[val[j]];
        }
        else if(n-indx[val[j]]<0){
            res+=val[j]*(n);
            break;
            }
    }
    cout<<res;
    }

#include <iostream>
#include <vector>
#include <algorithm>
#define _LL long long
using namespace std;
struct pak{
	_LL a,b;
	bool operator < (const pak &tp)const{return b>tp.b;};
	}tp;
int main(){
	_LL n,m;
	while(cin>>n>>m){
		_LL res=0;
		vector<pak>L;
		for(int i=1;i<=m;i++){
			cin>>tp.a>>tp.b;
			L.push_back(tp);
			}
		sort(L.begin(),L.end());
		for(int i=0;i<L.size()&&n;i++){
			_LL take=min(n,L[i].a);
			res+=take*L[i].b;
			n-=take;
			}
		cout<<res<<endl;
		}
	return 0;
	}

#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> ii;
ii A[100];
long int ans;
int n,m,a,b, i;
int main()
{
    cin>>n>>m;
    for (int i=0; i<m; i++)
	{
        cin>>a>>b;
        A[i].first=b;
        A[i].second=a;
    }
    sort(A,A+m);
    ans=0;
    i=m-1;
    while ((n>0) && (i>=0))
	{
        if (A[i].second>n)
		{
            ans+=(n*A[i].first);
            n=0;
        }
        else{
            ans+=(A[i].second*A[i].first);
            n-=A[i].second;
        }
        i--;
    }
    cout<<ans<<endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
}a[21];
bool cmp(node a,node b){return a.y>b.y;}
int n,m,s,t;
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++)cin>>a[i].x>>a[i].y;
	sort(a+1,a+1+m,cmp);
        for(int i=1;i<=m;i++){
    	    for(int j=1;j<=a[i].x;j++){
    			t++;
    			s+=a[i].y;
    			if(t==n)break;
    	    }
    	if(t==n)break;
        }
        cout<<s;
}

#include <IOSTREAM>
using namespace std;

typedef struct {
	int a;
	int b;
} con;

void order (con* cons, int n)
{
	int i,j;
	con temp;
	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n - i - 1; j++)
		{
			if (cons[j].b < cons[j + 1].b)
			{
				temp = cons[j];
				cons[j] = cons[j + 1];
				cons[j + 1] = temp;
			}
		}
	}
}

int main(int argc, char* argv[])
{
	int m,n;
	int add = 0;
	cin >> n >> m;
	int i;
	con cons[50];
	int result = 0;
	for (i = 0; i < m; i++)
	{
		cin >> cons[i].a >> cons[i].b;
	}
	order (cons,m);
	for (i = 0; i < m && add < n; i++)
	{
		if (n - add > cons[i].a)
		{
			result += cons[i].b * cons[i].a;
		}
		else
		{
			result += cons[i].b * (n - add);
		}
		add += cons[i].a;
	}
	cout << result << endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int a;
    int b;
};
int cmp(node x,node y)
{
    if(x.b>y.b)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    int n,m,i,ans=0;
    node s[25];
    cin>>n>>m;
    for(i=0;i<m;i++)
    {
        cin>>s[i].a>>s[i].b;
    }
    sort(s,s+m,cmp);
    for(i=0;i<m;i++)
    {
        if(n==0)
        {
            break;
        }
        else if(n>s[i].a)
        {
            ans+=s[i].a*s[i].b;
            n-=s[i].a;
        }
        else
        {
            ans+=n*s[i].b;
            n=0;
        }
    }
    cout<<ans<<endl;
return 0;
}

/*
    We touch I feel a rush
    We clutch
    It isn't much but its enough to make me wonder what's in store for us
    It's lust its torturous,
    You must be a sorceress 'cause you just did the impossible
    Gained my trust don't play games it'll be dangerous, if you fuck me over
    'Cause if I get burnt I'ma show you what it's like to hurt
    'Cause I've been treated like dirt before you and love is "evol"
    Spell it backwards I'll show ya

        Nobody knows me I'm cold, walk down this road all alone
        It's no one's fault but my own, it's the path I've chosen to go
        Frozen as snow I show no emotion whatsoever so
        Don't ask me why I have no love for these mofuckin' hos
        Bloodsucking succubuses, what the fuck is up with this,
        I've tried in this department but
        I ain't had no luck with this,
        It sucks but it's exactly what I thought it would be like tryin' to start over
        I gotta hole in my heart for some kind of emotional rollercoaster
        Something I wont go until you toy with my emotion so it's over
        It's like an explosion every time I hold ya, I wasn't jokin' when I told ya
        You take my breathe away
        Your a supernova, and I'm a

    I'm a space bound rocket ship and your heart's the moon
    And I'm aiming right at you
    Right at you
    Two hundred fifty thousand miles on a clear night in June
    And I'm aiming right at you
    Right at you
    Right at you

        I'll do whatever it takes, when I'm with you I get the shakes
        My body aches when I ain't
        With you I have zero strength
        There's no limit on how far I would go
        No boundaries no lengths
        Why do we say that until we get that person that we think is
        Gonna be that one and then once we get 'em it's never the same
        You want them when they don't want you, soon as they do, feelings change
        It's not a contest and I ain't on no conquest for no mate
        I wasn't looking when I stumbled onto you, must've been fate
        But so much is at stake what the fuck does it take,
        Let's cut to the chase, 'fore a door shuts in your face
        Promise me if I cave in and break and leave myself open, that I wont be makin' a mistake

    I'm a space bound rocket ship and your heart's the moon
    And I'm aiming right at you
    Right at you
    Two hundred fifty thousand miles on a clear night in June
    And I'm aiming right at you
    Right at you
    Right at you

        So after a year and six months, no longer me that you want
        But I love you so much it hurts,
        Never mistreated you once
        I poured my heart out to you,
        Let down my guards swear to God
        I'll blow my brains in your lap, lay here and die in your arms
        Drop to my knees and I'm pleadin' I'm tryin' to stop you from leavin'
        You won't even listen so fuck it, I'm tryin' to stop you from breathin'
        I put both hands on your throat
        I sit on top of you squeezin' til I snap ya neck like a popsicle stick
        Ain't no possible reason , I could think of to let you walk up out this house and let you live
        Tears stream down both of my cheeks
        Then I let you just go and just give
        Before I put that gun to my temple, I told you this

    And I would've done anything for you
    To show you how much I adored you
    But it's over now, It's too late to save our love
    Just promise me you'll think of me
    Every time you look up in the sky and see a star 'cause I'm a

        I'm a space bound rocket ship and your heart's the moon
        And I'm aiming right at you
        Right at you
        Two hundred fifty thousand miles on a clear night in June
        And I'm so lost without you
        Without you
        Without you

*/

////////////////////////////////////// - Template - ///////////////////////////////////

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define fastio() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define rep(i,a,b) for(int i=a;i<b;++i)
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define rpe(i,a,b) for(i=a-1;i>=b;i--)
#define rp(i,n) rep(i,0,n)
#define fre(i,n) fr(i,1,n)
#define trav(a, x) for (auto& a : x)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define endl "\n"
#define F first
#define S second
#define vi vector<int>
#define vii vector< vector<int> >
#define vpi vector< pair<int,int> >
#define viii vector< vector<vector<int> > >
#define cd complex<double>
#define PI acos(-1)
#define pie 3.14159265358979323846
#define all(c) (c).begin(),(c).end()
#define prec(n) fixed << setprecision(n)
#define print_time {cout << flush; cerr << endl << "Time taken: " << fixed << setprecision(3) << 1.0 * clock() / CLOCKS_PER_SEC << " secs\n";}
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b)) / gcd((a),(b))
#define bc(n) __builtin_popcountll(n)
#define leadzero(a) __builtin_clz(a) // count leading zeroes
#define trailzero(a) __builtin_ctz(a) // count trailing zeroes
#define bitcount(a) __builtin_popcount(a) // count set bits

typedef long double ld;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define TRACE

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<"  ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int inft = (1LL<<61) ;
const int inf = 5e18;
const int mod = 1e9+7;

int modpow(int x, int n, int mod)
{
    x %= mod ;
    int res = 1;
    while(n>0){
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

const int N = 1e6+1e4+1e2+1 ;
const int M = 3*(1e5+1e3+1e1) ;

///////////////////////////  Code Starts from here /////////////////////////////

signed main()
{
    fastio() ;
    cin.exceptions(cin.failbit);
    srand(chrono::steady_clock::now().time_since_epoch().count());

    #ifndef ONLINE_JUDGE
       //freopen("input.txt", "r", stdin);
    #endif // ONLINE_JUDGE

    int n,m,ans=0 ;
    cin >> n >> m ;

    pair<int, int> p[m] ;

    rp(i,m)
    {
        int a,b ;
        cin >> a >> b ;
        p[i].S = a ;
        p[i].F = b ;
    }

    sort(p,p+m) ;

    for(int i=m-1 ; i>=0 ; i--)
    {
        ans+=min(n,p[i].S)*(p[i].F) ;
        n-=min(n,p[i].S) ;

        if(n==0)
        {
            break ;
        }

    }
    cout << ans ;

return 0 ;
}

#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <set>
using namespace std;
long long gcd(long long a, long long b)
{
     if (b == 0)
         return a;
    return gcd(b, a % b);
}
void dalikliai(long long n,long long dal[],long long &ind)
{
  int kiek=0;
  long long kur=n;
  if(n%2==0)
  {
    kiek++;
    n/=2;
    while(n%2==0)
    {
      n/=2;
      kiek++;
    }
    dal[ind++]=kiek;
  }
  kur=n;
  long long da=3;
  while(n!=1 && da<sqrt(n))
  {
    kiek=0;
    if(n%da==0)
    {
      kiek++;
      n/=da;
      while(n%da==0)
      {
        n/=da;
        kiek++;
      }
      dal[ind++]=kiek;
    }
    da+=2;

  }
  if(n!=1)
    {
      if(da*da==n)
      dal[ind++]=2;
      else
      dal[ind++]=1;
      n=1;
    }
}
int main()
{
  long long n,m;
  long long a,b,sum=0;
  cin>>m>>n;
  map<long long,long long>maps;
  set<long long>aibe;
  for(int i=0;i<n;i++)
  {
    cin>>a>>b;
    aibe.insert(b);
    maps[b]+=a;
  }
  long long max;
  while(m!=0)
  {
    max=*aibe.rbegin();
    aibe.erase(max);
    if(maps[max]<=m)
    {
      m-=maps[max];
      sum+=maps[max]*max;
    }
    else
    {
      sum+=max*m;
      m=0;
    }
    if(aibe.size()==0)
    m=0;
    //cout<<m<<endl;
  }
  cout<<sum;

}

#include<bits/stdc++.h>
using namespace std;
long long n,m;
struct node{
	long long a,b;
}s[110];
bool cmp(node x,node y)
{
	return x.b>y.b;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>s[i].a>>s[i].b;
	}
	sort(s+1,s+m+1,cmp);
	long long ans=0;
	for(int i=1;i<=m;i++)
	{
		if(s[i].a<=n)
		{
			n-=s[i].a;
			ans+=s[i].a*s[i].b;
		}
		else
		{
			ans+=s[i].b*n;
			n=0;
		}
		if(n==0)
		{
			break;
		}
	}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>
/*
 **
 *** Your best is not enough
 **
 */
using namespace std;
#define all(v)          ((v).begin()), ((v).end())
#define finish(x) return cout << x << endl, 0;
#define pb push_back
#define Compiler_Beso ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define clr(v,d)        memset(v, d, sizeof(v))
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;

void fre(){
    freopen("","r",stdin);freopen("","w",stdout);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int FixMod(int s,int k){return (s%k+k)%k;}
double dic(pair<int,int>a,pair<int,int>b)
{return (sqrt( (a.first-b.first)*(a.first-b.first) +  (a.second-b.second)* (a.second-b.second) ));}

enum dir       { DOWN = 0, UP, RIGHT, LEFT, DOWN_RIGHT, DOWN_LEFT, UP_RIGHT, UP_LEFT };
int  dir_r[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int  dir_c[] = { 0, 0, 1, -1, 1, -1, 1, -1 };

int main(){

    Compiler_Beso

    int n,m;
    cin>>n>>m;
    priority_queue<pair<int,int>>p;
    while (m--) {
        int x,y;
        cin>>x>>y;
        p.push({y,x});
    }
    ll ans=0;
    while (!p.empty() && n) {
        pair<int, int>cur=p.top();
        p.pop();
        if(cur.second>n){
            ans+=cur.first*n;
            n=0;
        }
        else
            ans+=cur.first*cur.second,n-=cur.second;
    }
    cout<<ans;

    return 0;

}

#include<bits/stdc++.h>
using namespace std;

#define endl "\n"
#define ll long long int
#define u unsigned
#define vii vector<ll>
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz size()
#define tt int t;cin>>t;while(t--)
#define loop(n) for(int i=0;i<n;i++)
#define lop(a,n) for(int i=a;i<=n;i++)
#define rloop(n) for(int i=n-1;i>=0;i--)
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define ioboost ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

struct ara
{
    ll a,b;
};
bool gre(struct ara aa,struct ara bb){return aa.b>bb.b;}

int main()
{
    ioboost;
    ll n,m,sna=0,total=0;cin>>n>>m;
    struct ara a[m+3];
    loop(m){
        cin>>a[i].a>>a[i].b;
        sna+=a[i].a*a[i].b;
        total+=a[i].a;
    }
    sort(a,a+m,gre);
    //loop(m) cout<<a[i].a<<" "<<a[i].b<<endl;
    if(total<n){
        cout<<sna<<endl;
        return 0;
    }
    ll i=0,j=0,ans=0;
    while(j<m){
        if(i>=n) break;
        if((a[j].a+i)<=n){
            ans+=a[j].a*a[j].b;
            i+=a[j].a;
            j++;
            //cout<<"ans1 = "<<ans<<endl;
        }
        else{
            ans+=(n-i)*a[j].b;
            i+=(n-i);
            j++;
            //cout<<"ans2 = "<<ans<<endl;
        }
    }
    cout<<ans<<endl;
    return 0;
}

// B.cpp: определяет точку входа для консольного приложения.
//

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>

#define fr(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define fd(i,a,b) for(int i=(int)(a);i>=(int)(b);i--)
#define be(m) (m).begin(),(m).end()
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef double d;
typedef vector<int> vi;
typedef vector<d> vd;
typedef vector <string> vs;
typedef stack<int> si;
typedef stack<string> ss;
typedef map<string,int> msi;
typedef map<int,int> mii;
typedef list<int> li;
typedef list<string> ls;
typedef pair<int,int> pii;
typedef pair<string,string> pss;
typedef vs::iterator vs_it;
typedef msi::iterator msi_it;
typedef mii::iterator mii_it;
typedef li::iterator li_it;
typedef ls::iterator ls_it;

const int e9=1000*1000*1000;
const ll e18=(ll)e9*(ll)e9;
const d pi=3.1415926535;

void pV(int num){cout << num << " ";}
int ri(){int x;scanf("%d",&x);return x;}
d rd(){d x;scanf("%lf",&x);return x;}
ll rll(){ll x;scanf("%lld",&x);return x;}
string rs(){string x;cin >> x;return x;}
string rsf(){string s;char c; while(c=getchar()){if(c<65 || c>90)break;s+=c;}return s;}
void wi(int x){printf("%d ",x);}
void wd(d x){printf("%lf ",x);}
void wll(ll x){printf("%lld ",x);}
void ws(string x){cout << x << " ";}

pii mas[25];

bool q(pii x, pii y)
{
    return x.second>y.second;
}

int main()
{
   int n=ri();int m=ri();
    fr(i,0,m-1)
    {
        mas[i].first=ri();
        mas[i].second=ri();
    }
    sort(mas,mas+m,q);
    ll sum=0;int j=0;
    while(n!=0 && j<m)
    {
        if (mas[j].first<=n)
        {
            sum+=mas[j].first*mas[j].second;
            n-=mas[j].first;
        }
        else
        {
            sum+=n*mas[j].second;
            n=0;
        }
        j++;
    }
    wll(sum);
    return 0;
}

#include<stdio.h>
int main()
{
    int n,m,k,a[50],b[50],i,j,mm,sum;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++)
       scanf("%d%d",&a[i],&b[i]);
    k=n;sum=0;
    for(i=1;i<=m;i++)
    {
       mm=1;
       for(j=2;j<=m;j++)
          if(b[mm]<b[j])mm=j;
       if(k<a[mm])
       {
          sum+=k*b[mm];
          break;
       }
       else
       {
          sum+=a[mm]*b[mm];
          b[mm]=0;
          k-=a[mm];
          if(k==0)break;
       }
    }
    printf("%d\n",sum);
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#define _CRT_USES_NO_DEPRECATE

using namespace std;

long long gcd (long long a, long long b)
{
    return b ? gcd (b, a%b) : a;
}

long long binpow (long long a, int n)
{
    if ((!a) && (!n) ) return 0;
    long long res =1;
    while (n)
    {
        if (n & 1)
            res *= a;
        a *= a;
        n = n >> 1;
    }
    return res;
}

void InSort(pair <int , int  >  *s, long long n)
{
    int min;
    for (int i = 0; i < n; ++i)
    {
        min = i;
        for (int j = i + 1; j < n; ++j)
            if (s[j].first < s[min].first) min = j;
        swap(s[i], s[min]);
    }
}

void read( long long *s, int n)
{
    for (int i = 0; i < n; ++i)
        scanf("%lld", &s[i]);
}

#pragma comment (linker, "/stack:64000000")
pair <long long, long long  > pack[32];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
        long long n,m;

        cin >> n >> m;
        for (int i = 0; i < m; ++i)
            cin >> pack[i].second >> pack[i].first;

        sort (pack, pack + m);
        //InSort(pack, m);
        reverse (pack, pack + m);
        /*for (int i = 0; i < m; ++i)
            cout << pack[i].first << ' ';*/
        long long ans = 0;
        for (int i = 0; i < m && n > 0 ; ++i)
        {
            if (pack[i].second <= n)
            {
                n -= pack[i].second;
                ans += pack[i].second*1ll*pack[i].first;
            }
            else
            {
                ans += pack[i].first*1ll*n;
                n -= pack[i].second;
            }
        }
        cout << ans;

        return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
//bool cmp(int a, int b)return a>b;
int main()
{
	pair<ll,ll>ar[25];
	ll int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		ll a,b;
		cin>>ar[i].second>>ar[i].first;
	}
	sort(ar,ar+m);
	ll cnt=0;
	ll ans=0;
	int i=m-1;
	while(cnt<n && i>=0)
	{

			if(ar[i].second+cnt<n)
			{

				ans+=(ar[i].second*ar[i].first);
				cnt+=ar[i].second;
				//cout<<cnt<<" "<<ans<<endl;
			}
			else
			{

				ans+=(n-cnt)*ar[i].first;
				n=cnt;

			}
			i--;
	}
	cout<<ans<<endl;
}

#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long n,m,ans=0;
    cin>>n>>m;
    long long b[m],c[m],temp,nob=0;
    for(int i=0;i<m;++i)
    {
        cin>>b[i]>>c[i];
    }
    for(int i=0;i<m;++i)
    {
        for(int j=i+1;j<m;++j)
         {
             if(c[i]<c[j])
             {
             temp=b[i];
             b[i]=b[j];
             b[j]=temp;

             temp = c[i];
             c[i] = c[j];
             c[j] = temp;
             }
         }
    }
    int k=0;
    while(nob<=n)
    {
    	if(k>=m)
    	break;
        ans = ans + b[k]*c[k];
        nob = nob + b[k];

		++k;

    }
    --k;
    if(nob>n)
    ans = ans - (nob-n)*c[k];
    cout<<ans;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
struct node{
	int a,b;
}ca[23];
bool cmp(node na,node nb)
{
	return na.b>nb.b;
}
int m,n,ans;
int main()
{
	cin>>n>>m;
	for(int i=0;i<m;i++)cin>>ca[i].a>>ca[i].b;
	sort(ca,ca+m,cmp);
	for(int i=0;i<m;i++)
	{
		if(ca[i].a>=n)
		{
			ans+=n*ca[i].b;
			break;
		}
		ans+=ca[i].a*ca[i].b;
		n-=ca[i].a;
	}
	cout<<ans;
	return 0;
}

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <queue>
#include <ctime>
#include <iomanip>
#include <tuple>
#include <functional>
#include <map>
#include <numeric>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define vi vector<int>
#define vp vector<pi>
#define msa(A, a, n) memset(A, a, n*sizeof(A))
#define msv(A, a) fill(A.begin(), A.end(), a);
#define msc(A, n) memset(A, '\0', n)
#define rep_itr(itr, c) for(itr = (c).begin(); itr != (c).end(); itr++)
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define pern(i, b) per(i, b, 0)
#define repn(i, b) rep(i, 0, b)
#define repe(i, a, b) for(int i = a; i <= b; i++)
#define repen(i, b) repe(i, 1, b)
#define fast ios_base::sync_with_stdio();cin.tie(0);
#define endl "\n"
//#define MYDEBUG

void my_return(int code)
{
#ifdef MYDEBUG
	std::cout << "\n\nTime = " << fixed << std::setprecision(3) << double(clock()) / CLOCKS_PER_SEC << endl;
#endif
	exit(code);
}

struct MyStruct
{
	int match_boxes;
	int matches;
	bool operator<(const MyStruct& ms) {
		if (matches == ms.matches) return match_boxes < ms.match_boxes;
		else return matches < ms.matches;
	}
};

int main()
{
	fast
#ifdef MYDEBUG
		freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	/*int t;
	cin >> t;
	while (t--)
	{

	}*/

	int n, c, total_match_boxes = 0;
	cin >> n >> c;
	vector<MyStruct> vec(c);
	repn(i, c) {
		int a, b;
		cin >> a >> b;
		MyStruct ms; ms.match_boxes = a, ms.matches = b;
		vec[i] = ms;
		total_match_boxes += a;
	}

	sort(vec.begin(), vec.end());

	ll res_matches = 0;
	int i = c - 1, remaining_match_boxes = n;
	while (remaining_match_boxes != 0 && i >= 0)
	{
		MyStruct current = vec[i--];
		if (remaining_match_boxes > current.match_boxes) {
			res_matches += (ll)current.match_boxes * current.matches;
			remaining_match_boxes -= current.match_boxes;
		}
		else {
			res_matches += (ll)remaining_match_boxes * current.matches;
			remaining_match_boxes = 0;
		}
	}

	cout << res_matches << endl;

	my_return(0);
}

#include <bits/stdc++.h>

using namespace std;

bool gsp (pair<long long, long long> a, pair<long long, long long> b){
    if (a.second > b.second){
        return true;
    }
    else{
        return false;
    }
}

int main() {
    long long n, m;
    cin >> n >> m;
    vector<pair<long long, long long>> v1;
    for (int i = 0; i < m; ++i){
        int x, y;
        cin >> x >> y;
        v1.push_back({x, y});
    }
    if (m == 1){
        if (v1[0].first > n){
            cout << n * v1[0].second;
            return 0;
        }
        else{
            cout << v1[0].first * v1[0].second;
            return 0;
        }
    }
    sort (v1.begin(), v1.end(), gsp);
    /*for (pair<int, int> b : v1){
        cout << b.first << " " << b.second << endl;
    }*/
    long long x = n, l = 0;
    long long sum = 0;
    while ((x > 0) && (l < m)){
        if (x > v1[l].first){
            x -= v1[l].first;
            sum += v1[l].first * v1[l].second;
            l++;
        }
        else if (x <= v1[l].first){
            sum += x * v1[l].second;
            x = 0;
            l++;
        }
    }
    cout << sum;
}

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int a[m], b[m];
    for (int i = 0; i< m; i++)
        cin >> a[i] >> b[i];
    for (int i = 0; i <m-1; i++)
    for (int j = 0; j <m-1; j++)
    if (b[j]<b[j+1]) {
                     swap(b[j], b[j+1]);
                     swap(a[j], a[j+1]);
                     }
    int res = 0;
    int j = 0;
    while (n>0 && j < m)
    {
          res += a[j]*b[j];
          n -= a[j];
          j++;
    }
    if (n<0)
    {
            res -= abs(n)*b[j-1];
    }
    cout << res;
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n, m, x, y;
    int boxes[11] = {0};
    cin>>n>>m;
    for (int i = 0; i < m; ++i){
        cin>>x;
        cin>>y;
        boxes[y] += x;
    }

    long long count = 0;
    for(int i = 10; i > 0; --i){
        //cout<<i<<" "<<n<<" "<<boxes[i]<<"\n";
        if (n - boxes[i] < 0){
            count += n * i;
            break;
        }

        else{
            count += boxes[i] * i;
            n -= boxes[i];
        }
    }
    cout<<count;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
int capacity,containers;
cin>>capacity>>containers;
int matchbox[containers],matches[containers];
for(int i=0;i<containers;i++){
    cin>>matchbox[i]>>matches[i];
    }
int temp[containers];
for(int i=0;i<containers;i++){
    temp[i]=matches[i];
    }
sort(temp,temp+containers,greater<int>());
int flag=0;
int answer=0;
for(int i=0;i<containers;i++){
    for(int j=i+1;j<containers;j++){
        if(matches[i]==matches[j]){
           matchbox[i]+=matchbox[j];
            matchbox[j]=0;
        }
    }
}
// for(int i=0;i<containers;i++){
//     cout<<matchbox[i]<<" "<<matches[i]<<endl;
// }

for(int i=0;i<containers;i++){
        if(capacity>0){
            for(int j=0;j<containers;j++){
                if(temp[i]==matches[j]){
                    int multiplier=min(capacity,matchbox[j]);
                    capacity-=multiplier;
                    matchbox[j]-=multiplier;
                    answer+=multiplier*matches[j];
                    break;
                }
            }

        }
    }
cout<<answer;
}

/*****************************************************************************
 * codeforces:   knst
 * topcoder:     knstqq
 * projecteuler: knstqq
 * **************************************************************************/

#include <algorithm>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>

using namespace std;
//test speed
/*
namespace std {
    template <class T1, class T2>
    class hash<pair<T1, T2>> {
    public:
        size_t operator()(const pair<T1, T2>& p) const {
            return hash<T1>()(p.first) & hash<T2>()(p.second);
        }
    };
}; */
namespace std {
    template <>
    class hash<pair<long long,long long>> {
    public:
        size_t operator()(const pair<long long, long long>& p) const {
            return hash<long long>()(p.first) ^ (hash<long long>()(p.second) << 32);
        }
    };
}

constexpr long double const_pi() { return std::atan(static_cast<long double>(1))*4; }

const int dx4[] = {-1, 0, 0, 1};
const int dy4[] = {0, -1, 1, 0};

const int dx8[] = {-1, -1, -1,  0,  0,  1,  1,  1};
const int dy8[] = {-1,  0,  1, -1,  1, -1,  0,  1};

int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int main() {
    std::ios_base::sync_with_stdio(false);

    int n;
    int m;
    cin >> n >> m;
    vector<pair<int,int>> v;
    for (int i = 0; i < m; ++i) {
        int a;
        int b;
        cin >> a >> b;
        v.push_back({b, a});
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    long long result = 0;
    for (auto i : v) {
        if (i.second <= n) {
            n -= i.second;
            result += i.first * i.second;
        } else {
            result += n * i.first;
            n = 0;
        }
    }
    cout << result << endl;
    return 0;
}

#include <iostream>
#include <iomanip>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cmath>

using namespace std;

int main()
{
        int n,m;
        cin >> n >> m;

        multimap<int,int> match;
        for(int i = 0 ; i < m ; i++)
        {
                int a,b;
                cin >> a >> b;
                match.insert(multimap<int,int>::value_type(-b,a));
        }

        int res = 0;
        multimap<int,int>::iterator it= match.begin();
        while(1)
        {
                if(n-(*it).second >= 0)
                {
                        n-=(*it).second;
                        res += -(*it).first*(*it).second;
                }
                else
                {
                        res += -n*(*it).first;
                        break;
                }

                it++;
        	if(it == match.end())break;
        }

        cout << res << endl;

}

// #include <boost/multiprecision/cpp_int.hpp>
// using boost::multiprecision::cpp_int;
#include <bits/stdc++.h>
using namespace std;
// ¯\_(ツ)_/¯
#define f first
#define s second
#define p push
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define foi(i, a, n) for (i = (a); i < (n); ++i)
#define foii(i, a, n) for (i = (a); i <= (n); ++i)
#define fod(i, a, n) for (i = (a); i > (n); --i)
#define fodd(i, a, n) for (i = (a); i >= (n); --i)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define endl " \n"
#define newl cout<<"\n"
#define MAXN 100005
#define MOD 1000000007LL
#define EPS 1e-13
#define INFI 1000000000             // 10^9
#define INFLL 1000000000000000000ll //10^18
// ¯\_(ツ)_/¯
#define l long int
#define d double
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vvll vector<vll>
//vector<vector<int>> v(10, vector<int>(20,500)); 2d vector initialization. of 10 rows and 20 columns, with value 500.
#define mii map<int, int>
#define mll map<long long, long long>
#define pii pair<int, int>
#define pll pair<long long, long long>

#define fast_io()                   \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);                    \
  cout.tie(NULL);

ll tc, n, m, k;
// ll ans = 0, c = 0;
// ll i, j;
// ll a, b;
// ll x, y;

bool compare(pll& a, pll& b) {
    if(a.s == b.s) return a.f > a.s;
    return a.s > b.s;
}

int main()
{
    fast_io();
#ifndef ONLINE_JUDGE
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
#endif

    cin>>n>>k;
    vector<pll> arr(k);
    rep(i, 0, k) {
        int mb, ms;
        cin>>mb>>ms;
        arr[i] = {mb, ms};
    }
    sort(all(arr), compare);

    int ans = 0;
    int ms = 0;
    int i = 0;
    while(i < k && ans + arr[i].f < n) {
        ans += arr[i].f;
        ms += arr[i].f*arr[i].s;
        i++;
    }
    if(i < k)
        ms += arr[i].s*(n-ans);

    cout<<ms;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int sum;
int main(){
    int n,m; cin>>n>>m;
    pair<int,int>p[25];
    for(int i=0;i<m;i++){
        cin>>p[i].second>>p[i].first;
    }
    sort(p,p+m);
    int i=m-1;
    while(n>0&&i>=0){
        sum += (min(n,p[i].second))*p[i].first;
        n -=p[i].second;
        i--;

    }
    cout<<sum;

	return 0;
}

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
	int bagCapacity, m, arr[20][20], count = 0, countMbox = 0, countm = 0;

	cin >> bagCapacity >> m;

	for (int i = 0;i < m;i++)
	{
		cin >> arr[i][0] >> arr[i][1];
		countMbox += arr[i][0];                              // inputs
		countm += arr[i][0] * arr[i][1];
	}

	if (countMbox <= bagCapacity)
	{
		cout << countm;                                      //  Special case
		return 0;
	}

	while (bagCapacity)
	{
		int x = 0;
		for (int i = 0;i < m;i++)
			if (arr[x][1] < arr[i][1])                         // Determine matchboxes that contain the largest matches
				x = i;

		if (bagCapacity <= arr[x][0])
		{
			count += bagCapacity*arr[x][1];
			cout << count;
			return 0;
		}
		else                                                    // count number of matches can steal
		{
			count += arr[x][0] * arr[x][1];
			bagCapacity = bagCapacity - arr[x][0];
			arr[x][1] = 0;

		}

	}

	return 0;
}

#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
class dato{
   public:
   unsigned long long a,b;
};
bool operator < (dato a,dato b){
    if (a.b>b.b)
        return true;
    else
     if (a.b==b.b && a.a>=b.a)
         return true;
     else
        return false;
}
dato arr[20];
long i,j,k;
unsigned long long res,m,n;
int main(){
   cin>>n>>m;
   for(i=0;i<m;i++){
       cin>>arr[i].a>>arr[i].b;}
   sort(arr,arr+m);
   for(i=0;i<m;i++)
       if(arr[i].a<n){
           res+=arr[i].a*arr[i].b;
           n-=arr[i].a;
       }
       else{
          res+=n*arr[i].b;
          break;
       }
   cout<<res<<"\n";

}

#include<cstdio>
#include<algorithm>
using namespace std;

int n, m, srt[22], a[22], b[22], ans, t1;

bool cmp(int i, int j) {return b[i] > b[j];}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &a[i], &b[i]);
    srt[i] = i;
  }
  sort(srt + 1, srt + m + 1, cmp);

  for (int i = 1; i <= m; i++) {
    t1 = min(n, a[srt[i]]);
    n -= t1;
    ans += t1 * b[srt[i]];
  }

  printf("%d\n", ans);

  return 0;
}

#include <stdio.h>
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long lld;

struct Matchbox {
    lld a, b;
    lld num;
    bool operator<(Matchbox o) const {
        return b>o.b;
    }
} box[25];

int main(int argc, char **argv)
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF) {
        int i;
        lld a, b;
        for(i = 0; i < m; i++) {
            cin>>a>>b;
            box[i].a = a;
            box[i].b = b;
            box[i].num = a*b;
        }
        lld total = 0;
        sort(box, box+m);
        i  = 0;
        while(n>0 && i < m) {
            n -= box[i].a;
            total += box[i].num;
            i++;
        }
        if(n < 0) {
            i--;
            n += box[i].a;
            total -= box[i].num;
            total += n*box[i].b;
        }
        cout<<total<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

struct container{
    unsigned long box;
    int matches;
};

bool cmp(container &a, container &b)
{
    return a.matches>b.matches;
}

int main()
{
    unsigned int n,i;
    int m;
    unsigned long long ans = 0;
    cin>>n>>m;
    container con[m];
    for(i=0;i<m;++i)
        cin>>con[i].box>>con[i].matches;
    sort(con,con+m,cmp);

    i=0;
    while(n)
    {
        if(n>=con[i].box)
        {
            ans += (con[i].box * con[i].matches);
            n -= con[i].box;
        }
        else
        {
            ans += (n * con[i].matches);
            break;
        }
        ++i;
        if(i>=m) break;
    }
    cout<<ans<<endl;
}

#include <iostream>
#include <algorithm>

using namespace std;

pair<long long,long long> p[25];
long long n,m,i,tot,cant;

int main()
{
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>p[i].second>>p[i].first;
    }
    /*for(i=0;i<m;i++){
        cout<<p[i].first<<" "<<p[i].second<<endl;
    }*/
    sort(p,p+m);
    /*cout<<endl;
    for(i=0;i<m;i++){
        cout<<p[i].first<<" "<<p[i].second<<endl;
    }*/
    i=m-1;cant=0;
    while(cant+p[i].second<=n && p[0].second>0){
        if(p[i].second>0){
            tot+=(p[i].first*p[i].second);
            cant+=p[i].second;
            p[i].second=0;
            i--;
        }
    }
    //cout<<n<<" "<<cant<<" "<<tot<<"\n";

    tot+=(p[i].first*(n-cant));
    /*cant+=p[i].second;
    p[i].second=0;*/
    cout << tot << endl;
    return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{

	int can = 0, boxes = 0, max = 0, maches = 0;
	cin >> can >> boxes;
	vector<pair<int, int>>box(boxes);
	for (size_t i = 0; i < boxes; i++)
	{
		cin >> box[i].second >> box[i].first;
	}
	sort(box.rbegin(), box.rend());
	int i = 0;
	while (can > 0)
	{
		if (can > box[i].second) {
			maches += box[i].second * box[i].first;
			can -= box[i].second;
		}
		else
		{
			cout << (maches += can * box[i].first); return 0;
		}
		i++;
		if (i == boxes) { cout << maches; return 0; }
	}

}

#include<iostream>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<vector>
#include<utility>

// #include<bits/stdc++.h>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<pair< int,int> > vp(m);
    for(int i = 0;i < m;i++){
        cin>>vp[i].second>>vp[i].first;
    }
    sort(vp.begin(),vp.end());
    reverse(vp.begin() , vp.end());
    int tot = 0,num = 0, i = 0;
    while(i < m && num + vp[i].second <= n ){
        tot+=(vp[i].first*vp[i].second);
        num+=(vp[i].second);
        i++;
    }
    if(num == n || i>= m){
        cout<<tot<<'\n';
    }
    else{
            int r = n-num;
            tot+= ((r)*vp[i].first);
            cout<<tot<<'\n';
    }
    return 0;
}

#include<cstdio>
#include<string>
struct box
{
int matchbox;
int match;
}a[20],t;

int main(void)
{
int n,m,sum,start,i,k,j;
struct box t;
while(scanf("%d%d",&n,&m) != EOF)
{
for(i = 0;i <m;i++)
scanf("%d%d",&(a[i].matchbox),&(a[i].match));
for(i = 0;i < m-1;i++)
for(j= i+1;j <m;j++)
if(a[i].match < a[j].match)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i = 0,start = 1,sum=0;i< m;i++)
{
for(j = 0;j < a[i].matchbox;j++)
if(start<=n)
{
start++;
sum += a[i].match;
}
}
printf("%d\n",sum);
}
return 0;
}

#include<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
    int a,b;
}x[1111111];
int cmp(node a,node b)
{
    return a.b<b.b;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
        scanf("%d%d",&x[i].a,&x[i].b);
    sort(x+1,x+1+m,cmp);
    long long ans=0;
    for(int i=m;i>=1;i--)
        if(n>x[i].a)
    {
        n-=x[i].a;
        ans+=x[i].a*x[i].b;
    }
    else
    {
        ans+=x[i].b*n;
        break;
    }
    printf("%I64d\n",ans);
}
/*2017-07-23 19:15:05.466*/

#include<iostream>
#include<cstdio>
#include<queue>
#define N 21
#define ll __int64
using namespace std;
struct matches
{
    ll ai;
    ll bi;
    bool friend operator<(matches m1,matches m2)
    {
        return m1.bi<m2.bi;
    }
};
int main()
{
    ll n,m;
    matches match[N];
    while (cin>>n>>m)
    {
        priority_queue<matches>que;
        for (int i=0;i<m;i++)
        {
            cin>>match[i].ai>>match[i].bi;
            que.push(match[i]);
        }

        ll ans=0;
        while (n>0 && !que.empty())
        {
            matches tmp=que.top();
            que.pop();
            if (n-tmp.ai>=0)
            {
                ans+=tmp.ai*tmp.bi;
                n-=tmp.ai;
            }
            else if (n-tmp.ai<0)
            {
                ans+=n*tmp.bi;
                break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iterator>
#include <cstdio>
#include <vector>
#include <string>
#include <numeric>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include<utility>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pdd> vpdd;
typedef vector<string> vs;
typedef set<int> si;
typedef multiset<int> Si;
typedef map<int, int> mi;
typedef multimap<int, int> Mi;
void read(char& a) { do scanf("%c", &a); while (isspace(a)); }
void write(const char& a) { printf("%c", a); }
char BUF[1234560];
void read(string& a) { scanf("%s", BUF); a = string(BUF); }
void write(const string& a) { printf("%s", a.c_str()); }
void read(int& a) { scanf("%d", &a); }
void write(const int& a) { printf("%d", a); }
void read(ll& a) { scanf("%I64d", &a); }
void write(const ll& a) { printf("%I64d", a); }
void read(double& a) { scanf("%lf", &a); }
void write(const double& a) { printf("%lf", a); }
void read(ld& a) { scanf("%Lf", &a); }
void write(const ld& a) { printf("%Lf", a); }
void read(pair<int, int>& a) { scanf("%d%d", &a.first, &a.second); }
void write(const pair<int, int>& a) { printf("%d %d", a.first, a.second); }
template<template<typename T, typename A = allocator<T> > class C, class T>
void read(C<T>& a) { for (auto i = a.begin(); i != a.end(); ++i) read(*i); }
template<template<typename T, typename A = allocator<T> > class C, class T>
void write(const C<T>& a) { int sz = a.size(); if (sz != 0) write(a[0]); for (int i = 1; i < sz; ++i) { write(' '); write(a[i]); } }
template<typename T1, typename T2>
void read(T1& t1, T2& t2) { read(t1); read(t2); }
template<typename T1, typename T2>
void write(const T1& t1, const T2& t2) { write(t1); write(' '); write(t2); }
template<typename T1, typename T2, typename T3>
void read(T1& t1, T2& t2, T3& t3) { read(t1); read(t2); read(t3); }
template<typename T1, typename T2, typename T3, typename T4>
void read(T1& t1, T2& t2, T3& t3, T4& t4) { read(t1); read(t2); read(t3); read(t4); }
template<typename T1, typename T2, typename T3>
void write(const T1& t1, const T2& t2, const T3& t3) { write(t1); write(' '); write(t2); write(' '); write(t3); }
template<template<typename T, typename A = allocator<T> > class C, class T>
void sort(C<T>& a) { sort(a.begin(), a.end()); }
template<template<typename T, typename A = allocator<T> > class C, class T>
void rsort(C<T>& a) { sort(a.rbegin(), a.rend()); }
template<typename T>
int sz(const T& a) { return int(a.size()); }
#define MX 1000000000
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define T(n) for(int _VARIABLE{(n) + 1}; --_VARIABLE;)
#define F(b,e,i) for(auto i = (b) < decltype(b)(e) ? (b) : decltype(b)(e); i != ((b) < decltype(b)(e) ? decltype(b)(e) : (b)); ++i)
#define G(f,l,i) for(auto i = ((f) < decltype(f)(l) ? (f) : decltype(f)(l)) - 1; i++ < decltype(i)((f) < decltype(f)(l) ? decltype(f)(l) : (f)) ;)
#define M(f,l,i,j) for(auto i = (f) , j = decltype(f)(l) ; i < j; ++i, --j)
#define r(...) read(__VA_ARGS__)
#define R(type, ...) type __VA_ARGS__; r(__VA_ARGS__)
#define w(...) write(__VA_ARGS__)
#define W(...) write(__VA_ARGS__), write('\n')
#define bgn begin()
#define rbgn rbegin()
#define nd end()
#define rnd rend()
#define pb push_back
#define fnt front()
#define bck back()

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	R(int, n, m);
	vpii v;
	T(m) {
		R(int, a, b);
		v.pb({ b, a });
	}
	rsort(v);
	int s{};
	for (auto& V : v) {
		int S{ min(n,V.second) };
		s += S * V.first;
		n -= S;
		if (n == 0)
			break;
	}
	W(s);
}
//
// ArtemParkhomets
//

#include<iostream>
using namespace std;
int main()
{int n,m;cin>>n>>m;int a[m],b[m];
    for(int i=0;i<m;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(int i=0;i<m;i++)
    {
        for(int j=i;j<m;j++)
        {
            if(b[i]<b[j])
            {
                swap(b[i],b[j]);
                swap(a[i],a[j]);
            }
            if(b[i]==b[j])
            {
                if(a[i]<a[j])swap(a[i],a[j]);
            }
        }
    }
   // for(int i=0;i<m;i++)cout<<a[i]<<" "<<b[i]<<endl;
   int sum1=0,sum2=0;
   for(int i=0;i<m;i++)
   {sum1=sum1+a[i];
     if(sum1==n)
     {
         sum2=sum2+a[i]*b[i];
         break;
     }
     else if(sum1<n)
     {
         sum2=sum2+a[i]*b[i];
     }
     else
     {
      int r=sum1-a[i];
      int p=n-r;
      sum2=sum2+p*b[i];
      break;
     }
   }
   cout<<sum2;
    return 0;
}

#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define fs first
#define sc second
#define endl '\n'
#define rep(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x)       (x).begin(), (x).end()
#define descorder greater<int>() //for descending sort
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

string blank = "";

void solve(){
	int n, m;
	cin >> n >> m;
	vector< pair<int,int> > cont(m, pair<int,int>(0,0));
	rep(i, 0, m){
		cin >> cont[i].sc >> cont[i].fs;
	}
	sort(all(cont));
	// rep(i, 0, m){
	// 	cout << cont[i].sc << " " << cont[i].fs << endl;
	// }
	int i = cont.size();
	int ans = 0;
	while(n && i > 0){
		i--;
		if(n >= cont[i].sc){
			ans += cont[i].fs * cont[i].sc;
			n-=cont[i].sc;
		}
		else{
			ans += cont[i].fs * n;
			break;
		}
	}
	cout << ans;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	solve();
}

#include <climits>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

struct pair_t {
  unsigned long long a, b;
  pair_t(unsigned long long aa = 0, unsigned long long bb = 0) {
    a = aa; b = bb;
  }
  bool operator<(const pair_t &p) const {
    return b >= p.b;
  }
};

pair_t s[28];

int main() {
  unsigned long long n, m, a, b, r = 0;

  cin>>n>>m;
  for(int i = 0; i < m; ++i) {
    cin>>a>>b;
    s[i] = pair_t(a, b);
  }
  sort(s, s + m);

  int i = 0;
  while(i < m && n > 0) {
    if(s[i].a >= n) {
      r += n * s[i].b;
      n = 0;
    } else {
      r += s[i].a * s[i].b;
      n -= s[i].a;
    }
    ++i;
  }
  cout << r << endl;
  return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
struct M
{
    int a,b;
    bool operator < (M A){
        if (b<=A.b) return false;
        else return  true;
    }
}match[20];
int main()
{
    int n,m,a[20],b[20];
    cin>>n>>m;
    for (int i=0;i<m;++i)
        cin>>match[i].a>>match[i].b;
    sort(match,match+m);
    int set=0,t=0;
    long long pik=0;
    while (set!=n){
        int p;
        if (n-set>match[t].a) {
            set+=match[t].a;
            p=match[t].a;
        }
        else{
            p=n-set;
            set=n;
        }
        pik+=match[t].b*p;
        ++t;
        if (t==m) break;
    }
    cout<<pik<<endl;
    return 0;
}

#include <iostream>
#include <stdio.h>
# include <string>
using namespace std;

int main () {

  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
	long long n,t,s,p;
	int m,k,i;
	int a[21][3]={0};
	cin>>n>>m;
	for (k=1; k<=m; k++)
		cin>>a[k][1]>>a[k][2];
	for (k=1; k<m; k++)
		for (i=k+1; i<=m; i++)
			if (a[k][2]<a[i][2]){
			t=a[k][2]; a[k][2]=a[i][2]; a[i][2]=t;
			t=a[k][1]; a[k][1]=a[i][1]; a[i][1]=t;

			}
			s=0;

			for (k=1; k<=m; k++){
				p=n;
			 n=n-a[k][1];
			 if (n>0)
				 s=s+a[k][1]*a[k][2]; else {
					 s=s+p*a[k][2];
			    cout<<s; return 0;

			 }

			}
			cout<<s;
return 0;
}

#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#include<stdio.h>
using namespace std;
void insertionSort(pair<long long, int>arr[] , int m)
{
	int i, key1 , key, j;
	for (i = 1; i < m; i++) {
		key = arr[i].second;
		key1 = arr[i].first;
		j = i - 1;
		while (j >= 0 && arr[j].second < key) {
			arr[j + 1].second = arr[j].second;
			arr[j + 1].first = arr[j].first;
			j = j - 1;
		}
		arr[j + 1].second = key;
		arr[j + 1].first = key1;

	}
}
int main()
{
	int m;
	long long n , matches=0 ;
	cin >> n >> m;
	pair<long long, int > arr[20];
	for (int i = 0; i < m; i++)
		cin >> arr[i].first >> arr[i].second;
	insertionSort(arr, m);
	for (int i = 0; n!=0 && i < m; i++)
	{
		if (n >= arr[i].first)
		{
			matches += arr[i].first * arr[i].second;
			n -= arr[i].first;
		}

		else
		{
			matches += n * arr[i].second;
			n=0;
		}

	}
	cout << matches << endl;

	return 0;
	//system("Pause");
}

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

int main()
{
	int a[20][2],n,m,s=0;
	cin>>n>>m;
	for(int i=0;i<m;i++)
		for(int j=0;j<2;j++)
			cin>>a[i][j];
	while(1)
	{
	int max=0;
	for(int i=0;i<m;i++)
		if(a[max][1]<a[i][1])max=i;
	if(a[max][0]>=n) {s+=n*a[max][1];break;}
	else {n-=a[max][0]; s+=a[max][0]*a[max][1]; a[max][1]=0;}
	}
	cout<<s;
	cout<<endl;

}

#include <iostream>

using namespace std;

__int64 a[21];
__int64 b[21];
void qs(__int64* s_arr,int first, int last)
{
    int i = first, j = last, x = s_arr[(first + last) / 2];

    do {
        while (s_arr[i] < x) i++;
        while (s_arr[j] > x) j--;

        if(i <= j) {
            if (i < j)
            {
                int t =s_arr[i];
                s_arr[i] = s_arr[j];
                s_arr[j] =t;
                t =a[i];
                a[i] = a[j];
                a[j] = t;
            }
            i++;
            j--;
        }
    } while (i <= j);

    if (i < last)
        qs(s_arr,i, last);
    if (first < j)
        qs(s_arr,first,j);
}
int main ()
{

    __int64 n,m;
    cin>>n>>m;
    int v=0;

    for(int i=1;i<=m; i++)
    {
        cin>>a[i]>>b[i];
        v +=a[i];
    }
    qs(b,1,m);
    int k=0;
    __int64 sum =0;
    if (v<n)
    {
        for(int i=1; i<=m; i++)
            sum+=a[i]*b[i];
    }
    else
    {
        int i=m;
        while(k<n)
        {
            if(a[i]-(n-k)>0)
            {
                sum+=b[i]*(n-k);
                k=n;

            }
            else
            {
                sum+=b[i]*a[i];
                k+=a[i];

            }
            i--;
        }
    }
    cout<<sum;
}

#include<bits/stdc++.h>
#include<string>
#include<cstdlib>
#include<cmath>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include<vector>
#include<string>
#define  ll long long

/*BISHOY MAGDY ADEEB*/
using namespace std;

void BISHOY() {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

int main(){
BISHOY();

ll n;int m;

cin>>n>>m;

vector<pair<int,int> >ab;

for (int i = 0; i < m; i++) {
int a,b;
cin>>a>>b;
ab.push_back(make_pair(b,a));

}

sort(ab.begin(),ab.end());
reverse(ab.begin(),ab.end());

ll sum=0;
for(ll i=0;i<m;i++){

  if(n>=ab[i].second){
    n-=ab[i].second;
    sum+=ab[i].first*ab[i].second;

  }

  else{

if(n>0&& n<ab[i].second){
ll N=n;

sum+=n*ab[i].first;

n-=N;

}

  }

}

   cout<<sum;

return 0;
}

#include<bits/stdc++.h>

using namespace std;

int a[50000],b[50000];
long long ans=0;

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&a[i],&b[i]);
	for(int i=1;i<=m-1;i++)
		for(int j=i+1;j<=m;j++)
		{
			if(b[i]<b[j]){
				swap(a[i],a[j]);
				swap(b[i],b[j]);
			}
		}
	for(int i=1;;i++){
		if(a[i]<n){
			ans+=a[i]*b[i];
			n-=a[i];
		}
		else {
			ans+=n*b[i];
			break;
		}
	}
	cout<<ans<<endl;

	return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<sstream>
#include<stdio.h>
#include<set>
using namespace std;
typedef pair < int, int > pii;
typedef long long ll;
#define endl '\n';
#define FOR(n) for(int i=0;i<n;i++)
#define Size 5000001
long double pi = 22.0 / 7;
pair<int, int>p[22];
int main() {
	int n, m;
	cin >> n >> m;	for (int i = 0; i < m; i++)
	{
		int a, b;
		cin >> a >> b;
		p[i] = make_pair(b, a);
	}
	sort(p, p + m);
	reverse(p, p + m);
	ll ans = 0;
	for (int i = 0; i < m && n>0; ++i)
	{
		if (n>p[i].second)
		{
			ans += (p[i].second*p[i].first);
			n -= p[i].second;
		}
		else{
			ans += (n*p[i].first);
			break;
		}
	}
	cout << ans << endl;
}

#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vecto
long long fibar[100];

using namespace std;
long long  fib(int x)
{if(fibar[x]!=-1)return fibar[x];
    else {
    if(x==0||x==1){ return 1;}
    else
    {
        return fibar[x]=fib(x-1)+fib(x-2);
    }
    }
}
long long  fib2(int x)
{

    if(x==0||x==1){ return 1;}
    else
    {
        return fib2(x-1)+fib2(x-2);
    }

}
int pow(int x,int y)
{int m;
    if(y==1)return x;
    else
    {
        m=y/2;
        if(y%2==0)return pow(x,m)*pow(x,m);
    else return pow(x,m)*pow(x,m)*x;
    }

}

int sum(int x, int y)
{int m;
    if(y==1)return x;
    else {
            m=y/2;
        if(y%2==0)return sum(x,m)+sum(x,m);
    else return sum(x,m)+sum(x,m)+x;
    }
}

int main()
{

long long x,y,z=0,d,n,c=0,h=0,sum=0,ma=0;
cin>>x>>y;
long long  a[y],a2[y],a3[y];
for(int i=0;i<y;i++)
{
    cin>>a[i]>>a2[i];
c+=a[i];
h+=a2[i]*a[i];
a3[i]=a2[i];
}

if(x>=c)cout<<h;
else{
    while(x)
    {ma=-1;
        for(int i=0;i<y;i++)
        {
            if(a[i]>0){if(a2[i]>ma)
                {
                    n=i;
                    ma=a2[i];
                }
            }
        }
        if(a[n]>=x)
        {
            sum+=x*a2[n];
            x=0;

        }
       else
       {
          sum+=a[n]*a2[n];
            x-=a[n];
            a[n]=0;
       }
    }

    cout<<sum;

}

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b;
    int arr[b],arr2[b],arr3[b];
    for(int i=0;i<b;i++)
    {
        cin>>c>>d;
        arr[i]=c;
        arr2[i]=d;
    }
    for(int i=0;i<b-1;i++)
    {
        for(int j=0;j<b-i-1;j++)
        {
            if(arr2[j]>arr2[j+1])
            {
                swap(arr2[j],arr2[j+1]);
                swap(arr[j],arr[j+1]);
            }
        }
    }
    int sum=0;
    for(int i=b-1;i>=0;i--)
    {
        if(arr[i]<a)
        {
            sum+=arr[i]*arr2[i];
            a=a-arr[i];
        }
        else
        {
            sum+=a*arr2[i];
            break;
        }
    }
    cout<<sum;
}

#include <iostream>
#include <math.h>
#include <string.h>
#include <bits/stdc++.h>
#include <vector>
using namespace std;

int main()
{
    long long int n,m,q;
    cin>>n>>m;
    long long int i,a[m],b[m],b2[m];
    for(i=0;i<m;i++)
    {
        cin>>a[i]>>b[i];
        b2[i]=b[i];
    }
    //sort(b2,b2+m,greater<long long int>());
    long long int j=0,sum=0;
    /*for(i=0;i<m;i++)
    {
        if(b[i]==b2[j])
        {
            sum+=min(n,a[i])*b[i];
            n-=min(n,a[i]);
            j++;
        }
    }*/
    for (i=0;i<m;i++){
    for (int j=i+1;j<m;j++){
            if (b[j]>b[i]){
                swap(a[i],a[j]);
                swap(b[i],b[j]);
            }
        }
    }
    for(i=0;i<m;i++){
        q=min(n,a[i]);
        sum+=b[i]*q;
        n-=q;
    }
    cout<<sum;
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ans,n,m;
struct node{
	int a,b;
}a[10010];
bool cmp(node x,node y){return x.b>y.b;}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)scanf("%d%d",&a[i].a,&a[i].b);
	sort(a+1,a+m+1,cmp);
	for(int i=1;;i++){
		if(a[i].a>=n){ans+=n*a[i].b;break;}
		ans+=a[i].a*a[i].b;
		n-=a[i].a;
	}
	printf("%lld",ans);
	return 0;
}

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
typedef long long LL;
using namespace std;

vector< pair<LL,LL> > V;

int main(){

	LL ans = 0;
	LL n,m;int i,j,k;
	cin>>n>>m;

	for(i=0;i<m;i++){
		LL a,b;
		cin>>a>>b;
		V.push_back(make_pair(b,a));
	}

	sort(V.rbegin(),V.rend());

	i = 0;
	while(n>0 && i<V.size()){

		LL h = min(n,V[i].second);
		n-=h;
		ans+=(h*V[i].first);
		i++;

	}

	cout<<ans<<endl;
	return 0;

}

#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=17;
long long a[MAXN];
int main()
{
    int n,m;
    cin>>m>>n;
    for(int i=1;i<=n;i++)
    {
        int v,b;
        cin>>v>>b;
        a[b]+=v;
    }
    long long ans=0;
    for(int i=10;i>=1;i--)
    {
        if(m>=a[i])
        {
            m-=a[i];
            ans+=a[i]*i;
        } else
        {
            ans+=m*i;
            m=0;
            break;
        }
    }
    cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
void help()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
int v1[11],n;
int main()
{
    help();

long long m,a,b,c=0,j=0;
cin>>n>>m;
for(int i=0;i<m;i++)
{
 cin>>a>>b;
v1[b]+=a;

}
for(int i=10;i>=0;i--)
{
    j=min(v1[i],n);
    c += i*j;
    n -= j;
}
cout<<c<<endl;
 return 0;
}

///我要水过题量
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

//定义变量
int n,m,ans=0;
struct u{
	int a,b;
}arr[25];

bool cmp(u x,u y){
	return x.b>y.b;
}

int main(){

	cin>>n>>m;
	for (int i=1;i<=m;i++){
		cin>>arr[i].a>>arr[i].b;
	}

	sort (arr+1,arr+m+1,cmp);

	for (int i=1;i<=m;i++){
		if (arr[i].a<=n){
			n-=arr[i].a;
			ans+=arr[i].b*arr[i].a;
			continue;
		}
		if (arr[i].a>n){
			ans+=n*arr[i].b;
			break;
		}
	}

	cout<<ans<<endl;

	return 0;

}

#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
int arr[21],x,y;
int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
	   int x,y;
		cin >> x >> y;
		arr[y] += x;
	}
	int r = 0;
	for (int i = 20; i >= 0; i--)
	{
		int z = min(n, arr[i]);
		r += z * i;
		n -= z;

    }

	cout << r;

}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int maxBoxes, containers;
	int boxes, matches;

	cin >> maxBoxes >> containers;

	vector<int> a;
	vector<int> b;
	long long int size = 0;
	for (int i = 0; i < containers; i++) {
		cin >> boxes >> matches;
		a.push_back(matches);
		b.push_back(boxes);
		size += boxes;
	}

	int key, j, keyb;
	for (int i = 1; i < a.size(); i++) {
		key = a[i];
		keyb = b[i];
		j = i - 1;
		while (j >= 0 && a[j] < key) {
			a[j + 1] = a[j];
			b[j + 1] = b[j];
			j--;
		}
		a[j + 1] = key;
		b[j + 1] = keyb;
	}

	long long int ans = 0;
	int current = 0;
	if (maxBoxes > size)maxBoxes = size;
	for (int i = 0; i < size; i++) {
		if (b[i] < maxBoxes) {
			maxBoxes -= b[i];
			ans += b[i] * a[i];
		}
		else {
			ans += maxBoxes * a[i];
			break;
		}
	}

	cout << ans;
}

#include<bits/stdc++.h>
using namespace std;
struct node{
	int num,val;
}k[21];
inline bool cmp(node a,node b)
{
	return a.val>b.val;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d",&k[i].num,&k[i].val);
	}
	sort(k+1,k+m+1,cmp);
	long long ans=0;
	for(int i=1;i<=m;++i)
	{
		if(n<k[i].num)
		{
			ans+=n*k[i].val;
			break;
		}
		else
		{
			ans+=k[i].val*k[i].num;
			n-=k[i].num;
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
using namespace std;

struct cont
{
    long long a, b;
    bool operator < (cont a)
    {
        return (this->b>a.b);
    }
};

int main ()
{
    //freopen ("input.txt", "rt", stdin);
    //freopen ("output.txt", "wt", stdout);

    int n, m;
    cont a[30];
    cin>>n>>m;

    for (int i=0;i<m;i++)
        cin>>a[i].a>>a[i].b;

    sort(a, a+m);

    long long s=0, kol=0;

    for (int i=0;i<m;i++)
    {
        if (kol==n)
            break;

        if (kol+a[i].a<=n)
        {
            kol+=a[i].a;
            s+=a[i].a*a[i].b;
        }
        else
        {
            s+=a[i].b*(n-kol);
            kol=n;
        }
    }

    cout<<s;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
int n,m,sum=0,totalbox=0,y; cin>>n>>m; int arr[m],b[m],so[m];
 for (int i=0;i<m;i++){ cin>>arr[i]>>b[i];  so[i]=b[i];  totalbox+=arr[i]; } sort (b,b+m);
 y= min(n,totalbox);

while (y>=0){
for (int i=m-1 ;i>=0;i--) {
        for (int j=0;j<m;j++){
            if (b[i]==so[j]) { if (arr[j]<=y){ sum+=  (arr[j]*so[j]); y-=arr[j]; so[j]=0; }
                               else if (arr[j]>y) { sum+=  (y*so[j]);y=0; so[j]=0;} }
         if (y==0) {cout<< sum ; return 0 ; } } }}

    return 0;
}

/********************************************//**
 ****** Handle: techno_phyle    *****************
 ***********************************************/

#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long ll;
#define mod 1000000007
#define N 100010
#define M 200010
#define db(a) cout << a << endl
#define db2(a,b) cout << a << " " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a) for(auto it:a) cout << it << " "; cout << endl
#define adbp(a) for(auto it:a) cout << it.first << " " << it.second << endl
#define pb push_back
#define mp make_pair
#define endline "\n"
const ll INF = 1e18L + 5;
const int inf = 1e9 + 5;
typedef pair<double, long> key;
struct comp {
	bool operator()(pair<int, int> p1, pair<int, int> p2) {
		if (p1.first < p2.first) return false;
		if (p2.first < p1.first) return true;
		return p1.second < p2.second;
	};
};
//greater<pair<int,int> , greater<int>>

// vector<int> graph[N];
// vector<int> weight(N);
// void dfs(int node, int parent)
// {
// 	for (int to : graph[node]) {
// 		if (to == parent)
// 			continue;
// 		dfs(to, node);
// 		weight[node] += weight[to];
// 	}
// }

int main() {
	fastIO;
#ifndef ONLINE_JUDGE
	freopen("Input.txt", "r", stdin);
	freopen("Output.txt", "w", stdout);
#endif
	int test = 1;
	//cin >> test;
	while (test--) {
		int n, m;
		cin >> n >> m;
		int v[m];
		map<int, int> mpp;
		for (int i = 0; i < m; i++) {
			int a, b;
			cin >> a >> b;
			v[i] = b;
			mpp[b] += a;
		}
		sort(v, v + m , greater<int>());
		int ans = 0 , count = n;
		for (int i = 0; i < m; i++) {
			if (count == 0) break;
			int val = min(count, mpp[v[i]]);
			ans += val * v[i];
			count = count - val;
			mpp[v[i]] = mpp[v[i]] - val;
		}
		cout << ans << endl;

	}
	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
using namespace std;

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
using namespace std;
#define ll long long

int main()
{
	ll max,  n;
	cin >> max >> n;
	ll m[11] = { 0 };

	for (ll i = 0; i < n; i++)
	{
		ll a, b;
		cin >> a >> b;
		m[b] += a;
	}
	ll index = 10;
	ll ans = 0;

	while (max > 0 && index >= 1)
	{
		if (max <= m[index])
		{
			ans += max * index;
			max = 0;
		}
		else
		{
			ans += m[index] * index;
			max = max - m[index];
		}
		--index;
	}
	cout << ans;
}

#include <bits/stdc++.h>
using namespace std;

struct node{
    int a,b;
}cont[25];

bool cmp(node x,node y){
    return x.b>y.b;
}

int main(){
    int n,m,ans=0,sum=0;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>cont[i].a>>cont[i].b;
    }

    sort(cont+1,cont+m+1,cmp);

    for(int i=1;i<=m;i++){
        for(int j=1;j<=cont[i].a;j++){
            sum++;
            ans+=cont[i].b;
            if(sum==n)
                break;
        }
        if(sum==n)
            break;
    }

    cout<<ans<<endl;
    return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define ll long long
pair <ll,ll > a[30];
ll n,m,ans=0,t,i;
int main()
{
    cin >> n >> m;
    for(ll i=1;i<=m;++i)     cin >> a[i].S >> a[i].F;
    sort(a+1,a+1+m);
    i=m;
    while (n>0 && i>0)
    {
        t=min(n,a[i].S);
        ans=ans+t*a[i].F;
        n-=t;i--;
       // cout << t <<' '<< a[i].F << endl;
    }
    cout << ans << endl;
    return 0;
}

#include<stdio.h>
#include<string.h>
struct box
{
int matchbox;
int match;
}a[20],t;

int main(void)
{
int n,m,sum,start,i,k,j;
struct box t;
while(scanf("%d%d",&n,&m) != EOF)
{
for(i = 0;i <m;i++)
scanf("%d%d",&(a[i].matchbox),&(a[i].match));
for(i = 0;i < m-1;i++)
for(j= i+1;j <m;j++)
if(a[i].match < a[j].match)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i = 0,start = 1,sum=0;i< m;i++)
{
for(j = 0;j < a[i].matchbox;j++)
if(start<=n)
{
start++;
sum += a[i].match;
}
}
printf("%d\n",sum);
}
return 0;
}

#include <iostream>
using namespace std;
#include <algorithm>
#include <string>
#include <map>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
const double PI = 3.14159265358979323846264338;
#define l(n) for(int i = 0; i < n; i++)
#define le(n) for(int i = 0; i <= n; i++)
#define all(v) ((v).begin()) , ((v).end())
#define sz(v)	((int)((v).size()))

typedef	vector<int>		vi;
typedef	map<int, int>	mii;
typedef long long  ll;

bool isprime(long long x)
{
	if (x == 7 || x == 2 || x == 3 || x == 5) return true;
	for (int i = 2; i < 9; i++)
	{
		if (x%i == 0) return false;
	}
	return true;
}
int main()
{

	vector< pair<int, int > >vp;
	pair<int, int>p;
	ll ans = 0;
	int n, m, f, s;
	cin >> n >> m;
	l(m)
	{
		cin >> p.second >> p.first;
		vp.push_back(p);
	}

	sort(all(vp));
	while (!vp.empty())
	{

		vp[vp.size() - 1].second < n ? ans += (vp[vp.size() - 1].first) * (vp[vp.size() - 1].second) :
			ans += (vp[vp.size() - 1].first)*(n);
		vp[vp.size() - 1].second < n ? n=n - vp[vp.size() - 1].second : n = 0;

		vp.pop_back();
		vp.resize(vp.size());
		if (n == 0)
			break;
		continue;

	}
	cout << ans;
	return 0;
}

#include<iostream>
#include<cstdio>
#include<queue>
#define N 21
#define ll __int64
using namespace std;
struct matches
{
    ll ai;
    ll bi;
    bool friend operator<(matches m1,matches m2)
    {
        return m1.bi<m2.bi;
    }
};
int main()
{
    ll n,m;
    matches match[N];
    while (cin>>n>>m)
    {
        priority_queue<matches>que;
        for (int i=0;i<m;i++)
        {
            cin>>match[i].ai>>match[i].bi;
            que.push(match[i]);
        }

        ll ans=0;
        while (n>0 && !que.empty())
        {
            matches tmp=que.top();
            que.pop();
            if (n-tmp.ai>=0)
            {
                ans+=tmp.ai*tmp.bi;
                n-=tmp.ai;
            }
            else if (n-tmp.ai<0)
            {
                ans+=n*tmp.bi;
                break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

#include <iostream>
#include <string.h>
using namespace std;
int main() {
    int n, m, i, j, k, l, vs(0);
    cin >> n >> m;
    short matchboxes[n];
    short amountC, vC;
    for(i = 0; i < n; i++) {
        matchboxes[i] = 0;
    }
    for(i = 0; i < m; i++) {
        cin >> amountC >> vC;
        for(j = 0; j < n; j++) {
            if(matchboxes[j] < vC) {
                if(n - j <= amountC) {
                    for(k = j; k < n; k++) {
                        matchboxes[k] = vC;
                    }
                } else {
                    for(k = n - 1; k >= j + amountC; k--) {
                        matchboxes[k] = matchboxes[k - amountC];
                    }
                    for(k = j; k < j + amountC; k++) {
                        matchboxes[k] = vC;
                    }
                }
                break;
            }
        }
    }
    for(i = 0; i < n; i++) {
        vs += matchboxes[i];
    }
    cout << vs << endl;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void Preset()
{
	cin.sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

long long cmp(pair<int, int> a, pair<int, int> b)
{
	if (a.second > b.second)
		return 1;
	else
		return 0;
}

int main()
{
	Preset();

	long long N, M;
	vector<pair<long long, long long> > match;
	long long havingMatch = 0, curmatchboxidx = 0;
	long long numofMatch = 0;

	cin >> N >> M;

	for (int i = 0; i < M; i++)
	{
		int a, b;

		cin >> a >> b;

		match.push_back({ a, b });
	}

	sort(match.begin(), match.end(), cmp);

	while (havingMatch < N)
	{
		if (match[curmatchboxidx].first != 0)
		{
			match[curmatchboxidx].first--;
			havingMatch++;
			numofMatch += match[curmatchboxidx].second;
		}
		else
			curmatchboxidx++;
	}

	cout << numofMatch << "\n";

	return 0;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <iterator>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int containerNumber;
    long long backpack, matchboxes, matches,i=0,answ=0;
    int j;
    vector<int> matchesVector;
    map<long long, long long>bestHeist;
    cin >> backpack >> containerNumber;
    while (containerNumber > 0) //Se introduc datele in MAP #chibrituri==cheia
    {
        cin >> matchboxes >> matches;
        //cout<<"insert_match:"<<matches<<"   insert_box:"<<matchboxes<<endl;
        //bestHeist.insert(pair<long long, long long>(matches, matchboxes));
        bestHeist[matches]=bestHeist[matches]+matchboxes;
        matchesVector.push_back(matches);
        i++;
        containerNumber--;
    }
    sort(matchesVector.begin(), matchesVector.end()); //sortam vectorul de #chibrituri
    matchesVector.erase( unique( matchesVector.begin(), matchesVector.end() ), matchesVector.end() );
    /*cout<<"UNIQUE matchesVector:";
    for(j=matchesVector.size()-1;j>=0;j--)
        cout<<matchesVector[j]<<"  ";
    cout<<endl;
    /*for (j = i-1; j >=0; j--)
        cout << matchesVector[j]<<"  ";
    cout << endl;
    for (map<long long, long long>::iterator it = bestHeist.begin();
        it != bestHeist.end(); ++it)
    {
        cout << it->first << " " << it->second << endl;
    }*/
    while(backpack>=0)
    {
       for(j=matchesVector.size()-1;j>=0;j--)
       {
           //cout<<"Working j:"<<j<<endl;
           matchboxes=bestHeist[matchesVector[j]];
           //cout<<"box="<<matchboxes<<endl;
           if(backpack>=matchboxes)
           {
               //cout<<"matches:"<<matchesVector[j]<<"  ";
               answ=answ+(matchesVector[j]*matchboxes);
               backpack=backpack-matchboxes;
           }
           else
           {
               while(backpack!=0&&matchboxes!=0)
               {
                   answ=answ+matchesVector[j];
                   matchboxes--;
                   backpack--;
               }
           }
           //cout<<endl<<"answ:"<<answ<<"  "<<"Backpack="<<backpack<<endl;
       }
       //cout<<"j:"<<j<<endl;
       if((backpack==0)&&(j==-1))
       {
           //cout<<"BREAK"<<endl;
           break;
       }

    }
    cout<<answ;

    return 0;
}

#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;

#define p pair<int, int>

int n, m, x, y;

bool cmp(p a, p b)
{
    return a.second > b.second;
}

p a[25];

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        cin >> x >> y;
        a[i] = make_pair(x, y);
    }
    sort(a + 1, a + m + 1, cmp);
    int ans = 0, s = 0;
    for(int i = 1; i <= n; i++)
    {
        s += a[i].first;
        if(s > n)
        {
            ans += (n - s + a[i].first) * a[i].second;
            break;
        }
        ans += a[i].first * a[i].second;

    }
    cout << ans << endl;
    return 0;
}

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Disable warning messages C4996.
//#pragma warning(disable:4996)

int main(int argc, char **argv)
{
	const int mmax=20;
	unsigned int n, m, at, bt, i, j, s;
	unsigned int a[mmax], b[mmax];
	char c;

	for(i=0;i<mmax;i++){
		a[i]=0;
		b[i]=0;
	}

	n=0;
	m=0;

	if(scanf("%u %u", &n, &m)==2){
		//c=getchar();
		for(i=0;i<m;i++){
			if(scanf("%u %u", &(a[i]), &(b[i]))==2){}
		}
	}

	for(i=0;i<m-1;i++){
		for(j=i+1;j<m;j++){
			if(b[j]>b[i]){
				bt=b[i];
				b[i]=b[j];
				b[j]=bt;
				at=a[i];
				a[i]=a[j];
				a[j]=at;
			}
		}
	}

	i=0;//container
	j=0;//boxes
	s=0;

	while(j<n){
		if(n-j>a[i]){
			s+=a[i]*b[i];
			j+=a[i];
			i++;
		}else{
			s+=(n-j)*b[i];
			j=n;
		}
	}

	printf("%u", s);

	return 0;
}

#include <bits/stdc++.h>
#define go ios::sync_with_stdio(false); cin.tie(nullptr);
#define cya return 0;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

using namespace std;

int main()
{
    go
    vector<pair<int,int>> v;
    int n,m,a,b,sum=0,i=0;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> a >> b;
        v.push_back(make_pair(b,a));
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());

    if(v.size()==1)
    {
        cout << abs(n-v[0].second)*v[0].first;
        cya
    }
    while(n!=0)
    {
        if(v[i].second<=n)
        {
            sum+=(v[i].first*v[i].second);
            n-=v[i].second;
        } else
        {
            sum+=n*v[i].first;
            n-=abs(n-v[i].second);
            break;
        }
        i++;
    }
    cout << sum;
    cya
}

#include <iostream>

using namespace std;

int main (){

    long n , a[20], kol=0;
    int m, b[20];

    cin >> n >> m;

    for (int i=0; i < m; i++ ){

        cin >> a[i];
        cin >> b[i];
    }

    for (int i=0; i<m; i++)
        for (int j=0; j<m-1; j++)
              if (b[j] > b[j+1])
              {
                  swap (b[j], b[j+1]);
                  swap (a[j], a[j+1]);
              }

    for (int i=m-1; i>=0; i--){
        if (n > a[i])
            {
                kol += a[i] * b[i];
                n -= a[i];
            }
        else {

            kol += n * b[i];
            cout << kol;
            exit (0);
        }

    }
}

#include<bits/stdc++.h>
using namespace std;
//Fractional Knapsack
struct match
{
    int a;
    int b;
};
bool com(match i1,match i2)
{
    return (i1.b>i2.b);
}
int main()
{
    match ar[100];
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
     cin>>ar[i].a>>ar[i].b;

    sort(ar,ar+n,com);

    int cur=0,final=0;
    for(int i=0;i<m;i++)
    {
        if(cur+ar[i].a<=n)
        {
            cur+=ar[i].a;
            final+=(ar[i].b*ar[i].a);
        }
        else
        {
             int remain=n-cur;
            final+=(ar[i].b*remain);
            break;
        }
    }
    cout<<final<<endl;
    return 0;
}

//
#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef vector<lli>    vlli;
typedef multiset<lli> mulset;
typedef pair<lli,lli>   pii;

#define dbg(x)           cout<<#x<<": "<<x<<endl;
#define all(x)          x.begin(), x.end()
#define boom            ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl             "\n"
#define f1i(n)           for(lli i=1;i<=(n);i++)
#define f0i(n)           for(lli i=0;i<(n);i++)
#define f1j(n)           for(lli j=1;j<=(n);j++)
#define f0j(n)           for(lli j=0;j<(n);j++)
#define f0k(n)           for(lli k=0;k<(n);k++)
#define frst0(x)       __builtin_clz(x)
#define lst0(x)       __builtin_ctz(x)
#define tt1s(x)        __builtin_popcount(x)
#define t_prty1s(x)       __builtin_parity(x)
#define pb              push_back
#define srt(x)          sort((x).begin(),(x).end())
#define rsrt(x)          sort((x).rbegin(),(x).rend())
#define yes              cout<<"YES"<<endl
#define no              cout<<"NO"<<endl
#define maxv(v)                        *max_element(all(v))
#define ms(a,val)                 memset(a,val,sizeof(a))
#define vecfind(v,x)      binary_search ((v).begin(), (v).end(), (x))
const lli mod = (lli)1e9+7;
const lli inf = 1e18;

auto sgn=[&](lli x){
    if(x<0) return -1;
    else return 1;
};

int solve(){

lli n,m;
cin>>n>>m;

lli tm,tp;

vlli v;
f1i(m){
    cin>>tm>>tp;
    f1i(tm){
        v.pb(tp);
    }
}
rsrt(v);
lli tot=0;
f0i(n){
    tot+=v[i];
}
cout<<tot<<endl;

   return 0;
}

int main(){
 boom
    lli t;
   t=1;
 // cin>>t;

    while(t--){
         solve();
    }

    return 0;

}

#include<iostream>
#include<cmath>
//#include<cstring>
//#include<cstdlib>

using namespace std;

//#define long long lll

int main()

{
long long    n,m,element;
cin>>n>>m;

const long long n2=2;
const long long  m2=m;

long long   A[m2][n2];

for(long long   i=0;i<m2;i++)
{  for(long long   j=0;j<n2;j++)
  {  cin>>element;
     A[i][j]=element;
  }
}
/*cout<<"\n \n"<<"A[i][j]:"<<endl;
for(long long  i=0;i<m2;i++)
{   for (long long  j=0;j<n2;j++)
    {cout<<A[i][j];
    }
    cout<<"\n";
}*/
for (long long  flag=0;flag<m2;flag++)
{
 for(long long   i=0;i<m2-1;i++)
{
   if(A[i+1][1]>A[i][1])
   {long long   t1=A[i][1];long long   t2=A[i][0];
   A[i][0]=A[i+1][0];
   A[i][1]=A[i+1][1];
   A[i+1][1]=t1;
   A[i+1][0]=t2;
   }
}
}
/*cout<<"\n \n"<<"A[i][j]: After SWAP"<<endl;
for(long long  i=0;i<m2;i++)
{   for (long long  j=0;j<n2;j++)
    {cout<<A[i][j];
    }
    cout<<"\n";
}
cout<<"End of SWAP";
*/
long long  l,matchboxes=0,matches=0;
long long  ctr=0;
for(;ctr<m2,matchboxes<=n;ctr++)
{
matchboxes+=A[ctr][0];
//cout<<"matchboxes"<<matchboxes;
matches+=A[ctr][1]*A[ctr][0];
//cout<<"matches"<<matches;
}
//cout<<"ctr"<<ctr;
if(matchboxes>n)
{
matches-=A[ctr-1][1]*A[ctr-1][0];
matchboxes-=A[ctr-1][0];
}

long long  dif=n-matchboxes;
//cout<<"dif"<<dif;
matches+=A[ctr-1][1]*dif;

cout<<matches;

cin.get();
cin.ignore();

return 0;
}

/*
n-���-�� �������(������� ���������� ����)[1..2e8]
m- ���-�� �����������[1...20]
		n m
		7 3
		x y
		5 10	5 10
		2 5		3 6
		3 6		2 5
*/
#include<iostream>
using namespace std;
const int q = 11;
int main(){
	int n, m;
	cin >> n >> m;
	int a[q] = { 0 };
	for (int i = 0; i < m; i++){
		int tmp1, tmp2;
		cin >> tmp1 >> tmp2;
		a[tmp2] = tmp1;
	}
	int i = 10;
	long long sum = 0;
	while (n&&i){
		int f = n;
		f -= a[i];
		if (f <= 0){
			sum += i * n;
			break;
		}
		else {			//f>0
			sum += i * a[i];
			n -= a[i];
		}
		i--;
	}
	cout << sum << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll ;
int main(){
    ll n,m;
    cin>>n>>m;
    vector<ll> fq;
    vector<ll> mts;
    ll cnt[100]={0};
    ll rp[100]={0};
    for(int i=0;i<m;i++){
        ll p,q;
        cin>>p>>q;
        fq.push_back(p);
        mts.push_back(q);
        rp[q]++;
        if(rp[q]==1){
            cnt[q]=i;
        }
        if(rp[q]>1){
            mts[cnt[q]]+=q;
            fq[cnt[q]]+=fq[i];
            fq.pop_back();
            mts.pop_back();

        }

    }
    sort(mts.begin(),mts.end(),greater<ll>());

    ll  num=n;
    ll ans=0;
    for(int i=0;i<mts.size();i++){
        int c=mts[i];
        if(fq[cnt[c]]<=num){
            ans+=fq[cnt[c]]*c;
            num=num-fq[cnt[c]];
            if(num==0){
                break;
            }
        }
        else{
            ans+=num*c;
            break;

        }

    }
    cout<<ans;

    return 0;
}

#include<stdio.h>
#include<algorithm>
using namespace std;
struct po{
int n,v;
}in[22];
bool Comp(po a,po b){
return a.v>b.v;
}
int main()
{
int n,m,i;
_int64 tot;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<m;i++)
scanf("%d%d",&in[i].n,&in[i].v);
sort(in,in+m,Comp);
tot=0;i=0;
while(n>0){
if(n>=in[i].n){
tot+=in[i].n*in[i].v;
n-=in[i].n;i++;
}else{
tot+=n*in[i].v;
n=0;
}
}
printf("%I64d\n",tot);
}
return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
const int inf = 0x3f3f3f3f;

#define syncC ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define io freopen("i.txt","r",stdin);freopen("o.txt","w",stdout);
#define f1(n,m) for(int i=n;i<=m;i+=1)
#define f2(n,m) for(auto n:m)
#define sq(a) ((a)*(a))

bool sortSec(const pair<int,int> &a,const pair<int,int> &b){return (a.second>b.second);}
ll solve(){};

int main(){
    syncC;
    //io;

    ld x,y,a,b,c,z;
    vector<pair<int,int>>vp;

    cin>>x>>y;
    f1(0,y-1){
        cin>>a>>b;
        vp.push_back(make_pair(a,b));
    }
    sort(vp.begin(),vp.end(),sortSec);
    z=0;a=0;b=0;c=0;
    f1(0,vp.size()-1){
        a+=vp[i].first;
        b=vp[i].first;
        if(a<x){z+=b*vp[i].second;}
        else if(a==x){z+=b*vp[i].second;break;}
        else if(a>x){z+=b*vp[i].second*((x-(a-b))/b);break;}
    }
    cout<<z;

   return 0;
}

#include <bits/stdc++.h>
using namespace std;
/*class num{
    public:
    int first;
    int second;
};*/
bool comp(pair<int,int> k1,pair<int,int> k2){
    if(k1.second > k2.second)
        return true;
    else
        return false;
}
int main() {
    vector<pair<int,int>> v;
    int n,m;
    cin >> n >> m;
    for(int i =0;i< m;i++){
        int x,y;
        cin >> x >> y;
        v.push_back(make_pair(x,y));
    }
    sort(v.begin(),v.end(),comp);
    /*for(int i=0;i<v.size();i++){
        cout << v[i].first << " " << v[i].second << endl;
    }*/
    int w=0,i=0;
    while(n > 0){
        if(n >= v[i].first){
            w = w+v[i].first*v[i].second;
            n -= v[i].first;
            i++;
        }
        else{
            w = w+n*v[i].second;
            n =0;
        }
    }
    cout << w << endl;
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>

using namespace std;

struct sa
{
       int j;
       int f;
}data[55];
int cmp( const sa &a,const sa &b)
{
 return (a.f) > (b.f);
}

int main()
{
    int n,m;
    cin>> n >> m;
    int i;
    for(i=0;i<m;i++)
    {
                    cin>> data[i].j >>data[i].f;
    }
    sort(data,data+n,cmp);
    int sum=0;
    for(i=0;i<m;i++)
    {
                    if( n<=data[i].j )
                    {
                        sum+=data[i].f*n;
                       break;
                    }
                    else
                    {
                        n=n-data[i].j;
                        sum+=data[i].j*data[i].f;
                    }
    }
    cout<<sum<<endl;
//    getchar();
//    getchar();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,m,a,b,i,sum=0;
    cin >> n >> m;
    vector<long long int>v,w;
    for(i=1;i<=m;i++)
    {
        cin >> a >> b;
        v.push_back(a);
        b=a*b;
        w.push_back(b);
    }
    for(i=0;i<w.size();i++)
    {
        for(int j=i+1;j<w.size();j++)
        {
            if(w[i]<w[j])
            {
                swap(w[i],w[j]);
                swap(v[i],v[j]);
            }
        }
    }
    for(i=0;i<v.size();i++)
    {
        if(n>=v[i])
        {
            sum+=w[i];
            n=n-v[i];
        }
        else
        {
            long long int x=w[i]/v[i];
            sum+=(n*x);
            cout << sum << endl;
        }
    }
}

// {{{
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <list>
#include <complex>
#include <stack>
#include <cmath>
#include <iostream>
#include <sstream>
#include <cctype>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <assert.h>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<string> VS;
typedef long long LL;
typedef long double LD;
typedef pair<int,int> PI;
typedef pair<LD,LD> PD;

#define VAR(v,n) __typeof(n) v=(n)
#define REP(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define FORD(i,a,b) for(int i=(a); i>=(b); i--)
#define FORE(i,c) for(VAR(i,(c).begin()); i!=(c).end(); i++)
#define CLR(A,v) memset((A),v,sizeof((A)))

#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ((int)(x).size())
// }}}

#define M 1005

int n,m;
PI T[M];

int main() {
	scanf("%d%d",&n,&m);
	REP(i,m) scanf("%d%d",&T[i].SE,&T[i].FI);
	sort(T,T+m);
	reverse(T,T+m);

	int res=0,akt=0;
	while (n && akt<n) {
		res+=min(n,T[akt].SE)*T[akt].FI;
		n-=min(n,T[akt].SE);
		akt++;
	}

	printf("%d\n",res);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int a,b,g,i;
struct c
{
	int d,e;
};
bool h(c a,c b)
{
	return a.e>b.e;
}
c f[21];
int main()
{
	cin>>a>>b;
	for(;g<b;g++)
	{
		cin>>f[g].d>>f[g].e;
	}
	sort(f,f+b,h);
	g=0;
	while(1)
	{
		if(a>f[g].d)
		{
			a-=f[g].d;
			i+=f[g].d*f[g].e;
		}
		else
		{
			i+=a*f[g].e;
			break;
		}
		g++;
	}
	cout<<i;
	return 1-1+1-1;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<vector>
#include<utility>

// #include<bits/stdc++.h>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    vector<pair< int,int> > vp(m);
    for(int i = 0;i < m;i++){
        cin>>vp[i].second>>vp[i].first;
    }
    sort(vp.begin(),vp.end());
    reverse(vp.begin() , vp.end());
    int tot = 0,num = 0, i = 0;
    while(num + vp[i].second <= n && i < m){
        tot+=(vp[i].first*vp[i].second);
        num+=(vp[i].second);
        i++;
    }
    if(num == n){
        cout<<tot<<'\n';
    }
    else{
        int r = n-num;
        tot+= ((r)*vp[i].first);
        cout<<tot<<'\n';
    }
    return 0;
}

#include <bits/stdc++.h>
#include<unordered_map>
/*
**
Success doesn't come to you ,
You Go To IT .
**
*/
using namespace std;
#define all(v)          ((v).begin()), ((v).end())
#define finish(x) return cout << x << endl, 0;
#define pb push_back
#define Compiler_Beso ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define clr(v,d)        memset(v, d, sizeof(v))
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

enum dir       { DOWN = 0, UP, RIGHT, LEFT, DOWN_RIGHT, DOWN_LEFT, UP_RIGHT, UP_LEFT };
int  dir_r[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int  dir_c[] = { 0, 0, 1, -1, 1, -1, 1, -1 };

int main()
{
	Compiler_Beso

		ll n, m;
	cin >> n >> m;
	priority_queue<pair<ll, ll>>q;
	while (m--)
	{
		int item, cost;
		cin >> item >> cost;
		q.push({cost,item});
	}
	ll ans = 0;
	while (n)
	{
		pair<ll, ll>cur = q.top();
		q.pop();
		ans += min(cur.second, (ll)n)*cur.first;
		n -= min(cur.second, (ll)n);
	}

	cout << ans;

	return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct matchboxs {
	int x, y;
};
bool cmp (const matchboxs &a, const matchboxs &b) {
	return a.y > b.y;
}
int main () {
	int n, m;
	matchboxs shop[25];
	while (~scanf ("%d%d", &n, &m)) {
		for (int i = 1; i <= m; i++) {
			scanf ("%d%d", &shop[i].x, &shop[i].y);
		}
		sort (shop + 1, shop + n + 1, cmp);
		long long val = 0;
		for (int i = 1; i <= m; i++) {
			if (n - shop[i].x < 0) {
				val += (long long)n * shop[i].y;
				n = 0;
			} else {
				val += (long long)shop[i].x * shop[i].y;
				n -= shop[i].x;
			}
		}
		printf ("%lld\n",val);
	}
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std ;
__int64 ans ;
struct Box
{
    int a , b , c ;
} box[25] ;

int cmp ( Box i , Box j ) { return i.c > j.c ; }
int main ()
{
    int n , m , i , j ;
    scanf ( "%d%d" , &n , &m ) ;
    for ( i = 1 ; i <= m ; i ++ )
    {
        scanf ( "%d%d" , &box[i].a , &box[i].b ) ;
        box[i].c = box[i].a * box[i].b ;
    }
    sort ( box + 1 , box + m + 1 , cmp ) ;
    ans = 0 ;
    j = 0 ;
    int k ;
    for ( i = 1 ; i <= m ; i ++ )
    {
        j += box[i].a ;
        if ( j >= n )
        {
            ans += box[i].b * box[i].a ;
            k = i ;
            break ;
        }
        ans += box[i].b * box[i].a ;
    }
    if ( j > n ) ans = ans - box[k].b * ( j - n ) ;
    printf ( "%I64d\n" , ans ) ;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <bitset>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>

using namespace std;
#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORV(i, a, b) for(int i = (int)(a)-1; i >= (int)(b); --i)
#define FORE(i, a) for(int i = 0; i < (int)a.size(); ++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

#define sz(a) (int)a.size()
#define shows(a) if(a++)cout<<endl
#define PB push_back
#define X first
#define Y second
#define mp(x,y) make_pair(x, y)
#define println(a) cout<<a<<endl
#define sys system("pause")
#define print(a) cout<<a
#define clr(a,b) memset(a,b,sizeof(a))
#define OO 1e8
#define EPS 1e-6
#define SQ(x) ((x)*(x))

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<vd> vvd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vpi;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<ull> vull;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?0:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}
string reverse(string& s){ int n=s.size();REP(i,n/2) s[i]=s[n-1-i];return s;}
inline int isvalid(int i,int j,int H,int L){ return (i>=H||i<0||j>=L||j<0)? 0:1;}
inline int doubleComare(double x,double y){if(fabs(x-y)<=EPS) return 0; if(x<y) return -1; return 1;}
/*
int main() // ACCEPTED
{

    int n,d,m;
    cin>>n>>d;
    vi tab(n);
    REP(i,n) cin>>tab[i];
    sort(ALL(tab));
    cin>>m;
    int r=min(n,m);
    int s=0;
    for(int i=0;i<r;i++)
        s+=tab[i];
    if(m>n) s-=d*(m-n);
    cout<<s<<endl;
    return 0;
}*/
/*
int main()
{
    int n,m,r;
    scanf("%d %d",&n,&m);
    //cin>>n>>m;
    vi tab(n);
    REP(i,n) scanf("%d",&tab[i]);
    sort(ALL(tab));
    REP(i,m) {
        scanf("%d",&r);
        int s=1,t=tab[r-1];
        for(int j=r;j<n;j++)
            if(tab[j]!=t) {t=tab[j];s++;}
        printf("%d\n",s);
    }
    return 0;
}*/
/*
int main()
{
    ll w,m,k,r=0;
    cin>>w>>m>>k;
    int t=1+(int)log10((double)m);
    while(w>0){
        ll b=pow(10.0,t)-m;
        if((w/t)/k>=b) r+=b;
        else { r+=w/t/k; break;}
        w-=b*t*k;
        m=pow(10.0,t);t++;
    }
    cout<<r<<endl;
    return 0;
}*/
/*
int main()
{
    int t[10]={0};
    char r;
    int n;
    cin>>n;
    REP(i,16){ cin>>r; if(isdigit(r))t[r-'0']++;}
    REP(i,10) if(t[i]>2*n) {cout<<"NO"<<endl; return 0;}
    cout<<"YES"<<endl;
    return 0;
}*/
/*
int main()//ACCEPTED
{

    string s;
    cin>>s;
    ull md=0,mg=0,si=0;
    FORE(i,s) if(s[i]=='^') si=i;
    FORE(i,s) if(isdigit(s[i])){if(i<si) mg+=(si-i)*(s[i]-'0'); else md+=(i-si)*(s[i]-'0'); }
        if(md==mg) cout<<"balance"<<endl;
        else if(md<mg) cout<<"left"<<endl;
        else cout<<"right"<<endl;
    return 0;
}*/
/*
struct Edge{
    int from,to,w;
    Edge(int from,int to,int w) : from(from),to(to),w(w){}
};
int main()
{
    int n,m;
    cin>>n>>m;
    vector< vector< Edge > > graph(n);
    vi o(m),e(m),w(m);
    REP(i,m){
        cin>>o[i]>>e[i]>>w[i];
        graph[o[i]-1].pb(Edge(o[i],e[i],w[i]));
    }
    REP(i,m){

    }
    return 0;
}*/
/*
class point{

public:
    int x;
    int y;

    int & get_x(){
        return x;
    }
    void set_x(int xx){
        x=xx;
    }
};
int main()
{
    point p;
    p.set_x(100);
    cout<<p.get_x()<<endl;
    p.get_x()=10;
    cout<<p.x<<endl;
    system("pause");
    return 0;
}*//*
int isPrime(ull n) //O(Vn)
{
    if(n==2) return 1;
    if(n<2||n%2==0) return 0;
    for(ll i=3;i*i<=n;i+=2)
        if(n%i==0)
            return 0;
    return 1;
}
int main()
{
    ull n;
    REP(i,8){
        cin>>n;
        if(n==5) n=(ull)pow(2.0,35),n--;
        if(isPrime(n)) cout<<i+1;
        else cout<<0;
    }
    system("pause");
    return 0;
}*/
//******************************************Round 1********************************************************
/*int main()//Prob 1
{
    ull n,m,a,x,y;
    cin>>n>>m>>a;
    x=(n%a==0)?n/a:1+n/a;
    y=(m%a==0)?m/a:1+m/a;
    cout<<x*y<<endl;
    return 0;
}*//*
string x;
void solve(ll n){if(!n)return ; solve((n%26==0)?n/26-1:n/26); x.pb((n%26==0)?'Z':n%26+'A'-1);}
int main()//prob 2
{
    int n;
    cin>>n;
    while(n--){
        string s,r;
        ll a=0,b=0;
        int g=0;
        cin>>s;
        istringstream is(s);
        FORE(i,s){
            if(isdigit(s[i])) g=1;
            else if(g) {g=0;break;}
        }
        if(!g){
            x.clear();
            char d; is>>d>>a>>d>>b;
            solve(b);
            cout<<x<<a<<endl;
        }
        else{
            FORE(i,s){
                if(isdigit(s[i])) a=a*10+s[i]-'0';
                else r.pb(s[i]);
            }
            FORE(i,r) b=b*26+r[i]-'A'+1;
            cout<<"R"<<a<<"C"<<b<<endl;
        }
    }
    return 0;
}/*
#define eps 1e-4
#define PI 3.14159265359
double gcd(double x, double y) {
    while (fabs(x) > eps && fabs(y) > eps) {
        if (x > y) x -= floor(x / y) * y;
        else y -= floor(y / x) * x;
    }
    return x + y;
}
int main()//prob 3
{
    //ifstream cin("a.in");
    double s,t,u,v,x,y;
    cin>>s>>t>>u>>v>>x>>y;
    double a=hypot(s-u,t-v),b=hypot(s-x,t-y),c=hypot(x-u,y-v);
    double p=(a+b+c)/2,m=sqrt(p*(p-a)*(p-b)*(p-c)),r=(a*b*c)/(4*m);
    double A=acos((b*b+c*c-a*a)/(2*b*c)),B=acos((-b*b+c*c+a*a)/(2*a*c)),C=acos((b*b-c*c+a*a)/(2*b*a));
    double n=(PI/gcd(C,gcd(A,B)));
    //cout<<r<<" "<<a<<" "<<b<<" "<<c<<" "<<m<<" "<<A<<" "<<B<<" "<<C<<" "<<n<<endl;
    m=n/2*r*r*sin(2*PI/n);
    cout.precision(8);
    cout<<fixed<<m<<endl;
    //system("pause");
    return 0;
}
*/
///******************************************ROUND 2*******************************************
/*
int main()//Prob 1
{
    int n;
    //ifstream cin("a.in");
    cin>>n;
    map<string,ll> a,z;

    vector <pair<string,int> > moves(1001);
    ll ma=-OO;string ms;
    string s;int sc;
    REP(kk,n){
        cin>>s>>sc;
        moves[kk]=mp(s,sc);
        z[s]+=sc;
        ma=-OO;
        for(map<string,ll>::iterator it=z.begin();it!=z.end();++it)
            if(it->nd>ma) ma=it->nd;
    }
    set<string> ss;
    for(map<string,ll>::iterator it=z.begin();it!=z.end();++it)
        if(it->nd==ma) ss.insert(it->st);
    REP(kk,n){
        a[moves[kk].st]+=moves[kk].nd;
        if(ss.find(moves[kk].st)!=ss.end()&&a[moves[kk].st]>=ma) {cout<<moves[kk].st<<endl;break;}
    }
    //system("pause");
    return 0;
}*//*
int n;
int mat[1001][1001];
int x[1001][1001][2]={0};
ll dp[1001][1001][2];
ll solve(int r,int c,int z)
{
    if(!isvalid(r,c,n,n)) return INT_MAX;
    ll &ret=dp[r][c][z];
    if(ret!=-1) return ret;
    if(r==n-1&&c==n-1) return ret=x[r][c][z];
    ret=x[r][c][z]+min(solve(r+1,c,z),solve(r,c+1,z));
    return ret;
}

string path;
void solves(int r,int c,int z)
{
    ll &ret=dp[r][c][z];
    if(r==n-1&&c==n-1) return ;
    ret=x[r][c][z]+min(solve(r+1,c,z),solve(r,c+1,z));
    if(ret==x[r][c][z]+solve(r+1,c,z)){
        path.pb('D');
        solves(r+1,c,z);
        return;
    }
    path.pb('R');
    solves(r,c+1,z);
    return;
}
int a=-1,q;
int main()//prob 2
{
    ifstream cin("a.in");
    cin>>n;
    clr(dp,-1);
    REP(i,n) REP(j,n){ cin>>mat[i][j];x[i][j][0]=x[i][j][1]=0;
        int b=mat[i][j];
        if(b==0){b=10,a=i,q=j;}
        while(b&&b%2==0) x[i][j][0]++,b/=2;
        while(b&&b%5==0) x[i][j][1]++,b/=5;
    }
    ll r2=solve(0,0,0),r5=solve(0,0,1);
    if(r2<=r5){
        if(r2>1&&a!=-1){
            cout<<1<<endl;
            REP(k,a) cout<<'D';
            REP(k,q) cout<<'R';
            REP(k,n-1-a) cout<<'D';
            REP(k,n-1-q) cout<<'R';
            return 0;
        }
        solves(0,0,0);
        cout<<r2<<endl;
    }
    else{
        if(r5>1&&a!=-1){
            cout<<1<<endl;
            REP(k,a) cout<<'D';
            REP(k,q) cout<<'R';
            REP(k,n-1-a) cout<<'D';
            REP(k,n-1-q) cout<<'R';
            return 0;
        }
    solves(0,0,1);
    cout<<r5<<endl;}
    cout<<path<<endl;
    system("pause");
    return 0;
}*//*
double xi,yi,x[3],y[3],r[3],dis[3];
double chk(double a,double b)
{
    double rt=0;
    REP(i,3) dis[i]=sqrt(SQ(x[i]-a)+SQ(y[i]-b));
    REP(i,3) rt+=SQ(dis[i]/r[i] - dis[(i+1)%3]/r[(i+1)%3]);
    return rt;
}
int main()//prob 3
{
    //ifstream cin("a.in");
    REP(i,3) cin>>x[i]>>y[i]>>r[i];
    double ra=1;
    xi=(x[0]+x[1]+x[2])/3,yi=(y[0]+y[1]+y[2])/3;
    while(ra>EPS){
        if(chk(xi,yi)>chk(xi-ra,yi)) xi-=ra;
        else if(chk(xi,yi)>chk(xi+ra,yi)) xi+=ra;
        else if(chk(xi,yi)>chk(xi,yi-ra)) yi-=ra;
        else if(chk(xi,yi)>chk(xi,yi+ra)) yi+=ra;
        else ra*=0.5;
    }
    cout.precision(5);
    if(chk(xi,yi)<EPS) cout<<fixed<<xi<<" "<<yi<<endl;
    //system("pause");
    return 0;
}*/

//****************************************ROUND 3*****************************************************
/*
int main()//prob1
{
    int a,b,x,y,i,j;
    vs v;
    char c;cin>>c>>b;
    a=c-'a';
    cin>>c>>y; x=c-'a';
    i=a,j=b;
    while(i!=x||j!=y){
        string s;
        if(x>i) s.pb('R'),i++; else if(x<i) s.pb('L'),i--;
        if(y>j) s.pb('U'),j++; else if(y<j) s.pb('D'),j--;

        v.pb(s);
    }
    cout<<sz(v)<<endl;
    FORE(z,v) cout<<v[z]<<endl;
    //system("pause");
    return 0;
}*//*
int main()
{
    ifstream cin("a.in");
    int n,v,a,b,s1=0,s2=0;
    vector<pii > v1(100002,mp(0,0)),v2(100002,mp(0,0));
    cin>>n>>v;
    REP(i,n) {cin>>a>>b;if(a==1)v1[s1++]=mp(-b,i+1);else v2[s2++]=mp(-b,i+1);}
    sort(v1.begin(),v1.begin()+s1);sort(v2.begin(),v2.begin()+s2);
    int c1=0,c2=0;
    ll ans=0;
    ostringstream out;
    while(v>0&&(c1<s1||c2<s2)){
        //cout<<v<<endl;
        if(v>1){
            if(-v2[c2].st>=-v1[c1].st-v1[c1+1].st)
                out<<v2[c2].nd<<" ",ans+=v2[c2].st,v-=2,c2++;
            else out<<v1[c1].nd<<((1+c1<s1)?" "+toStr(v1[c1+1].nd):"")<<" ",ans+=v1[c1].st+v1[1+c1].st,v-=(1+c1<s1)?2:1,c1+=(1+c1<s1)?2:1;
        }
        else if(c1<s1)  out<<v1[c1].nd<<" ",ans+=v1[c1].st,v-=1,c1++;
        else break;
    }
    cout<<-ans<<"\n"<<out.str()<<endl;
    system("pause");
    return 0;
}*/
//*********************************************************ROUND 4*************************************************************
/*int r=0,n,w,h;
vector<pair<pii,int> > v;
int dp[5002];
int pre[5002];
int solve(int i)
{
    int &ret = dp[i];
    if(ret!=-1)  return ret;
    ret=1;
    REP(j,n+1){
        if(v[j].st.st>v[i].st.st&&v[j].st.nd>v[i].st.nd){
            if(ret<1+solve(j)){
                ret=1+solve(j);
                pre[i]=v[j].nd;
            }
        }
    }
    return ret;
}
int main()//prob 4
{
    ifstream cin("a.in");
    int a,b;

    cin>>n;
    v.resize(n+1);
    REP(i,n+1) {
        cin>>v[i].st.st>>v[i].st.nd;
        v[i].nd=i;
    }
    clr(dp,-1);
    clr(pre,-1);
    cout<<solve(0)-1<<endl;
    //out(vi(pre,pre+3));
    for (int i = pre[0]; i != -1; i = pre[i])
        cout<<i<<" ";
    //prin(pre[0]);
    system("pause");
    return 0;
}
/*
int main()//Prob 3
{
    ifstream cin("a.in");
    int n;
    cin>>n;
    string s;
    map<string,int> m;
    REP(i,n) {
        cin>>s;
        if(m.find(s)==m.end()){
            m.insert(mp(s,0));
            cout<<"OK"<<endl;
        }
        else{
            m.insert(mp(s+toStr(++m[s]),0));
            cout<<s<<m[s]<<endl;
        }
    }
    system("pause");
    return 0;
}*/
/*
int main()//prob 2
{
    ifstream cin("a.in");
    int d,st,a=0,smin=0,smax=0;

    cin>>d>>st;
    vi sm(d),sx(d);
    REP(i,d)
        cin>>sm[i]>>sx[i],smin+=sm[i],smax+=sx[i];

    if(st<smin||st>smax)  cout<<"NO"<<endl;
    else{
        cout<<"YES"<<endl;
        REP(i,d){
            FOR(j,sm[i],sx[i]){
                if(!(st-j<smin-sm[i]||st-j>smax-sx[i])){

                    cout<<j<<" ";
                    st-=j;
                    smin-=sm[i],smax-=sx[i];
                    break;
                }
            }
        }
    }
    system("pause");
    return 0;
}*/
/////////////////////////////////////////////////////////ROUND 5/////////////////////////////////////////////////////////
/*
int main()//prob1
{
    ifstream cin("a.in");
    string r;
    int t=0;
    set<string> s;
    while(getline(cin,r)){
        if(r[0]=='+') s.insert(r.substr(1));
        else if(r[0]=='-') s.erase(r.substr(1));
        else{
            t+=s.size()*(r.size()-r.find(':')-1);
        }
    }
    cout<<t<<endl;
    system("pause");
    return 0;
}*//*
int main()//prob2
{
    ifstream cin("a.in");
    vs v;
    int m=0,s=0,w=1;
    string r;
    while(getline(cin,r)){
        v.pb(r);
        s++;
        if(sz(r)>m) m=sz(r);
    }
    cout<<string(m+2,'*')<<endl;
    REP(i,s){
        if((m-sz(v[i]))%2==0)
            cout<<"*"+string((m-sz(v[i]))/2,' ')+v[i]+string((m-sz(v[i]))/2,' ')+"*"<<endl;
        else if(w) cout<<"*"+string((m-sz(v[i]))/2,' ')+v[i]+string(1+(m-sz(v[i]))/2,' ')+"*"<<endl,w=0;
        else cout<<"*"+string(1+(m-sz(v[i]))/2,' ')+v[i]+string((m-sz(v[i]))/2,' ')+"*"<<endl,w=1;
    }
    cout<<string(m+2,'*')<<endl;
    system("pause");
    return 0;
}*/
/*
int main()//prob3
{
    ifstream cin("a.in");
    ios::sync_with_stdio(false);
    string r;
    cin>>r;
    vi d(sz(r)+1,0), c(sz(r)+1,0);
    int sz = 0, ma = 0;
    stack<int > s;
    FORE(i,r){
        if(r[i]=='(') s.push(i);
        else{
            if(sz(s)) {
                d[i] = s.top();
                s.pop();
                c[i] = i + 1 - d[i];
                if (d[i] - 1 >= 0 && r[d[i] - 1] == ')')
                    c[i] += c[d[i] - 1];
            }
        }
    }
    ma = *max_element(c.begin(), c.begin() + sz(r));
    if (!ma) cout << "0 1" << endl;
    else cout << ma << " " << count(c.begin(), c.begin() + sz(r), ma) << endl;
    system("pause");
    return 0;
}*/
/*
int main()
{
    int d, l, w, v;

    return 0;
}
*//*
int main()
{
    ifstream cin("a.in");
    vs ss(3);
    vi sym(3); sym[0] = 2, sym[1] = 1, sym[2] = 0;
    REP(i, 3) cin >> ss[i];
    int z = 0;
    REP(i, 3) REP(j, 3) if (ss[i][j] == 'X'&&ss[sym[i]][sym[j]] != 'X') z = 1;
    cout << ((z) ? "NO" : "YES") << endl;

    system("pause");
    return 0;
}*/
/*
int main()
{
    string n, m;
    cin >> n >> m;
    sort(ALL(n));
    int j=-1;
    FORE(i, n){
        if (n[i] != '0'){ j = i; break;}
    }
    //cout << n << endl;
    string an;
    if (j != -1){
        an.pb(n[j]);
        FORE(i, n){
            if (i == j){ continue; }
            an.pb(n[i]);

        }
    }
    else{
        an = string(sz(n), '0');
    }
    //cout << an << endl;
    if (m == an) cout << "OK";
    else cout << "WRONG_ANSWER";
    //system("pause");
    return 0;
}
*/
/*
int main()
{
    //ifstream cin("a.in");
    int n, m;
    cin >> n >> m;
    map<string, int> ma;
    string s;
    vi tag(n);
    REP(i, n) cin >> tag[i];
    REP(i, m){
        cin >> s;
        ma[s]++;
    }
    vi tab(ma.size());
    vi bat(ma.size());
    int j = 0;
    for (map<string, int>::iterator it = ma.begin(); it != ma.end(); it++, j++)
        tab[j] = it->second;
    sort(RALL(tab));
    sort(ALL(tag));
    ll mi = 0, mam = 0;
    FORE(i, tab){
        mi += tab[i] * tag[i];
    }
    sort(RALL(tag));
    FORE(i, tab){
        mam += tab[i] * tag[i];
    }

    cout << mi << " " << mam << endl;

    //system("pause");
    return 0;
}*/
/*
int main()
{
    int n;
    cin >> n;
    vector<pair<pii, pii > > tab(n);
    REP(i, n){
        cin >>tab[i].st.st>>tab[i].st.second>>tab[i].second.first;
        tab[i].second .nd= i;
    }
    sort(ALL(tab));
    FORE(i, tab) cout << " " << tab[i].second.second;
    int an = 0;
    REP(i, n-1){
        if (tab[i].first.first < tab[n - 1].first.first&&tab[i].first.nd < tab[n - 1].first.nd
            &&tab[i].nd.first < tab[n - 1].nd.first)
            an++;
    }
    cout << an << endl;

    system("pause");
    return 0;
}*/

/*int v[1002][1002];
int main()
{

    int n;
    cin >> n;
    REP(i, n) REP(j, n) v[i][j] = (i == j) ? 0 :  (i + j) % (n);
    REP(i, n) FOR(k, 1, n - 1){
        int a = 0;
        REP(j, n)
        if (v[i][j] == k){ a = 1; break; }
        if (!a)v[i][n - 1] = v[n - 1][i] = k;
    }

    REP(i, n){
        REP(j, n)
            cout << v[i][j] << " ";
        cout << endl;
    }
    system("pause");
    return 0;

    }*//*
int main()
{

    //ifstream cin("a.in");
    int r, g, b;
    cin >> r >> g >> b;
    int an = 29,t=0;
    while (r || g || b){
        if (t % 3 == 0 && r) r -= (r >= 2) ? 2 : 1;
        else if (t % 3 == 1 && g) g -= (g >= 2) ? 2 : 1;
        else if (t % 3 == 2&& b) b -= (b >= 2) ? 2 : 1;
        an++, t++;
    }
    cout << an << endl;
    system("pause");
    return 0;

}*/
/*int main()
{

    ifstream cin("a.in");
    int n,m;
    cin >> n >> m;
    vs v(n);
    int vis[101][101];
    clr(vis, 0);
    REP(i, n) cin >> v[i];
    REP(i, n) {
        vector<char> f(128, 0);
        REP(j, m) {
            f[v[i][j]]++;
        }
        REP(j,m){
            if (f[v[i][j]] > 1) vis[i][j] = 1;
        }
    }
    REP(i, m) {
        vector<char> f(128, 0);
        REP(j, n) {
            f[v[j][i]]++;
        }
        REP(j, n){
            if (f[v[j][i]] > 1) vis[j][i] = 1;
        }
    }
    string s;
    REP(i, n) {
        REP(j, m) {
            if (!vis[i][j])
            s.pb(v[i][j]);
        }
    }
    cout << s << endl;
    system("pause");
    return 0;

}*/
/*int n, m;
char v[5001][5001];
char res[5001][5001];
int solve(int r, int c)
{
    if (!isvalid(r, c, n, m)) return 0;
    res[r][c] = 1;
    if (v[r][c] == 'D'){
        int x = r + 1;
        for (; x < n&&v[x][c] == '.'; x++);
        if (isvalid(x, c, n, m)) {
            v[r][c] = '.';
            x = 1 + solve(x, c);
            v[r][c] = 'D';
            return x;
        }

    }
    else if (v[r][c] == 'U'){
        int x = r - 1;
        for (; x >= 0&&v[x][c] == '.'; x--);
        if (isvalid(x, c, n, m)) {
            v[r][c] = '.';
            x = 1 + solve(x, c);
            v[r][c] = 'U';
            return x;
        }

    }
    else if (v[r][c] == 'R'){
        int y = c + 1;
        for (; y < m&&v[r][y] == '.'; y++);
        if (isvalid(r, y, n, m)) {
            v[r][c] = '.';
            y = 1 + solve(r, y);
            v[r][c] = 'R';
            return y;
        }
    }
    else {
        int y = c - 1;
        for (; y>=0&&v[r][y] == '.'; y--);
        if (isvalid(r, y, n, m)) {
            v[r][c] = '.';
            y= 1 + solve(r, y);
            v[r][c] = 'L';
            return y;
        }
    }
    return 0;
}
void main()
{
    ifstream cin("a.in");
    cin >> n >> m;
    REP(i, n) {
        REP(j, m) res[i][j] = 0, cin >> v[i][j];
        cin.ignore();
    }
    int ma = 0, c = 0;
    if (n == 1 && v[0][0] == 'R'&&v[0][1] == 'R'&&v[0][2] == 'R'){ cout << "5000 2" << endl; return;}
    if (m == 1 && v[0][0] == 'D'&&v[1][0] == 'D'&&v[2][0] == 'D'){ cout << "5000 2" << endl; return; }
    REP(i, n) REP(j, m)if(v[i][j]!='.') {
        int l = 1+solve(i, j);
        if (l > ma)ma = l, c = 1;
        else if (l == ma) c++;
    }
    cout << ma << " " << c << endl;
    system("pause");
    }*//*
void main()
{
    //ifstream cin("a.in");
    vi r(4);
    REP(i, 4) cin >> r[i];
    int f = 0;
    REP(i, 4){
        vi v;
        REP(j, 4){
            if (i == j) continue;
            v.pb(r[j]);
        }
        //out(v);
        if (v[0] + v[1] > v[2] && v[0] + v[2] > v[1] && v[2] + v[1] > v[0]) { cout << "TRIANGLE"; return; }
        else if (v[0] + v[1] == v[2] || v[0] + v[2] == v[1] || v[2] + v[1] == v[0]) f = 1;
    }
    cout << ((f) ? "SEGMENT" : "IMPOSSIBLE") << endl;;

    system("pause");
}*/

/*
void main()
{
    ifstream cin("a.in");
    vs v(8) ;
    int a=0;
    vi r(8,0),c(8,0);
    REP(i,8){ cin>>v[i]; if(v[i]=="BBBBBBBB")r[i]=1;}
    REP(i,8) {
        a=0;
        REP(j,8) if(v[j][i]=='W') {a=1;break;}
        if(!a) c[i]=1;
    }
    if(r==vi(8,1)) cout<<8<<endl;
    else{
        cout<<count(ALL(r),1)+count(ALL(c),1)<<endl;
    }

    sys;
}*/
/*
void main()
{
    ifstream cin("a.in");
    int t,n,r;
    cin>>t>>n;
    vi v(n,0),gap(n);
    string s;
    REP(i,t){
        cin>>s;
        if(s=="defragment"){

        }
        else if(s=="alloc"){
            cin>>r;
        }
        else {
            cin>>r;
        }
    }
    sys;
}*//*
void main()
{
    ifstream cin("a.in");
    int n,m;
    cin>>n>>m;
    vs v(n);
    int last=-1,l=0;
    REP(i,n) {
        l=0;
        cin>>v[i];
        REP(j,10) {
            if(v[i]==string(m,j+'0')&&j!=last){
                //cout<<last<<" "<<j<<endl;
                last=j;l=1;
                break;
            }
        }
        if(l==0) break;
    }
    if(l==0)  cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
    sys;
}*/

void main()
{
    //ifstream cin("a.in");
    int n,m,a,b;
    cin>>n>>m;
    vpi v(m);
    ll ans=0;
    REP(i,n) {
        cin>>a>>b;
        v[i]=mp(b,a);
    }
    sort(RALL(v));
    REP(i,m){
        if(v[i].Y>=n) n-=n,ans+=v[i].X*n;
        else ans+=v[i].X*v[i].Y,n-=v[i].Y;
        if(!n) break;
    }
    cout<<ans<<endl;
    //sys;
}

#include<iostream>
#include<map>
#include<iterator>
using namespace std;

int main() {
	int m, n;
	int a, b;
	map<int, int> l;
	scanf("%d %d", &m, &n);
	while(n--) {
		scanf("%d %d", &a, &b);
		l.insert(pair<int, int>(b, a));
	}
	int sum = 0;
	for(map<int, int>::reverse_iterator rit = l.rbegin(); rit != l.rend(); ++rit) {
		if (m == 0) {
			break;
		}else if (rit->second <= m) {
			sum+= rit->first*rit->second;
			m -= rit->second;
		} else {
			sum += m*rit->first;
			m = 0;
		}
	}
	printf("%d\n", sum);
	return 0;
}

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	long long int m,n;
	cin>>m>>n;
	long long int a[n],b[n];
	for(long long int i=0;i<n;i++)
	cin>>a[i]>>b[i];
	for(long long int i=0;i<n-1;i++)
	{
		if(b[i+1]>b[i])
		{
			swap(b[i+1],b[i]);
			swap(a[i+1],a[i]);
		}
	}
	long long int i=0,ans=0;
	while(m>0)
	{
		if(a[i]<=m)
		{
			ans=ans+a[i]*b[i];
			m=m-a[i];
			i++;
		}
		else
		{
			ans=ans+m*b[i];
			i++;
		}
	}
	cout<<ans<<endl;

}

#include <bits/stdc++.h>

using namespace std;

bool gsp (pair<int, int> a, pair<int, int> b){
    if (a.second > b.second){
        return true;
    }
    else{
        return false;
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> v1;
    for (int i = 0; i < m; ++i){
        int x, y;
        cin >> x >> y;
        v1.push_back({x, y});
    }
    if (m == 1){
        if (v1[0].first > n){
            cout << n * v1[0].second;
            return 0;
        }
        else{
            cout << v1[0].first * v1[0].second;
            return 0;
        }
    }
    sort (v1.begin(), v1.end(), gsp);
    /*for (pair<int, int> b : v1){
        cout << b.first << " " << b.second << endl;
    }*/
    int x = n, l = 0;
    long long sum = 0;
    while ((x > 0) || (l < m)){
        if (x > v1[l].first){
            x -= v1[l].first;
            sum += v1[l].first * v1[l].second;
            l++;
        }
        else if (x <= v1[l].first){
            sum += x * v1[l].second;
            x = 0;
            l++;
        }
    }
    cout << sum;
}

#include <iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<iomanip>
#include<set>
#include<map>
#include<cmath>
#include<sstream>
#include<iterator>
//#include <bits/stdc++.h>
#define el 	puts("YES");
#define ell 	puts("NO");
#define fast std::ios_base::sync_with_stdio(NULL);cin.tie(NULL); cout.tie(NULL);
//#include<bits/stdc++.h>
using namespace std;
int prm[70] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 };
long long gcd(long long  a, long long  b)
{
	return b == 0 ? a : gcd(b, a % b);
}
long long  lcm(long long  a, long long  b)
{

	return (a * b) / gcd(a, b);
}
long long fact(long long a)
{
	long long ans = 1;
	for (int i = 1; i <= a; i++)
	{
		ans *= i;
	}
	return ans;
}
bool isprime(int x)
{
	int c = 1;
	for (int i = 2; i*i <= x; i++)
	{
		if (x%i == 0)
		{
			c = 0;
			break;
		}
	}
	if (c)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int fre[10001], freb[100001], frec[100001];
int arr[100001], com[10001], s[10001];
long long fib(long long n)
{
	if (n <= 2)
	{
		return 1;
	}
	return fib(n - 1) + fib(n - 2);
}
int main()
{
	fast;
	long long  n, m,cnt=0;
	cin >> n>>m;
	pair<long long, long long>p[21];
	int x, y;
	for (int i = 0; i < m; i++)
	{
		cin >> x >> y;
		p[i].second = x; p[i].first = y;
	}
	sort(p, p + m);
	for (int i = m-1; i >= 0; i--)
	{
		if (n == 0)
		{
			break;
		}
		if (p[i].second <= n)
		{
			n -= p[i].second;
			cnt += (p[i].second*p[i].first);
		}
		else
		{
			int a = p[i].second - n+1;
			cnt += (p[i].first*a);
		}
	}

	cout << cnt;
	//system("pause");
}

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v)	((v).begin()), ((v).end())
#define sz(v)	((int)((v).size()))
typedef long long ll;
int main(){
	ll n, m, s = 0; cin >> n >> m;pair<ll, ll>v[21]; ll j = m - 1;
	for (ll x = 0; x<m; x++){ cin >> v[x].second >> v[x].first; }
	sort(v,v+m);
	while (n>0){
		if (n >= v[j].second)
			s+= ((v[j].second)*v[j].first);
		else s += (n*v[j].first);
	n -= v[j].second;
	j--; }
	cout << s;

}

#include <iostream>
using namespace std;

int n, m;
int box[20], num[20];

int main(void)
{
	cin>>n>>m;
	for(int i=0; i<m; ++i)
		cin>>box[i]>>num[i];
	int ans=0;
	for(int i=0; i<m; ++i)
	{
		int max=0;
		int j=0;
		while(j<m)
		{
			if(num[max]<num[j])
				max=j;
			++j;
		}
		if(ans+box[max]*num[max]>n)
		{
			ans+=(n-ans)/num[max]*num[max];
			break;
		}
		ans+=box[max]*num[max];
		num[max]=-1;
	}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

struct node{
    int a,b;
}cont[25];

bool cmp(node x,node y){
    return x.b>y.b;
}

int main(){
    int n,m,ans=0;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>cont[i].a>>cont[i].b;
    }

    sort(cont+1,cont+m+1,cmp);

    for(int i=1;i<=m;i++){
        if(n>cont[i].a){
            ans+=(cont[i].a)*(cont[i].b);
            n-=cont[i].a;
        }
        else{
            ans+=n*(cont[i].b);
            cout<<ans<<endl;
            return 0;
        }
    }
}

#include <iostream>
#include <sstream>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <string.h>
#include <iomanip>
#include <cmath>
#define all(v) v.begin(), v.end()
#define ll long long
#define MP make_pair
#define sz(v) v.size()
#define PI 3.14159265358979323846
#define lp(i, n) for (long long i = 0; i < n; i++)
#define rep(i, v) for (int i = 0; i < int(v.size()); i++)
using namespace std;
ll sum,cnt, max_l=-1e9,min_sub, ans;
const int N = 1e3 + 9;
bool vis[N];
int arr[26];
map<int, ll> rep;
bool cmp(const pair<ll,ll> a,const pair<ll,ll> b)
{
	return a.second>b.second;
}
void run() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
}
int main()
{
	run();
	ll n,m;
	cin>>n>>m;
	vector<pair<ll,ll> > pr;
	while(m--)
	{
		ll a,b;
		cin>>a>>b;
		pr.push_back({a,b});
	}
	sort(all(pr),cmp);
	while(n!=0)
	{
		ans+=min(pr[cnt].first,n)*pr[cnt].second;
		n-=min(n,pr[cnt].first);
		cnt++;
	}
	cout<<ans;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b;
    int arr[b],arr2[b],arr3[b];
    for(int i=0;i<b;i++)
    {
        cin>>c>>d;
        arr[i]=c;
        arr2[i]=d;
    }
    for(int i=0;i<b-1;i++)
    {
        for(int j=0;j<b-i-1;j++)
        {
            if(arr2[j]>arr2[j+1])
            {
                swap(arr2[j],arr2[j+1]);
                swap(arr[j],arr[j+1]);
            }
        }
    }
    cout<<a;
    int sum=0;
    for(int i=b-1;i>=0;i--)
    {
        if(arr[i]<a)
        {
            sum+=arr[i]*arr2[i];
            a=a-arr[i];
        }
        else
        {
            sum+=a*arr[i];
            break;
        }
    }
    cout<<sum;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long int
#define ull unsigned long long int
#define fasterInOut ios::sync_with_stdio(false); cin.tie(0);
#define pi acos(-1)

int main()    {
//	freopen("/home/mimi0007/Desktop/Temp/in", "r", stdin);
	fasterInOut;
	ll n, m;
	cin >> n >> m;
	ll ar[20], aa[m+2];
	memset(ar, 0, sizeof(ar));
	ll a, b;
	for(ll i = 0; i < m; i++) {
		cin >> a >> b;
		ar[b] = a;
		aa[i] = b;
	}
	ll sum = n;
	ull ss = 0;
	sort(aa, aa+m, greater <ll> ());
	for(ll i = 0; i < m; i++) {
		if(sum > 0) {
			if(ar[aa[i]] < sum) {
				ss += aa[i]*ar[aa[i]];
				sum -= ar[aa[i]];
			}

			else {
				ss += sum * aa[i];
				sum = 0;
				break;
			}
		}
	}
	cout << ss << endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

int n,m;

void takeInput()
{
	int n,m;
	cin>>n>>m;
	priority_queue<pair<long long int,long long int>>pq;
	for(int i=0;i<m;i++)
	{
        long long  int a,b;
		 cin>>a>>b;
		 pq.push({b,a});
	}
	long long int total=0;
	while(n>0)
	{
		pair<long long int,long long int> p=pq.top();
		if(p.second>=n)
		{
			total+=(n*p.first);
			n-=p.second;
		}
		else
		{
			/* code */
			total+=(p.second*p.first);
			n-=p.second;
			pq.pop();
		}

	}
	cout<<total;
}
int main(){
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
    #endif
	takeInput();
}

#include<bits/stdc++.h>

using namespace std;

/// Driver function to sort the vector elements
/// by second element of pairs
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
{
    return (a.second < b.second);
}

int main()
{
    long int n,m,ans=0,ck=0,a=0,b=0;

    vector<pair<int,int> >piv;

    cin>>n>>m;

    for(int i=0; i<m; i++){
        cin>>a>>b;
        piv.push_back(make_pair(a,b));
    }
    sort(piv.begin(),piv.end(),sortbysec);
    int j=m-1;
    if(m==1){
        if(piv[j].first<=n){
            ans+=(piv[j].first*piv[j].second);
            ///cout<<n<<" "<<ck<<" "<<ans<<endl;
        }
        if(piv[j].first>n){
            ans+=(n*piv[j].second);
        }
        cout<<ans<<endl;
    }
    else{
        while(ck<=n){
            if(piv[j].first<=n){
                n-=piv[j].first;
                ck+=piv[j].first;
                ans+=(piv[j].first*piv[j].second);
                ///cout<<n<<" "<<ck<<" "<<ans<<endl;
                if(j>0)
                    j--;
            }
            if(piv[j].first>n){
                ck+=n;
                ans+=(n*piv[j].second);
                ///cout<<n<<" "<<ck<<" "<<ans<<endl;
                if(j>0)
                    j--;

                n=0;
            }
            ///cout<<j<<endl;
        }
        cout<<ans<<endl;
    }

    return 0;
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std ;
__int64 ans ;
struct Box
{
    __int64 a , b ;
    __int64 c ;
} box[25] ;

int cmp ( Box i , Box j ) { return i.c > j.c ; }
int main ()
{
    int n , m , i , j ;
    scanf ( "%d%d" , &n , &m ) ;
    for ( i = 1 ; i <= m ; i ++ )
    {
        scanf ( "%d%d" , &box[i].a , &box[i].b ) ;
        box[i].c = box[i].a * box[i].b ;
    }
    sort ( box + 1 , box + m + 1 , cmp ) ;
    ans = 0 ;
    j = 0 ;
    int k ;
    for ( i = 1 ; i <= m ; i ++ )
    {
        j += box[i].a ;
        if ( j >= n )
        {
            ans += box[i].b * box[i].a ;
            k = i ;
            break ;
        }
        ans += box[i].b * box[i].a ;
    }
    if ( j > n ) ans = ans - box[k].b * ( j - n ) ;
    printf ( "%I64d\n" , ans ) ;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b;
    int arr[b],arr2[b];
    for(int i=0;i<b;i++)
    {
        cin>>c>>d;
        arr[i]=c;
        arr2[i]=d;
    }
    for(int i=0;i<b-1;i++)
    {
        for(int j=0;j<b-i-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                swap(arr[j],arr[j+1]);
                swap(arr2[j],arr2[j+1]);
            }
        }
    }
    int max=0,sum=0,e=0;
    for(int i=0;i<b;i++)
    {
        for(int j=0;j<b;j++)
        {
            sum+=arr2[j]*arr[j];
            e+=arr[j];
            if(e==a)
            {
                if(sum>max)
                {
                    max=sum;
                }
                e=0;
                sum=0;
            }
        }
    }
    cout<<max;
}

//KUNAL FARMAH
//@kunalfarmh98@gmail.com

#include<bits/stdc++.h>
using namespace std;

typedef long long unsigned ll;
	bool comp(pair<int,int> a, pair<int,int> b){
		ll ar = a.second*a.first * a.second/a.first;
		ll br = b.second*b.first * b.second/b.first;

		if(ar==br){
			return(a.second>b.second);
		}
		else{
			return ar>br;
		}
	}

int main(){
	int n,m;
	cin>>n>>m;
	pair<int,int> box[m];
	for(int i=0; i<m; i++){
		cin>>box[i].first>>box[i].second;
	}
	sort(box,box+m,comp);

	// for(int i=0 ; i<m; i++){
	// 	cout<<box[i].first<<"->"<<box[i].second<<endl;
	// }

	cout<<endl;
	ll ans=0;
	int i=0;
	for(; i<m && box[i].first<=n; i++){
		ans+=(box[i].first*box[i].second);
		n-=box[i].first;
	}

	if(i<m && n>0)
	ans+=n*box[i].second;

	cout<<ans<<endl;

	return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<utility>
#include<algorithm>

#define pb push_back
#define REP(a,b) for(int(a)=0;(a)<(b); (a)++)
using namespace std;

int main(){
	int n, k;
	scanf("%i%i",&n,&k);
	int kol;
	int hod;
	vector<pair<int,int> > vse;
	REP(a,k){
		scanf("%i%i",&kol,&hod);
		vse.pb(make_pair(hod,kol));
	}
	sort(vse.rbegin(),vse.rend());
	int d = 0;
	long long kolik = 0;
	while(n>0){
		if(vse[d].second <= n){
			kolik+=(long long)vse[d].second*vse[d].first;
			n-= vse[d].second;
		}else{
			kolik+=(long long)vse[d].first*n;
			n=0;
		}
		d++;
	}
	printf("%i\n",kolik);

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	vector<int>vec;
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		for(int j=0;j<a;j++)
			vec.push_back(b);

	}
	sort(vec.begin(),vec.end());
	int ans=0,z=vec.size()-n;
	for(int i=vec.size()-1;i>=z;i--)
		ans+=vec[i];
	cout<<ans;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
//int arr[m],arr1[m];
unordered_map<int,int> up;
    vector<int> v;
for(int i=0;i<m;i++){
    int a,b;

    cin>>a>>b;

    v.push_back(b);up[b]=a;
}
sort(v.begin(),v.end());
int sum=0,count=0;
for(int i=0;i<v.size();i++){
    if(up[v[i]]==n){
    sum=sum+(v[i]*up[v[i]]);
    break;
    }
     else if(up[v[i]]<n){
        sum=sum+(v[i]*up[v[i]]);
        n=n-up[v[i]];
    }
        else{
        int temp=up[v[i]]-n;
        sum=sum+(temp*v[i]);
        break;
    }
}
cout<<sum+4;
return 0;
}

#include <iostream>
#include <iomanip>
#include <climits>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdlib>

using namespace std;

int main()
{
    long long n;
    short m;
    long a[m],b[m];
    cin>>n>>m;

    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i];

    long temp1=0,mx=0,ans=0;
    for(int i=0;i<m;i++)
    {
        mx=i;
        for(int j=i+1;j<m;j++)
            if(b[j]>b[mx])
                mx=j;
        temp1 = b[i];
        b[i] = b[mx];
        b[mx] = temp1;

        if(n>a[i])
        {
            n-=a[i];
            ans += (b[i]*a[i]);
        }
        else
        {
            ans += (b[i]*n);
            n=0;
            break;
        }

    }
    cout<<ans;

    return 0;
}
/*comments*/
// a = 97  z = 122
// A = 65  Z = 90

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<iomanip>
#include<math.h>
#include<string>
#include<algorithm>
#include<stack>
#include<limits.h>
#include<numeric>
#include<map>
#include<vector>
#include<queue>
#define ll long long
#define str string
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define vec vector<int>
#define long_vec vector<ll>
#define out cout<<
#define in cin>>
#define rep(a,b) for(int i=a;i<b;i++)
#define nl cout<<endl;
#define sortv(v) sort(v.begin(),v.end())
#define ret return 0;
#define countr(v,a) (int)count(v.begin(),v.end(),a)
#define FILL(a,b) memset((a),(b),sizeof((a)))
#define start int main()
using namespace std;
start
{
    ll n,inp,k,a,b;
    in k;in n;
    long_vec va,vb;
    rep(0, n)
    {
        in a;in b;
        va.push_back(a);
        vb.push_back(b);
    }
    ll temp,temp1;
    for(int i=0;i<vb.size()-1;i++)
    {
        for(int j=i+1;j<va.size();j++)
        {
            if(vb[i]<vb[j])
            {
                temp=vb[i];
                vb[i]=vb[j];
                vb[j]=temp;

                temp1=va[i];
                va[i]=va[j];
                va[j]=temp1;
            }
        }
    }
    ll total_matches=0;
    ll counter=0;
    ll i=0;
    /*if(n==1)
    {
        if(va[i]<k)
        {
            out va[i]*vb[i];
        }
        else
            out k*vb[i];
        exit(0);
    }*/
    while(counter<k)
    {
       // out "VA "<<va[i]<<" VB "<<vb[i]<<" K "<<k<<" counter "<<counter;nl
        if((k-counter)<=va[i])
        {
            out total_matches;nl
            total_matches+=(k-counter)*vb[i];
            out total_matches;
            exit(0);
        }
        else
        {
            counter+=va[i];
            total_matches+=va[i]*vb[i];
        }
        i++;
    }
    out total_matches;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int main ()
{
    vector < vector<int>> v;
    string x;
    int y, n, m, f;

    cin>> n;
    cin>>m;
    if ( cin.peek() == '\n' )
        cin.ignore();
    getline( cin, x, '\n');
    v.resize(m);
    f= x.find(' ');
    x.erase(f,1);
     v[0].resize(2);
     y =atoi (x.c_str());
     if (f == x.size()-2)
     {
         v[0][0] = y/100;
         v[0][1] = y%100;
     }
     else
     {
         v[0][0] = y/10;
        v[0][1] = y%10;
     }

    for (int i = 1; i< m; i++)
    {
        getline( cin, x, '\n');
    f= x.find(' ');
    x.erase(f,1);
     v[i].resize(2);
     y =atoi (x.c_str());
     if (f == x.size()-2)
     {
         v[i][0] = y/100;
         v[i][1] = y%100;
     }
     else
     {
         v[i][0] = y/10;
        v[i][1] = y%10;
     }

    }

    for (int i =0; i< m-1; i++)
        for (int j=i+1; j< m; j++)
        {
            if (v[i][1] < v[j][1])
            {
                swap ( v[i], v[j]);

            }

        }

    int p =0, count =0, tot =0;

    for (int h =0; h< m; h++)
        tot+= v[h][0];

    if ( n> tot)
        for (int a =0; a< m; a++)
                count+= (v[a][0]* v[a][1]);
    else
    {
    while ( n> 0)
    {
        if (n>= v[p][0])
        {
            count += v[p][0]*v[p][1];
            n-= v[p][0];
            p++;

        }
        else
        {
            count+= n* v[p][1];
            n=0;
        }

    }
    }

    cout << count;

    return 0;
}

#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
int z,b;
cin >> z>>b;
int a[b][2];
for(int i=0;i<b;i++)
{
    cin >> a[i][0]>>a[i][1];
}
for (int i=0;i<b;i++)
    for(int f=0;f<b;f++)
{
    if (a[i][1]>a[f][1])
    {
        swap(a[i][1],a[f][1]);
                swap(a[i][0],a[f][0]);

    }
}
//cout <<a[0][0];
int s=0;
for(int i=0;z>0;i++)
{
    if (a[i][0]<z)
    {
        z-=a[i][0];
        s+=a[i][0]*a[i][1];
    }
    else
    {
        s+=z*a[i][1];
        z=0;
    }
}
cout << s<<endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7, M = 1e3, OO = 0x3f3f3f3f;
void IO()
{
#ifndef ONLINE_JUDGE
    freopen("in.in","rt",stdin);
    freopen("out.out","wt",stdout);
#endif
}
pair<long long,long long> arr[N];
bool comp(pair<long,long >a,pair<long,long> b)
{
    return a.first*a.second > b.first*b.second;
}
int main()
{
    long long n,m,i=0;
    long long ctr=0;
    scanf("%I64d%I64d",&n,&m);
    for(int i=0;i<m;++i)
    {
        long long x,y;
        scanf("%I64d%I64d",&x,&y);
        arr[i].first=x;
        arr[i].second=y;
    }
    sort(arr,arr+m,comp);
    while(i<=m && n)
    {
      if(n>=arr[i].first)
      {
          n-=arr[i].first;
          ctr+=arr[i].first*arr[i].second;
          i++;
      }
      else
      {
        ctr+=n*arr[i].second;
        n=0;
        i++;
      }
    }
    printf("%I64d",ctr);
    return 0;
}

#include <iostream>
#include<string>
#include<sstream>
#include<fstream>
#include<vector>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iomanip>
typedef long long ll;
using namespace std;
bool sortbysec(const pair<int,int> &a,
    const pair<int,int> &b)
{
    return (a.second > b.second);
}
int main()
{

    int N,M;
    scanf("%d%d",&N,&M);

    vector<pair<int,int>> V;

    for(int i=0;i<M;i++)
    {
        int X1,X2;
        scanf("%d%d",&X1,&X2);
        V.push_back(make_pair(X1,X2));

    }

    sort(V.begin(),V.end(),sortbysec);

    int Cnt=0;
    int Cnt2=0;
    int Sol=0;

    while(true)
    {
        if(V[Cnt].first > N)
        {
            int X = abs(N-V[Cnt].first);
            N=0;
            Sol+=(V[Cnt].first-X) * V[Cnt].second;
            Cnt++;

        }
        else
        {
            N-=V[Cnt].first;
            Sol+=(V[Cnt].first) * V[Cnt].second;
            Cnt++;

        }
        if(N==0 || Cnt>V.size())
        {
            break;

        }

    }

    cout<<Sol<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;

#define syncC ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define io freopen("i.txt","r",stdin);freopen("o.txt","w",stdout);
#define f1(n,m) for(int i=n;i<=m;i+=1)
#define f2(n,m) for(auto n:m)
#define sq(a) ((a)*(a))

bool sortSec(const pair<int,int> &a,const pair<int,int> &b){return (a.second>b.second);}
ll solve(){};

int main(){
    syncC;
    //io;

    double x,y,z,a,b;
    vector<pair<int,int>>vp;

    cin>>x>>y;
    f1(0,y-1){
        cin>>a>>b;
        vp.push_back(make_pair(a,b));
    }
    sort(vp.begin(),vp.end(),sortSec);
    z=0;a=0;b=0;
    f1(0,vp.size()-1){
        a+=vp[i].first;
        b=vp[i].first;
        if(a<x){z+=b*vp[i].second;}
        else if(a==x){z+=b*vp[i].second;break;}
        else if(a>x){z+=b*vp[i].second*((x-(a-b))/b);break;}
    }
    cout<<z;

   return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define inf 1e9
pair<int ,int>a[100000];
bool comp(const  pair<long long ,int>&p1 , const  pair<long long,long long>&p2)
{
    if(p1.s < p2.s)
        return false;
    return true;
}
int main()
{
long long n,m;
long long sum=0;
cin>>n>>m;
for(int i=0;i<m;i++)
    cin>>a[i].f>>a[i].s;
    sort(a,a+m,comp);
if(m==1&&n<=a[0].f){cout<<n*a[0].s;return 0;}
if(m==1&&n>=a[0].f){cout<<a[0].f*a[0].s;return 0;}
for(int i=0;i<m;i++)
{
    if(a[i].f>=n)
    {
        sum+=n*a[i].s;
        cout<<sum;
         return 0;
         }
    else
    {
        sum+=a[i].f*a[i].s;
        n-=a[i].f;
    }
}

    return 0;
}

#include <iostream>
#include <queue>
using namespace std;

int main()
{
	int n, m;
	int solenmatches = 0;
	priority_queue<pair<int, int>>container;

	cin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		int ai, bi;
		cin >> ai >> bi;
		//pair<int, int>cross(ai, bi);
		container.push(make_pair(bi, ai));
	}

	while (n > 0)
	{
		pair<int, int>cross = container.top();
		container.pop();

		if (n>=cross.second)
		{
			solenmatches += cross.second * cross.first;
			n -= cross.second;
		}
		else
		{
			solenmatches += n * cross.first;
			n -= n;
		}
	}

	cout << solenmatches;
}

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v)	((v).begin()), ((v).end())
#define sz(v)	((int)((v).size()))
typedef long long ll;
int main(){
	int n, m, s = 0; cin >> n >> m;pair<int, int>v[21]; int j = m - 1;
	for (int x = 0; x<m; x++){ cin >> v[x].second >> v[x].first; }
	sort(v,v+m);
	while (n>0){
		s+=(min(n,v[j].second)*v[j].first);
	n -= v[j].second;
	j--; }
	cout << s;

}

#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b){if(b==0){return a;}else{return gcd(b, a%b);}}
long long binpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
//sorts 2d vector on the basis of a column
bool sortcol( const vector<long long int>& v1,
               const vector<long long int>& v2 ) {
 return v1[1] > v2[1];
}
int main()
{
    long int n, m, suma = 0;
    cin>>n>>m;
    vector<long int> ara;
    for(long int i = 0; i < m; i++)
    {
        long int a, b;
        cin>>a>>b;
        suma += a;
        for(long int j = 0; j < a; j++)
        {
            ara.push_back(b);
        }

    }
    sort(ara.begin(), ara.end(), greater<long int>());
    long int cnt = 0;
    for(long int i = 0; i < n; i++)
    {
        if(i > suma)break;
        cnt += ara[i];
    }
    cout<<cnt<<endl;
    return 0;

}

#include <bits/stdc++.h>
#define go ios::sync_with_stdio(false); cin.tie(nullptr);
#define cya return 0;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

using namespace std;

int main()
{
    go
    vector<pair<int,int>> v;
    int n,m,a,b,sum=0,i=0;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> a >> b;
        v.push_back(make_pair(b,a));
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    while(n!=0 && i!=n+1)
    {
        if(v[i].second<=n)
        {
            sum+=(v[i].first*v[i].second);
            n-=v[i].second;
        } else
        {
            sum+=n*v[i].first;
            n-=abs(n-v[i].second);
            break;
        }
        i++;
    }
    cout << sum;
    cya
}

/* freopen example: redirecting stdout */

#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <sstream>
#include <cstddef>
#include <algorithm>
#include <utility>
#include <iterator>
#include <numeric>
#include <list>
#include <complex>
#include <cstdio>
#include <climits>
#include <fcntl.h>
#include <unistd.h>

typedef long long ll;

typedef signed   long long i64;
typedef unsigned long long u64;

/*
typedef set<int> SI;
typedef vector<ld> VD;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<string> VS;
typedef map<string,int> MSI;
typedef pair<int,int> PII;
*/

#define REP(i, x) for ( i = (x).begin(); i != (x).end(); i++)
#define IREP(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)    //foreach

#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)

//  GCD
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

// Constants
const ll PI = 3.1415926535897932384626433832795;
const ll EPS = 1e-11;

using namespace std;

class A
{

    long long match;
    int cases;
    long long total;
    int i,j;
    vector<long long> vec;

      public:

      A()
      {

            total=0;
            cin>>match;
            cin>>cases;

            long long a[cases];
            long long b[cases];

            for (i=0 ; i < cases ; i++ )
            {
                cin>>a[i];
                cin>>b[i];

                vec.push_back(b[i]);
            }

            sort( vec.begin(), vec.end() );
            reverse(vec.begin(),vec.end());

             for(vector<long long>::const_iterator it = vec.begin(); it != vec.end(); it++)
             {

                  for(i=0;i<cases ;i++)
                  {
                          if(*it == b[i])
                          {

                                 break;
                          }

                  }

                  if(match == a[i])
                  {
                         total+=(match*b[i]);

                           break;
                  }

                  if(match < a[i])
                  {

                           total+=(((a[i] - match)+1)*b[i]);

                           break;

                  }
                  else
                  {
                           total += (a[i] * b[i]);
                           match -= a[i];

                  }

             }
             cout<<total;

      }

};

int main ()
{

    new A();

    return 0;
}

#include<iostream>
#include<vector>
//#include <math.h>;
using namespace std;
#define PI 3.14
int main(){
    int n,m,sum=0;
    cin>>n>>m;
    vector<int>boxes(m);
    vector<int>matches(m);
    for(int i=0;i<m;i++){
      cin>>boxes[i]>>matches[i];
    }

    for(int i=0;i<m-1;i++){
      int max=matches[i];
      int index=i;
      for(int j=i+1;j<m;j++){
        if(matches[j]>max){
           max=matches[j];
           index=j;
        }
      }
      if(n>=boxes[index]){
        sum+=matches[index]*boxes[index];
        n-=boxes[index];
        matches[index]=boxes[index]=-1;
      }
      else {
        sum+=matches[index]*n;
        n=0;
        break;

      }

  }
cout<<sum<<endl;
 //  system("pause");

}

#include <iostream>
#include <algorithm>
using namespace std;
int n,sol=0,m;
struct interval
{
    int box,match;
}v[8005];
bool cmp(interval a ,interval b)
{
    return a.box*a.match > b.box*b.match;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
        cin>>v[i].box>>v[i].match;
    sort(v+1,v+n+1,cmp);
    for(int i=1;i<=m;i++)
    {
        if(n>=v[i].box)
        {
            sol=sol+v[i].match*v[i].box;
            n=n-v[i].box;
            continue;
        }
        if(n<v[i].box)
        {
            sol=sol+v[i].match*n;
            cout<<sol;
            return 0;
        }
    }
    cout<<sol;
    return 0;
}

#include <iostream>

using namespace std;

int main()
{
    int n;
    short m;
    cin>>n>>m;
    int a[m],b[m];

    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i];

    int temp1=0,mx=0,ans=0;
    for(int i=0;i<m;i++)
    {
        mx=i;
        for(int j=i+1;j<m;j++)
            if(b[j]>b[mx])
                mx=j;

        temp1 = b[i];
        b[i] = b[mx];
        b[mx] = temp1;

        if(n>a[i])
        {
            n-=a[i];
            ans += (b[i]*a[i]);
        }
        else
        {
            ans += (b[i]*n);
            n=0;
            break;
        }
//cout<<ans<<endl;
    }
    cout<<ans;

    return 0;
}
/*comments*/
// a = 97  z = 122
// A = 65  Z = 90

#include<iostream>
using namespace std;
int freq[11];
int main()
{
    int n,m,max=0;
    cin>>n>>m;
    int a[m],b[m];
    for(int i=0; i<m; i++)
    {
        cin>>a[i]>>b[i];
        freq[b[i]]=a[i];
    }
    for(int i=10; i>=1; i--)
    {
        if(freq[i])
        {
            if(freq[i]<=n)
            {
                max+=i*freq[i];
                n-=freq[i];
            }
            else if(freq[i]>n)
            {
                max+=n*i;
                n=0;
            }
            if(n<=0)
            {

                break;
            }
        }
        cout<<max<<endl;
        return 0;
    }

}

#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <sstream>
#include <cstddef>
#include <algorithm>
#include <utility>
#include <iterator>
#include <numeric>
#include <list>
#include <complex>
#include <cstdio>
#include <climits>
#include <fcntl.h>
#include <unistd.h>

typedef long long ll;

typedef signed   long long i64;
typedef unsigned long long u64;

/*
typedef set<int> SI;
typedef vector<ld> VD;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<string> VS;
typedef map<string,int> MSI;
typedef pair<int,int> PII;
*/

#define REP(i, x) for ( i = (x).begin(); i != (x).end(); i++)
#define IREP(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)    //foreach

#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)

//  GCD
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

// Constants
const ll PI = 3.1415926535897932384626433832795;
const ll EPS = 1e-11;

using namespace std;

class A
{

    u64 match;
    int cases;
    u64 total;
    int i,j;
    vector<u64> vec;

      public:

      A()
      {

            total=0;
            cin>>match;
            cin>>cases;

            u64 a[cases];
            u64 b[cases];

            for (i=0 ; i < cases ; i++ )
            {
                cin>>a[i];
                cin>>b[i];

                vec.push_back(b[i]);
            }

            sort( vec.begin(), vec.end() );
            reverse(vec.begin(),vec.end());

             for(vector<u64>::const_iterator it = vec.begin(); it != vec.end(); it++)
             {

                  for(i=0;i<cases ;i++)
                  {
                          if(*it == b[i])
                          {

                                 break;
                          }

                  }

                  if(match == a[i])
                  {
                         total+=(match*b[i]);

                           break;
                  }

                  if(match < a[i])
                  {

                           total+=(match*b[i]);

                           break;

                  }
                  else
                  {
                           total += (a[i] * b[i]);
                           match -= a[i];

                  }

             }
             cout<<total;

      }

};

int main ()
{

    new A();

    return 0;
}

#include<iostream>
#include<map>
#include<iterator>
using namespace std;

int main() {
	int m, n;
	int a, b;
	map<int, int> l;
	scanf("%d %d", &m, &n);
	while(n--) {
		scanf("%d %d", &a, &b);
		l.insert(pair<int, int>(b, a));
	}
	int sum = 0;
	for(map<int, int>::reverse_iterator rit = l.rbegin(); rit != l.rend(); ++rit) {
		if (m == 0) {
			printf("%d\n", sum);
			return 0;
		}else if (rit->second <= m) {
			sum+= rit->first*rit->second;
			m -= rit->second;
		} else {
			sum += m*rit->first;
			printf("%d\n", sum);
			return 0;
		}
	}
	printf("%d\n", sum);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void selectionSort(int *arr,int *arr2, int n)
{
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++)
        if (arr[j] > arr[min_idx])
            min_idx = j;

        // Swap the found minimum element with the first element
        swap(&arr[min_idx], &arr[i]);
        swap(&arr2[min_idx], &arr2[i]);

    }
}

int main() {

long long  n,m;
cin>>n>>m ;
int a[m];
int b[m];

for(int i=0;i<n;i++){
	cin>>a[i]>>b[i];
}

selectionSort(b,a,m);

long long ans =0;
for(int i=0;i<m;i++){

	if(n>=a[i]){
		n-=a[i];
		ans+=(a[i]*b[i]);
	}
	else{
		ans+=(n*b[i]);
		break;

	}

}

cout<<ans ;

}

#include <iostream>
#include <cstdio>
#include <set>

using namespace std;

set <pair<int, int>, greater <pair<int, int>>> S;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;

        S.insert(make_pair(b, a));
    }

    __int64 res = 0;
    for (set <pair<int, int>, greater <pair<int, int>>>::iterator it = S.begin(); it != S.end(); it++) {
        int a = (*it).second;
        int b = (*it).first;

        if (a < n) {
            n -= a;
            res += a*b;
        }
        else {
            res += n*b;
            break;
        }
    }

     cout << res;

    return 0;
}

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

const int N = 2e5+5 , M = 1e2+5, OO = 0x3f3f3f3f, mod = 1e9+7;

vector <char> ans;
int len;
string s;

int main()
{
  scanf("%d", &len);
  cin >> s;
  while (len!=0) {
    if(len&1)
      ans.push_back(s[0]);
    else
      ans.insert(ans.begin(),s[0]);
    s.erase(0,1);
    len= s.length();
  }
  for (int i = 0; i < ans.size() ; ++i) {
    printf("%c", ans[i]);
  }
  printf("\n");
  return 0;
}

#include <iostream>
#include <iomanip>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cmath>

#define LL __int64
//define LL (long long)
using namespace std;

int main()
{
	LL n,m;
	cin >> n >> m;

	multimap<LL,LL> match;
	for(LL i = 0 ; i < m ; i++)
	{
		LL a,b;
		cin >> a >> b;
		match.insert(multimap<LL,LL>::value_type(b,a));
	}

	LL res = 0;
	multimap<LL,LL>::reverse_iterator it= match.rbegin();
	while(1)
	{

		if(n-(*it).second >= 0 )
		{
			n-=(*it).second;
			res += (*it).first*(*it).second;
			if(n==0)break;
		}
		else
		{
			res += n*(*it).first;
			n = 0;
			break;
		}

		if(it == match.rend())break;
		it++;
	}

	cout << res << endl;

}

#include <bits/stdc++.h>
using namespace std;
struct items{
    int weight;
    int value;
};

bool comp(struct items a,struct items b){

    return a.value>b.value;
}

void fractionalknapsack(int W,struct items arr[],int n){
    sort(arr,arr+n,comp);
    int ans=0;
    for(int i=0;i<n||W!=0;i++){
        if(arr[i].weight<=W){
            ans+=arr[i].weight*arr[i].value;
            W-=arr[i].weight;
        }
        else{
            ans+=W*arr[i].value;
            break;
        }
    }
    cout<<ans<<endl;
}
int main(){
    int W,n;
    cin>>W>>n;
    items arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i].weight>>arr[i].value;
    }
    if(n==1){
        cout<<arr[0].weight*arr[0].value;
    }
    else{
        fractionalknapsack(W,arr,n);
    }

    return 0;
}

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef stack<int> si;
typedef pair<int,int> pi;
typedef vector<pair<long int,int>> vpi;

#define li(i, a, b) for (int i=a; i<(b); i++)
#define fl(i, a) for (int i=0; i<(a); i++)
#define ln(i,a,b) for (int i = (b)-1; i >= a; i--)
#define bl(i,a) for (int i = (a)-1; i >0; i--)
#define blm(i,a) for (int i = (a)-1; i >= 0; i--)
#define el "\n"
#define sp " "
#define pb push_back
#define m0(array,n) memset(array,0,n)
#define mem(array,data,n) memset(array,data,n)

void fast(){
	ios::sync_with_stdio(0);
	cin.tie(0);
}
bool sortbysec(const pair<long int,int>&a,const pair<long int,int>&b){
	return(a.second>b.second);
}
int main(){
	fast();
	long int m,n;
	cin>>n>>m;
	vpi a(m);
	fl(i,m)cin>>a[i].first>>a[i].second;
	sort(a.begin(),a.end(),sortbysec);
	int in=0,c=0,am=n;
	if(a[0].first>n){
			cout<<n*a[0].second;
			return 0;
	}
	for(int i=0;am!=0;i++){
		if(am>=a[in].first){
			c+=a[in].first*a[in].second;
			am-=a[in].first;
			in++;
		}
		else
		{
			c+=am*a[in].second;
			break;
		}
	}
	cout<<c;
	return 0;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
    vector < vector<int>> v;
    string x;
    int y, n, m, f;

    cin>> n;
    cin>>m;
    if ( cin.peek() == '\n' )
        cin.ignore();
    getline( cin, x, '\n');
    v.resize(m);
    f= x.find(' ');
    x.erase(1,1);
     v[0].resize(2);
     y =atoi (x.c_str());
     if (f == x.size()-2)
     {
         v[0][0] = y/100;
         v[0][1] = y%100;
     }
     else
     {
         v[0][0] = y/10;
        v[0][1] = y%10;
     }

    for (int i = 1; i< m; i++)
    {
        getline( cin, x, '\n');
    f= x.find(' ');
    x.erase(1,1);
     v[i].resize(2);
     y =atoi (x.c_str());
     if (f == x.size()-2)
     {
         v[i][0] = y/100;
         v[i][1] = y%100;
     }
     else
     {
         v[i][0] = y/10;
        v[i][1] = y%10;
     }

    }

    for (int i =0; i< m-1; i++)
        for (int j=i+1; j< m; j++)
        {
            if (v[i][1] < v[j][1])
            {
                swap ( v[i], v[j]);

            }

        }

    int p =0, count =0, tot =0;

    for (int h =0; h< m; h++)
        tot+= v[h][0];

    if ( n> tot)
        for (int a =0; a< m; a++)
                count+= (v[a][0]* v[a][1]);
    else
    {
    while ( n> 0)
    {
        if (n>= v[p][0])
        {
            count += v[p][0]*v[p][1];
            n-= v[p][0];
            p++;

        }
        else
        {
            count+= n* v[p][1];
            n=0;
        }

    }
    }

    cout << count;

    return 0;
}

//============================================================================
// Name        : ACM.cpp
// Author      : GhofranAbdelwahab
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>
using namespace std;

int main() {
    int  n ;
    int m ;
    cin>>n>>m;
    pair<int,int>A[m];
    for(int i = 0 ; i < m ; i ++)
        cin>>A[i].second>>A[i].first;
    sort(A,A+m);
    reverse(A,A+m);
    long long int count=0;
    int i = 0 ;
    while(n)
    {
        //cout<<A[i].second<<endl;
        if(A[i].second<n)
        {
            count+=A[i].first*A[i].second;
            n-=A[i].second;
            A[i].second=0;
        }
        else
        {
            count+=A[i].first*n;
                        n=0;
        }
        i++;
    }
    cout<<count ;
        return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<ctime>
#include<set>
typedef long long lld;
using namespace std;
#define debug(x) cout<<#x<<"="<<x<<endl;
#define here cout<<"_______________here "<<endl;
#define clr(NAME,VALUE) memset(NAME,VALUE,sizeof(NAME))
#define MAX 0x7f7f7f7f
#define N 25
struct node
{
	int a,b;
}q[N];
bool cmp(node a,node b)
{
	return a.b>b.b;
}
int n,m;
int sum;
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("a.txt","r",stdin);
	#endif
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=0;i<m;i++)
		scanf("%d%d",&q[i].a,&q[i].b);
		sort(q,q+m,cmp);
		int sum=0;
		int i=0;
		while(n>0)
		{
			if(n>q[i].a)
			{
				n-=q[i].a;
				sum+=q[i].a*q[i].b;
			}
			else
			{
				sum+=n*q[i].b;
				n=0;
			}
			i++;
		}
		printf("%d\n",sum);
	}
	#ifndef ONLINE_JUDGE
    while(1);
	#endif
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int m,a[25],b[25],box[25];
	long long n=0,boxes=0;
	scanf("%d%d",&n,&m);
	int remain=n,num=m-1;
	long long s=0;
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		boxes+=(long long)a[i];
		box[b[i]]=a[i];
	}
	if(boxes<=n)
	{
		for(int i=0;i<m;i++)
		{
			s+=(long long)a[i]*b[i];
			printf("%lld",s);
			return 0;
		}
	}
	sort(b,b+m);
	while(remain>0)
	{
		int w=min(remain,box[b[num]]);
		s+=(long long)w*b[num];
		remain-=w;
		num--;
	}
	printf("%lld",s);
	return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main()

{
    vector<int> v;
    long long n,m;
    cin>>n>>m;

    pair<int,int> array[m];
    for(int i=0;i<m;i++)
        cin>>array[i].first>>array[i].second;

        if(m==1)
        {
        if(array[0].first<=n)
        cout<<array[0].first*array[0].second;
        else cout<<array[0].second*(array[0].first-n);

         return 0;
        }

       for(int i=0;i<m;i++)
         v.push_back(array[i].second);

         stable_sort(v.begin(),v.begin()+v.size());

         long long sum=0;
         int i=0;
         int j=v.size()-1;

         while(n>=0 )
         {

          if(n-array[i].first>=0)
             {
             sum+=array[i].first*v[j];
             n=n-array[i].first;
             i++;
             j--;
             }
                else {
                      sum+=v[j]*n;
                      break;

         }
         }
         cout<<sum;
}

/************************************************************
*    In the name of God, the Merciful, the Compassionate   *                                                 *
************************************************************/
#include <bits/stdc++.h>
using namespace std;

template <typename T>
string to_str(T num)
{
    stringstream stream;
    stream << num;
    return stream.str();
}

int to_int(string str)
{
    int val;
    stringstream stream;
    stream << str;
    stream >> val;
    return val;
}
 void omar_medhat()
 {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
 }
 //printf("%.20f\n",mini);
// to reverse integer
int Reverse(int val)
{
int rev=0,rem;
while(val!=0)

{rem=val%10; rev=rev*10+rem; val/=10;}
    return rev;
}
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
bool prime (int x)
{
    int counter=0;
  for(int i=1;i<=x;i++)if(x%i==0)counter++;
  if(counter>2)return 0;
  else return 1;
}
//printf("%.10f\n",o);
#define ll long long
#define ull      unsigned long long
#define LB lower_bound
#define UB upper_bound
#define pll             pair<ll,ll>
#define pb push_back
#define mp make_pair
#define si2(x,y) scanf("%d%d",&x,&y)
#define sl2(x,y) scanf("%lld%lld",&x,&y)

#define si(a)    scanf("%d",&a)
#define sl(a)    scanf("%lld",&a)

#define pi(a)    printf("%d",a)
#define pl(a)    printf("%lld",a)

#define pi2(x,y)    printf("%d %d\n",x,y)
#define pl2(x,y)    printf("%lld %lld",x,y)

int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
char grid[101][101];
int seen[101][101];

/*bool valid (int i,int j)
{

    return i>=0&&j>=0&&i<n&&j<n;
}*/
  static ll S=0,I=0;
ll binary_to_decimal(ll x)
{
  ll h ,q;
  h=x%10;x=x/10;
  q=h*pow(2,I);
  S+=q;I++;
  if(x!=0) binary_to_decimal(x);
  return S;
}
#define frp(i,n,v) for(int i=n;i<v;i++)
#define frm(i,n,v) for(int i=n;i>=v;i--)
//double mcnt2=0,sum=0,d,mini=INT_MAX,maxi=0,miny,maxy,n,;
const double pi = 3.1415926535897;
int main()
{
    omar_medhat();
    int x,y,a1,a2,a3,a4,a5,a6,sum=0,f,sum2=0,j,cnt1=0,cnt2=0,cnt3=0,r,l,maxi=0;
    bool bo=1;
    deque<pair<int,int> > v,v1;
    cin>>x>>y;
    frp(i,0,y)
    {
        cin>>a1>>a2;
        f=a1;l=a2;
       v.push_back(make_pair(a1,a2));
        a6=i;
        if(y==1)break;
       if(i!=0)
       {
       frm(j,i-1,0)
       {
            if(v[i].second>v[j].second)
            {
                swap(v[i],v[j]);
                i=j;
            }
       }
       }
       i=a6;
    }
     while(x!=0)
    {
        int i=0;
         if(x>v[i].first)
         {
             sum+=v[i].first*v[i].second;
             x-=v[i].first;
              v.pop_front();
         }
         else if(x<=v[i].first)
         {
             sum+=x*v[i].second;
             x=0;
                v.pop_front();
         }
         if(y==1)break;
    }

     cout<<sum<<endl;

}

//#pragma comment (linker, "/STACK:0x800000")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
#define stream istringstream
#define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
#define repl(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define pb(x) push_back(x)
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define cclear(x,y) memset(x,y,sizeof(x));
#define eps 1e-9
#define pii pair<int,int>
#define pmp make_pair

#define SDi(x) scanf("%d",&x)
#define SDl(x) scanf("%lld",&x)
#define SDs(x) scanf("%s",x)

using namespace std;

template<class T> inline T gcd(T a,T b) {if(a<0)return
gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T lcm(T a,T b) {if(a<0)return
lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<class T> inline T sqr(T x){return x*x;}
template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1); }
template<class T> bool inside(T a,T b,T c){ return (b>=a && b<=c);}
typedef long long i64;
typedef unsigned long long ui64;

#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
const i64 INF64 = (i64)1E18;

double _dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));}
int distsq(int x1,int y1,int x2,int y2){return sqr(x1-x2)+sqr(y1-y2);}

int toInt(string s){int r=0;istringstream sin(s);sin>>r;return
r;}
i64 toInt64(string s){i64 r=0;istringstream sin(s);sin>>r;return
r;}
double toDouble(string s){double r=0;istringstream sin(s);sin>>r;return
r;}
double LOG(i64 N,i64 B){    return (log10l(N))/(log10l(B)); }

string itoa(long long a){if(a==0) return "0";string ret;for(long long i=a; i>0; i=i/10) ret.push_back((i%10)+48);reverse(ret.begin(),ret.end());return ret;}

vector< string > token( string a, string b )
{
    const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string >
    oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(),
    *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(),
    b.end(), *q ) ) q++;}return oot;
}
int isvowel(char s){s=tolower(s); if(s=='a' || s=='e' || s=='i' || s=='o' || s=='u')return 1; return 0;}
int isupper(char s) {if(s>='A' and s<='Z') return 1; return 0;}

//bool operator < ( const node& p ) const {      return dist > p.dist;   }

int SET(int N,int pos){ return N=N | (1<<pos);}
int RESET(int N,int pos){   return N= N & ~(1<<pos);}
int check(int N,int pos){   return (N & (1<<pos));}
int toggle(int N,int pos){if(check(N,pos))return N=RESET(N,pos);return N=SET(N,pos);}
void PRINTBIT(int N){   for(int i=8;i>=0;i--)   {bool x=check(N,i);cout<<x;}    printf("  %d",N);puts("");}

int main()
{

    //READ("in");
    int n,m;
    while(cin>>n>>m)
    {

        set<pii>ms;
          set<pii>::reverse_iterator it;

        rep(i,n)
        {
            int x,y;
            cin>>x>>y;
            ms.insert(pmp(y,x));

        }
        int taken=0;
        for(it=ms.rbegin();it!=ms.rend();it++)
        {
            int amount=it->second;
            while(n>0 and amount>0)
            {
                taken+=it->first;
                n--;
                amount--;

            }

        }
        cout<<taken<<endl;
    }

}

#include<bits/stdc++.h>
using namespace std;
int n, m;
int a[21], b[21];
int take(int p, int q) {
  return q > a[p] ? a[p] : q;
}
int maxmatch(int p, int q) {
  if(p == m + 1 || q <= 0) return 0;
  int t;
  t = take(p, q);
  int ans1, ans2;
  ans1 = maxmatch(p + 1, q);
  ans2 = b[p] * t + maxmatch(p + 1, q - t) ;
  return max(ans1, ans2);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; ++i) scanf("%d %d", &a[i], &b[i]);
  printf("%d", maxmatch(1, n));
  return 0;
}

#include <iostream>

using namespace std;

int main()
{
    long long x ,y;
    cin >> x >> y;
    long long a[y][2];
    long long m=0;
    int f[100009]={};
    int q[100009]={};
    for (int i=0;i<y;i++)
    {
        cin >> a[i][0]>>a[i][1];
        f[a[i][1]]=1;
        q[a[i][1]]+= a[i][0];

        m=max(m,a[i][1]);
    }
    long long s=0;
    for (int i=m;x!=0&&i>=0;i--)
    {
        if (f[i]==1)
        {
            if (q[i]<=x)
            {cout <<q[i]<<endl;
                s+=q[i]*i;
                x-=q[i];
            }
            else
            {
                s+=x*i;
                x=0;
            }
        }
    }

    cout << s << endl;

    return 0;
}

//#pragma comment (linker, "/STACK:0x800000")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
#define stream istringstream
#define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
#define repl(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define pb(x) push_back(x)
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define cclear(x,y) memset(x,y,sizeof(x));
#define eps 1e-9
#define pii pair<int,int>
#define pmp make_pair

#define SDi(x) scanf("%d",&x)
#define SDl(x) scanf("%lld",&x)
#define SDs(x) scanf("%s",x)

using namespace std;

template<class T> inline T gcd(T a,T b) {if(a<0)return
gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T lcm(T a,T b) {if(a<0)return
lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<class T> inline T sqr(T x){return x*x;}
template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1); }
template<class T> bool inside(T a,T b,T c){ return (b>=a && b<=c);}
typedef long long i64;
typedef unsigned long long ui64;

#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
const i64 INF64 = (i64)1E18;

double _dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));}
int distsq(int x1,int y1,int x2,int y2){return sqr(x1-x2)+sqr(y1-y2);}

int toInt(string s){int r=0;istringstream sin(s);sin>>r;return
r;}
i64 toInt64(string s){i64 r=0;istringstream sin(s);sin>>r;return
r;}
double toDouble(string s){double r=0;istringstream sin(s);sin>>r;return
r;}
double LOG(i64 N,i64 B){    return (log10l(N))/(log10l(B)); }

string itoa(long long a){if(a==0) return "0";string ret;for(long long i=a; i>0; i=i/10) ret.push_back((i%10)+48);reverse(ret.begin(),ret.end());return ret;}

vector< string > token( string a, string b )
{
    const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string >
    oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(),
    *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(),
    b.end(), *q ) ) q++;}return oot;
}
int isvowel(char s){s=tolower(s); if(s=='a' || s=='e' || s=='i' || s=='o' || s=='u')return 1; return 0;}
int isupper(char s) {if(s>='A' and s<='Z') return 1; return 0;}

//bool operator < ( const node& p ) const {      return dist > p.dist;   }

int SET(int N,int pos){ return N=N | (1<<pos);}
int RESET(int N,int pos){   return N= N & ~(1<<pos);}
int check(int N,int pos){   return (N & (1<<pos));}
int toggle(int N,int pos){if(check(N,pos))return N=RESET(N,pos);return N=SET(N,pos);}
void PRINTBIT(int N){   for(int i=8;i>=0;i--)   {bool x=check(N,i);cout<<x;}    printf("  %d",N);puts("");}

int main()
{

    //READ("in");
    i64 n,m;
    while(cin>>n>>m)
    {

        set<pii>ms;
          set<pii>::reverse_iterator it;

        rep(i,m)
        {
            i64 x,y;
            cin>>x>>y;
            if(ms.find(pmp(y,x))!=ms.end())
            {
                ms.erase(pmp(y,x));
                x++;
            }
            ms.insert(pmp(y,x));

        }
        i64 taken=0;
        for(it=ms.rbegin();it!=ms.rend();it++)
        {
        //  cout<<it->first<<endl;
            i64 amount=it->second;
            if(amount>=n)
            {
                taken+=(it->first)*n;
                n=0;
                break;
            }
            else
            {
                taken+=(it->first)*amount;
                n-=amount;

            }
        //  cout<<">>"<<taken<<endl;

        }
        cout<<taken<<endl;

        //puts("SSS");
    }

}

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using ll = long long int;
typedef vector<ll> vi;
ll n,m,a,b,tn=0,sm=0,d=0;
bool stop = 0;
ll fig(ll k)
{
	stop = 1;
	return k;
}
void brg_lr()
{
	cin>>n>>m;
	pair<ll,ll>po[m];
	for(ll i = 0;i<m;i++){
		cin>>a>>b;
		po[i]=mp(b,a);
	}
	sort(po,po+m);
	reverse(po,po+m);
	while(tn<n){
		sm+=((po[d].se--)>0?po[d].fi:(d<m-1?po[++d].fi:fig(0)));
		if(stop){
			break;
		}
		tn++;
	}
	cout<<sm<<"\n";
}
int main()
{
	brg_lr();
	return 0;
}

#include <cstdio>
int main()
{
    printf("62");
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

bool mySort( const pair <int, int>  &a,const pair <int, int>  &b)
{
    if(a.second != b.second){
        return(a.second > b.second);
    }
    return a.first > b.first;
}

int main()
{
    long long sum=0;
    int a, b ;
    vector< pair <int, int>  > vect;

    int n,m;
    cin>>n>>m;

    // Entering values in vector of pairs
    for (int i=0; i<m; i++) {
        cin>>a>>b;
        vect.push_back( make_pair( a , b )  );
    }
    sort(vect.begin(), vect.end(), mySort );

    for (int i=0; i<m; i++) {
        if(n == vect[i].first){
            sum += (vect[i].first * vect[i].second) ;
            cout <<sum;
            break;
        }
        else if(n < vect[i].first){
            sum += (n * vect[i].second) ;
            cout <<sum;
            break;
        }
        else if(n > vect[i].first){
            sum += (vect[i].first * vect[i].second) ;
            n -= vect[i].first;
        }
    }

    return 0;
}

#include <iostream>

using namespace std;

int main()
{
    int bagcapacity,m,cntbox=0,cntm=0,cnt=0;
    cin>>bagcapacity>>m;
    int arr[m][2];
    for(int i=0;i<m;i++){
        cin>>arr[i][0]>>arr[i][1];
        cntbox+=arr[i][0];
        cntm+=arr[i][1];
    }
    if(cntbox<=bagcapacity){
        cout<<cntm;
        return 0;
    }
    while(bagcapacity){
        int x=0;
        for(int i=0;i<m;i++)
            if(arr[x][1]<arr[i][1])x=i;

            if(bagcapacity<=arr[x][0]){
                cnt+=bagcapacity*arr[x][1];
                cout<<cnt;
                return 0;
            }
            else {
                cnt+=arr[x][1]*arr[x][0];
                bagcapacity-=arr[x][0];
                arr[x][1]=0;
            }
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>

using namespace std;

struct sa
{
       int j;
       int f;
}data[55];
int cmp( const sa &a,const sa &b)
{
 return (a.f) > (b.f);
}

int main()
{
    int n,m;
    cin>> n >> m;
    int i;
    for(i=0;i<m;i++)
    {
                    cin>> data[i].j >>data[i].f;
    }
    sort(data,data+n,cmp);
    int sum=0;
    for(i=0;i<m;i++)
    {
                    if( n<=data[i].j )
                    {
                        sum+=data[i].f*n;
                       break;
                    }
                    else
                    {
                        n=n-data[i].j;
                        sum+=data[i].j*data[i].f;
                    }
    }
    cout<<sum<<endl;
//    getchar();
//    getchar();
    return 0;
}

/* IN The Name Of The GOD */
/* There Is Nothing Is Impossible The Word Impossible Itself Say I'am Possible */
/*
                                               8888888888
                                             88          88
                                            88            88
                                           88              88
                                           88 ----+  +----  88
                                          888  +-+ || +-+   888
                                          888 + O |||| O  + 888
                                           88 +   +||+    + 88
                                           88     (..)      88
                                           88      ||       88
                                            88   (----)    88
                                             88    --     88
                                              88         88
                                                888888888
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double D;
typedef float F;
typedef string S;
typedef int I;
typedef bool B;
int main()
{
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    //freopen("input.in.txt", "r",stdin);
    //freopen("output.out", "w", stdout);
    int n,m,a,b,maxi=-99999,sum=0;
    vector<int>vec1,vec2;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>a>>b;
        vec1.push_back(a);
        vec2.push_back(b);
    }
    sort(vec2.rbegin(),vec2.rend());
    if(m==1)
    {
        sum=vec1[0]*vec2[0];
        cout<<sum<<endl;
        return 0;
    }
    if(n>1)
    {
    for(int i=0;i<m;i++)
    {
        for(int j=i;j<m;j++)
        {
            if(vec1[i]+vec1[j]==n)
            {
                sum=(vec1[i]*vec2[i])+(vec1[j]*vec2[j]);
            }
            if(sum>maxi)
                maxi=sum;
        }
    }
    }
    else
    {
        for(int i=0;i<m;i++)
        {
            if(n==1)
            {
                sum=vec1[i]*vec2[i];
            }
            if(sum>maxi)
                maxi=sum;
        }
    }
    cout<<maxi<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef pair<int , int> ii ;

bool cmp (const ii& lhs , const ii& rhs)
{
    return lhs.second > rhs.second;
}
int main()
{
    int n , k ;
    cin >> n >> k;
    ii a[21];
    for (int i = 0 ; i < n ; i++)
    {
        cin >> a[i].first >> a[i].second ;

    }

    sort(a, a+k , cmp);
    int ans = 0;
    for (int i = 0; i <k ; i++)
    {
        if(n>=a[i].first)
        {
            ans+=a[i].first*a[i].second;
            n-=a[i].first;
        }
        else
        {
            ans+=n*a[i].second;
            n=0;
        }
    }
    cout << ans;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <iterator>
using namespace std;
int main()
{
    int containerNumber;
    long long backpack, matchboxes, matches,i=0,j,answ=0;
    int matchesVector[21];
    map<long long, long long>bestHeist;
    cin >> backpack >> containerNumber;
    while (containerNumber > 0) //Se introduc datele in MAP #chibrituri==cheia
    {
        cin >> matchboxes >> matches;
        bestHeist.insert(pair<long long, long long>(matches, matchboxes));
        matchesVector[i] = matches;
        i++;
        containerNumber--;
    }
    sort(matchesVector, matchesVector+i); //sortam vectorul de #chibrituri
    /*for (j = i-1; j >=0; j--)
        cout << matchesVector[j]<<"  ";
    cout << endl;
    for (map<long long, long long>::iterator it = bestHeist.begin();
        it != bestHeist.end(); ++it)
    {
        cout << it->first << " " << it->second << endl;
    }
    cout<<endl<<bestHeist[matchesVector[i-1]];*/
    while(backpack>=0)
    {
       for(j=i-1;j>=0;j--)
       {
           matchboxes=bestHeist[matchesVector[j]];
           //cout<<"box="<<matchboxes<<endl;
           if(backpack>=matchboxes)
           {
               //cout<<"matches:"<<matchesVector[j]<<"  ";
               answ=answ+(matchesVector[j]*matchboxes);
               backpack=backpack-matchboxes;
           }
           else
           {
               while(backpack!=0&&matchboxes!=0)
               {
                   answ=answ+matchesVector[j];
                   matchboxes--;
                   backpack--;
               }
           }
           //cout<<endl<<"Backpack="<<backpack;
       }
       //cout<<j;
       if(backpack==0||j==-1)
        break;
    }
    cout<<answ;

    return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main() {
	int maxBoxes, containers;
	int boxes, matches;

	cin >> maxBoxes >> containers;

	vector<int> a;
	vector<int> b;
	long long int size = 0;
	for (int i = 0; i < containers; i++) {
		cin >> boxes >> matches;
		a.push_back(matches);
		b.push_back(boxes);
		size += boxes;
	}

	int key, j;
	for (int i = 1; i < a.size(); i++) {
		key = a[i];
		j = i-1;
		while (j >= 0 && a[j] < key) {
			a[j + 1] = a[j];
			j--;
		}
		a[j+1] = key;
	}

	long long int ans = 0;
	int current = 0;
	for (int i = 0; i < maxBoxes && i < size; i++) {
		ans += a[current];
		b[current]--;
		if (b[current] == 0) current++;
	}

	cout << ans;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>arr;
    int a,b;
    for(int i=0;i<m;i++){
        cin>>a>>b;
        for(int j=0;j<a;j++){
            arr.push_back(b);
        }
    }
    int sum=0;
    sort(arr.begin(), arr.end(), greater<int>());
    for(int i=0;i<n;i++){
        int y=arr[i];
        sum+=y;
    }
    cout<<sum;
}

//KUNAL FARMAH
//@kunalfarmh98@gmail.com

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
	bool comp(pair<int,int> a, pair<int,int> b){
		ll ar = a.second/a.first;
		ll br = b.second/b.first;

		if(ar==br){
			return(a.second>b.second);
		}
		else{
			return ar>br;
		}
	}

int main(){
	int n,m;
	cin>>n>>m;
	pair<int,int> box[m];
	for(int i=0; i<m; i++){
		cin>>box[i].first>>box[i].second;
	}
	sort(box,box+m,comp);

	for(int i=0 ; i<m; i++){
		cout<<box[i].first<<"->"<<box[i].second<<endl;
	}

	cout<<endl;
	ll ans=0;
	int i=0;
	for(; box[i].first<=n; i++){
		ans+=(box[i].first*box[i].second);
		n-=box[i].first;
	}

	ans+=n*box[i].second;

	cout<<ans<<endl;

	return 0;
}

#include <algorithm>
#include <iostream>
#include <cstdio>

#define s second
#define f first

typedef long long ll;

using namespace std;

int n, m;
ll res;
pair <ll, ll> a[40];

int main ()
{
	cin >> n >> m;

	for ( int i = 0; i < m; i++ )
		cin >> a[i].s >> a[i].f;

	sort (a, a + m);

	for ( int i = n - 1; i && n; i++ )
		if (n > a[i].s)
			res += a[i].f*a[i].s,
			n -= a[i].s;
		else
			res += a[i].f*n,
			n = 0;

	cout << res;

	return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define int long long
const int maxm = 23;
struct X{
	int a,b;
	bool operator<(const X &x)const{
		return b>x.b;
	}
}A[maxm];
int n,m;
signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=m;++i){
		scanf("%lld%lld",&A[i].a,&A[i].b);
	}
	sort(A+1,A+1+n);
	int ans=0;
	for(int i=1;i<=m;++i){
		if(n>=A[i].a)n-=A[i].a,ans+=A[i].b*A[i].a;
		else{
			ans+=n*A[i].b;
			n=0;
		}
	}
	printf("%lld\n",ans);
}

#include<bits/stdc++.h>
using namespace std;
long long a[10005] , b[10005] ;
int main(){
	long long n , m ;
	long long sum = 0 ;
	long long flag = 0 ;

	cin >> n >> m ;

	for( long long i = 1 ; i <= m ; i++ )
		cin >> a[i] >> b[i] ;

	for( long long i = 1 ; i <= m - 1 ; i++ ) {
		for( long long j = 1 ; j <= m - i ; j++ ) {
			if( b[j] < b[j + 1] )
				swap( b[j] , b[j + 1] );
		}
	}

	for( long long i = 1 ; i <= n ; ) {
		sum += b[i] ;
		a[i]-- ;
		flag++ ;
		if( a[i] == 0 )
			i++;
		if( flag == n )
			break;
	}

	cout << sum;

	return 0;
}