Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     for i in range(m):
          b,c = map(int,input().split())
          if c>min_a:
               a += [c]*b
     a.sort()
     print(sum(a[len(a)-n:]))
 
n,m = map(int,input().split())
f(n,m)

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     for i in range(m):
          b,c = map(int,input().split())
          if c>min_a:
               a += [c]*b
     a.sort()
     print(sum(a[len(a)-n:]))
 
n,m = map(int,input().split())
f(n,m)

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(reverse=True,key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[0][0]:
               ans += a[0][0]*a[0][1]
               n -= a[0][0]
               a.pop(0)
          else:
               ans += a[0][1]*n
               break
     print(ans)

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

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     for i in range(m):
          b_c = tuple(map(int,input().split()))
          if b_c[1]>min_a:
               a.append(tuple(b_c))
     a.sort(reverse=True,key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[0][0]:
               ans += a[0][0]*a[0][1]
               n -= a[0][0]
               a.pop(0)
          else:
               ans += a[0][1]*n
               break
     print(ans)

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

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [[1,i] for i in a]
     for i in range(m):
          b_c = list(map(int,input().split()))
          if b_c[1]>min_a:
               a += [b_c]
     a.sort(reverse=True,key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[0][0]:
               ans += a[0][0]*a[0][1]
               n -= a[0][0]
               a.pop(0)
          else:
               ans += a[0][1]*n
               break
     print(ans)

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

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     for i in range(m):
          b,c = map(int,input().split())
          if c>min_a:
               a += [c]*b
     a.sort(reverse=True)
     print(sum(a[:n]))

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

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     for i in range(m):
          b,c = map(int,input().split())
          if c>min_a:
               a += [c]*b
     a.sort(reverse=True)
     print(sum(a[:n]))

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

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f():
     n,m = map(int,input().split())
     a = list(map(int,input().split()))
     for i in range(m):
          b,c = map(int,input().split())
          a += [c]*b
     ans = 0
     for i in range(n):
          s = max(a)
          ans += s
          a.remove(s)
     print(ans)

f()

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
def f():
     n,m = map(int,input().split())
     a = list(map(int,input().split()))
     for i in range(m):
          b,c = map(int,input().split())
          a += [c]*b
     a.sort(reverse=True)
     print(sum(a[:n]))

f()

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = list(map(int,input().split()))
for i in range(m):
     b,c = map(int,input().split())
     a += [c]*b
a.sort(reverse=True)
print(sum(a[:n]))

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b_c = [list(map(int,input().split())) for i in range(m)]
b_c.sort(key=lambda x:x[1])
for i_j in b_c:
     b,c = i_j[0],i_j[1]
     for i in range(len(a)):
          if b == 0 or a[i] > c:
               break
          if a[i] < c:
               a[i] = c
               b -= 1
     a.sort()
print(sum(a))

Fixed submission:
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

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