peterverebics/CodeNet_Python_428 huggingface dataset:
# Judge status: Accepted
n,m = map(int, input().split())
ns = set(i+1 for i in range(n))
l_min = 0
r_max = n
for i in range(m):
    l, r = map(int, input().split())
    l_min = max(l_min, l)
    r_max = min(r_max, r)
print(max(r_max - l_min+1, 0))
# Judge status: Accepted
text = int(input())
answer = 0
for i in range(text):
  answer += i + 1
print(answer)
# Judge status: Accepted
import heapq

N, M = map(int, input().split())
A = list(map(int, input().split()))
A = list(map(lambda x: x*(-1), A))

heapq.heapify(A)

for _ in range(M):
  Max = heapq.heappop(A) * -1
  heapq.heappush(A, Max//2 * -1)
  
ans = 0
for i in range(N):
  ans += -1 * A[i]
  
print(ans)
# Judge status: Accepted
x = int(input())
print(1 if x-x//100*100 <= x//100*5 else 0)
# Judge status: Accepted
n = int(input())
print( (1+n)*n//2 )
# Judge status: Wrong Answer
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

C.append(0)
ans = sum(B) + sum(C) - C[A[N-1] - 1]
print(ans)
# Judge status: Accepted
n, k = map(int, input().split())
s, p, r = map(int, input().split())
t = input()
ans = 0

for i in range(k):
    pre = "0"
    for j in t[i::k]:
        if j != pre:
            pre = j
            if j == "s":
                ans += s
            elif j == "p":
                ans += p
            else:
                ans += r
        else:
            pre = "0"
print(ans)
# Judge status: Wrong Answer
X,Y,A,B,C = map(int,input().split())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
rs = list(map(int, input().split()))

ans = sorted(qs)[-X:] + sorted(ps)[-Y:] + sorted(rs)

print(str(sum(sorted(ans)[-X-Y:])))
# Judge status: Accepted
a = list(input())
k = int(input())
abc = "abcdefghijklmnopqrstuvwxyz"
c = 0
for e,i in enumerate(a[:-1]):
    # print(26-abc.index(i))
    if k-26+abc.index(i)>=0 and i!='a':
        a[e] = 'a'
        k-=26-abc.index(i)
    # print(k)  
k+=abc.index(a[-1])
k%=26
a[-1] = abc[k]

print("".join(a))
# Judge status: Accepted
n = int(input())
l = [[0, 0, 0]] + list(list(map(int, input().split())) for i in range(n))

for i in range(1, n+1):
    dt = l[i][0] - l[i-1][0]
    dx = abs(l[i][1] - l[i-1][1])
    dy = abs(l[i][2] - l[i-1][2])
    
    tmp = dt - dx - dy
    if tmp < 0 or tmp % 2 == 1:
        print('No')
        break
else:
    print('Yes')
# Judge status: Accepted
import math
def main():
    n = int(input())
    a_li = list(map(int,input().split()))
    ans = 0
    for i in range(1,n):
        if(a_li[i] >= a_li[i-1]):
            continue
        ans += a_li[i-1] - a_li[i]
        a_li[i] = a_li[i-1]
    print(ans)
    
        
if __name__ == "__main__":
    main()
# Judge status: Accepted
n, k = map(int, input().split())

ans = 0

for i in range(1,n+1):
    t = 1
    while i * t < k:
        t *= 2
    ans += 1 / t

print(ans / n)
# Judge status: Accepted
# 入力
A, B = map(int, input().split())

# 同じなら引き分け、1が出たら勝ち、大きい方が勝ち
if A == B:
    print('Draw')
elif A == 1:
    print('Alice')
elif B == 1:
    print('Bob')
elif A > B:
    print('Alice')
elif A < B:
    print('Bob')
# Judge status: Accepted
import sys
import math

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

for i in range(N):
    if A[i] == 1:
        ans = N
        for j in range(max(0, i-(K-1)), min(N, i+K-1)):
            ans = min(math.ceil(j/(K-1)) + math.ceil((N-j-1)/(K-1)), ans)
        break
print(ans)
# Judge status: Accepted
n,m  = map(int,input().split())
if m%2==1 or m//2<n or m//2-n>n:
    print('No')
    exit()
print('Yes')
# Judge status: Accepted
c = input()

if c == 'a' or  c == 'i' or  c == 'u'  or  c == 'e'  or  c == 'o':
    print('vowel')
else:
    print('consonant')
# Judge status: Accepted
n=int(input())
a=list(map(int,input().split()))
two=0
four=0
other=0
for i in range(n):
    if a[i]%4==0:
        four+=1
    elif a[i]%2==0:
        two+=1
    else:
        other+=1
if four>=two+other-1:
    print("Yes")
elif four>=two%2+other-1:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
n, k = map(int, input().split())
s = input()
ans = ''
for i in range(n):
    if i == k - 1:
        ans += s[i].lower()
    else:
        ans += s[i]

print(ans)
# Judge status: Accepted

x, y, z = (int(x) for x in input().split())

temp = y

y = x
x = temp

temp = z
z = x
x = temp

print(x,y,z)
# Judge status: Wrong Answer
S = list(input())
if S[2] == S[3]:
  if S[4] == S[5]:
    print("Yse")
  else:
    print("No")
else:
  print("No")
# Judge status: Accepted
n = int(input())
a = list(map(int,input().split()))
ans = [0]*(n)
for i in a:
    ans[i-1] += 1
for j in ans:
    print(j)
# Judge status: Accepted
n,k=map(int,input().split());h=sorted([int(input()) for _ in range(n)])
print(min([h[i+k-1]-h[i] for i in range(n-k+1)]))
# Judge status: Accepted
k,x=map(int,input().split())
if 500*k>=x:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
# solution
import io

n = input()
m = sorted(list(map(int,input().split())),reverse=True)
print(sum(m[0::2])-sum(m[1::2]))
# Judge status: Accepted
n, q = map(int, input().split())
s = list(input())

s.append("")

cusum = [0]*(len(s))
for i in range(len(s)-1):
    if s[i] == "A" and s[i+1] == "C":
        cusum[i+1] = cusum[i] + 1
    else:
        cusum[i+1] = cusum[i]

for _ in range(q):
    l, r = map(int, input().split())
    print(cusum[r-1] - cusum[l-1])
# Judge status: Wrong Answer
n = int(input())
a = [int(x) for x in input().split()]
buf = sum(a)
mn = min(a)

if (buf-mn) % mn == 0:
    print(mn)
else:
    print((buf-mn)%mn)
# Judge status: Accepted
a,b,c=map(int,input().split())
print("YNEOS"[(b-a!=c-b)::2])
# Judge status: Accepted
x = int(input())

cnt = 2 * (x // 11)
cnt += (0 if x % 11 == 0 else 1 if x % 11 <= 6 else 2)

print(cnt)
# Judge status: Accepted
d = int(input())
s = ['Christmas Eve Eve Eve','Christmas Eve Eve','Christmas Eve','Christmas']
print(s[d-22])
# Judge status: Wrong Answer
s=input()
k='keyence'
if k in s:
    exit(print('YES'))

for i in range(len(k)):
    print(s[:i], s[len(k)-i:])
    if s[:i]+s[len(s)-len(k)+i:] == k:
        exit(print('YES'))
print('NO')
# Judge status: Accepted
k= int(input())

seen = [0]*(k+1)
rem=0
for i in range(0,k+1):
    rem = (rem*10+7) % k
    # print(rem)
    if rem == 0:
        print(i + 1)
        break
    elif seen[rem] ==1:
        print('-1')
        break
    else:
        seen[rem] =1
# Judge status: Wrong Answer
import sys
n, y = map(int, input().split())

for i in range(n):
    for j in range(n - i):
        k = n - i - j
        if 10000 * i + 5000 * j + 1000 * k == y:
            print(i, j, k)
            sys.exit()

print('-1, -1, -1')
# Judge status: Accepted
# -*- coding: utf-8 -*-

def check_palindrome(S):
    for i in range(int(len(S) / 2)):
        if S[i] != S[-1 * (i + 1)]:
            return False
    return True

S = input()

if not check_palindrome(S):
    print('No')
else:
    if not check_palindrome(S[:int(len(S) / 2)]):
        print('No')
    else:
        if not check_palindrome(S[int(len(S) / 1) + 2:]):
            print('No')
        else:
            print('Yes')
# Judge status: Accepted
num=int(input())
num2=[10, 9 ,8]

num2a=map(int, num2)

def multi(x):
	y=x**num
	return y


a, b, c=map(multi, num2a)
print((a-2*b+c)%(int(1e9+7)))
# Judge status: Accepted
N=int(input())

if N==1:
    print(0)
    exit()

MOD=10**9+7

ALL=pow(10,N,MOD)
No0=pow(9,N,MOD)
No9=pow(9,N,MOD)
No09=pow(8,N,MOD)

ans=ALL-No9-No0+No09+MOD
print(ans%MOD)
# Judge status: Accepted
#E
import numpy as np
n, m = list(map(int, input().split()))
lists = list(np.arange(1, n+1))
list1 = lists[:m]
list2 = lists[m:]
num = m
for i in range(m//2 +1):
    if num == 0:
        break
    print(list2[i],list2[m+1-i-1])
    num -= 1
    if num == 0:
        break
    print(list1[i],list1[m-i-1])
    num -= 1
# Judge status: Accepted
N=int(input())
A=list(map(int,input().split()))
ans=[0]*(max(A)+3)
for a in A:
    ans[a-1]+=1
    ans[a]+=1
    ans[a+1]+=1
print(max(ans))
# Judge status: Wrong Answer
n,m=map(int,input().split())
TF=[0]*n
p=[0]*n
for _ in range(m):
    i,s =map(str,input().split())
    i=int(i)-1
    if (s=="WA") and TF[i]==0:
        p[i]+=1
    elif (s=="WA")and TF[i]==1:
        pass
    elif s=="AC" and TF[i]==0:
        TF[i]=1
    else:
        pass
ac=sum(TF)
pe=sum(p)
print(ac,pe)
# Judge status: Accepted
D=int(input())
C=list(map(int,input().split()))
import sys
S=sys.stdin.readlines()
A=[0]*26
B=[0]*D
k=0
p=C.index(max(C))
for i in range(26):
  if C[i]!=0:
    k+=C[i]
for i in range(26):
    A[i]=int(C[i]/k*D)

for i in range(26):
  for j in range(A[i]):
    m=max([l if B[l]==0 and l<(D+1)//A[i]*j else 0 for l in range(D)])
    B[m]=i+1

for i in range(D):
  if B[i]==0:
    B[i]=p+1

for i in range(D):
  print(B[i])
# Judge status: Wrong Answer
A,B,C,D = map(int, input().split())
S = 0
while S != 1:
    A -=B
    C -=D
    if A <= 0:
        S = 1
        print("Yes")
    elif C <= 0:
        S = 1
        print("No")
    else:
        continue
# Judge status: Accepted
# -*- coding: utf-8 -*-
n, k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))

h.sort()
ans = 10 ** 5 * 10 ** 10
for i in range(n - k + 1):
    #print(i, i + k - 1)
    if h[i + k - 1] - h[i] < ans:
        ans = h[i + k - 1] - h[i]
print(ans)
# Judge status: Wrong Answer
n = list(map(str, input().split()))

n = int(n[-1])
if n==2 or n==4 or n==5 or n==7 or n==9 :
    print('hon')
elif n==0 or n==1 or n==6 or n==8:
    print('pon')
else:
    print('bon')
# Judge status: Accepted
n = int(input())
s = str(input())
t = str(input())

if s == t:
    print(n)
    exit()

for i in range(n):
    #print(s[i:], t[:n-1-i+1])
    if s[i:] == t[:n-1-i+1]:
        break
else:
    i = n
    #pass
ans = i*2+(n-i)
print(ans)
# Judge status: Accepted
N = int(input())
print(int(N**(1/2))**2)
# Judge status: Accepted
a,b,c=map(int,input().split())
if min(a,b)<c and c<max(a,b):
  print("Yes")
else:
  print("No")
# Judge status: Accepted
a,b=list(map(int,input().split()))
c = [0] * a
for i in range(b):
	x,y=list(map(int,input().split()))
	c[x-1]=c[x-1]+1
	c[y-1]=c[y-1]+1

for j in range(a):
	print(c[j])
# Judge status: Wrong Answer
n, m = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(m)]
A.sort()
aa = []
bb = []
for i in range(m):
    if A[i][0]==1 or i>0 and A[i][0] in bb:
        aa.append(A[i][0])
        bb.append(A[i][1])
        if bb[-1]==n and len(set(aa))<=2:
            print('POSSIBLE')
            exit()
print('IMPOSSIBLE')
# Judge status: Accepted
s = input()
t = input()
ans = []
for i in range(len(s)-len(t)+1):
    tmp = s[i:i+len(t)]
    flg = True
    for j in range(len(t)):
        if tmp[j]!=t[j]:
            if tmp[j]!='?':
                flg = False
    if flg:
        c = s[:i]+t+s[i+len(t):]
        ans.append(c.replace('?', 'a'))
print('UNRESTORABLE' if len(ans)==0 else min(ans))
# Judge status: Accepted
x = int(input())
a = []
if x==1:
    print(1)
else:
    for b in range(1,x+1):
        for i in range(2,x+1):
            c = b**i
            if c>x:
                break
            else:
                a.append(c)
    print(max(a))
# Judge status: Accepted
n=int(input())
if n<=999:
    print('ABC')
else:
    print('ABD')
# Judge status: Accepted
a=[int(input()) for i in range(5)]
k=int(input())
ok=True
for i in range(4):
    for j in range(i+1,5):
        if abs(a[i]-a[j])>k:
            ok=False
            break
if ok:
    print("Yay!")
else:
    print(":(")
# Judge status: Accepted
n = int(input())
a = list(map(int, input().split()))
a_cur = a[0]

m = 1000
s = 0

for i in range(1, n):
    a_next = a[i]
    if a_cur < a_next:
        # buy
        s += m // a_cur
        m -= m // a_cur * a_cur
    else:
        # sell
        m += s * a_cur
        s = 0
    a_cur = a_next
m += s * a_cur
print(m)
# Judge status: Accepted
a,b,c=map(int,input().split())
if a+b>=c:
  print('Yes')
else:
  print('No')
# Judge status: Wrong Answer
n = int(input())
num = 1
a = list(map(int, input().split()))
if 0 in a:
    print(0)
    exit(0)
for i in a:
    num *= i
if num >= 10**8:
    print(-1)
    exit(0)
print(num)
# Judge status: Accepted
X, Y, Z = map(int, input().split())

if X < (2*Z + Y):
    print(0)
else:
    print(int((X-Z)/(Z+Y)))
# Judge status: Accepted
from collections import deque

s = deque(list(input()))
q = int(input())

asc = True
for _ in range(q):
    qs = input()
    if qs[0]=="1":
        asc = not asc
        continue

    _,f,c = qs.split()
    if f=="1":
        if asc:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if asc:
            s.append(c)
        else:
            s.appendleft(c)

if not asc:
    s.reverse()
print("".join(s))
# Judge status: Accepted
A,B,M=map(int,input().split())
at=list(map(int,input().split()))
bt=list(map(int,input().split()))
xyc=[list(map(int,input().split())) for i in range(M)]

minc=min(at)+min(bt)

for i in range(M):
    minc=min(minc,at[xyc[i][0]-1]+bt[xyc[i][1]-1]-xyc[i][2])
    
print(minc)
# Judge status: Wrong Answer
A,B = map(int,input().split())

#a,bの最大公約数
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

li = {1}
g = gcd(A,B)

for i in range(2,int(g**0.5)+1):
    while g%i == 0:
        g = g//i
        li.add(i)


print(len(li))
# Judge status: Accepted
import sys
input = sys.stdin.readline
def main():
    K, S = map(int, input().split())
    CNT = 0
    for x in range(K+1):
        for y in range(K+1):
            if S - x -y >= 0 and S - x -y <= K:
                CNT += 1
    print(CNT)            

if __name__ == '__main__':
    main()
# Judge status: Accepted
from fractions import gcd
a,b,c,d = map(int, input().split())

lcm = (c*d)//gcd(c,d)

x = ((a-1)//c)+((a-1)//d)-((a-1)//lcm)
y = (b//c)+(b//d)-(b//lcm)

print((b-(a-1))-(y-x))
# Judge status: Accepted
X = int(input())
shou = X // 100
val = shou*5
mod = X % 100
if val >= mod:
    print(1)
else:
    print(0)
# Judge status: Wrong Answer
S, T = input().split()
A,B = map(int,input().split())
U = input()
print(A,B)
if U == S:
  A -= 1
  print(A,B)
else:
  B -= 1
  print(A,B)
# Judge status: Accepted
n, a, b = map(int, input().split())
print([(b-a)//2, min(a-1, n-b) + 1 + (b-a-1)//2][a%2-b%2])
# Judge status: Accepted
N=int(input())
A=list(map(int,input().split()))
mi=0
ans=0
zero=0
a=abs(A[0])
for i in A:
    if i<0:
        mi+=1
    a=min(a,abs(i))
    ans+=abs(i)
if mi%2==0:
    print(ans)
else:
    print(ans-2*a)
# Judge status: Accepted

n = int(input())
ans = [0]*10001

for i in range(1, 101):
    for j in range(1, 101):
        for z in range(1, 101):
            aaa = i**2+j**2+z**2+i*j+j*z+z*i
            if aaa <= 10**4:
                ans[aaa] += 1
for i in range(n):
    print(ans[i+1])
# Judge status: Accepted
s=str(input())
print("2018"+s[4:])
# Judge status: Accepted
A1,A2,A3=map(int , input().split())
if A1+A2+A3<=21:
    print("win")
else:
    print("bust")
# Judge status: Accepted
import sys
input = sys.stdin.readline

def main():
    S = input()
    a = len(S)
    
    ans = 10**3
    for i in range(a-2):
        num = int(S[i:i+3])
        ans = min(ans,abs(num-753))
        
    print(ans)
    
if __name__ == "__main__":
    main()
# Judge status: Accepted
N, K = map(int, input().split())

d = []
A_set = set()

for i in range(K):
  d_i = int(input())
  A = list(map(int, input().split()))
  for j in A:
    A_set.add(j)

print(N - len(A_set))
# Judge status: Accepted
D=int(input())
clist=list(map(int,input().split()))
smat=[[int(x) for x in input().split()] for _ in range(D)]
#print(clist)
#print(smat)

for i in range(D):
  print(i%26+1)
# Judge status: Wrong Answer
S=str(input())

ans = 0
for i in S:
	if i == "R":
		ans += 1
print(ans)
# Judge status: Wrong Answer
a,b,c,k = map(int,input().split())
ans = 0
if a>=k:
  ans = a
elif a+b>=k:
  ans = a
else:
  ans = a - (k-a-b)
print(ans)
# Judge status: Accepted
n, m = map(int, input().split())

if n == m:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
N = int(input())
a = -(-N//(10**9))
b = a*10**9-N
print(0, 0, 10**9, b, 1, a)
# Judge status: Wrong Answer
n = int(input())
s = input()

mxcnt = 0
for i in range(n):
    xy = set(s[:i]) & set(s[i:])
    print(xy)
    mxcnt = max(mxcnt, len(xy))
print(mxcnt)
# Judge status: Accepted
s=input()
t=input()
dicST={}
dicTS={}

for i,j in zip(s,t):
  if (dicTS.get(j,0) != 0 and dicTS[j] != i) or \
    (dicST.get(i,0) != 0 and dicST[i] != j):
    print("No")
    break
  else:
    dicTS[j] = i
    dicST[i] = j
else:
  print("Yes")
# Judge status: Accepted
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code
def resolve():
    N=int(input())
    A=tupin()

    from collections import Counter
    c=Counter(A)
    total=0
    
    for t in c.items():
        ag,b=t
        total+=(b*(b-1))//2
    #print(total)
    for m in A:
        a=c[m]
        print(total-((a*(a-1))//2)+((a-1)*(a-2))//2)
#%%submit!
resolve()
# Judge status: Accepted
n, c, k = map(int, input().split())
t = [int(input()) for i in range(n)]

t.sort()

diff = 0
cnt = 1
ans = 0
for i in range(n - 1):
    diff += abs(t[i + 1] - t[i])
    cnt += 1
    if diff > k or cnt > c:
        cnt = 1
        diff = 0
        ans += 1
print(ans + 1)
# Judge status: Accepted
from collections import deque

K = int(input())

queue = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])

x = 1
count = 0
while K != count:
    x = queue.popleft()
    if (x % 10) != 0:
        queue.append(10 * x + (x % 10) - 1)
    queue.append(10 * x + (x % 10))
    if (x % 10) != 9:
        queue.append(10 * x + (x % 10) + 1)
    count += 1
print(x)
# Judge status: Wrong Answer
n, k = map(int, input().split())

print(n % k)
# Judge status: Accepted
L,R,d=map(int, input().split())
cnt=0

for _ in range(L, R+1):
  if _%d==0:
    cnt +=1
    
print(cnt)
# Judge status: Accepted
D,T,S=map(int,input().split())
if D/S <=T:
    print('Yes')
else:
    print('No')
# Judge status: Accepted
A, B, C = map(int,input().split())
if C > min(A, B) and C < max(A, B):
  print("Yes")
else:
  print("No")
# Judge status: Wrong Answer
n = int(input())
d = dict()
for _ in range(n):
    k = int(input())
    if d.get(k) == None:
        d[k] = 1
    else:
        d[k] = 0

print(sum(d.values()))
# Judge status: Accepted
import sys, math
from functools import lru_cache
from collections import defaultdict
from decimal import Decimal
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N, P = mi()
    A = list(mi())

    if all(a%2==0 for a in A):
        print(0 if P else pow(2, N))
    else:
        print(pow(2, N-1))


if __name__ == '__main__':
    main()
# Judge status: Accepted
N, M = map(int, input().split())

WA = [0] * (10 ** 5 + 1)
AC = [False] * (10 ** 5 + 1)
for i in range(M):
    _no , re = input().split()
    no = int(_no)
    if re == 'AC':
        AC[no] = True
    if not AC[no]:
        if re == 'WA':
            WA[no] += 1
pena = 0
for i in range(10 ** 5 + 1):
    if AC[i]:
        pena += WA[i]

print(AC.count(True), pena)
# Judge status: Accepted
a =["SUN","MON","TUE","WED","THU","FRI","SAT"]
b = input()
for i in range(7):
    if a[i]==b:
        print(7-i)
        break
# Judge status: Wrong Answer
# A - Between Two Integers

# A B C の3つの整数が与えられる
#  整数 C が　A以上　かつ　B以下である時　Yes
# そうでないとき　No

# A B C を標準入力から得る
A, B, C = map(int, input().split(maxsplit=3))

if C >= A and A <= B:
    answer = "Yes"
else:
    answer = "No"

# 結果を出力
print(answer)
# Judge status: Accepted
N, K = map(int, input().split())
x = list(map(int, input().split()))
answer = float('inf')
for l, r in zip(x, x[K - 1:]):
    # print(l, r)
    cost = r - l + min(abs(l), abs(r))
    answer = cost if cost < answer else answer

print(answer)
# Judge status: Accepted
A,B = map(int, input().split())

print('Possible' if A*B*(A + B)%3 == 0 else 'Impossible')
# Judge status: Accepted
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
cd = [list(map(int, input().split())) for _ in range(m)]

for a, b in ab:
    ans = 10 ** 10
    dist = 10 ** 10
    for i in range(m):
        c, d = cd[i]
        temp = abs(a - c) + abs(b - d)
        if dist > temp:
            dist = temp
            ans = i + 1
    print(ans)
# Judge status: Wrong Answer
n = int(input())
a = sorted(list(map(int, input().split())), reverse = True)
S = a[0]
A = a[n - 1]
for i in a[1:n - 1]:
    if abs(S + i - A) <= abs(S - A - i):
        S += i
    else:
        A += i
print(abs(S - A))
# Judge status: Wrong Answer
x=0
N,K=map(int,input().split())
P=list(map(int,input().split()))
S=[]
for i in range(N):
  S.append((1+P[i])/2)
#print(S)
i=1
while i+K<N+1:
  #T.append(sum(S[i:i+K]))
  x=max(x,sum(S[i:i+K]))
  i=i+1
#print(S)
#print(T)
print("{0:.12f}".format(x))
# Judge status: Wrong Answer
import math 

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

x = math.gcd(a,b)
y = a * b
ans = y / x
print(ans)
# Judge status: Accepted
n = int(input())
v = list(map(int, input().split()))

v.sort()

total = v[0]
for i in range(1, n):
    total = (total + v[i])/ 2
    
print(total)
# Judge status: Wrong Answer
from itertools import*
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())))[::-1]
q=sorted(list(map(int,input().split())))[::-1]
r=sorted(list(map(int,input().split())))[::-1]
sa=list(accumulate(p))[x-1]
sb=list(accumulate(q))[y-1]
for i,v in enumerate(r):
  if x and v>p[x-1]:
    if p[x-1]<q[y-1] or y==0:
      sa+=v-p[x-1]
      x-=1
  elif y and v>q[y-1]:
    sb+=v-q[y-1]
    y-=1
print(sa+sb)
# Judge status: Accepted
n=int(input())
print(sum([i for i in range(1,n+1)]))
# Judge status: Accepted
A,B,C,D = (int(T) for T in list(input()))
ABCD = [A+B+C+D,A+B+C-D,A+B-C+D,A+B-C-D,A-B+C+D,A-B+C-D,A-B-C+D,A-B-C-D]
Op   = ['+++','++-','+-+','+--','-++','-+-','--+','---']
Disp = list(Op[ABCD.index(7)])
print('{}{}{}{}{}{}{}=7'.format(A,Disp[0],B,Disp[1],C,Disp[2],D))
# Judge status: Accepted
[A,B,T] = list(map(int,input().split()))

print(int(((T+0.5) // A) * B))
# Judge status: Accepted
n,k = list(map(int,input().split()))
r,s,p = list(map(int,input().split()))
t = str(input())

t_list = []

for i in range(k):
  t_list.append(t[i])

for i in range(k,n):
  if t[i] != t_list[i-k] or t_list[i-k] == 'all':
    t_list.append(t[i])
  else:
    t_list.append('all')

print(t_list.count('r') * p + t_list.count('s') * r + t_list.count('p') * s)
# Judge status: Wrong Answer
N, M = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N)]
cd = [list(map(int, input().split())) for i in range(M)]

ans = []
for i, (a, b) in enumerate(ab):
  min_dist = 10**20
  idx = -1
  for j, (c, d) in enumerate(cd):
    dij = abs(a - c) + abs(b - d)
    if dij < min_dist:
      min_dist = dij
      idx = j
  ans.append(idx)

print(*ans, sep='\n')
# Judge status: Accepted
N,Q = map(int,input().split())
S = input()
X = [list(map(int, input().split())) for _ in range(Q)]
L =[0]*(N+1)
for i in range(N-1):
  if S[i]=="A" and S[i+1]=="C":
    L[i+1] = L[i]+1 
  else:
    L[i+1] = L[i]
L[N] = L[N-1]
for x in X:
  print(L[x[1]-1]-L[x[0]-1])
# Judge status: Accepted
a,f = map(int,input().split(" "))
j = 0
for i in range(a + 1):
  x = a - i
  c = (a - i) * 2 + i * 4
  if c == f:
    print("Yes")
    j = 1
    break
if j == 0:
  print("No")
# Judge status: Accepted
a, b=map(int,input().split())
if a<=b:
  print(a)
else:
  print(a-1)
# Judge status: Accepted
#python3
def main():
    n, m = map(int, input().split())
    cards = [(int(i), 1) for i in input().split()]
    for _ in range(m):
        b, c = map(int, input().split())
        cards.append((c, b))
    cards.sort(reverse=True)
    ans = 0
    sumv = 0
    for i in range(n):
        v = cards[i][1]
        cv = cards[i][0] * v
        ans += cv
        sumv += v
        if sumv >= n:
            ans -= cards[i][0] * (sumv - n)
            break
    print(ans)
main()
# Judge status: Wrong Answer
import math

x,y = map(int,input().split())
y += 1
print(int(math.log2(y/x))+1)