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

import sys

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

# 各文字が最初に出現するインデックス
posFirst = {}
for i, A in enumerate(As):
    if A not in posFirst:
        posFirst[A] = i + N

# 次に同じ文字が出現するインデックス
nexts = [-1] * N
posLast = {}
for i in reversed(range(N)):
    A = As[i]
    if A not in posLast:
        nexts[i] = posFirst[A]
    else:
        nexts[i] = posLast[A]
    posLast[A] = i

v0s = [-1] * N
v2s = [-1] * N
for i in range(N):
    if nexts[i] < N-1:
        v2s[i] = nexts[i] + 1
        v0s[nexts[i] + 1] = i
    else:
        v2s[i] = i

v9s = [-1] * N
for i in reversed(range(N)):
    if v9s[i] == -1:
        v9s[i] = v2s[i]
    v9s[v0s[i]] = v9s[i]

numLoop = 0
vNow = 0
while True:
    vNow = nexts[vNow]
    if vNow >= N:
        numLoop += 1
    vNow %= N
    if vNow == N-1:
        break
    vNow += 1

K %= (numLoop+1)

if K == 0:
    print()
    sys.exit()

numLoop = 0
vNow = 0
while True:
    vNow = nexts[vNow] + 1
    if vNow >= N:
        numLoop += 1
    vNow %= N
    if numLoop == K-1:
        break

ss = []
for v in range(vNow, N):
    X = As[v]
    if X not in ss:
        ss.append(X)
    else:
        s = ss.pop()
        while s != X:
            s = ss.pop()

print(' '.join(map(str, ss)))


### FIXED CODE ###

def solve():
    N, K = map(int, input().split())
    As = list(map(int, input().split()))

    def getDistNexts(As):
        lenA = len(As)
        poss = dict()
        for i, A in enumerate(As):
            if A not in poss:
                poss[A] = i + lenA

        distNexts = [0]*(lenA)
        for i in reversed(range(lenA)):
            distNexts[i] = poss[As[i]] - i
            poss[As[i]] = i
        return distNexts

    distNexts = getDistNexts(As)

    NK = N*K
    maxD = (NK).bit_length() - 1

    dp = [[0]*(N) for _ in range(maxD+1)]
    for i in range(N):
        dp[0][i] = distNexts[i] + 1
    for d in range(1, maxD+1):
        for i in range(N):
            i2 = i + dp[d-1][i]
            dp[d][i] = i2 + dp[d-1][i2%N] - i

    iNow = 0
    dist = 0
    for d in reversed(range(maxD+1)):
        if dist+dp[d][iNow] < NK:
            dist += dp[d][iNow]
            iNow = dist%N

    Ss = []
    setS = set()
    for A in As[iNow:]:
        if A in setS:
            S = ''
            while S != A:
                S = Ss.pop()
                setS.remove(S)
        else:
            Ss.append(A)
            setS.add(A)

    if Ss:
        print(' '.join(map(str, Ss)))


solve()
### BUGGY CODE ###

import math
from collections import defaultdict as dd

def count_divisor(x):
  if x == 1:
    return 1
  
  divided = x
  d = dd(int)
  for i in range(2, math.floor(math.sqrt(x))+1):
    while divided%i == 0:
      d[i] += 1
      divided /= i
  if divided != 1:
    d[int(divided)] += 1
  
  count = 1
  for v in d.values():
     count *= v+1
  return count

N = int(input())
ans = 0
for c in range(1, N):
  ans += count_divisor(N-c)
print(ans)

### FIXED CODE ###

N = int(input())
ans = 0
for a in range(1, N):
  ans += N//a
  if N%a == 0:
    ans -= 1
print(ans)
### BUGGY CODE ###

import numpy as np
k,s=map(int,input().split())
l=np.array(range(k+1))
count=0
for i in l:
    for j in l:
        for k in l:
            if i+j+k==s:
                count+=1
print(count)

### FIXED CODE ###

k,s=map(int,input().split())
l=list(range(k+1))
count=0
for i in l:
    for j in l:
        q=s-i-j
        if 0<=q<=k:
            count+=1
            
print(count)
### BUGGY CODE ###

from sys import stdin
# import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]


import math
from functools import reduce


def gcd_list(numbers):
    return reduce(math.gcd, numbers)


K = int(input())
ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            ans += gcd_list([a, b, c])
print(ans)


### FIXED CODE ###

from sys import stdin
# import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]

import math

K = int(input())
ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        d = math.gcd(a, b)
        for c in range(1, K+1):
            ans += math.gcd(d, c)
print(ans)
### BUGGY CODE ###

N = input()
P = list(map(int, input().split()))
count = 0

for i in range(int(N)):
    temp = 0
    P2 = P[:i+1]
    P3 = P[:i]
    if i == 0:
        if P[i]<=(min(P2)):
            count = count + 1
    else:
        if P[i]<=min(P3):
            count = count + 1
print(count)
            

### FIXED CODE ###

N = input()
P = list(map(int, input().split()))
count = 0

now_mini= 1<<20
for i in P:
    if i < now_mini:
        count = count + 1
        now_mini=i
print(count)
### BUGGY CODE ###

x,y,z,k = [int(num) for num in input().split()]
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True)[:k] for _ in range(3)]

sum_cake = sorted([a+b+c for a in a_cake for b in b_cake for c in c_cake],reverse=True)[:k]

for i in sum_cake:
    print(i)




### FIXED CODE ###

x,y,z,k = [int(num) for num in input().split()]
a_cake,b_cake,c_cake = [sorted([int(num) for num in input().split()],reverse=True)[:k] for _ in range(3)]

sum_cake=list()
for i,a in enumerate(a_cake,1):
    for j,b in enumerate(b_cake,1):
        if(i*j>k):
            break
        for n,c in enumerate(c_cake,1):
            if(i*j*n>k):
                break
            sum_cake.append(a+b+c)


sum_cake.sort(reverse=True)

for i in sum_cake[:k]:
    print(i)
### BUGGY CODE ###

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    S = readline().strip()

    dp = [[0] * (N + 1) for _ in range(N + 1)]
    for i in range(N - 1, -1, -1):
        for j in range(N - 1, -1, -1):
            if S[i] == S[j]:
                dp[i][j] = dp[i + 1][j + 1] + 1

    ans = 0
    for i in range(N):
        for j in range(N):
            if ans < min(dp[i][j], j - i):
                ans = min(dp[i][j], j - i)

    print(ans)
    return


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    S = readline().strip()

    ans = 0
    dp = [[0] * (N + 1) for _ in range(N + 1)]
    for i in range(N - 1, -1, -1):
        for j in range(N - 1, i - 1, -1):
            if S[i] == S[j]:
                dp[i][j] = dp[i + 1][j + 1] + 1
            if ans < min(j - i, dp[i][j]):
                ans = min(j - i, dp[i][j])

    print(ans)
    return


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

from collections import Counter
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
N = int(input())
A = input().split()
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        count = 0
        C[A[i]] -= 1
        for j in C.values():
            if j >= 2:
                count = count + cmb(j, 2)
        print(count)
        C[A[i]] += 1
banned_count(A)

### FIXED CODE ###

from collections import Counter
from scipy.misc import comb
N = int(input())
A = input().split()
C = Counter(A)
s = sum([comb(j, 2, exact=True) for j in C.values()])
for i in range(N):
    print(s-C[A[i]]+1)
### BUGGY CODE ###

import math

N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

for i in range(M):
    A[0] = A[0] // 2
    A.sort(reverse=True)

print(sum(A))

### FIXED CODE ###

# sys.stdin.readline()
import sys
import time
input = sys.stdin.readline
import math
import heapq

N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
A = [-i for i in A]
# print(A)
heapq.heapify(A)
# print(heapq.heappop(A))

# t1 = time.time()
for i in range(M):
    a = -heapq.heappop(A) // 2
    heapq.heappush(A, -a)
    # j = A.index(max(A))
    # A[j] = a
    # A[0] = A[0]//2
    # A.sort(reverse=True)
    # if sum(A) <1:
    #     break

print(-sum(A))
### BUGGY CODE ###

s = input()
q = int(input())
tot_1 = 0
for i in range(q):
    l = list(input().split())
    if l[0] == '1':
        tot_1 += 1
    elif l[0] == '2':
        if (tot_1 + int(l[1])) % 2 == 0:
            s = s + l[2]
        else:
            s = l[2] + s
if tot_1 % 2 == 1:
    s = s[::-1]
print(s)


### FIXED CODE ###

import sys
from collections import deque

readline = sys.stdin.readline
readlines = sys.stdin.readlines

s = readline().rstrip()
q = readline()
queries = readlines()
s = deque(s)
tot_1 = 0
for i in queries:
    if i[0] == '1':
        tot_1 += 1
        continue
    c = i[4]
    if (tot_1 + int(i[2])) % 2 == 0:
        s.append(c)
    else:
        s.appendleft(c)
ans = "".join(s) if tot_1 % 2 == 0 else "".join(reversed(s))
print(ans)
### BUGGY CODE ###

from collections import Counter
import itertools

def verify_anagrams(first_word, second_word):
    return Counter(first_word) == Counter(second_word)

N = int(input())
S = [Counter(input()) for _ in range(N)]
sum = 0

x = itertools.combinations(S, 2)
for combi in x:
    if combi[0] == combi[1]:
        sum += 1
# print(Counter(S))
print(sum)

### FIXED CODE ###

from collections import Counter
import itertools

N = int(input())
S = ["".join(sorted(input())) for _ in range(N)]
print(sum((x * (x - 1)) // 2 for x in Counter(S).values()))
### BUGGY CODE ###

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

tmp = 1
for i in range(k):
  tmp *= a[k-1-i]

for i in range(k, n):
  total = tmp * a[i] / a[i - k]
  if total > tmp:
    print('Yes')
  else:
    print('No')
  tmp = total
  

### FIXED CODE ###

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

for i in range(k, n):
  if a[i] > a[i - k]:
    print('Yes')
  else:
    print('No')
### BUGGY CODE ###

# coding: utf-8
# Your code here!
from functools import reduce
import collections

S = list(map(int,input().rstrip()))
N=len(S)
mod_list = [0]
for i in range(N):
    T = reduce( lambda a,b:10*a+b, S[i:N+1])%2019
    mod_list.append(T)
    #print(T)
count = collections.Counter(mod_list)
ans =0
for j in range(2019):
    n = count[j]
    ans+= n*(n-1)/2
print(int(ans))

### FIXED CODE ###

# coding: utf-8
# Your code here!
def main():
    from functools import reduce
    import collections
     
    S = list(map(int,input().rstrip()))
    N=len(S)
    mod_list = [0]
    for i in range(1,N+1):
        new1 =pow(10,i-1,2019)
        new = (new1*S[-i]+mod_list[-1])%2019
        #print(new)
        mod_list.append(new)
        
    count = collections.Counter(mod_list)
    ans =0
    for j in range(2019):
        n = count[j]
        ans+= n*(n-1)/2
    print(int(ans))
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import numpy as np
N = int(input())

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

matA = [[0] * N for _ in range(N)]
y = [0] * N
for i in range(N):
    matA[i][i] = 1
    matA[i][(i+1)%N] = 1

matA = np.matrix(matA)

y = []
for i in A:
    y += [[2 * i]]

inverseA = np.linalg.inv(matA) * y

# print (matA)
# print (inverseA)

lst = inverseA.tolist()
for i in lst:
    print (int(i[0]), end =' ')
print ()

### FIXED CODE ###

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

a = 0
for i in range(N):
    a += A[i] * ((-1) ** i)

print (a, end = ' ')
for i in range(N-1):
    print (2 * (A[(i)%N] - a//2 ), end = ' ')
    a = 2 * (A[(i)%N] - a//2)
print ()
### BUGGY CODE ###

N, Q = map(int, input().split())
S = str(input())

P = [list(map(int,input().split())) for i in range(Q)]

for i in range (0, Q):
	s = S[(P[i][0]-1):(P[i][1])]
	print(s.count('AC'))

### FIXED CODE ###

N, Q = map(int, input().split())
S = str(input())

l = [0]*N
count = 0

for i in range (1, N):
	if S[i-1] == 'A' and S[i] == 'C':
		count+=1
		l[i] = count
	else:
		l[i] = count

for i in range(0, Q):
	O, P = map(int, input().split())
	print(l[P-1] - l[O-1])
### BUGGY CODE ###





def solve():
    us = [[i] for i in range(0,n+1)]
    for i in range(1, n + 1):
        for fr in fs[i]:
            if not us[i] is us[fr]:
                us[i].extend(us[fr])
                for aa in us[fr]:
                    us[aa] = us[i]
    # for i in len(us):
    #     us[i] = list(set(us[i]))
    for i in range(1, n + 1):
        frens = len(us[i]) - 1 - len(fs[i])
        for blo in bs[i]:
            if blo in us[i]:
                frens -= 1
        print(frens, end=" ")
    print()
    return


n, m, k = [int(s) for s in input().split(" ")]

fs = [[] for i in range(n + 1)]
bs = [[] for i in range(n + 1)]

for i in range(m):
    a, b = [int(s) for s in input().split(" ")]
    fs[a].append(b)
    fs[b].append(a)
for i in range(k):
    c, d = [int(s) for s in input().split(" ")]
    bs[c].append(d)
    bs[d].append(c)
solve()


### FIXED CODE ###


def solve():
    us = [[i] for i in range(0,n+1)]
    for i in range(1, n + 1):
        for fr in fs[i]:
            if not us[i] is us[fr]:
                if len(us[i])>len(us[fr]):
                    us[i].extend(us[fr])
                    for aa in us[fr]:
                        us[aa] = us[i]
                else:
                    us[fr].extend(us[i])
                    for aa in us[i]:
                        us[aa] = us[fr]
    for i in range(1, n + 1):
        frens = len(us[i]) - 1 - len(fs[i])
        for blo in bs[i]:
            if us[i] is us[blo]:
                frens -= 1
        print(frens, end=" ")
    print()
    return



n, m, k = [int(s) for s in input().split(" ")]

fs = [[] for i in range(n + 1)]
bs = [[] for i in range(n + 1)]
for i in range(m):
    a, b = [int(s) for s in input().split(" ")]
    fs[a].append(b)
    fs[b].append(a)
for i in range(k):
    c, d = [int(s) for s in input().split(" ")]
    bs[c].append(d)
    bs[d].append(c)


solve()
### BUGGY CODE ###

n = int(input())
s_li=[input() for i in range(n)]
ans = 0
for i in range(n):
  s=list(s_li[i])
  ss=sorted(s)
  for j in range(i+1,n):
    t = list(s_li[j])
    tt = sorted(t)
    if ss == tt:
      ans+= 1
print(ans)

### FIXED CODE ###

n = int(input())
dic = {}
ans = 0
for i in range(n):
  li_1 = list(input())
  li_1.sort()
  word = "".join(li_1)
  if word in dic:
    dic[word] += 1
  else:
    dic[word] = 1
for key in dic:
  s = dic[key]
  ans += s*(s-1)//2
print(ans)
### BUGGY CODE ###

# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H, W = map(int, input().split())

grid = [[v for v in input()[:-1]] for _ in range(H)]
dist = [[float('inf')]*W for _ in range(H)]

Q = []
cnt = 0
for row in range(H):
    for col in range(W):
        if grid[row][col] == "#":
            dist[row][col] = 0
            Q.append([row, col])
            cnt += 1

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
current_dist = 0
while cnt < H*W and Q:
    current_dist += 1
    tmp = []
    for sx, sy in Q:
        for x, y in zip(dx, dy):
            new_x = sx+x
            new_y = sy+y
            if 0 <= new_x < H and 0 <= new_y < W:
                if dist[new_x][new_y] == float('inf'):
                    dist[new_x][new_y] = current_dist
                    tmp.append([new_x, new_y])
                    cnt += 1
    Q = tmp
print(max([max(l) for l in dist]))


### FIXED CODE ###

# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H, W = map(int, input().split())
grid = [[v for v in input()[:-1]] for _ in range(H)]

Q = []
cnt = 0
for i in range(H):
    for j in range(W):
        if grid[i][j] == "#":
            Q.append([i, j])
            cnt += 1

DX = [-1, 1, 0, 0]
DY = [0, 0, -1, 1]
d = 0
# for _ in range(5):
while cnt < H*W:
    d += 1
    tmp = []
    #print("d->", d)
    # print(Q)
    # print(dist)
    # print(cnt)
    # print("-------")
    for x, y in Q:
        for dx, dy in zip(DX, DY):
            nx = x+dx
            ny = y+dy
            if 0 <= nx < H and 0 <= ny < W:
                if grid[nx][ny] == ".":
                    grid[nx][ny] = "#"
                    tmp.append([nx, ny])
                    cnt += 1
            else:
                continue
    Q = tmp
# print(dist)
print(d)
### BUGGY CODE ###

n, m=map(int, input().split())
a=[int(n) for n in input().split()]
ans=0
a.sort(reverse=True)
for i in range(m):
  a[0]=a[0]//2
  a.sort(reverse=True)
for i in a:
  ans=ans+i
print(ans)

### FIXED CODE ###

import heapq

n, m=map(int, input().split())
a=[-1*int(n) for n in input().split()]
heapq.heapify(a)

for i in range(m):
  num=-1*(-1*heapq.heappop(a)//2)
  heapq.heappush(a,num)

print(-1*sum(a))
### BUGGY CODE ###

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import math

    
def main():
	num = int(input())
	sum = 0
	for i in range(num):
		a = i+1
		for j in range(num):
			b = j+1
			for k in range(num):
				c = k+1
				if a == b:
					sum += math.gcd(a,c)
				elif b == c or a == c:
					sum += math.gcd(a,b)
				else:
					sum += math.gcd(math.gcd(a,b),c)
	print (sum)

if __name__ == '__main__':
	main()


### FIXED CODE ###

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import math

    
def main():
	num = int(input())
	sum = 0
	for i in range(num):
		a = i+1
		for j in range(num):
			b = j+1
			for k in range(num):
				c = k+1
				sum += math.gcd(math.gcd(a,b),c)
	print (sum)

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

n=int(input())
import math
res=0
for a in range(1,n+1):
    for b in range(1, n + 1):
        for c in range(1, n + 1):
           gcd1=math.gcd(a,b)
           gcd2=math.gcd(gcd1,c)
           res+=gcd2
print(res)

### FIXED CODE ###

n=int(input())
import math
res=0
for a in range(1,n+1):
    for b in range(1, n + 1):
        gcd1 = math.gcd(a, b)
        for c in range(1, n + 1):
           gcd2=math.gcd(gcd1,c)
           res+=gcd2
print(res)
### BUGGY CODE ###

nums = [int(e) for e in input().split()]
Min=nums[0]
y=nums[1]
t=True
while t:
  if(abs(Min -y)<Min):
    Min = abs(Min-y)
  elif(abs(Min - y) > Min):
    break
print(Min)

### FIXED CODE ###

nums = [int(e) for e in input().split()]
Min=nums[0]
y=nums[1]
ans = Min % y
t = True
if (ans <= Min):
    Min = ans
    while t:
        a = abs(Min - y)
        if(a<Min):
            Min = a
        elif(a>Min):
            break
    print(Min)
else:
    print(ans)
### BUGGY CODE ###

def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  #import collections
  #c = collections.Counter
  j = 0
  for i in range(len(s)):
    j += int(a.count(s[i])*(a.count(s[i])-1)/2)
  #print(j)
  for i in range(n):
    if a.count(a[i]) >= 2:
      print(int(j-(a.count(a[i])-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  import collections
  c = collections.Counter(a)
  j = 0
  #print(c[a[0]])
  for i in range(len(s)):
    j += int(c[s[i]]*(c[s[i]]-1)/2)
  #print(j)
  for i in range(n):
    if c[a[i]] >= 2:
      print(int(j-(c[a[i]]-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

n,k,q = map(int, input().split())
l=[int(input()) for x in range(q)]
x=k-q
if x>=1:
    for i in range(n):
        print("Yes")
elif x<1:
    bd=abs(x)+1
    for i in range(n):
        if l.count(i+1)>=bd:
            print("Yes")
        else:
            print("No") 

### FIXED CODE ###

n,k,q = map(int, input().split())
l=[int(input()) for x in range(q)]
p=[0]*n
for i in l:
    p[i-1]=p[i-1]+1
for i in p:
    if i>=(q-k)+1:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

l, r = map(int, input().split())
ans = 2*10**9
for i in range(l,r):
  for j in range(i+1,r+1):
    ans = min(ans, i*j%2019)
print(ans)

### FIXED CODE ###

l, r = map(int, input().split())
ans = 2017*2018
for i in range(l,r):
  for j in range(i+1,r+1):
    ans = min(ans, i*j%2019)
    if ans == 0:
      print(ans)
      exit()
print(ans)
### BUGGY CODE ###



import copy

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def gcdm(x):
	g=x[0]
	for i in range(1,len(x)):
		g=gcd(g,x[i])
	return(g)

def score(A):
	return(sum(A)-len(A))

def Ifavor(A):
	return(score(A)%2==1)	

def playmove(A,i):
	A[i]-=1
	g=gcdm(A)
	A=[x//g for x in A]
	
def testmove(N,A,i):
	if A[i]<=1:
		return(False)
	A[i]-=1
	g=gcdm(A)
	A=[x//g for x in A]
	if Ifavor(A):
		return(False)
	else:
		j=0
		while j<N:
			if A[j]<=1:
				continue
			A[j]-=1
			h=gcdm(A)	
			if Ifavor([x//h for x in A])==False:
				return(False)
			A[j]+=1
			j+=1
		return(True)

N=int(input())
B=[int(x) for x in input().split()]
'''
inp=St.splitlines()
N=int(inp[0])
B=[int(x) for x in inp[1].split()]
'''
if Ifavor(B):
	print('First')
else:
	e=0
	while e<N:
		A=copy.deepcopy(B)
		if testmove(N,A,e):
			break
		e+=1
	
	if e==N:
		print('Second')
	else:
		print('First')
	





	

### FIXED CODE ###

import random
def tester(N=0):
	'''制約
1≦N≦105
1≦Ai≦109
'''
	maxno1=1e5
	maxno2=1e9
	s=input()
	if s!='':return(s)
	
	if N==0:
		return(random.randint(2,maxno1))
	else:
		print('Testing...')
		print('N=',N)
		A=[]
		for i in range(N):
			A.extend([random.randint(1,maxno2)])
		return(' '.join(list(map(str,A))))

import copy

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def gcdm(x):
	g=x[0]
	for i in range(1,len(x)):
		if g==1:
			return(g)
		g=gcd(g,x[i])
	return(g)

def playmove(A,i):
	A[i]-=1
	g=gcdm(A)
	return([x//g for x in A])

def noofevens(A):
	r=0
	for i in A:
		if i%2==0:
			r+=1
	return(r)
			

N=int(tester())
A=[int(x) for x in tester(N).split()]


isFirstmove=True
while True:
	e=noofevens(A)
	if e%2==1:
		if isFirstmove:
			print('First')
		else:
			print('Second')
		break
	elif N-e>1:
		if isFirstmove:
			print('Second')
		else:
			print('First')
		break
	else:	
		for i in range(N):
			if A[i]%2==1:
				break
		if A[i]==1:
			if isFirstmove:
				print('Second')
			else:
				print('First')
			break
		else:
			A=playmove(A,i)
			isFirstmove=(isFirstmove!=True)
### BUGGY CODE ###

n,m = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
C=[]
for i in range(m):
	b,c = map(int,input().split())
	C.append((b,c))

C = sorted(C,key = lambda x:x[1])

for b,c in C[::-1]:
	cou = 0
	for i in range(len(A)):
		if A[i] < c :
			A[i] = c
			cou += 1
		else :
			break
		if cou == b:
			break
	A.sort()
print(sum(A))

### FIXED CODE ###


def test_function() :
	print(__name__) # __main__

#print("called :",test_function.__name__)
n,m = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
C=[]
for i in range(m):
	b,c = map(int,input().split())
	C.append((b,c))
C = sorted(C,key = lambda x:x[1])
before = 0

for b,c in C[::-1]:
	cou = 0  
	#print("[before]=",before)
	for i in range(len(A)-before):
		if A[before+i] < c :
			A[before+i] = c
			cou += 1
			#print("A=",A)
		else :
			before += i+1
			break
		if cou == b:
			before += i+1
			break
print(sum(A))