Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import math
import bisect
import copy

n = int(input())

list1 = list(map(int,input().split()))
list1.sort()
cout = 0
m,k = 0,0
while True:
	if m >= n-2:
		break
	o = list1.pop(0)
	list1_c = copy.deepcopy(list1) #ここでjのためのdeepcopyを更新
	while True:
		if k >= len(list1)-1:
			break
		p = list1_c.pop(0)

		a = abs(o-p) +1
		b = o+p -1 #<= ではなく<条件に揃える

		c = bisect.bisect_left(list1_c,a)
		d = bisect.bisect_right(list1_c,b)

		if c < d:
			cout += d-c
		k+=1

	k = 0
	m += 1

print(cout)

### FIXED CODE ###

import bisect
import copy

n = int(input())
list1 = list(map(int,input().split()))
list1.sort()

cout = 0
m,k = 0,0

for i in range(n-2):
	o = list1.pop(0)
	for j in range(n-i-1):
		p = list1[j]

		a = abs(o-p) +1
		b = o+p -1 #<= ではなく<条件に揃える

		d = bisect.bisect_right(list1[j+1:],b)
		
		cout += d

print(cout)
### BUGGY CODE ###

import copy


def make_divisors(n):
    lower_divisors, upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]


N = int(input())
A = list(map(int, input().split()))

setA = set(A)
result = 0
for i in range(N):
    divisors = make_divisors(A[i])

    B = copy.copy(A)
    B[i] = 0
    setA = set(B)

    for d in divisors:
        if d in setA:
            break
    else:
        result += 1

print(result)


### FIXED CODE ###

from collections import Counter
N = int(input())
A = list(map(int, input().split()))

sorted_A = sorted(A)
result = 0
c = Counter(sorted_A)
# 倍数を管理するテーブル
lendp = sorted_A[-1] + 1
dp = [0] * lendp

for a in sorted_A:
    dp[a] += 1
    if dp[a] == 1:
        b = a * 2
        while b < lendp:
            dp[b] += 2
            b += a

print(dp.count(1))
### BUGGY CODE ###

R, G, B, N = map(int, input().split())
count = 0
for i in range(N//R+1):
    for j in range(N//G+1):
        if (N - R*i - G*j) % B == 0 and (N-R*i-G*j)>=0:
            count += 1
print(count)


### FIXED CODE ###

R, G,B,N = map(int, input().split())
l = [R,G,B]
l = sorted(l)
ma = l[2]
mi = l[0]
mid = l[1]
id = N // ma
count = 0
for i in range(id+1):
    res = N - ma * i
    if res >= mid:
        for j in range(res//mid+1):
            if res % mi == 0:
                count += 1
            res -= mid
    else:
        if res % mi == 0:
            count +=1
print(count)
### BUGGY CODE ###

N, M, K = map(int, input().split())
F = [[] for _ in range(N)]
B = [[] for _ in range(N)]
 
# フレンドの隣接リスト
for _ in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    F[a].append(b)
    F[b].append(a)
 
# ブロックの隣接リスト
for _ in range(K):
    c, d = map(int, input().split())
    c, d = c - 1, d - 1
    B[c].append(d)
    B[d].append(c)
 
 
# 交友関係グループ（辞書型）
D = {}
# グループの親
parent = [-1] * N
# 訪問管理
visited = [False] * N
 
for root in range(N):
    if visited[root]:
        continue
 
    D[root] = set([root])
    # 訪問先のスタック
    stack = [root]
    # 訪問先が無くなるまで
    while stack:
        # 訪問者をポップアップ
        n = stack.pop()
        if visited[n]:
            continue
        # 訪問者を訪問済み
        visited[n] = True
        # 訪問者のグループの親を設定
        parent[n] = root
 
        # root のフレンドをグループと訪問先に追加
        D[root]=D[root] | set(F[n])
        stack.extend(F[n])
 
ans = [0] * N
for iam in range(N):
    # 自分の交友関係グループを設定
    group = D[parent[iam]]
    # グループからフレンドと自分を除く
    tmp_ans = len(group) - len(F[iam]) - 1
    # グループからブロックを除く
    for block in B[iam]:
        if block in group:
            tmp_ans -= 1
    ans[iam] = tmp_ans
 
print(*ans, sep=' ')

### FIXED CODE ###

from collections import deque

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

cango = [[]for i in range(n)]
cannotgo=[[]for i in range(n)]
for i in range(m):
    a,b = (map(int,input().split()))
    a,b = a-1,b-1
    cango[a].append(b)
    cango[b].append(a)

for i in range(k):
    c,d =(map(int,input().split()))
    c,d = c-1,d-1
    cannotgo[c].append(d)
    cannotgo[d].append(c)
# print(a)
# cango = [[]for i in range(n)]
# for i in a:
#     cango[i[0]-1].append(i[1]-1)
#     cango[i[1]-1].append(i[0]-1)
# print(cango)

# cannotgo=[[]for i in range(n)]
# for i in c:
#     cannotgo[i[0]-1].append(i[1]-1)
#     cannotgo[i[1]-1].append(i[0]-1)
# # print(cannotgo)
alre = [False]*n
parent = [-1]*n
gro = {}
y = deque()
# print(gro)
for i in range(n):
    # if alre[i]==True:
    #     continue
    # gro[i]=set([i])
    y.append(i)
    leader = i
    cnt = 0 
    while len(y)>0:
        x = y.popleft()
        if parent[x]!=-1:
            continue
            # print(to)
        parent[x]=leader
        y.extend(cango[x])
        cnt +=1
    if cnt!=0:
        gro[leader]=cnt
# print(gro)
# print(parent)
ans = [0]*n
# print(ans)
for iam in range(n):
    # group = gro[parent[iam]]
    tmp_ans = gro[parent[iam]]
    tmp_ans -=1
    tmp_ans -= len(cango[iam])
    for block in cannotgo[iam]:
        if parent[block]==parent[iam] :
            tmp_ans -=1
    ans[iam]=tmp_ans
print(*ans, sep=' ')
### BUGGY CODE ###

def abs_min(current, move):
    f = current+move
    b = current-move
    if abs(f) <= abs(b):
        return f
    else:
        return b
    
nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]

for i in range(num):
    current = abs_min(current, move)

print(abs(current))


### FIXED CODE ###

nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]


current = abs(current)

res = current // move

if res >= num:
    print(current - move*num)
else:
    current = current - move * res
    num -= res
    if num%2 == 0:
        print(current)
    else:
        print(abs(current-move))
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
sum_num=sum(a)
mylist=[]
for i in range(1,10**5+1):
    mylist.append(a.count(i))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    sum_num+=(c-b)*mylist[b-1]
    print(sum_num)
    mylist[c-1]+=mylist[b-1]
    mylist[b-1]=0

### FIXED CODE ###

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
sum_num=sum(a)
cnt=Counter(a)
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    sum_num+=(c-b)*cnt[b]
    print(sum_num)
    cnt[c]+=cnt[b]
    cnt[b]=0
### BUGGY CODE ###

from scipy.misc import comb


def main():
    n, m = map(int, input().split())
    MOD = 10 ** 9 + 7

    m0 = m
    primes = {}
    p = 2
    # prime factorization
    while m0 > 1:
        if m0 % p == 0:
            m0 /= p
            if p in primes:
                primes[p] += 1
            else:
                primes[p] = 1
        else:
            p += 1

    ans = 1
    for _, v in primes.items():
        ans *= comb(n - 1 + v, v, exact=True)
        ans %= MOD
    print(ans)


main()


### FIXED CODE ###

from operator import mul
from functools import reduce


def cmb(n, r):
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


def main():
    n, m = map(int, input().split())
    MOD = 10 ** 9 + 7

    m0 = m
    primes = {}
    p = 2
    rtm = int(m ** 0.5 + 1)
    # prime factorization
    while m0 > 1:
        if m0 % p == 0:
            m0 /= p
            if p in primes:
                primes[p] += 1
            else:
                primes[p] = 1
        else:
            p += 1
        # m is prime
        if rtm <= p:
            primes[1] = 1
            break

    ans = 1
    for _, v in primes.items():
        ans *= cmb(n - 1 + v, v)
        ans %= MOD
    print(ans)


main()
### BUGGY CODE ###

N, K = map(int, input().split())

saitei = N
for i in range(N//K+2):
    N = abs(N-K)
    saitei = min(saitei, N)

print(saitei)

### FIXED CODE ###

N, K = map(int, input().split())

a = N//K
b = N%K
c = abs(b-K) 

print(min(b, c))
### BUGGY CODE ###

N,K = map(int,input().split())
H = tuple(map(int,input().split()))

dp = [0]

for i in range(1,N):
  h = H[i]
  dp.append(min(dp[j]+abs(H[j]-h) for j in range(max(i-K,0),i)))

print(dp[-1])


### FIXED CODE ###

N,K = map(int,input().split())
H = tuple(map(int,input().split()))

dp = [0]

for i in range(1,N):
  h = H[i]
  dp.append(min(dp[j]+abs(H[j]-h) for j in range(max(i-K,0),i)))

print(dp[-1])
### BUGGY CODE ###

N = int(input())
a = [int(input()) for _ in range(N)]

for i in range(N):
  _a = a[:i] + a[i+1:]
  print(max(_a))
  

### FIXED CODE ###

N = int(input())
a = [int(input()) for _ in range(N)]
first = max(a)
f_id = a.index(max(a))
second = max(a[:f_id] + a[f_id+1:])

for i in range(N):
  if a[i] == first:
    print(second)
  else:
    print(first)
### BUGGY CODE ###

S = str(input())
k=int(0)

while S.find('ABC')>=0:
#    S[S.find('ABC'):S.find('ABC')+3] = "BCA"
    S = S[:S.rfind('ABC')] + 'BCA' + S[(S.rfind('ABC')+3):]
    k +=1

ans = (int(k))

print(ans)

### FIXED CODE ###

s = input()
ans = 0
cntA = 0
for i in range(0, len(s) - 1):
    if s[i] == 'A':
        cntA += 1
    elif s[i: i + 2] == "BC":
        ans += cntA
    elif i > 0 and s[i - 1: i + 1] == "BC":
        continue
    else:
        cntA = 0
print(ans)
### BUGGY CODE ###

n = int(input())
alist = list(map(int,input().split(' ')))

from collections import defaultdict

adict = defaultdict(int)

resdict = {}

for a in alist:
    adict[a] += 1

for a in alist:
    if a in resdict:
        # print('used')
        print(resdict[a])
        continue
    # print('not used')
    adict[a] -= 1
    temp = 0
    for k,v in adict.items():
        # print(k, v)
        if v >= 2:
            temp += v*(v-1)//2
    print(temp)
    resdict[a] = temp
    adict[a] += 1

### FIXED CODE ###

n = int(input())
alist = list(map(int,input().split(' ')))

from collections import defaultdict

resdict = {}

from collections import Counter

c = Counter(alist)

cdict = dict(c)

# cdict = dict((key, value) for (key, value) in old_cdict.items() if value >= 2)

# print(cdict)

maxi = 0

for k, v in cdict.items():
    maxi += v * (v - 1) // 2

for a in alist:
    num = cdict[a]
    if num in resdict:
        # print('used')
        print(resdict[num])
        continue
    # print('not used')
    if a in cdict:
        cdict[a] -= 1
        temp = 0
        for k,v in cdict.items():
            temp += v*(v-1)//2
        print(temp)
        resdict[num] = temp
        cdict[a] += 1
    else:
        print(maxi)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
import heapq


def solve():
    n, m = MI()

    AB = []
    for i in range(n):
        a, b = MI()
        AB.append((a, b))

    AB = sorted(AB, key=lambda x: x[0])
    # print(AB)

    hp = [-AB[0][1]]
    heapq.heapify(hp)
    pre = AB[0][0]
    memo = {}
    for i in range(1, n):
        a, b = AB[i]
        if a == pre:
            heapq.heappush(hp, -b)
        else:
            memo[pre] = hp
            hp = [-b]
            heapq.heapify(hp)
            pre = a
    memo[pre] = hp

    # print(memo)

    ans = 0
    # for i in sorted(memo.keys()):
    for i in range(1, m+1):
        hp = memo.get(i, [])
        # print(i, hp)
        if len(hp) > 0:
            ans += - heapq.heappop(hp)

        hp_next = memo.get(i + 1, []) + hp
        heapq.heapify(hp_next)
        memo[i+1] = hp_next
    print(ans)




if __name__ == '__main__':
    solve()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
from heapq import heapify, heappush, heappop


def solve():
    n, m = MI()

    jobs = [[] for _ in range(m)]
    for i in range(n):
        a, b = MI()
        if m-a >= 0:
            jobs[m-a].append(b)

    # print(jobs)
    hp = []
    heapify(hp)
    ans = 0
    for i in range(m-1, -1, -1):
        js = jobs[i]
        for j in js:
            heappush(hp, -j)

        if len(hp) > 0:
            ans += -heappop(hp)
    print(ans)





if __name__ == '__main__':
    solve()
### BUGGY CODE ###

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

an = []
for i in range(1,n+1):
    print(a.count(i))

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
al = [0 for i in range(n)]

for i in a:
    al[i-1] += 1
for i in range(n):
    print(al[i])
### BUGGY CODE ###

import heapq
x, y, z, k = map(int, input().split())

a = sorted(list(map(int, input().split())), reverse=True)
b = sorted(list(map(int, input().split())), reverse=True)
c = sorted(list(map(int, input().split())), reverse=True)

abc = []
for s in a:
    for t in b:
        for u in c:
            abc.append(-s-t-u)
heapq.heapify(abc)
for i in range(k):
    print(-heapq.heappop(abc))

### FIXED CODE ###

import heapq
import sys
x, y, z, k = map(int, input().split())

a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))

abc = {(0,0,0)}

stu = [[-a[0]-b[0]-c[0], 0,0,0]]
heapq.heapify(stu)
result = []
while k > 0:
    out_p, n, m, l = heapq.heappop(stu)
    if n+1<x and not((n+1,m,l) in abc):
        abc.add((n+1,m,l))
        heapq.heappush(stu, [-a[n+1]-b[m]-c[l], n+1, m, l])
    if m+1<y and not((n,m+1,l) in abc):
        abc.add((n,m+1,l))
        heapq.heappush(stu, [-a[n]-b[m+1]-c[l], n, m+1, l])
    if l+1<z and not((n,m,l+1) in abc):
        abc.add((n,m,l+1))
        heapq.heappush(stu, [-a[n]-b[m]-c[l+1], n, m, l+1])
    result.append(-out_p)
    k -= 1
result.sort(reverse=True)
for i in result:
    print(i)
### BUGGY CODE ###

import functools
import operator

n = int(input())
a = [int(i) for i in input().split(' ')]
if a[0] == 0:
    print(0)
else:
    x = functools.reduce(operator.mul, a)
    if x > 10**18:
        print(-1)
    else:
        print(x)


### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split(' ')]
a = sorted(a)
if a[0] == 0:
    print(0)
else:
    x = 1
    for i in a:
        x = x * i
        if x > 10**18:
            print(-1)
            break
    else:
        print(x)
### BUGGY CODE ###

N = int(input())
a = list(map(int, input().split()))
b = [0]*N

for i in range(N - 1, -1, -1):
    if i > N // 2:
        b[i] = a[i]
    else:
        si = 0
        for j in range(i + 1, N):
            if j % (i+1) == 0:
              si += b[j]
        if si % 2 == a[i]:
            b[i] = 0
        else:
            b[i] = 1

if a == b:
    print(sum(b))
    for i in range(N):
        if b[i] == 1:
            print(i+1 ,end=' ')
else:
    print(-1)    

### FIXED CODE ###

N = int(input())
a = list(map(int, input().split()))
b = [0]*N

for i in range(N, 0, -1):
    if i > N // 2:
        b[i-1] = a[i-1]
    else:
        si = 0
        for j in range(i+i, N+1, i):
            si += b[j-1]
        if si % 2 == a[i-1]:
            b[i-1] = 0
        else:
            b[i-1] = 1

print(sum(b))
for i in range(N):
    if b[i] == 1:
        print(i+1 ,end=' ')
### BUGGY CODE ###

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

s = input()

lr = []
for i in range(q):
    l, r = map(int, input().split())
    l -= 1
    lr.append([l, r])

for l, r in lr:
    substr = s[l: r]
    print(substr.count('AC'))

### FIXED CODE ###

n, q = map(int, input().split())
s = input()
dp = [0 for _ in range(len(s))]

for i in range(1, len(s)):
    if(s[i-1: i+1] == 'AC'): dp[i] = dp[i-1] + 1
    else: dp[i] = dp[i-1]

for _ in range(q):
    l, r = map(int, input().split())
    print(dp[r-1] - dp[l-1])
### BUGGY CODE ###

# https://atcoder.jp/contests/abc051/tasks/abc051_b

# n n,
K, S = input().split()

# initialize
ans = 0

# logic: 全探索: 基本形
for i in range(int(K) + 1):
    if i + int(K) + 1 + int(K) + 1 < int(S):
        continue
    for j in range(int(K) + 1):
        if j + int(K) + 1 + int(K) + 1 < int(S):
            continue
        for k in range(int(K) + 1):
            xyz = i + j + k
            if xyz == int(S):
                ans += 1

print(ans)


### FIXED CODE ###

k,s=map(int,input().split())
count=0
for a in range(k+1):
  for b in range(k+1):
    if 0<=s-a-b<=k:
      count+=1
print(count)
### BUGGY CODE ###

s = list(input())
n = len(s) + 1
al, ar = [0] * n, [0] * n

for i in range(len(s)):
    if s[i] == "<":
        al[i+1] = al[i] + 1

for i in range(len(s)):
    if s[-(i+1)] == ">":
        ar[i+1] = ar[i] + 1

ans = 0
for i in range(len(al)):
    if al[i] >= ar[::-1][i]:
        ans += al[i]
    else:
        ans += ar[::-1][i]

print(ans)

### FIXED CODE ###

s = list(input())
n = len(s) + 1
al, ar = [0] * n, [0] * n

for i in range(len(s)):
    if s[i] == "<":
        al[i+1] = al[i] + 1

for i in range(len(s)):
    if s[-(i+1)] == ">":
        ar[i+1] = ar[i] + 1

ans = 0
ar = ar[::-1]

for i in range(len(al)):
    if al[i] >= ar[i]:
        ans += al[i]
    else:
        ans += ar[i]

print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n,m,q = map(int,input().split())
c=[[0 for i in range(n+1)] for j in range(n+1)]
x=[[0 for i in range(n+1)] for j in range(n+1)]
for i in range(m):
    l,r = map(int,input().split())
    x[l][r] += 1
for i in range(1,n+1):
    for j in range(1,n+1):
        c[i][j] = c[i][j-1] + x[i][j]
for i in range(q):
    p,q = map(int,input().split())
    sum = 0
    for j in range(p,q+1):
         sum+= c[j][q] - c[j][p-1]
    print(sum)


### FIXED CODE ###

import sys
input = sys.stdin.readline

n,m,q = map(int,input().split())
c=[[0 for i in range(n+1)] for j in range(n+1)]
x=[[0 for i in range(n+1)] for j in range(n+1)]
for i in range(m):
    l,r = map(int,input().split())
    x[l][r] += 1
for i in range(1,n+1):
    for j in range(1,n+1):
        c[i][j] = c[i][j-1] + x[i][j]
for i in range(q):
    p,q = map(int,input().split())
    sum = 0
    for j in range(p,q+1):
         sum+= c[j][q] - c[j][p-1]
    print(sum)
### BUGGY CODE ###

n, k = map(int, input().split())
ps = list(map(int, input().split()))
cs = list(map(int, input().split()))
ps = [p-1 for p in ps]
visited = [False] * n
from collections import defaultdict
from collections import deque
ds = []
sums = []
for i in range(n):
    if visited[i]: continue
    q = deque([])
    q.append(i)
    s = cs[i]
    d = [i]
    while len(q) > 0:
        u = q.popleft()
        visited[u] = True
        nx = ps[u]
        if visited[nx]: continue
        d.append(nx)
        q.append(nx)
        s += cs[nx]
    sums.append(s)
    ds.append(d)

# print(ds,sums)
def calcmax(d,s,k,cs):
    cnt = 0
    l = len(d)
    div, rest_ = divmod(k-1, l)
    rest_ += 1
    if s > 0:
        cnt += div*s
    d2 = d*2
    mx = - 1<<50

    for rest in range(1,rest_+1):
        for i in range(rest,l*2):
            # print(i,d2[i-rest:i])
            c_ = [cs[j] for j in (d2[i-rest:i])]
            c = sum(c_)
            mx = max(mx, c)
    # print(d,cnt,mx)
    return cnt + mx

ans = -1 << 50
for d,s in zip(ds,sums):
    # print("aaa",d,s)
    c = calcmax(d,s,k,cs)
    ans = max(ans,c)
print(ans)

### FIXED CODE ###

n, k = map(int, input().split())
ps = list(map(int, input().split()))
cs = list(map(int, input().split()))
ps = [p-1 for p in ps]
visited = [False] * n
from collections import deque
ds = []
sums = []
for i in range(n):
    if visited[i]: continue
    q = deque([])
    q.append(i)
    s = cs[i]
    d = [i]
    while len(q) > 0:
        u = q.popleft()
        visited[u] = True
        nx = ps[u]
        if visited[nx]: continue
        d.append(nx)
        q.append(nx)
        s += cs[nx]
    sums.append(s)
    ds.append(d)

# print(ds,sums)
from itertools import accumulate
def calcmax(d,s,k,cs):
    cnt = 0
    l = len(d)
    div, rest_ = divmod(k-1, l)
    rest_ += 1
    if s > 0:
        cnt += div*s
    d2 = d*2
    c_ = [cs[j] for j in d2]
    acc = list(accumulate(c_))
    mx = - 1<<50
    if s < 0:
        for rest in range(1,l+1):
            for i in range(rest,l*2):
                c = acc[i] - acc[i-rest]
                mx = max(mx,c)
        return mx
    for rest in range(1,rest_+1):
        for i in range(rest,l*2):
            c = acc[i] - acc[i-rest]
            # print(i,rest,c,"debug")
            mx = max(mx, c)
    # print(d,cnt,mx)
    return cnt + mx

ans = -1 << 50
for d,s in zip(ds,sums):
    # print("aaa",d,s)
    c = calcmax(d,s,k,cs)
    ans = max(ans,c)
print(ans)
### BUGGY CODE ###

N, M = [int(i) for i in input().split()]
Groups = [[i] for i in range(N)]
dsts = [-1 for i in range(N)]
ABs = [[int(i)-1 for i in input().split()] for i in range(M)]
for A, B in ABs:
    dst = min(A, B)
    src = max(A, B)
    # print('A', A, B, dst, src)
    while(dsts[dst] != -1):
        dst = dsts[dst]
    while(dsts[src] != -1):
        src = dsts[src]
    if dst != min(A, B):
        omdst = min(A, B)
        while(dsts[omdst] != dst):
            n_omdst = dsts[omdst]
            dsts[omdst] = dst
            omdst = n_omdst
    if src != max(A, B):
        omsrc = max(A, B)
        while(dsts[omsrc] != src):
            n_omsrc = dsts[omsrc]
            dsts[omsrc] = src
            omsrc = n_omsrc

    if dst == src:
        continue
    dst2 = min(dst, src)
    src2 = max(dst, src)
    dsts[src2] = dst2
    Groups[dst2].extend(Groups[src2])
    Groups[src2] = []
    # print(Groups)
    # print(dsts)
ans = 0
for group in Groups:
    ans = max(ans, len(group))
print(ans)


### FIXED CODE ###

N, M = [int(i) for i in input().split()]
Groups = [1 for i in range(N)]
dsts = [-1 for i in range(N)]
ABs = [[int(i)-1 for i in input().split()] for i in range(M)]
for A, B in ABs:
    dst = min(A, B)
    src = max(A, B)
    # print('A', A, B, dst, src)
    while(dsts[dst] != -1):
        dst = dsts[dst]
    while(dsts[src] != -1):
        src = dsts[src]
    if dst != min(A, B):
        omdst = min(A, B)
        while(dsts[omdst] != dst):
            n_omdst = dsts[omdst]
            dsts[omdst] = dst
            omdst = n_omdst
    if src != max(A, B):
        omsrc = max(A, B)
        while(dsts[omsrc] != src):
            n_omsrc = dsts[omsrc]
            dsts[omsrc] = src
            omsrc = n_omsrc

    if dst == src:
        continue
    dst2 = min(dst, src)
    src2 = max(dst, src)
    dsts[src2] = dst2
    Groups[dst2] += Groups[src2]
    Groups[src2] = 0
    # print(Groups)
    # print(dsts)
print(max(Groups))
### BUGGY CODE ###

#D問題
N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(M):
    B, C = map(int, input().split())
    for j in range(B):
            if min(A) < C:
                A.remove(min(A))
                A.append(C)

print(sum(A))




### FIXED CODE ###

import heapq as hq
N, M = map(int, input().split())
A = list(map(int,input().split()))
A = [(-i,1) for i in A]
hq.heapify(A)
for i in range(M):
    B, C = map(int, input().split())
    hq.heappush(A,(-C,B))
sum_A = 0
for i in range(N):
    add_num = hq.heappop(A)
    sum_A += add_num[0]*-1
    if add_num[1] > 1:
        hq.heappush(A,(add_num[0],add_num[1]-1))
print(sum_A)