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.

"""
  ____          _      _____
 / ___|___   __| | ___|  ___|__  _ __ ___ ___  ___
| |   / _ \ / _` |/ _ \ |_ / _ \| '__/ __/ _ \/ __|
| |__| (_) | (_| |  __/  _| (_) | | | (_|  __/\__ \
 \____\___/ \__,_|\___|_|  \___/|_|  \___\___||___/

"""
"""
░░██▄░░░░░░░░░░░▄██
░▄▀░█▄░░░░░░░░▄█░░█░
░█░▄░█▄░░░░░░▄█░▄░█░
░█░██████████████▄█░
░█████▀▀████▀▀█████░
▄█▀█▀░░░████░░░▀▀███
██░░▀████▀▀████▀░░██
██░░░░█▀░░░░▀█░░░░██
███▄░░░░░░░░░░░░▄███
░▀███▄░░████░░▄███▀░
░░░▀██▄░▀██▀░▄██▀░░░
░░░░░░▀██████▀░░░░░░
░░░░░░░░░░░░░░░░░░░░
"""

import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt
from bisect import bisect_right, bisect_left

#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')

from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2**20)

def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)

def ncr(n, r):
    r = min(r, n - r)
    numer = reduce(op.mul, range(n, n - r, -1), 1)
    denom = reduce(op.mul, range(1, r + 1), 1)
    return numer // denom  # or / in Python 2

def prime_factors(n):
    i = 2
    factors = []
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
    if n > 1:
        factors.append(n)
    return len(set(factors))

def isPowerOfTwo(x):
    return (x and (not(x & (x - 1))))

def factors(n):
    return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))

MOD = 10**9 + 7
T = 1
# T = int(stdin.readline())
for _ in range(T):
    # a, b = list(map(int, stdin.readline().split()))
    # s1 = list(stdin.readline().strip('\n'))
    # s2 = list(stdin.readline().strip('\n'))
    # n = int(stdin.readline())
    # a, b, n = list(map(int, stdin.readline().split()))
    # n, k = list(map(int, stdin.readline().split()))
    # s = str(stdin.readline().strip('\n'))
    # s2 = str(stdin.readline().strip('\n'))
    n, m = list(map(int, stdin.readline().split()))
    s = []
    for i in range(m):
        a, b = list(map(int, stdin.readline().split()))
        s.append([b, a])
    s.sort(reverse=True)
    ans = 0
    i = 0
    while n > 0 and i < m:
        if s[i][1] >= n:
            ans += n * s[i][0]
            n = 0
        else:
            n -= s[i][1]
            ans += s[i][1] * s[i][0]
        i += 1
    print(ans)

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys, io
import re, math
start = time.clock()
l=[]
chk,ans=0,0
(n,m)=map(int, raw_input().split())
for _ in range(m):
    i=map(int, raw_input().split())
    l.append(i)
#print l
l1=sorted(l, key=lambda l:l[1])
# print l1
l1.reverse()
for i in l1:
    if n==chk: break
    elif n>=chk+i[0]:
        ans+=i[0]*i[1]
        chk+=i[0]
    else:
        ans+=(n-chk)*i[1]
        chk+=(n-chk)
print ans

a, b = map(int,input().split())
mapped =[]

for i in range(b):
   mapped.append(list(map(int,input().split(" "))))
mapped.sort(key = lambda x:x[1] , reverse =True)
result =0
for i in mapped:
    if a>i[0]:
        result+=i[0]*i[1]
        a-=i[0]
    else:
        result+=i[1]*a
        break

print(result)

# your code goes here
from sys import stdin, stdout

n, m = map(int, stdin.readline().split())
ar = []
for i in range(m):
	a, b = map(int, stdin.readline().split())

	ar.append((a, b))

ar.sort(key=lambda x:x[1], reverse=True)

matches = 0

for box in ar:
	if n >= box[0]:
		matches += box[0]*box[1]
		n -= box[0]
	else:
		matches += n*box[1]
		break

stdout.write(str(matches))

import operator

total_size, containers = map(int, raw_input().split())
matchboxes = sorted([map(int, raw_input().split()) for _ in range(containers)], key=operator.itemgetter(1))[::-1]

volume = 0
size = 0
for s, v in matchboxes:
    delta = min(total_size-size, s)
    volume += v*delta
    size += delta

    if size == total_size:
        break

print volume

[n,m] = [int(x) for x in input().split()]
a=[]

for i in range (m) :
	a.append([int(x) for x in input().split()])

a = sorted(a , key = lambda x : x[1] ,reverse = True)
k = 0

while n > 0 and len(a) > 0:
	if n  >=  a[0][0] :
		k += a[0][0]*a[0][1]
		n -= a[0][0]
		a.pop(0)
	else :
		k += n *a[0][1]
		n = 0

print(k)

# ip = open("testdata.txt", "r")

# def input():
# 	return ip.readline().strip()

n, m = map(int, input().split())
cont = [0]*m

for i in range(m):
	boxes, matches = map(int, input().split())
	cont[i] = [matches, boxes]

cont.sort(reverse=True)
count = 0

for match, box in cont:
	x = min(n, box)
	count += match*x
	n -= x
	if n==0:
		break

print(count)

import sys, math
input = sys.stdin.readline

n, m = map(int, input().split())
matches = sorted([map(int, input().split()) for _ in range(m)], key=lambda x:x[1])[::-1]
ans = 0
for match in matches:
  if not n: break
  ans += min(n, match[0]) * match[1]
  n -= min(n, match[0])

print ans

n,m=map(int,input().split())
cont=[]
match=[]

for _ in range(m):
    a,b=map(int,input().split())
    cont.append(a)
    match.append(b)
count=0
if len(cont)>1:
    while n>0:
        if len(cont)>0:
            ma=max(match)
            ind=match.index(ma)
            if n>=cont[ind]:
                n=n-cont[ind]
                count=count+cont[ind]*match[ind]
            else:
                count=count+n*match[ind]
                n=0
            match.pop(ind)
            cont.pop(ind)
        else:
            break
else:
    if n>cont[0]:
        count=cont[0]*match[0]
    else:
        count=n*match[0]
print(count)

# key not available ""
n, m = [int(i) for i in input().split()]
arr = []
for _ in range(m):
    a, b = [int(i) for i in input().split()]
    arr.append([b,a])
arr.sort(reverse=True)
ans = 0
for i in range(m):
    temp = min(n, arr[i][1])
    ans += (temp*arr[i][0])
    n -= temp
    if n == 0:
        break
print(ans)

from math import *
from Queue import *

s = raw_input()
l = s.split(' ')
n = int(l[0])
m = int(l[1])
total = 0
dic = []
for i in range(m):
    s = raw_input()
    l = s.split(' ')
    dic.append([int(l[1]), int(l[0])])
for b in range(10, -1, -1):
    if n <= 0:
        break
    for i in range(m):
        if dic[i][0] == b:
            total += min(n, dic[i][1]) * b
            n -= dic[i][1]
            if n <= 0:
                break
print(total)

"""class segment(object):
    def __init__(self, x1, y1, x2, y2):
        self.ver, self.hor, self.point = False, False, False
        if x1 == x2:
            self.ver = True
        if y1 == y2:
            self.hor = True
        if (x1 == x2) and (y1 == y2):
            self.point = True
        minx = min(x1,x2)
        maxx = max(x1,x2)
        miny = min(y1,y2)
        maxy = max(y1,y2)
        self.x1 = minx
        self.x2 = maxx
        self.y1 = miny
        self.y2 = maxy

def rectangle(horseg, verseg):
    if (horseg[0].x1 != horseg[1].x1) or (horseg[0].x2 != horseg[1].x2):
        return False
    if (verseg[0].y1 != verseg[1].y1) or (verseg[0].y2 != verseg[1].y2):
        return False
    miny = min(horseg[0].y1, horseg[1].y1)
    maxy = max(horseg[0].y1, horseg[1].y1)
    minx = min(verseg[0].x1, verseg[1].x1)
    maxx = max(verseg[0].x1, verseg[1].x1)
    if (minx != horseg[0].x1) or (maxx != horseg[0].x2):
        return False
    if (miny != verseg[0].y1) or (maxy != verseg[0].y2):
        return False
    return True

horseg = []
verseg = []
for i in xrange(4):
    s = raw_input()
    l = s.split(' ')
    new_seg = segment(int(l[0]), int(l[1]), int(l[2]), int(l[3]))
    if (new_seg.hor == False) and (new_seg.ver == False):
        print('NO')
        break
    if new_seg.point:
        print('NO')
        break
    if new_seg.hor:
        horseg.append(new_seg)
    else:
        verseg.append(new_seg)
if (len(horseg) != 2):
    print('NO')
else:
    if rectangle(horseg, verseg):
        print('YES')
    else:
        print('NO')"""

"""from math import *
from Queue import *

def bfs(G, start):
    visited = set([start])
    Q = Queue()
    Ret = [start]
    Q.put(start)
    while not Q.empty():
        vertex = Q.get()
        for v in G[1][vertex]:
            if v not in visited:
                Q.put(v)
                visited.add(v)
                Ret.append(v)
    return Ret

def longest_path(G):
    l = bfs(G,G[0][0])
    marked = set()
    result = dict()
    for i in l:
        result[i] = [0,i,0,i]
    for i in range(len(l)-1, -1, -1):
        for j in G[1][l[i]]:
            if j in marked:
                if (result[j][2] > result[l[i]][2]) or ((result[j][2] == result[l[i]][2]) and (result[j][3] < result[l[i]][3])):
                    result[l[i]][2] = result[j][2]
                    result[l[i]][3] = result[j][3]
                if (result[l[i]][0] + result[j][0] + 1 > result[l[i]][2]):
                    result[l[i]][2] = result[l[i]][0] + result[j][0] + 1
                    result[l[i]][3] = min(result[l[i]][1], result[j][1])
                if ((result[l[i]][0] + result[j][0] + 1 == result[l[i]][2]) and (min(result[l[i]][1],result[j][1]) < result[l[i]][3])):
                    result[l[i]][3] = min(result[l[i]][1],result[j][1])
                if (result[j][0] + 1 > result[l[i]][0]) or ((result[j][0] + 1 == result[l[i]][0]) and (result[j][1] < result[l[i]][1])):
                    result[l[i]][0] = result[j][0] + 1
                    result[l[i]][1] = result[j][1]
        marked.add(l[i])
    return (result[l[0]][2], result[l[0]][3])

def remove(G,v):
    ver = G[0]
    ver.remove(v)
    edg = dict()
    for i in ver:
        nb = []
        for j in G[1][i]:
            if j != v:
                nb.append(j)
        edg[i] = nb
    return (ver, edg)

def harvest(G,M):
    l = bfs(G,M[0])
    seen = set()
    delete = []
    for i in range(len(l)-1, -1, -1):
        seen.add(l[i])
        if l[i] in M:
            for j in G[1][l[i]]:
                if j not in seen:
                    M.append(j)
        if l[i] not in M:
            delete.append(l[i])
    for i in delete:
        G = remove(G,i)
    return G

s = raw_input()
l = s.split(' ')
n = int(l[0])
m = int(l[1])
V = []
Adj = [[] for i in range(n+1)]
for i in range(1,n+1):
    V.append(i)
E = dict()
for i in range(n-1):
    s = raw_input()
    l = s.split(' ')
    h = int(l[0])
    t = int(l[1])
    Adj[h].append(t)
    Adj[t].append(h)
E = dict()
for i in range(1, n+1):
    E[i] = Adj[i]
G = [V,E]
s = raw_input()
l = s.split(' ')
M = []
for i in range(m):
    M.append(int(l[i]))
G = harvest(G,M)
sol = longest_path(G)
print(sol[1])
print(2*len(G[0]) - 2 - sol[0])"""

n_m = [int(x) for x in input().split(' ')]
n = n_m[0]
m = n_m[1]
l1 = []
l2 = []
l3 = []
l4 = []
for i in range(m):
    a_b = [int(y) for y in input().split(' ')]
    l1.append(a_b[0])
    l2.append(a_b[1])
    l3.append(a_b[1])
l3.sort(reverse=True)
for i in l3:
    for j in range(len(l3)):
        if i == l2[j]:
            l4.append(l1[j])
            l2[j] = -1
matches = 0
for i in range(len(l2)):
    if n > l4[i]:
        matches += l3[i] * l4[i]
        n -= l4[i]
    else:
        matches += n * l3[i]
        n = 0
        break
print(matches)

from sys import stdin, stdout # only need for big input

def read_int_from_line():
    return list(map(int, input().split()))

def solve():
    n, m = read_int_from_line()
    matches = []
    for _ in range(m):
        a, b = read_int_from_line()
        matches.append((a,b))
    matches.sort(key= lambda x : x[1], reverse=True)
    # print(matches)
    cur_holding = 0
    ans = 0
    for m in matches:
        pick = min(m[0], n - cur_holding)
        ans += m[1] * pick
        cur_holding += pick
    print(ans)

def main():
    solve()

if __name__ == "__main__":
    main()

R=lambda:map(int,raw_input().split())
n,m=R()
print reduce(lambda x,y:x[0]and(x[0]-min(x[0],y[0]),x[1]+min(x[0],y[0])*y[1])or x,sorted([R()for i in range(m)],key=lambda x:-x[1]),(n,0))[1]

n, m = [int(x) for x in input().split(' ')]
mbs = {i: 0 for i in range(1, 11)}
for i in range(m):
    a, b = [int(x) for x in input().split(' ')]
    mbs[b] += a
    matches = 0
    matchboxes = 0
    i = 10
    while matchboxes < n and i > 0:
        x = min(n - matchboxes, mbs[i])
        matches += x * i
        matchboxes += x
        i -= 1
print(matches)

'''
INPUT SHORTCUTS
N, K = map(int,input().split())
N ,A,B = map(int,input().split())
string = str(input())
arr = list(map(int,input().split()))
N = int(input())
'''

N , M = map(int,input().split())
warehouse = []
for _ in range(M):
	boxes , matches = map(int,input().split())
	warehouse.append([matches,boxes])
warehouse.sort()
# print(warehouse)
ans = 0
for i in range(M-1,-1,-1):
	if(N==0):
		break
	if(N-warehouse[i][1]>0):
		ans += warehouse[i][1]*warehouse[i][0]
		N -= warehouse[i][1]
	else:
		ans+= N*(warehouse[i][0])
		N = 0
	# print(ans)
print(ans)

n,m=map(int,input().split())
l=[]
for i in range(m):
	l1=list(map(int,input().split()))
	l.append(l1)
l.sort(reverse=True,key=lambda x:x[1])
s=0
for i in range(m):
	if l[i][0]<=n:
		s+=l[i][0]*l[i][1]
		n-=l[i][0]
	else:
		s+=n*l[i][1]
		break
print(s)

n, m = map(int, input().split())
v = 0
for b in sorted((list(map(int, input().split()))[::-1] for i in range(m)), reverse=True):
    c = min(b[1], n)
    n -= c
    v += c * b[0]
print(v)

l1 = [int(x) for x in input().split()]
n,m = l1[0],l1[1]
l2=[]
for x in range(m):
    l2.append([int(x) for x in input().split()])
def myfunc(l1):
    return l1[1]
l2.sort(key=myfunc,reverse=True)
i=0
ans=0
#print(l2)
while i<len(l2):
    if l2[i][0]>=n:
        ans+=n*l2[i][1]
        break
    else:
        n-=l2[i][0]
        ans+=l2[i][0]*l2[i][1]
    i+=1
print(ans)

from sys import stdin,stdout

def main():
	L=[]
	n,m=map(int,stdin.readline().split())

	for i in range(m):
		L.append(stdin.readline().split())

	A=(sorted(L, key=lambda L: int(L[1]),reverse=True))

	matches=0

	for i in range(m):
		temp=min(n,int(A[i][0]))
		n-=temp
		matches+=temp*int(A[i][1])

	print(matches)

if __name__ == '__main__':
	main()

n,m=map(int,input().split())
l=[]
for i in range(m):
    a,b=map(int,input().split())
    l.append((a,b))
l.sort(key=lambda x: x[1],reverse=True)
ans=0
for i in range(m):
    if n==0:
        break
    x=min(l[i][0],n)
    ans+=x*l[i][1]
    n-=x
print(ans)

n, m = map(int, input().split())
A = []
for _ in range(m):
    A.append(list(map(int, input().split())))

A.sort(key = lambda el: el[1], reverse = True)
i = ans = 0
while n and i < len(A):
    cnt = min(n, A[i][0])
    ans += cnt*A[i][1]
    n -= cnt
    i += 1
print(ans)

R = lambda:map(int,input().split())

n, m  = R()
arr = sorted([[*R()] for i in range(m)], key=lambda x: x[1], reverse=True)
ans = 0
for i in arr:
    v = min(n, i[0])
    ans += v*i[1]
    n -= v
    if n < 1:
        break
print(ans)

n, m = map(int, raw_input().split())
c = []

for i in range(m):
    c_temp = map(int, raw_input().split())
    c.append(c_temp)

sorted_boxes = sorted(c, key=lambda x:x[1], reverse=True)

m_sum = 0
n_sum = 0
ii = 0
while n_sum < n and ii < m:
    boxes_taken = min(sorted_boxes[ii][0], n-n_sum)
    n_sum += boxes_taken
    m_sum += boxes_taken * sorted_boxes[ii][1]
    ii += 1

print m_sum

R=lambda:map(int,raw_input().split())
n,m=R()
print reduce(lambda x,y:(x[0]-min(x[0],y[0]),x[1]+min(x[0],y[0])*y[1]),sorted([R()for i in range(m)],key=lambda x:-x[1]),(n,0))[1]

n, m = map(int, input().split())
info = []
for _ in range(m):
    info.append(list(map(int, input().split())))

info_sorted = info.copy()
for i in range(m-1):
    for j in range(m)[i+1:]:
        if info_sorted[i][1]<info_sorted[j][1]:
            info_sorted[j],info_sorted[i] = info_sorted[i],info_sorted[j]

sum = 0
cnt = 0
for a,b in info_sorted:
    sum=sum+a
    if sum<=n:
        cnt = cnt + a*b
    else:
        cnt = cnt + (n-(sum-a))*b
        break

print(cnt)

import sys

a,b = map(int, raw_input().split())

l = []
for i in range(b):
    l.append( tuple( map(int, raw_input().split() )) )

l = sorted(l, key=lambda (x,y):y, reverse=True )

res = 0
for u,b in l:
    if a==0: break
    v = min(u, a)
    a -= v
    res += v*b

print res

def solve():
    n, m = map(int, input().split())
    ab = []
    for _ in range(m): ab.append((tuple(map(int, input().split()))))
    put = 0
    for a, b in sorted(ab, key=lambda x: x[1], reverse=True):
        n -= a
        if n < 0:
            put += (a+n) * b
            return put
        put += a * b
    return put
print(solve())

import sys
a,b=map(int,input().split())
c=[]
k=0
for i in range(b):
    e,f=map(int,input().split())
    x=[f,e]
    c.append(x)
c=sorted(c,reverse=True)
for i in c :
    d=a-i[1]
    if a>=0:
        if d>=0:
            z = i[0] * i[1]
            k = k + z
            a = d
        if d<0:
            z = i[0] * a
            k = k + z
            a = d
    else:
        print(k)
        sys.exit()
print(k)

#!/usr/bin/env python

from sys import stdin

N, M = map(int, stdin.readline().split())

v = []
for i in xrange(M):
   a, b = map(int, stdin.readline().split())
   v.append((b, a))
v.sort()
v.reverse()
res = 0
for t in v:
#  print t[0], t[1], res
   if t[1] >= N:
      res += t[0] * N
      break
   N -= t[1]
   res += t[0] * t[1]

print res

temp = input().split(' ')
n = int(temp[0])
m = int(temp[1])
l = []
maxtemp = 0
maxind = 0
s = 0
for i in range(m):
    temp = input().split(' ')
    l.append([int(temp[0]), int(temp[1])])
while(n > 0):
    maxtemp = 0
    maxind = 0
    for i in range(len(l)):
        if(maxtemp < l[i][1]):
            maxtemp = l[i][1]
            maxind = i
    if maxind >= len(l):
        print(s)
        exit(0)
    if l[maxind][0] < n :
        n -= l[maxind][0]
        s += l[maxind][0]*l[maxind][1]
        del l[maxind]
    else:
        s += l[maxind][1]*n
        l[maxind][0] -= n
        n = 0
        if l[maxind][0] == 0:
            del l[maxind]
print(s)

n,m=map(int,input().split())
l=[]
for i in range(m):
	y=[]
	a,b=map(int,input().split())
	y=[b,a]
	l.append(y)
l.sort(reverse=True)
# print(l)
s=0
c=0
for j in range(m):
	if((n-(s+l[j][1]))>0):
		c=c+l[j][0]*l[j][1]
		s=s+l[j][1]
		# print(s,c)
	else:
		r=n-s
		# print(r)
		c=c+r*l[j][0]
		break
print(c)

total_box, m = map(int, raw_input().split())
container = [0] * 11
for _ in range(m):
    box, matches = map(int, raw_input().split())
    container[matches] += box
r = 0
for i, box in enumerate(container[ : : -1]):
    r += (10 - i) * min(box, total_box)
    total_box -= min(box, total_box)
    if total_box == 0: break

#print total_box, m
#print container
print r

z=list(map(int,input().split()))

di=[]
for i in range(z[1]):
    x=list(map(int,input().split()))
    x.reverse()
    di.append(x)
di.sort(reverse=True)
c=0

for i in di:

    if z[0]<=0:
            break
    if i[1]>=z[0]:
        c+=z[0]*i[0]
        break
    else:
        c+=i[1]*i[0]
        z[0]-=i[1]

print(c)

__author__ = 'kovshi'

m, n = tuple([int(x) for x in raw_input().split()])
boxes = []
for i in xrange(n):
    boxes.append([int(x) for x in raw_input().split()])
    boxes[i].reverse()
boxes.sort()
boxes.reverse()
sum = 0
i = 0
while m > 0 and i < len(boxes):
    sum += min(boxes[i][1], m) * boxes[i][0]
    m -= boxes[i][1]
    i += 1
print sum

I = lambda: map(int, input().split())

n, m = I()
C = sorted((tuple(I()) for _ in range(m)), key=lambda x: x[1])
s = 0

while n and C:
    a, b = C.pop()
    x = min(a,n)
    s += b*x
    n -= x

print(s)

n, m = map(int, raw_input().split())
f = []
for i in range(m):
	a, b = map(int, raw_input().split())
	f.append((b,a))
f.sort()
ans = 0
for i in range(len(f)-1,-1,-1):
	t = min(f[i][1],n)
	n -= t;
	ans += t * f[i][0]
print ans

def s():
    [n,m] = list(map(int,input().split()))
    a = [list(reversed(list(map(int,input().split()))))for _ in range(m)]
    a.sort(reverse=True)
    r = 0
    for i in a:
        if n > 0:
            r += min(n,i[1])*i[0]
            n -= min(n,i[1])
        else:
            break
    print(r)
s()

def main():
    n, m = map(int, input().split())

    containers = []
    for _ in range(m):
        containers.append(tuple(map(int, input().split())))

    containers.sort(reverse=True, key=lambda pair: pair[1])
    total_matches = 0
    for matchboxes, matches in containers:
        if matchboxes < n:
            total_matches += matchboxes * matches
            n -= matchboxes

        else:
            total_matches += n * matches
            break

    print(total_matches)

if __name__ == '__main__':
    main()

n,m=map(int,input().split())
ans=0
d=[]
for x in range(m):
	a,b=map(int,input().split())
	d.append([b,a])
d.sort(key=lambda index: index[0],reverse=True)
for x in d:
	ans+=x[0]*(x[1] if x[1]<=n else n)
	n-=(x[1] if x[1]<=n else n)
print(ans)

R=lambda:map(int,raw_input().split())
n,m=R()
print reduce(lambda x,y:(x[0]-min(x[0],y[0]),x[1]+min(x[0],y[0])*y[1]),sorted([R()for i in range(m)],key=lambda x:-x[1]),(n,0))[1]

#!/bin/python
# -*- coding: utf-8 -*-

from operator import itemgetter

n, m = map(int, input().split())
containers = []

for i in range(m):
    a, b = map(int, input().split())
    containers.append((a, b))

containers.sort(key=itemgetter(1), reverse=True)

ans = 0
for (ai, bi) in containers:
    if n > ai:
        ans += (ai * bi)
        n -= ai
    else:
        ans += (n * bi)
        break

print(ans)

n,m=map(int,input().split())
x=[]
y=[]
A=0
for i in range(m):
    a,b=map(int,input().split())
    x.append(a)
    y.append(b)
if sum(x)>n:
    while n>0:
        a=y.index(max(y))
        if x[a]>=n:
            A+=n*y[a]
            n=0
        else:
            A+=x[a]*y[a]
            n=n-x[a]
        x.pop(a)
        y.pop(a)
    print(A)
else:
    for i in range(m):
        A+=x[i]*y[i]
    print(A)

n,m=[int(x) for x in raw_input().split()]
A=[]
for i in range(m):
	A.append([int(x) for x in raw_input().split()])
A.sort(key=lambda x:x[1])
A.reverse()
ans=0
i=0
while n and i<len(A):
	k=min(n,A[i][0])
	ans+=A[i][1]*k
	n-=k
	i+=1
print ans

'''input
3 3
1 3
2 2
3 1
'''
# int(stdin.readline().strip())
# list(map(int, stdin.readline().split()))
from sys import stdin

# main starts
n, m = list(map(int, stdin.readline().split()))
arr = []
for _ in range(m):
	a, b = list(map(int, stdin.readline().split()))
	arr.append([a, b, a * b])

arr = sorted(arr, key =lambda x : x[1],  reverse = True)
bags = 0
ans = 0
# print(arr)
for i in range(m):
	# print(ans)
	a, b, matches = arr[i]

	if bags + a <= n:
		ans += matches
		bags += a
		continue
	else:
		t = 1
		while bags + t <= n and t <= a:
			ans += b
			t += 1
		break

print(ans)

class A:

    def solve(self):
        [n, m] = [int(x) for x in input().split(" ")]
        flag = []
        for i in range(n):
            flag.append(input())

        if any([len(set(x)) != 1 for x in flag]):
            print("NO")
            return

        first_colors = [x[0] for x in flag]
        if any([x == y for x, y in zip(first_colors, first_colors[1:])]):
            print("NO")
            return

        print("YES")

class B:

    def solve(self):
        [n, m] = [int(x) for x in input().split(" ")]
        matches = []
        for i in range(m):
            matches.append([int(x) for x in input().split(" ")])

        matches = sorted(matches, key=lambda x: x[1], reverse=True)

        total_matches = 0

        for (nums, matchsticks) in matches:
            if nums < n:
                n -= nums
                total_matches += nums * matchsticks
            else:
                total_matches += n * matchsticks
                n = 0

        print(total_matches)

B().solve()

n, m = [int(x) for x in input().split(' ')]
mbs = {i: 0 for i in range(1, 11)}
for i in range(m):
    a, b = [int(x) for x in input().split(' ')]
    mbs[b] += a
    matches = 0
    matchboxes = 0
    i = 10
    while matchboxes < n and i > 0:
        x = min(n - matchboxes, mbs[i])
        matches += x * i
        matchboxes += x
        i -= 1
print(matches)

raw = list(map(int, input().split(" ")))
n, m = raw[0], raw[1]
container = []
for i in range(m):
    container.append(list(map(int, input().split(" "))))
    container.sort(key = lambda x: x[1], reverse = True)

suma = 0
for i in container:
    if n >= i[0]:
        suma +=i[0] * i[1]
        n -= i[0]
    else:
        suma += n * i[1]
        n = 0
    if n == 0:
        break
print(suma)

n,m = [int(x) for x in input().split()]
D = {}
B = []
for i in range(0,m):
    a,b = [int(x) for x in input().split()]
    if not (b in B):
        B.append(b)
    if b in D:
        D[b] += a
    else:
        D[b] = a
B.sort()
B.reverse()
c1 = 0
c2 = 0
s = 0
while (c2 < n and c1 <= len(B)-1):
    c2 += D[B[c1]]
    s += D[B[c1]]*B[c1]
    c1 += 1

if c2 > n:
    s -= (c2-n)*B[c1-1]
print(s)

n , m = map(int , input().split())
l=[]
for i in range(int(m)):
    t = list(map(int, input().split() ))
    t= t[::-1]
    l.append(t)
l.sort(reverse = True)
ans = 0
i=0
while n>0 and i<len(l):
    temp = min(n , l[i][1])
    ans = ans + temp*l[i][0]
    n = n - temp
    i = i+1
print(ans)

import collections
s=list(map(int,input().split()))
n=s[0]
m=s[1]
mydict={}
arrofsize=[]
arrofwts=[]
for i in range(m):
    t=input().split()
    arrofsize.append(int(t[0]))
    arrofwts.append(int(t[1]))
arrofwts2=list(set(arrofwts))
for i in arrofwts2:
    counts=0
    for j in range(0,len(arrofwts)):
        if(i==arrofwts[j]):
            counts+=arrofsize[j]
    mydict[i]=counts

size=list(mydict.values())
wt=list(mydict.keys())
arr=sorted(mydict.items())
count=0
i=len(wt)-1
while(i>=0):
    w1=arr[i][0]
    s1=arr[i][1]
    if(n>=s1):
        count=count+(s1*w1)
        n=n-s1

    elif(n<s1):
        count=count+(n*w1)
        n=n-n
    i=i-1
print(count)

n,m=[int(x) for x in input().split()]
l1,l2=[],[]
for i in range(m):
    a,b=input().split()
    l1.append(int(a))
    l2.append(int(b))
l2, l1 =(list(t) for t in zip(*sorted(zip(l2, l1))))
l1=l1[::-1]
l2=l2[::-1]
c,s,v=0,0,0
for i in range(m):
    s+=l1[i]
    if(s<=n):
        c+=(l1[i]*l2[i])
    else:
        c+=((n-v)*l2[i])
        break
    v=s
print(c)

n,m=map(int,raw_input().split())
c=[0]*16
for _ in range(m):
  a,b=map(int,raw_input().split())
  c[b]+=a
s=0
for i in range(10):
  d=min(n,c[10-i])
  n-=d
  s+=d*(10-i)
print s

n, m = map(int,input().split())
s = []
ans = 0
for i in range(m):
        s.append(tuple(map(int, input().split())))
s.sort(reverse=True, key=lambda x:x[1])
for i in s:
        m = min(n,i[0])
        ans += m*i[1]
        n -= m
        if n == 0:
                break
print(ans)

import sys

class pythonin:
    _data = []
    _cur = 0
    def __init__(self):
        while True:
            try: sm = raw_input().split(" ")
            except EOFError: break
            for x in sm :
                if x != "" and x != "\t" :
                    self._data.append(x)

    def eof(self) :
        return self._cur == len(self._data)

    def nextToken(self) :
        self._cur += 1
        return self._data[self._cur - 1]

    def nextInt(self) :
        return (int)(self.nextToken())

#sys.stdin = open("input.txt", "r")
#sys.stdout = open("output.txt", "w")

pin = pythonin()

n = pin.nextInt()
m = pin.nextInt()

a = [[pin.nextInt(),pin.nextInt()] for i in xrange(0, m)]

for x in a :
    x[0], x[1] = x[1], x[0]

a.sort()
a.reverse()

res = 0

for x in a :
    res += min(n, x[1]) * x[0]
    n -= min(n, x[1])

print res

#print ("Press any key to continue")
#raw_input()

a,b=input().strip().split(" ")
a,b=[int(a),int(b)]
x=[]
for i in range(b):
    c,d=input().strip().split(" ")
    c,d=[int(c),int(d)]
    x.append([d,c])
x.sort(reverse=True)
s1=0
s2=0
for i in range(b):
    if s1+x[i][1]>a:
        s2+=(a-s1)*x[i][0]
        break
    else:
        s2+=x[i][0]*x[i][1]
        s1+=x[i][1]
print(s2)

n,m=raw_input().strip().split()
n,m=int(n),int(m)
arr=[list(map(int,raw_input().strip().split()))[::-1] for _ in xrange(m)]
arr.sort(reverse=True)
maxs,i=0,0
while i<m and n>0:
	maxs+=min(n,arr[i][1])*arr[i][0]
	n-=min(n,arr[i][1])
	i+=1
print maxs

from operator import itemgetter
n,m=map(int,raw_input().split())
p=[]
for i in range(m):
    p.append(map(int,raw_input().split()))

p.sort(key=itemgetter(1,0),reverse=True)
t,s=0,0
for i in range(m):
    t+=p[i][0]
    if t<=n:
        s+=p[i][0]*p[i][1]
    else:
        s+=(n-(t-p[i][0]))*p[i][1]
        break

print s

value = input()
n = int(value.split()[0])
matrix = [[0] * 2 for i in range(int(value.split()[1]))]
total = 0
for i in range(len(matrix)):
    value = input()
    int(value.split()[0])
    matrix[i][0] = int(value.split()[0])
    matrix[i][1] = int(value.split()[1])

# defines a mini function inside the call using lambda
# is tells the sort function to use the 2nd item to sort on it
# rather than the 1st value (it's default value)
matrix.sort(key=lambda x: x[1])

for i in range(len(matrix)):
    if n >= matrix[-i - 1][0]:
        n -= matrix[-i - 1][0]
        total += matrix[-i - 1][0] * matrix[-i - 1][1]
        continue
    else:
        total += n * matrix[-i - 1][1]
        break

print(total)

#! /usr/bin/python
import sys

#nacteni 2 cisel
numbers = sys.stdin.readline()[:-1].split()
n = int(numbers[0])
m = int(numbers[1])

# zde seznam seznamu
list = []
#nacitani cisel v radku do promenne
for i in range(m):
    line = sys.stdin.readline()[:-1].split()
    a = int(line[0])
    b = int(line[1])
    list.append((b,a))

list.sort()
list.reverse()

number = 0
taken = 0
while (list != []) and (taken + list[0][1] <= n):
    taken += list[0][1]
    number += list[0][0]*list[0][1]
    x = list.pop(0)

if (list != []) and (taken < n):
    number += list[0][0]*(n-taken)

print number

n, m = map(int, input().split())

main = []
for i in range(m):
    box, matches = map(int, input().split())
    main.append([box, matches])

main.sort(key = lambda x: x[1], reverse = True)

total = 0

j = 0

while n != 0 and j < len(main):
    n -= main[j][0]
    if n < 0:
        total += ((main[j][0] + n) * main[j][1])
        break
    else: total += (main[j][0] * main[j][1])

    if n < 0: break

    j += 1

print(total)

n,m=[int(i) for i in input().split()]
cand=[]
for i in range(m):
    cand.append([int(i) for i in input().split()])

cand.sort(key=lambda x:x[1],reverse=True)
res=0
for i in range(m):
    temp=min(n,cand[i][0])
    res+=cand[i][1]*temp
    n-=temp
    if temp<=0:
        break
print(res)

n,m=[int(x) for x in raw_input().split()]
A=[]
for i in range(m):
	A.append([int(x) for x in raw_input().split()])
A.sort(key=lambda x:x[1])
A.reverse()
ans=0
i=0
while n and i<len(A):
	k=min(n,A[i][0])
	ans+=A[i][1]*k
	n-=k
	i+=1
print ans

def solve():
    n, m = map(int, input().split())
    ab = []
    for _ in range(m): ab.append((tuple(map(int, input().split()))))
    put = 0
    for a, b in sorted(ab, key=lambda x: x[1], reverse=True):
        n -= a
        if n < 0:
            put += (a+n) * b
            return put
        put += a * b
    return put
print(solve())

from itertools import *
n, m = map (int, raw_input ().split ())
cont = sorted ([map (int, raw_input ().split ())[::-1] for i in xrange (m)], reverse=True)
ans = 0
for b, a in cont:
    ans += min (a, n) * b
    n -= min (a, n)
    if n <= 0:
        break
print ans

#!/usr/bin/env pypy
from __future__ import division, print_function
from collections import defaultdict, Counter, deque
from future_builtins import ascii, filter, hex, map, oct, zip
from itertools import imap as map, izip as zip, permutations, combinations, combinations_with_replacement
from __builtin__ import xrange as range
from math import ceil, factorial, log,tan,pi,cos,sin,radians
from _continuation import continulet
from cStringIO import StringIO
from io import IOBase
import __pypy__
from bisect import bisect, insort, bisect_left, bisect_right
from fractions import Fraction
from functools import reduce
import string
import sys
import os
import re
inf = float('inf')
mod_ = int(1e9) + 7
mod = 998244353

def factors(n):
    from functools import reduce
    return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

def sieve(m):
    n=1
    primes = {}
    arr=set([])
    for i in range(2, int(m ** 0.5) + 1):
        a = n // i
        b = m // i
        for k in range(max(2, a), b + 1):
            c = i * k
            primes[c] = 1

    for i in range(max(n, 2), m + 1):
        if i not in primes:
            arr.add(i)

    return arr

class DisjointSetUnion:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n
        self.num_sets = n

    def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a, b):
        a, b = self.find(a), self.find(b)
        if a != b:
            if self.size[a] < self.size[b]:
                a, b = b, a

            self.num_sets -= 1
            self.parent[b] = a
            self.size[a] += self.size[b]

    def set_size(self, a):
        return self.size[self.find(a)]

    def __len__(self):
        return self.num_sets

def main():
    n,m=map(int,input().split())
    arr=[]
    mat=[]
    cnt=0
    for i in range(m):
        a,b=map(int,input().split())
        arr.append((b,a))
    arr.sort(reverse=True)
    s=0
    cnt=0
    for i in range(m):
        t=min(arr[i][1],n)
        n-=t
        cnt+=t*arr[i][0]
    print(cnt)

# region fastio

BUFSIZE = 8192

class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()

class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()

def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

n,m = map(int,input().split())
A = []
for i in range(m):
    a,b = map(int,input().split())
    A.append([b,a])
A.sort()
#print(A)
i=m-1
ans=0
while(n>0 and i>=0):
    ans+=A[i][0]*min(n,A[i][1])
    n-=A[i][1]
    i-=1
print(ans)

n,m=map(int,input().split())
l=[]
for i in range(m):
    a,b=map(int,input().split())
    t=(b,a)
    l.append(t)
l.sort(reverse=True)
r=0
le=len(l)
while(n):
    if n>=l[0][1]:
        r+=l[0][1]*l[0][0]
        n-=l[0][1]
        l.pop(0)
        le-=1
    else:
        r+=n*l[0][0]
        break
    if le==0:
        break
print(r)

# https://codeforces.com/contest/16/problem/B
from itertools import combinations

n , m = map(int,raw_input().split(' '))
k = m
conatiners = []
while k:
    conatiners.append(map(int,raw_input().split(' ')))
    k = k-1
op = []
conatiners.sort(key=lambda x: x[1],reverse = True)
for row in conatiners:
    if row[0]< n:
        op.append(row[1] * row[0])
        n = n - row[0]
    else:
        op.append(n * row[1])
        n = 0
        break

print sum(op)

R = lambda:map(int,input().split())

n, m  = R()
arr = sorted([[*R()] for i in range(m)], key=lambda x: x[1], reverse=True)
ans = 0
for i in arr:
    v = min(n, i[0])
    ans += v*i[1]
    n -= v
    if n < 1:
        break
print(ans)

from operator import itemgetter

L0 = input().split()
n = int(L0[0])  # the rucksack con hold n matchboxes
m = int(L0[1])  # there are m containers
L = list()
for i in range(m):
    L.append(input().split())
for i in range(m):
    for j in range(2):
        L[i][j] = int(L[i][j])
# L[i][0]= number of matchboxes
# L[i][1]= number of matches in these matchboxes
L1 = sorted(L, reverse=True, key=itemgetter(1))
if sum(L1[k][0] for k in range(m)) < n:
    print(sum(L1[k][0] * L1[k][1] for k in range(m)))
    exit(0)
s = 0
k = 0
while n > 0 and k <= m - 1:
    if L1[k][0] <= n:  # we can take the entiere container
        s += L1[k][0] * L1[k][1]
        n -= L1[k][0]
    else:
        s += n * L1[k][1]
        n = 0
    k += 1
print(s)

n,m=map(int,input().split())
X=[]
for i in range(m):
    X.append(list(map(int,input().split()))[::-1])
X.sort(reverse=True)
Box=0
for i in X:
    if n==0:
        break
    if i[1]>n:
        Box+=i[0]*n
        n=0
    else:
        Box+=i[0]*i[1]
        n-=i[1]

print(Box)

n,m=[int(i) for i in input().split()]
l=sorted([int(i) for i in reversed(input().split())] for j in range(m))
s=0
for i in reversed(l):
    if i[1]>=n:
        print(s+n*i[0])
        break
    n-=i[1]
    s+=i[1]*i[0]
else:
    print(s)

def to_list(s):
    return list(map(lambda x: int(x), s.split(' ')))

def solve(boxes,matches,n):

    sorted_ = sorted(zip(boxes,matches), key=lambda x: x[1], reverse=True)
    match_count = 0
    box_residue = n
    for item in sorted_:
        if box_residue > 0:
            min_ = min(box_residue, item[0])
            box_residue -= min_
            match_count += item[1]*min_
        else:
            return match_count
    return match_count

boxes = []
matches = []

n, m = to_list(input())
for i in range(m):
    s = to_list(input())
    boxes.append(s[0])
    matches.append(s[1])

print(solve(boxes,matches,n))

def read_int():
    return int(input().strip())

def read_ints():
    return list(map(int, input().strip().split(' ')))

def rev(lst):
    lst.reverse()
    return lst

def solve():
    """
    m containers
    i-th container has a[i] matchboxes, each matchbox has b[i] matches
    rucksack holds n matchboxes exactly
    """
    n, m = read_ints()
    containers = [rev(read_ints()) for _ in range(m)]
    containers.sort()
    answer = 0
    while containers:
        matches, boxes = containers.pop()
        if n <= boxes:
            answer += n*matches
            break
        else:
            answer += boxes*matches
            n -= boxes
    return answer

if __name__ == '__main__':
    print(solve())

[n,m] = [int(x) for x in input().split()]
a=[]

for i in range (m) :
	a.append([int(x) for x in input().split()])

a = sorted(a , key = lambda x : x[1] ,reverse = True)
k = 0

while n > 0 and len(a) > 0:
	if n  >=  a[0][0] :
		k += a[0][0]*a[0][1]
		n -= a[0][0]
		a.pop(0)
	else :
		k += n *a[0][1]
		n = 0

print(k)

# -*- coding:utf-8 -*-
import sys

def some_func():
    """
    """
    n,m = map(int,raw_input().split(' '))
    count = 0
    temp_list = []
    for v in range(m):
        a,b = map(int,raw_input().split(' '))
        temp_list.append([b,a])
    temp_list.sort(reverse=1)
    for v in temp_list:

        if n<=0:
            break
        if n> v[1]:
            count+= v[1]*v[0]
            n -= v[1]
        else:
            count += n*v[0]
            n = 0
    print count

if __name__ == '__main__':
  some_func()

from  bisect import bisect_right as br
import sys
from collections import *
from math import *
import re
def sieve(n):
    prime=[True for i in range(n+1)]
    p=2
    while p*p<=n:
        if prime[p]==True:
            for i in range(p*p,n+1,p):
                prime[i]=False
        p+=1
    c=0
    for i in range(2,n):
        if prime[i]:
            #print(i)
            c+=1
    return c
def totient(n):
    res,p=n,2
    while p*p<=n:
        if n%p==0:
            while n%p==0:
                n=n//p
            res-=int(res/p)
        p+=1
    if n>1:res-=int(res/n)
    return res

def iseven(n):return[False,True][0 if n%2 else 1]
def inp_matrix(n):return list([input().split()] for i in range(n))
def inp_arr():return list(map(int,input().split()))
def inp_integers():return map(int,input().split())
def inp_strings():return input().split()
def lcm(a,b):return (a*b)/gcd(a,b)
max_int = sys.maxsize
mod = 10**9+7
flag1=False
flag2=False

n,m=inp_integers()
L=[list(map(int,input().split())) for i in range(m)]
L.sort(key=lambda x:x[1],reverse=True)
#print(L)
ans=0
for i in range(m):
    ans+=min(L[0][0],n)*L[0][1]
    n-=min(L[0][0],n)
    L.pop(0)
print(ans)

n,m = map(int, input().split(" "))
a = []
for i in range(m):
    a.append(list(map(int,input().split(" "))))
a.sort(key = lambda x : x[1],reverse = True)
sum = 0
for x in a:
    if n >= x[0]:
        sum = sum + x[0]*x[1]
        n = n - x[0]
    else:
        sum = sum + n*x[1]
        break
print(sum)

n , m = map(int,input().split())
mapped =[]

for i in range(m):
   mapped.append(list(map(int,input().split(" "))))
mapped.sort(key = lambda x:x[1] , reverse =True)
result =0
for a in mapped:
    if n>a[0]:
        result+=a[0]*a[1]
        n-=a[0]
    else:
        result+=a[1]*n
        break

print(result)

def q16b():
	n, m = tuple([int(num) for num in input().split()])
	as_ = []
	bs_ = []
	for _ in range(m):
		a, b = tuple([int(num) for num in input().split()])
		as_.append(a)
		bs_.append(b)
	argsorted = sorted(range(len(bs_)), key=bs_.__getitem__)
	argsorted.reverse()
	total = 0
	for i in range(len(argsorted)):
		if(n <= 0):
			break
		total += min(as_[argsorted[i]], n) * bs_[argsorted[i]]
		n -= as_[argsorted[i]]
	print(total)
q16b()

# def q16b():
# 	n, m = tuple([int(num) for num in input().split()])
# 	boxes = []
# 	for _ in range(m):
# 		a, b = tuple([int(num) for num in input().split()])
# 		boxes = boxes + [b for _ in range(a)]
# 	boxes.sort()
# 	boxes.reverse()
# 	print(sum(boxes[:n]))

# q16b()

n,m=map(int,input().split())
x=[]
y=[]
A=0
for i in range(m):
    a,b=map(int,input().split())
    x.append(a)
    y.append(b)
if sum(x)>n:
    while n>0:
        a=y.index(max(y))
        if x[a]>=n:
            A+=n*y[a]
            n=0
        else:
            A+=x[a]*y[a]
            n=n-x[a]
        x.pop(a)
        y.pop(a)
    print(A)
else:
    for i in range(m):
        A+=x[i]*y[i]
    print(A)

n, m = map(int, input().split())
res = []
for _ in range(m):
     a, b = map(int, input().split())
     res.append((a, b))
res.sort(key=lambda x: x[1], reverse=True)
matches = 0
current = n
for a, b in res:
     if current > 0:
          matches += min(current, a)*b
     current -= min(current, a)
print(matches)

n,m = [int(x) for x in input().split()]
D = {}
B = []
for i in range(0,m):
    a,b = [int(x) for x in input().split()]
    if not (b in B):
        B.append(b)
    if b in D:
        D[b] += a
    else:
        D[b] = a
B.sort()
B.reverse()
c1 = 0
c2 = 0
s = 0
while (c2 < n and c1 <= len(B)-1):
    c2 += D[B[c1]]
    s += D[B[c1]]*B[c1]
    c1 += 1

if c2 > n:
    s -= (c2-n)*B[c1-1]
print(s)

n,m=map(int,input().split())
l=[]
for i in range(m):
    a,b=map(int,input().split())
    t=(b,a)
    l.append(t)
l.sort(reverse=True)
r=0
le=len(l)
while(n):
    if n>=l[0][1]:
        r+=l[0][1]*l[0][0]
        n-=l[0][1]
        l.pop(0)
        le-=1
    else:
        r+=n*l[0][0]
        break
    if le==0:
        break
print(r)

n,m=map(int,input().split())
l=[]
for i in range(m):
    a,b=map(int,input().split())
    l.append([b,a])
l.sort()
l.reverse()
#print(l)
cntr=0
for i in l:
    if n==0:
        break
    if i[1] > n:
        cntr+= n * i[0]
        break
    if i[1]<=n:
        cntr+=i[0]*i[1]
        #print(cntr)
        n-=i[1]
print(cntr)

# ip = open("testdata.txt", "r")

# def input():
# 	return ip.readline().strip()

n, m = map(int, input().split())
cont = [0]*m

for i in range(m):
	boxes, matches = map(int, input().split())
	cont[i] = [matches, boxes]

cont.sort(reverse=True)
count = 0

for match, box in cont:
	x = min(n, box)
	count += match*x
	n -= x
	if n==0:
		break

print(count)

a,b=input().strip().split(" ")
a,b=[int(a),int(b)]
x=[]
for i in range(b):
    c,d=input().strip().split(" ")
    c,d=[int(c),int(d)]
    x.append([d,c])
x.sort(reverse=True)
s1=0
s2=0
for i in range(b):
    if s1+x[i][1]>a:
        s2+=(a-s1)*x[i][0]
        break
    else:
        s2+=x[i][0]*x[i][1]
        s1+=x[i][1]
print(s2)

n, m = map(int, input().split())

main = []
for i in range(m):
    box, matches = map(int, input().split())
    main.append([box, matches])

main.sort(key = lambda x: x[1], reverse = True)

total = 0

j = 0

while n != 0 and j < len(main):
    n -= main[j][0]
    if n < 0:
        total += ((main[j][0] + n) * main[j][1])
        break
    else: total += (main[j][0] * main[j][1])

    if n < 0: break

    j += 1

print(total)

n,m=[int(x) for x in raw_input().split()]
A=[]
for i in range(m):
	A.append([int(x) for x in raw_input().split()])
A.sort(key=lambda x:x[1])
A.reverse()
ans=0
i=0
while n and i<len(A):
	k=min(n,A[i][0])
	ans+=A[i][1]*k
	n-=k
	i+=1
print ans

import sys

n, m = map(int, sys.stdin.readline().split())

t = []

for i in xrange(m):
    a, b = map(int, sys.stdin.readline().split())
    t.append((a, b))

t = sorted(t, key=lambda x: x[1], reverse=True)

result = 0
i = 0
while i < m and n > 0:
    if n < t[i][0]:
        result += n*t[i][1]
        n = 0
    else:
        result += t[i][0]*t[i][1]
        n -= t[i][0]
    i += 1

print result

n, m = map(int, raw_input().split())
a = [ map(int, raw_input().split()) for _ in xrange(m) ]

a.sort(key = lambda x:x[1], reverse = True)
ans = 0
for i in range(m):
    get = min(n, a[i][0])
    n -= get
    ans += get * a[i][1]
print ans

from collections import defaultdict
n,m = map(int,raw_input().split())
counter = defaultdict(int)
for i in range(m):
    a,b = map(int,raw_input().split())
    counter[b]+=a
counter = sorted(counter.items(), key=lambda x: x[0],reverse=True)
print(counter)
i = 0
counter_length = len(counter)
score = 0
while (n > 0 and i < counter_length):
    if counter[i][1] >= n:
        itemsToTake = n
        score+=counter[i][0]*itemsToTake
        n -=itemsToTake
    else:
        score+=counter[i][0]*counter[i][1]
        n-=counter[i][1]
    i+=1
print(score)

import collections
s=list(map(int,input().split()))
n=s[0]
m=s[1]
mydict={}
for i in range(m):
    t=input().split()
    mydict[int(t[1])]=int(t[0])
size=list(mydict.values())
wt=list(mydict.keys())
arr=sorted(mydict.items())
count=0
i=len(wt)-1
while(i>=0):
    w1=arr[i][0]
    s1=arr[i][1]
    if(n>=s1):
        count=count+(s1*w1)
        n=n-s1

    elif(n<s1):
        count=count+(n*w1)
        n=n-n
    i=i-1
print(count)

# https://codeforces.com/contest/16/problem/B
from itertools import combinations

n , m = map(int,raw_input().split(' '))
k = m
conatiners = []
while k:
    conatiners.append(list(map(int,raw_input().split(' '))))
    k = k-1
op = []
for item in combinations (conatiners,2):
    t1 , t2 = item[0], item[1]
    op.append( t1[0]*t1[1]+ t2[0]* t2[1])

# print op
op  = sorted(op)
print op[-1]

from sys import stdin,stdout

def main():
	L=[]
	n,m=map(int,stdin.readline().split())

	for i in range(m):
		L.append(stdin.readline().split())

	A=(sorted(L, key=lambda L: int(L[0])*int(L[1]),reverse=True))

	matches=0

	for i in range(m):
		if n==0: break
		temp=int(A[i][0])
		for j in range(int(A[i][0])):
			n-=1
			temp-=1
			matches+=int(A[i][1])
			if n==0 or temp==0:
				break

	print(matches)

if __name__ == '__main__':
	main()

n, m = map(int, input().split())
con = {}
c = 0
for i in range(m):
    x, y = map(int, input().split())
    con[x] = y
d = dict(sorted(con.items(), key=lambda kv: kv[1], reverse=True))
for k, v in d.items():
    for i in range(k):
        c += v
        n -= 1
        if n == 0:
            print(c)
            exit()

import sys
import math
import functools
input = sys.stdin.readline

n, m = map(int, input().strip().split())
a = []

for _ in range(m):
    z = list(map(int, input().strip().split()))
    a.append(z)
def compare(A, B):
    if A[0] * A[1] > B[0] * B[1]: return 1
    elif A[0] * A[1] < B[0] * B[1]:  return -1
    else:
        if A[0] < B[0]: return 1
        elif A[0] > B[0]: return -1
        else: return 0

a.sort(key = functools.cmp_to_key(compare), reverse=True)
ans = 0
for el in a:
    ans += min(n, el[0]) * el[1]
    n -= min(n, el[0])
print(ans)

#	Author	: debugster
#	Email	: alive.dew@gmail.com
#	Date	: 2020-06-03 21:49:06

import sys
import os

def get_int():
    return map(int, input().split())

def get_array():
    return list(map(int, input().split()))

if os.environ.get("DEBUGSTER_PYTHON"):
	sys.stdin = open('in.txt', 'r')
	sys.stdout = open('out.txt','w')

class MatchBox:
	def __init__(self, a, b):
		self.a, self.b = a, b

	def __lt__(self, other):
		if self.a * self.b == other.a * other.b:
			return self.b > other.b

		return self.a * self.b > other.a * other.b

n, m = get_int()
all_m = list()
for _ in range(m):
	a, b = get_int()
	all_m.append(MatchBox(a, b))
all_m = sorted(all_m)

ans = 0
remain = n
for x in all_m:
	take = min(x.a, remain)
	ans += take * x.b
	remain -= take
	if remain <= 0:
		break

print(ans)

n, m = list(map(int, input().split()))

match = {}

for _ in range(m):
    Ai, Bi = list(map(int, input().split()))

    match[Ai] = Bi

matchs = [(k, match[k]) for k in sorted(match, key=match.get, reverse=True)]

ans = 0
for k, v in matchs:
    if k <= n:
        ans += k * v
        n -= k
    elif n < k:
        ans += n * v
        break

print(ans)

n,m = map(int,input().split())

f=[]

for k in range(m):
    f.append(list(map(int,input().split()))[::-1])

f.sort()

g=f[::-1]
p=0

while n:
    for u in g:
        if u[1]<=n:
            p+=u[1]*(u[0])
            n-=u[1]
        elif u[1]>n:
            p+= u[0]*n
            n=0

print(p)

n, m = list(map(int, input().split()))
d = {}
lst1 = []
lst2 = []
for i in range(m):
	val1, val2 = list(map(int, input().split()))
	lst1.append(val1)
	lst2.append(val2)
cnt = 0
while n > 0:
	maxx, ind = -1, 0
	for i in range(len(lst2)):
		if lst2[i]*lst1[i] > maxx:
			maxx = lst2[i]*lst1[i]
			ind = i
	i = ind
	if lst1[i] <= n:
		cnt += lst2[i]*lst1[i]
		n -= lst1[i]
		lst2 = lst2[:i] + lst2[i+1:]
		lst1 = lst1[:i] + lst1[i+1:]
	else:
		cnt += n * lst2[i]
		break

	if lst1 == []:
		break
print(cnt)

n, m = map(int, input().split())
a = [[int(j) for j in input().split()] for i in range(m)]
a.sort(key=lambda x: x[1], reverse=True)
c = 0
for k, v in a:
    if n >= k:
        c += k*v
        n -= k
        if n == 0:
            print(c)
            exit()
    else:
        c += n*v
        print(c)
        exit()

# con = {}
# c = 0
# for i in range(m):
#     x, y = map(int, input().split())
#     con[y] = x
# #d = dict(sorted(con.items(), key=lambda kv: kv[1], reverse=True))
# d = dict(sorted(con.items(), reverse=True))
#
# for v, k in d.items():
#     for i in range(k):
#         c += v
#         n -= 1
#         if n == 0:
#             print(c)
#             exit()

arr1 = raw_input()
l1 = map(int,arr1.split())

# print l1
n = l1[0]
#No of containers
m = l1[1]

# l2 = [[1,3],[2,2],[3,1]]

l2 = []

for i in range(m):
	arr2 = raw_input()
	l2.append(map(int,arr2.split()))

# print l2

l2.sort(reverse= True, key=lambda x: x[1])
print l2

count = 0

while(n>0):
	for i in range(m):
		if n <= l2[i][0]:
			count = count + n*l2[i][1]
			n = 0
		else:
			count = count + l2[i][0]*l2[i][1]
			n = n - l2[i][0]

print count

n,m=[int(x) for x in input().split()]
list1=[]
mx=0
z=0
for i in range(m):
    list2=[int(x) for x in input().split()]
    list1.append(list2)

mn=0
for i in range(m):
    list1[i][0],list1[i][1]=list1[i][1],list1[i][0]

list1.sort(reverse=True)

for i in range(m):
    if n<mn:
        mn=n
    if n>0:
        n=n-list1[i][1]
        mx=mx+list1[i][1]*list1[i][0]

    if mn<=0:
        mx=mx-((-1)*(mn)*list1[i-1][0])
print(mx)

n,m=[int(i) for i in input().split()]
l=sorted([int(i) for i in reversed(input().split())] for j in range(m))
s=0
for i in reversed(l):
    if i[1]>=n:
        print(s+n*i[0])
        break
    n-=i[1]
    s+=i[1]*i[0]

arr1 = raw_input()
l1 = map(int,arr1.split())

# print l1
n = l1[0]
#No of containers
m = l1[1]

# l2 = [[1,3],[2,2],[3,1]]

l2 = []

for i in range(m):
	arr2 = raw_input()
	l2.append(map(int,arr2.split()))

# print l2

l2.sort(reverse= True, key=lambda x: x[1])
# print l2

count = 0

while(n>0):
	for i in range(m):
		if n <= l2[i][0]:
			count = count + n*l2[i][1]
			n = 0
		else:
			count = count + l2[i][0]*l2[i][1]
			n = n - l2[i][0]

print count

import os

box_cont = input().rstrip().split()
n=int(box_cont[0])
m=int(box_cont[1])
hah=[0]*11
for i in range(m):
    a_b=input().rstrip().split()
    a=int(a_b[0])
    b=int(a_b[1])
    hah[b]=a
total=0
for i in range(10,0,-1):
    if hah[i]!= 0:
        if n-hah[i]>=0:
            total+=(hah[i]*i)
            n=n-hah[i]
        else:
            total+=i*n
            break
print(total)

'''D = dict()
for i in range(1,5):
	D[i] = i*i
del D[4]
for i in range(1,len(D)):
	value = D[i]
	print(value)
	print(D)
print(max(D))'''

def swap(i , j):
	i , j = j , i
if __name__ == '__main__':
	n,m = map(int, input().split())
	A,B=[] , []
	for _ in range(m):
		a , b = map(int , input().split())
		A.append(a)
		B.append(b)
	for i in range(m):
		for j in range(i+1,m):
			if B[j]>B[i]:
				swap(A[i],A[j])
				swap(B[i] , B[j])
	ans = 0
	for i in range(m):
		a = min(n , A[i])
		ans += B[i]*a
		n -=a
	print(ans)

n,m = map(int,input().split())
A = []
for i in range(m):
    a,b = map(int,input().split())
    A.append([b,a])
A.sort()
#print(A)
i=m-1
ans=0
while(n>0):
    ans+=A[i][0]*min(n,A[i][1])
    n-=A[i][1]
    i-=1
print(ans)

n,m=[int(i) for i in input().split()]
a=m*[0]
b=m*[0]
for i in range(m):
    a[i],b[i]=[int(i) for i in input().split()]
for i in range(1-1,m):
    bish=b[0]
    k=0
    for j in range(1,m-i):
        if b[j]>bish:
            bish=b[j]
            k=j
    a[k],a[m-1-i]=a[m-1-i],a[k]
    b[k],b[m-1-i]=b[m-1-i],b[k]
a.reverse()
b.reverse()
i=0
h=0
r=0
while i<m:
    if h+n<=n:
        r+=a[i]*b[i]
        h+=a[i]
        i+=1
    else:
        z=n-h
        r+=z*b[i]
        break
print(r)

arr1 = raw_input()
l1 = map(int,arr1.split())

# print l1
n = l1[0]
#No of containers
m = l1[1]

# l2 = [[1,3],[2,2],[3,1]]

l2 = []

for i in range(m):
	arr2 = raw_input()
	l2.append(map(int,arr2.split()))

# print l2

total_m = 0
total_mb = 0

for i in range(m):
	total_m = total_m + l2[i][0]*l2[i][1]
	total_mb = total_mb + l2[i][0]
# print total_m
# print total_mb

l2.sort(reverse= True, key=lambda x: x[1])
# print l2

count = 0

if total_m<n:
	count = total_mb
else:
	while(n>0):
		for i in range(m):
			if n <= l2[i][0]:
				count = count + n*l2[i][1]
				n = 0
			else:
				count = count + l2[i][0]*l2[i][1]
				n = n - l2[i][0]

print count

#!/usr/bin/env pypy
from __future__ import division, print_function
from collections import defaultdict, Counter, deque
from future_builtins import ascii, filter, hex, map, oct, zip
from itertools import imap as map, izip as zip, permutations, combinations, combinations_with_replacement
from __builtin__ import xrange as range
from math import ceil, factorial, log,tan,pi,cos,sin,radians
from _continuation import continulet
from cStringIO import StringIO
from io import IOBase
import __pypy__
from bisect import bisect, insort, bisect_left, bisect_right
from fractions import Fraction
from functools import reduce
import string
import sys
import os
import re
inf = float('inf')
mod_ = int(1e9) + 7
mod = 998244353

def factors(n):
    from functools import reduce
    return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

def sieve(m):
    n=1
    primes = {}
    arr=set([])
    for i in range(2, int(m ** 0.5) + 1):
        a = n // i
        b = m // i
        for k in range(max(2, a), b + 1):
            c = i * k
            primes[c] = 1

    for i in range(max(n, 2), m + 1):
        if i not in primes:
            arr.add(i)

    return arr

class DisjointSetUnion:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n
        self.num_sets = n

    def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a, b):
        a, b = self.find(a), self.find(b)
        if a != b:
            if self.size[a] < self.size[b]:
                a, b = b, a

            self.num_sets -= 1
            self.parent[b] = a
            self.size[a] += self.size[b]

    def set_size(self, a):
        return self.size[self.find(a)]

    def __len__(self):
        return self.num_sets

def main():
    n,m=map(int,input().split())
    arr=[]
    mat=[]
    cnt=0
    for i in range(m):
        a,b=map(int,input().split())
        arr.append(a)
        mat.append(b)
    mat.sort(reverse=True)
    s=0
    cnt=0
    for i in range(m):
        if s==n:
            break
        cnt+=arr[i]*mat[i]
        s+=arr[i]
    print(cnt)

# region fastio

BUFSIZE = 8192

class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()

class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()

def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

line1=input().split()
n=int(line1[0])
m=int(line1[1])
a=[]
while m>0:
    new=input().split()
    new.reverse()
    a.append(list(map(int,new)))
    m -=1
test=dict(a)
res=0
for i in range(0,len(test)):
    if n <=0:
        break
    num_of_matchbox=max(test)
    number_of_box=int(test[num_of_matchbox])
    if number_of_box > n:
        res +=n*num_of_matchbox
        n -=n
    elif n>=number_of_box:
       res += num_of_matchbox*number_of_box
       n -=number_of_box
       test.pop(max(test))
print(res)

from sys import stdin
import operator

def main():
    nm = [int(i) for i in stdin.readline().split()]
    n = nm[0]
    m = nm[1]
    boxes = {}
    matches = []
    answer = 0
    counter = 0
    for i in range(m):
        temp = [int(t) for t in stdin.readline().split()]
        if temp[1] in boxes:
            boxes[temp[1]] += temp[0];
            counter +=1
        else:
            boxes[temp[1]] = temp[0];
            matches.append(temp[1])

    matches.sort()
    #matches = matches[::-1]
    m -= counter
    delta = n
    i = m-1
    while(delta > 0):
        if delta-boxes[matches[i]] >= 0:
            delta -= boxes[matches[i]]
            answer += matches[i]*boxes[matches[i]]
        else:
            answer += delta * matches[i]
            delta = 0
        i -= 1
    print(answer)

main()

n,m = map(int,input().split())
d = {}
for i in range(m):
    a,b = map(int,input().split())
    d[a] = b
d = sorted(d.items(),reverse = True)
count = 0
# print(d)
for i in d:
    if n>0:
        if n-i[0] >0:
            count +=(i[0])*i[1]
            n -=i[0]
        else:
            count += (n)*i[1]
            n = 0
print(count)

n,m=input().split()
n,m=[int(n),int(m)]
a=[]
for i in range(0,m):
    a.append([int(i) for i in input().split()])
a.sort(key=lambda x:x[1], reverse=True)
c=0
for i in range(0,m):
    if a[0][0]<=n:
        c+=(a[i][0]*a[i][1])
        n=n-a[i][0]
    else:
        c+=(n*a[i][1])
        break
print(c)

#!/bin/python
# -*- coding: utf-8 -*-

n, m = map(int, input().split())
containers = []
for i in range(m):
    a, b = map(int, input().split())
    containers.append([a, b])
containers.sort(reverse=True)

ans = 0
for (ai, bi) in containers:
    if n > ai:
        ans += (ai * bi)
        n -= ai
    else:
        ans += (n * bi)
        break

print(ans)

l=list(map(int,raw_input().split()))
n = l[0]
m = l[1]
a=[]
for i in range (m):
	x = list(map(int,raw_input().split()))
	a.append(x)
a.sort(key=lambda x:x[1],reverse=True)
count = 0
total = 0
for i in a:
	# while count<n and i[0]>0:
	# 	count+=1
	# 	total+=i[1]
	# 	i[0]-=1
	if i[0]<n and count+i[0]<=n:
		count+=i[0]
		total+=i[1]*i[0]
	elif i[0]<n and count+i[0]>n:
		diff = n-count
		total+=i[1]*diff
	elif i[0]>n:
		diff = n-i[0]
		total+=i[1]*diff
	#print 't',total

print total

k,n=map(int,input().split())
d={}
d1=[]
res=0
for i in range(n):
    a,b=map(int,input().split())
    if b in d:
        d[b]+=a
    else:
        d[b]=a
    d1.append(b)
d1.sort()
d1.reverse()
# print(d)
# print(d1)
for i in d1:
    if k>=d[i]:
        res+=(d[i]*i)
        k-=d[i]
    else:
        res+=(k*i)
        k-=d[i]
    # print(res)
    if k<=0:
        break
print(res)

def list_input(data):
	return list(map(data, input().strip().split()))

def single_input(data):
	return map(data, input().strip().split())

n ,m = single_input(int)
r2 = []
res =  0
for a in range(0, m):
	a, b = single_input(int)

	r2.append([b, a])
r2.sort()
r2.reverse()

while n > 0:
	if r2[0][1] <= n:
		n -= r2[0][1]
		res += (r2[0][0]*r2[0][1])
		r2[0] = [0, 0]
		r2.sort()
		r2.reverse()
	else:
		n = 0
		r2[0][1] = 0
		res += r2[0][0]
		if r2[0][1] == 0:
			r2[0] = [0, 0]
		r2.sort()
		r2.reverse()
	if r2[0] == [0,0]:
		break
	# print(res,r2)
print(res)

n,m=map(int,input().split())
l=[]
for _ in range(m):
	k1,m1=map(int,input().split())
	l.append([m1,k1])
l.sort()
s,box=0,0
for x in range(m-1,-1,-1):
	m,k=l[x]
	box+=k
	if box>n:
		s+=m*(box-n+1)
		print(s)
		break
	elif box==n:
		s+=m*k
		print(s)
		break
	else:
		s+=m*k

import sys
input = sys.stdin.readline
read_tuple = lambda _type: map(_type, input().split(' '))

def solve():
    n, m = read_tuple(int)
    c = {}
    for _ in range(m):
        a, b = read_tuple(int)
        c[b] = a
    ans  = 0
    for b_i in sorted(c, reverse=True):
        while n and c[b_i]:
            n -= 1
            c[b_i] -= 1
            ans += b_i
    print(ans)

if __name__ == '__main__':
    solve()

n, m = [int(x) for x in raw_input().split()]
boxs = []
for x in xrange(m):
    tp = [int(x) for x in raw_input().split()]
    boxs += [(tp[0], tp[1])]
boxs.sort(key = lambda a: a[1])
ans = 0
i = m - 1
while n > 0:
    taken = min(n, boxs[i][0])
    ans += taken * boxs[i][1]
    n -= taken
    i -= 1
print ans

n, m = map(int, input().split())
data = []
for i in range(0,m):
    ai, bi = map(int, input().split())
    data.append([ai, bi])

data.sort(key = lambda row: row[1], reverse = True)
print("sorted data")
print(data)

space = n
total_matches = 0
for i in range(0,m):
    if data[i][0] < space:
        space = space - data[i][0]
        total_matches += data[i][0]*data[i][1]
        #print("matches collected:", total_matches)
        #print("space left:", space)
    elif space>0:
        total_matches += space * data[i][1]
        space = 0
        #print("matches collected*:", total_matches)
        #print("space left*:", space)

print(total_matches)

n,m = map(int,input().split())
a,b,countm,countMbox,p,x = [],[],0,0,0,0
for i in range(m):
    c,d = map(int,input().split())
    a.append(c)
    b.append(d)
    countMbox += c
    countm += c*d

if countMbox <= n:
    print(countm)
while n:
    x = b.index(max(b))
    if n <= a[x]:
        p += n*b[x]
        print(p)
        break
    else:
        p += a[x]*b[x]
        n = n - a[x]
        b[x] = 0

n, m = input().split()
n, m = int(n), int(m)
a = []
b = []
SUM = 0
for i in range(m):
    ai, bi = input().split()
    ai, bi = int(ai), int(bi)
    a.append(ai)
    b.append(bi)
for i in range(m):
    maxim = 0
    ind = 0
    for j in range(m-i):
        if maxim < b[j]:
            maxim = b[j]
            ind = j
    if n >= a[ind]:
        n-=a[ind]
        SUM+=a[ind]*b[ind]
        a.remove(a[ind])
        b.remove(b[ind])
    else:
        SUM+=n*b[ind]
        n = 0
        b.remove(b[ind])
print(SUM)

n, m = map(int, input().split(' '))
match = {}
for i in range (m):
    a,b = map(int, input().split(' '))
    match[b] = a
sizes = list(match.keys())
sizes.sort(reverse=True)
boxes = []
for i in sizes:
    boxes.append(match[i])
left = n
ans = 0
for i in range (len(boxes)):
    if boxes[i] < left:
        left = left - boxes[i]
        ans = ans + boxes[i] * sizes[i]
    else:
        if boxes[i] == left:
            ans = ans + boxes[i]*sizes[i]
            break
        else:
            if boxes[i] > left:
                ans = ans + left*sizes[i]
                break
print(ans)

n,m = map(int,input().split())
A = []
for i in range(m):
    a,b = map(int,input().split())
    A.append([b,a])
A.sort()
#print(A)
i=m-1
ans=0
while(n>0):
    ans+=A[i][0]*min(n,A[i][1])
    n-=A[i][1]
    i-=1
print(ans)

n,m=map(int,input().split())
z=0
l1=[]
l2=[]
while(z<m):
    a,b=map(int,input().split())
    l1.append(a)
    l2.append(b)
    z+=1
e=0
while(n>0):
    if(l1[l2.index(max(l2))]>n):
        e+=l2[l2.index(max(l2))]*n
        n=0
    else:
        e+=l2[l2.index(max(l2))]*l1[l2.index(max(l2))]
        n-=l1[l2.index(max(l2))]
        l2[l2.index(max(l2))]=-1
print(e)

n,m=map(int,input().split())
dict={}
ar=[]
br=[]
ans=0
for _ in range(m):
    a,b=map(int,input().split())
    ar.append(a)
    br.append(b)

for i in range(len(br)):
    for  j in range(len(br)):
        if br[j]<br[i]:
            br[i],br[j]=br[j],br[i]
            ar[i],ar[j]=ar[j],ar[i]

ans=0
a=0
co=0
mat=0
le=len(br)-1
while(ans<=n):
    if ans+ar[a]>=n:
        break
    else:

        ans=ans+ar[a]
    mat=mat+ar[a]*br[a]
    #print(ans,mat)

    if ans>=sum(br):
        break
    a+=1

if ans>=sum(br):
    pass
elif ans<n :
    mat+=(n-ans)*br[a]
    ans+=(n-ans)
print(mat)

n,m = input().strip().split(" ")
n = int(n)
m = int(m)
G = {}

for i in range(m):
	inp = input().strip().split(" ")
	inp[0] = int(inp[0])
	inp[1] = int(inp[1])
	G[inp[1]] = inp[0]

l = list(G.keys())
l.sort(reverse = True)
#print(l)
count = 0
for i in l:
	#print(n)
	if G[i] <= n :
		count += G[i]*i
		n = n - G[i]
	else :
		count += i*n
		break

print(count)

n, m = map(int, input().split())
con = {}
c = 0
for i in range(m):
    x, y = map(int, input().split())
    con[y] = x
#d = dict(sorted(con.items(), key=lambda kv: kv[1], reverse=True))
d = dict(sorted(con.items(), reverse=True))

for v, k in d.items():
    for i in range(k):
        c += v
        n -= 1
        if n == 0:
            print(c)
            exit()

n,m=[int(i) for i in input().split()]
l=sorted([int(i) for i in reversed(input().split())] for j in range(m))
s=0
for i in reversed(l):
    if i[1]>=n:
        print(s+n*i[0])
        break
    n-=i[1]
    s+=i[1]*i[0]

n,m=[int(i) for i in input().split()]
a=m*[0]
b=m*[0]
for i in range(m):
    a[i],b[i]=[int(i) for i in input().split()]
for i in range(1-1,m):
    bish=b[0]
    k=0
    for j in range(1,m-i):
        if b[j]>bish:
            bish=b[j]
            k=j
    a[k],a[m-1-i]=a[m-1-i],a[k]
    b[k],b[m-1-i]=b[m-1-i],b[k]
a.reverse()
b.reverse()
i=0
h=0
r=0
while i<m:
    if h+n<=n:
        r+=a[i]*b[i]
        h+=a[i]
        i+=1
    else:
        z=n-h
        r+=z*b[i]
        break
print(r)

n_m = [int(x) for x in input().split(' ')]
n = n_m[0]
m = n_m[1]
l1 = []
l2 = []
l3 = []
l4 = []
for i in range(m):
    a_b = [int(y) for y in input().split(' ')]
    l1.append(a_b[0])
    l2.append(a_b[1])
    l3.append(a_b[1])
l3.sort(reverse=True)
for i in l3:
    for j in range(len(l3)):
        if i == l2[j]:
            l4.append(l1[j])
matches = 0
for i in range(len(l2)):
    if n > l4[i]:
        matches += l3[i] * l4[i]
        n -= l4[i]
    else:
        matches += n * l3[i]
        n = 0
        break
print(matches)

n,m=map(int,input().split())
l=[]
for i in range(m):
    a,b=map(int,input().split())
    l.append([b,a])
l=sorted(l)
l=l[::-1]
c=s=0
for i in range (len(l)):
    if c<=n:
        s+=(l[i][0])*(min(n-c,(l[i][1])))
        c+=l[i][1]
        print(s,l[i][0])
    else:
        break
print(s)

n,m=map(int,input().split())
d={}

for i in range(m):
    a,b=map(int,input().split())
    if b in d:
        if a>d[b]:
            d[b]=a
    else:
        d[b]=a

ans=0
while(n>0):
    #print(d,ans)
    if  n==0 or m==0:
        break
    a=max(d)
    if n>d[a]:
        n-=d[a]
        ans+=a*d[a]
    elif n<d[a]:
        ans+=n*a
        n-=n
    elif n==d[a]:
        ans+=a*n
        n=0
    del d[a]
    #print(ans,n)
    m-=1
print(ans)

[n,m] = [int(x) for x in input().split()]
a=[]

for i in range (m) :
	a.append([int(x) for x in input().split()])

a = sorted(a , key = lambda x : x[1] ,reverse = True)
k = 0

print(a)
print(len(a))
while n > 0 and len(a) > 0:
	if n  >=  a[0][0] :
		k += a[0][0]*a[0][1]
		n -= a[0][0]
		a.pop(0)
	else :
		k += n *a[0][1]
		n = 0

print(k)

n,m=map(int, input().split())
d={}
for _ in range(m):
  v,k=map(int, input().split())
  d[k]=v
ans=0
for i in range(10,0,-1):
  if i in d:
    if d[i]<=n:
      n-=d[i]
      ans+=i*d[i]
    else:
      ans+=i*n
      n=0
print(ans)

import os

box_cont = input().rstrip().split()
n=int(box_cont[0])
m=int(box_cont[1])
hah=[0]*11
for i in range(m):
    a_b=input().rstrip().split()
    a=int(a_b[0])
    b=int(a_b[1])
    hah[b]=a
total=0
for i in range(10,0,-1):
    if hah[i]!= 0:
        if n-hah[i]>=0:
            total+=(hah[i]*i)
            n=n-hah[i]
        else:
            total+=i*n
            break
print(total)

n, m = input().split(" ")
n = int(n)
m = int(m)
row = {}
num = 0
som = 0
for i in range(m):
    x, y = input().split(" ")
    x = int(x)
    y = int(y)
    row[x] = y
    num += x
    som += x*y
if num <= n:
    print(som)
else:
    row = sorted(row.items(), key=lambda kv: kv[1],reverse=True)
    som = 0
    num = 0
    for elt, value in row:
        num += elt
        if num<=n:
            som += value*elt
        else:
            num -= elt
            while num <= n:
                som += value
                num += 1
            print(som-value)
            break

# Author: SaykaT
# Problem: 16B
# Time Created: July 21(Tuesday) 2020 || 12:16:23

#>-------------------------<#

# Helper Functions. -> Don't cluster your code.

# IO Functions. -> Input output
def io():
    n, m = map(int, input().split())
    containers = []
    for _ in range(m):
        a, b = map(int, input().split())
        containers.append([a, b])
    containers.sort(key=lambda x:x[1], reverse=True)
    return [n, m, containers]

# Main functions. -> Write the main solution here
def solve():
    n, m, containers = io()
    i = 0
    matches = 0
    while n != 0 or i < n:
        if n > containers[i][0]:
            matches += containers[i][0] * containers[i][1]
            n -= containers[i][0]
        else:
            matches += n * containers[i][i]

            n = 0
        i +=1
    print(matches)

# Multiple test cases. -> When you have T test cases.
solve()

import operator

total, containers = map(int, raw_input().split())
matchboxes = [map(int, raw_input().split()) for _ in range(containers)]
v_matchboxes = [(i, x[1]) for i, x in enumerate(matchboxes)]

v = []
count = 0
while count < total:
    m_index = max(v_matchboxes, key=operator.itemgetter(1))
    count += matchboxes[m_index[0]][0]
    v +=  [m_index[1]] * matchboxes[m_index[0]][0]
    v_matchboxes[m_index[0]] = (m_index[0], -1)

print sum(v[:total])

n, m = map(int, input().split(' '))
match = {}
bs = []
for i in range (m):
    a,b = map(int, input().split(' '))
    match[b] = a
    if b in bs:
        match[b] = match[b] + a
    else:
        bs.append(b)
sizes = list(match.keys())
sizes.sort(reverse=True)
boxes = []
for i in sizes:
    boxes.append(match[i])
left = n
ans = 0
for i in range (len(boxes)):

    if boxes[i] < left:

        left = left - boxes[i]
        ans = ans + boxes[i] * sizes[i]
    else:
        if boxes[i] == left:
            ans = ans + boxes[i]*sizes[i]
            break
        else:
            if boxes[i] > left:
                ans = ans + left*sizes[i]
                break
print(ans)

l=list(map(int,raw_input().split()))
n = l[0]
m = l[1]
a=[]
for i in range (m):
	x = list(map(int,raw_input().split()))
	a.append(x)
a.sort(key=lambda x:x[1],reverse=True)
count = 0
total = 0
for i in a:
	# while count<n and i[0]>0:
	# 	count+=1
	# 	total+=i[1]
	# 	i[0]-=1

	if i[0]<=n and count+i[0]<=n:
		count+=i[0]
		total+=i[1]*i[0]
	elif i[0]<n and count+i[0]>n:
		diff = n-count
		total+=i[1]*diff
		count+=diff

	elif i[0]>n:
		diff = n-i[0]
		total+=i[1]*diff
		count+=diff
	elif count<n and count+i[0]>=n:
		diff = n-count
		total+=i[1]*diff
		# print 'popp'
		break
	# print 'c',count
	# print 't',total

print total

s=input()
s=s.split(' ')

bag_size=int(s[0])

rows=int(s[1])

boxes_matches={}

sum=0

for i in range(rows):

    s=input()
    s=s.split(' ')

    num_boxes=int(s[0])
    matches=int(s[1])

    boxes_matches[num_boxes]=matches

boxes_matches=sorted(boxes_matches.items(),key=lambda t:t[1], reverse=True)

for key,val in boxes_matches:

    if key <= bag_size:

        bag_size-=key
        sum+=(key*val)

    else:

        sum+=(bag_size*val)
        bag_size=0

    if bag_size == 0:
        break

print(sum)

#sorted(data.values())

n , m = map(int,input().split())
l=[]
summ = 0

for i in range(m):
    x ,y = map(int,input().split())
    l.append([x,y])

l.sort(key = lambda y :y[0] ,reverse=True)

for i in range(m):
    a = l[i][0]
    b = l[i][1]
    if n >= a :
        summ += a*b
        n -= a

    elif n < a :
        summ += n*b
        break

print(summ)

n, m = list(map(int, input().split()))

match = {}

for _ in range(m):
    Ai, Bi = list(map(int, input().split()))

    match[Ai] = Bi

matchs = [(k, match[k]) for k in sorted(match, key=match.get, reverse=True)]

ans = 0
for k, v in matchs:
    if k <= n:
        ans += k * v
        n -= k
    elif n < k:
        ans += n * v
        break

print(ans)

n,m=map(int,input().split())
x=[]
y=[]
A=0
for i in range(m):
    a,b=map(int,input().split())
    x.append(a)
    y.append(b)
if sum(x)>n:
    while n>0:
        a=y.index(max(y))
        if x[a]>=n:
            A+=n*y[a]
            n=0
        else:
            A+=x[a]*y[a]
            n=n-x[a]
        x.remove(x[a])
        y.remove(y[a])
    print(A)
else:
    for i in range(m):
        A+=x[i]*y[i]
    print(A)

n,m=map(int,input().split())
z=0
l1=[]
l2=[]
while(z<m):
    a,b=map(int,input().split())
    l1.append(a)
    l2.append(b)
    z+=1
e=0
while(n>0):
    if(l1[l2.index(max(l2))]>n):
        e+=l2[l2.index(max(l2))]*n
        n=0
    else:
        e+=l2[l2.index(max(l2))]*l1[l2.index(max(l2))]
        n-=l1[l2.index(max(l2))]
        l2[l2.index(max(l2))]=-1
print(e)

n, m = map(int, input().split())
counter = {}
for _ in range(m):
    a, b = map(int, input().split())
    counter[b] = a

ans = 0
knapsack = {0: (0, 0)}
elm_sum = sum(counter.values())
while elm_sum > 0:
    for k, v in counter.items():
        for key in list(knapsack.keys()):
            if counter[k] > 0:
                val = knapsack[key][0] + k
                cnt = knapsack[key][1] + 1
                if key + k not in knapsack or (key+k in knapsack and knapsack[key+k][1] > cnt):
                    knapsack[key + k] = (val, cnt)

        counter[k] -= 1
        elm_sum -= 1

for k, v in knapsack.items():
    if v[1] <= n:
        ans = max(v[0], ans)

print(ans)

n,m=map(int,input().split())
a=[]
for i in range(m):
    x=list(map(int,input().split()))
    x.reverse()
    a.append(x)
s=0;y=0
for i in a:
    d=max(a)
    if d[1]>=n:
        s=s+(n)*d[0]
        break
    n-=d[1]
    s+=d[1]*d[0]
    a.remove(d)
print(s)

n,m=input().split(" ")
n=int(n)
m=int(m)
arr=[]
for i in range(0,m):
  arr.append(list(map(int,input().split(" "))))
sorted_arr=sorted(arr,key=lambda x:x[1],reverse=True)
#print(sorted_arr)
res=[]
remaining_capacity=n
i=0
while (remaining_capacity>0 and i<m):
  if(remaining_capacity>=n):
    res.append(sorted_arr[i][0])
    remaining_capacity-=sorted_arr[i][0]
    i+=1
  else:
    res.append(remaining_capacity)
    remaining_capacity=0
  #print(remaining_capacity)
product=0
#print(res)
for i in range(0,len(res)):
  product+=res[i]*sorted_arr[i][1];
print(product)

n,m=[int(i) for i in input().split()]
a=m*[0]
b=m*[0]
for i in range(m):
    a[i],b[i]=[int(i) for i in input().split()]
for i in range(1-1,m):
    bish=b[0]
    k=0
    for j in range(1,m-i):
        if b[j]>bish:
            bish=b[j]
            k=j
    a[k],a[m-1-i]=a[m-1-i],a[k]
    b[k],b[m-1-i]=b[m-1-i],b[k]
a.reverse()
b.reverse()
i=0
h=0
r=0
while i<m:
    if h+n<=n:
        r+=a[i]*b[i]
        h+=a[i]
        i+=1
    else:
        z=n-h
        r+=z*b[i]
        break
print(r)

n , m = map(int , input().split())
c= {}
for _ in range(m):
	a , b = map(int , input().split())
	c[b] = a
c = sorted(c.items() , key=lambda x:x[0] , reverse = True)
M = 0
for i , t in c:
	if n <= 0:
		break
	s = min(n , t)
	M += s*i
	n = n-t
print(M)

#!/usr/bin/python3.1
s = raw_input().split(" ")
n, m = int(s[0]), int(s[1])
a = []
res = 0
for i in range(0, m):
  x = list(map(lambda x: int(x), raw_input().split(" ")))
  a.append(x)
a.sort(key=lambda x: -x[0])
for q in a:
  if n == 0: break;
  t = min(n, q[0])
  res += q[1] * t
  n -= t
print(res)

n,m = map(int,input().split())
A = []
for i in range(m):
    a,b = map(int,input().split())
    A.append([b,a])
A.sort()
#print(A)
i=m-1
ans=0
while(n>0):
    ans+=A[i][0]*min(n,A[i][1])
    n-=A[i][1]
    i-=1
print(ans)

n, m = input().split()
n, m = int(n), int(m)
a = []
b = []
SUM = 0
for i in range(m):
    ai, bi = input().split()
    ai, bi = int(ai), int(bi)
    a.append(ai)
    b.append(bi)
for i in range(m):
    maxim = 0
    ind = 0
    for j in range(m-i):
        if maxim < b[j]:
            maxim = b[j]
            ind = j
    if n >= a[ind]:
        n-=a[ind]
        SUM+=a[ind]*b[ind]
        a.remove(a[ind])
        b.remove(b[ind])
    else:
        SUM+=n*b[ind]
        n = 0
        b.remove(b[ind])
print(SUM)

k,n=map(int,input().split())
d={}
d1=[]
res=0
for i in range(n):
    a,b=map(int,input().split())
    if b in d:
        d[b]+=a
    else:
        d[b]=a
    d1.append(b)
d1.sort()
d1.reverse()
# print(d)
# print(d1)
for i in d1:
    if k>=d[i]:
        res+=(d[i]*i)
        k-=d[i]
    else:
        res+=(k*i)
        k-=d[i]
    # print(res)
    if k<=0:
        break
print(res)

n,m = map(int,input().split())
d = {}
for i in range(m):
    a,b = map(int,input().split())
    d[a] = b
d = sorted(d.items(),reverse = True)
count = 0
# print(d)
for i in d:
    if n>0:
        if n-i[0] >0:
            count +=(i[0])*i[1]
            n -=i[0]
        else:
            count += (n)*i[1]
            n = 0
print(count)

n,m=list(map(int,input().split()))
d,l={},[]
for i in range(m):
	a,b=list(map(int,input().split()))
	d[b]=a
	l.append(b)
l.sort()
l.reverse()
c,t=0,0
for i in l:
	c+=d[i]
	if c<n:
		t+=i*d[i]
	else:
		t+=i*(d[i]-(c-n))
		break
print(t)

#!/usr/bin/env pypy
from __future__ import division, print_function
from collections import defaultdict, Counter, deque
from future_builtins import ascii, filter, hex, map, oct, zip
from itertools import imap as map, izip as zip, permutations, combinations, combinations_with_replacement
from __builtin__ import xrange as range
from math import ceil, factorial, log,tan,pi,cos,sin,radians
from _continuation import continulet
from cStringIO import StringIO
from io import IOBase
import __pypy__
from bisect import bisect, insort, bisect_left, bisect_right
from fractions import Fraction
from functools import reduce
import string
import sys
import os
import re
inf = float('inf')
mod_ = int(1e9) + 7
mod = 998244353

def factors(n):
    from functools import reduce
    return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

def sieve(m):
    n=1
    primes = {}
    arr=set([])
    for i in range(2, int(m ** 0.5) + 1):
        a = n // i
        b = m // i
        for k in range(max(2, a), b + 1):
            c = i * k
            primes[c] = 1

    for i in range(max(n, 2), m + 1):
        if i not in primes:
            arr.add(i)

    return arr

class DisjointSetUnion:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n
        self.num_sets = n

    def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a, b):
        a, b = self.find(a), self.find(b)
        if a != b:
            if self.size[a] < self.size[b]:
                a, b = b, a

            self.num_sets -= 1
            self.parent[b] = a
            self.size[a] += self.size[b]

    def set_size(self, a):
        return self.size[self.find(a)]

    def __len__(self):
        return self.num_sets

def main():
    n,m=map(int,input().split())
    arr=[]
    mat=[]
    cnt=0
    for i in range(m):
        a,b=map(int,input().split())
        arr.append(a)
        mat.append(b)
    mat.sort(reverse=True)
    arr.sort(reverse=True)
    s=0
    cnt=0
    for i in range(m):
        if s>=n:
            break
        cnt+=min(n-s,arr[i])*mat[i]
        s+=arr[i]
    print(cnt)

# region fastio

BUFSIZE = 8192

class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()

class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()

def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

line1=input().split()
n=int(line1[0])
m=int(line1[1])
a=[]
while m>0:
    new=input().split()
    new.reverse()
    # new.sort()
    a.append(list(map(int,new)))
    a.sort()
    m -=1
test=dict(a)
res=0
for i in range(0,len(test)):
    if n <=0:
        break
    num_of_matchbox=max(test)
    number_of_box=int(test[num_of_matchbox])
    if number_of_box >=n:
        res +=n*num_of_matchbox
        n -=n
        test.pop(max(test))
    elif n>=number_of_box:
       res += num_of_matchbox*number_of_box
       n -=number_of_box
       test.pop(max(test))
print(res)

[n,m] = [int(x) for x in input().split()]
a=[]

for i in range (m) :
	a.append([int(x) for x in input().split()])

a = sorted(a , key = lambda x : x[1] ,reverse = True)
k = 0

print(a)
print(len(a))
while n > 0 and len(a) > 0:
	if n  >=  a[0][0] :
		k += a[0][0]*a[0][1]
		n -= a[0][0]
		a.pop(0)
	else :
		k += n *a[0][1]
		n = 0

print(k)

n,m=map(int,input().split())
dict={}
ar=[]
br=[]
ans=0
for _ in range(m):
    a,b=map(int,input().split())
    ar.append(a)
    br.append(b)

for i in range(len(br)):
    for  j in range(len(br)):
        if br[j]<br[i]:
            br[i],br[j]=br[j],br[i]
            ar[i],ar[j]=ar[j],ar[i]

ans=0
a=0
co=0
mat=0
le=len(br)-1
while(ans<=n):
    if ans+ar[a]>n:
        break
    else:

        ans=ans+ar[a]
    mat=mat+ar[a]*br[a]
    #print(ans,mat)

    a+=1

if ans<n :
    mat+=(n-ans)*br[a]
    ans+=(n-ans)
print(ans)

n , m = map(int,input().split())
l=[]
summ = 0

for i in range(m):
    x ,y = map(int,input().split())
    l.append([x,y])

l.sort(key = lambda y :y[0] ,reverse=True)

for i in range(m):
    a = l[i][0]
    b = l[i][1]
    if n >= a :
        summ += a*b
        n -= a

    elif n < a :
        summ += n*b
        break

print(summ)

n,m = list(map(int,input().split()))
ref = {}
mat = 0
for x in range(m):
    a,b = input().split()
    ref[int(b)] = int(a)

for y in sorted(ref.keys())[::-1]:
    if n <= 0:
        break
    else:
        if n - ref[y] >= 0:
            n -= ref[y]
            mat += y*ref[y]

        else:
            mat += n*y
            n = 0
print (mat)

n,m=map(int,input().split())
dict={}
ar=[]
br=[]
ans=0
for _ in range(m):
    a,b=map(int,input().split())
    ar.append(a)
    br.append(b)

for i in range(len(br)):
    for  j in range(len(br)):
        if br[j]<br[i]:
            br[i],br[j]=br[j],br[i]
            ar[i],ar[j]=ar[j],ar[i]

ans=0
a=0
co=0
mat=0
le=len(br)-1
while(ans<=n):
    if ans+ar[a]>=n:
        break
    else:

        ans=ans+ar[a]
    mat=mat+ar[a]*br[a]
    #print(ans,mat)

    if ans>=sum(br):
        break
    a+=1
if ans<n :
    mat+=(n-ans)*br[a]
    ans+=(n-ans)
print(mat)

import sys

def main(n, containers):
    containers.sort(key=lambda x: x[1], reverse=True)
    s = 0
    for e, cont in enumerate(containers):
        num, per_num = cont
        if num < n:
            n -= num
            s += num * per_num
        else:
            # Can't take all, but can take sum.
            s += n * per_num

            # No more n left.
            break
    print(s)

if __name__ == "__main__":
    cases = []
    for e, line in enumerate(sys.stdin.readlines()):
        if e == 0:
            n, _ = map(int, line.strip().split())
        cases.append(list(map(int, line.strip().split())))
    main(n, cases)

def funct(l):
    x = 0
    t = 0
    for i in range(len(l)):
        if x < int(l[i][1]):
            x = int(l[i][1])
            t = i
    return t

temp = input().split(' ')
n = int(temp[0])
m = int(temp[1])
l = []
for i in range(m):
    temp = input().split(' ')
    l.append([temp[0], temp[1]])
print(l)
s = 0
while(n > 0):
    n -= 1
    ind = funct(l)
    if(ind < len(l)):
        s += int(l[ind][1])
        l[ind][0] = str(int(l[ind][0])-1)
        if l[ind][0] == '0':
            del l[ind]
print(s)