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

#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):


    bc = [c for b,c in zip(B, C) for b_ in range(b)]
    k = sorted(A+bc)[-N:]
    print(sum(k))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys

def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):

    # bc = [c for b,c in zip(B, C) for b_ in range(b)]
    # print(A+bc)
    # k = sorted(A+bc)[-N:]
    a = [(l, 1) for l in A]
    for x, y in zip(C, B):
        a.append((x,y))


    n = N
    ans = 0
    for x, y in sorted(a, reverse=True):
        if n >= y:
            n -= y
            ans += x*y
        else:
            ans += x*n
            break
    
    print(ans)

    # print(sum(k))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)

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

S = input()
Q = int(input())
x = 0
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        x += 1
    elif x%2 ==0:
        if A[1] == '1':
            S = A[2] + S
        else:
            S = S + A[2]
    else:
        if A[1] == '1':
            S = S + A[2] 
        else:
            S = A[2] + S
if x%2 == 1:
    S = ''.join(reversed(list(S)))
print(S)

### FIXED CODE ###

S = input()
Q = int(input())
x = 0
p = ''
q = ''
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        x += 1
    elif x%2 ==0:
        if A[1] == '1':
            p = A[2] + p
        else:
            q = q + A[2]
    else:
        if A[1] == '1':
            q = q + A[2] 
        else:
            p = A[2] + p
S = p + S + q
if x%2 == 1:
    S = ''.join(reversed(list(S)))
print(S)
### BUGGY CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
pos = 1
lst = [1]
for i in range(k):
    pos = a[pos-1] 
    if pos in lst:
        num = lst.index(pos)
        loop = len(lst) - num
        rem = (k - i - 1) % loop
        print(lst[num + rem])
        break
    lst.append(pos)
else:
    print(pos)

### FIXED CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
pos = 1
if k < n:
    for _ in range(k):
        pos = a[pos - 1]
    print(pos)
else:
    lst = [1]
    for _ in range(n):
        pos = a[pos-1] 
        lst.append(pos)
    
    count = 0
    for num, i in enumerate(lst):
        if i == pos and count == 1:
            loop = num - before
            rem = (k - before) % loop
            print(lst[before + rem])
            break
        elif i == pos:
            count = 1
            before = num
### BUGGY CODE ###

import re
s = input()
s = s[::-1]

for i in range(len(s)):
    s = re.sub("^remaerd", "", s)
    s = re.sub("^maerd", "", s)
    s = re.sub("^resare", "", s)
    s = re.sub("^esare", "", s)

if s == "":
    print("YES")
else:
    print("NO")

### FIXED CODE ###

s = input()

while True:
    for word in ["dream", "dreamer", "erase", "eraser"]:
        if s.endswith(word):
            s = s[:-len(word)]
            break
    else:
        print("NO")
        exit()
    if s == "":
        print("YES")
        break
### BUGGY CODE ###

def main():
    import sys
    import heapq
    from copy import deepcopy
    from collections import deque
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    _N, K = [int(x) for x in input().strip().split()]
    V = deque([int(x) for x in input().strip().split()])

    def dfs(ans, q, V, k):
        # print(k, q, V)
        ans = max(ans, sum(q))
        if k == K:
            return ans
        if q:
            t = heapq.heappop(q)
            V.appendleft(t)
            ans = max(ans, dfs(ans, q[:], V, k+1))
            V.popleft()
            V.append(t)
            ans = max(ans, dfs(ans, q, V, k+1))
            V.pop()
            heapq.heappush(q, t)
        if V:
            q_l = q[:]
            q_r = q[:]
            t = V.popleft()
            heapq.heappush(q_l, t)
            ans = max(ans, dfs(ans, q_l[:], V, k+1))
            V.appendleft(t)
            t = V.pop()
            heapq.heappush(q_r, t)
            ans = max(ans, dfs(ans, q_r[:], V, k+1))
            V.append(t)
            
        return ans
    print(dfs(-float('inf'), [], V, 0))

if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    import sys
    import heapq
    from copy import deepcopy
    from collections import deque
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N, K = [int(x) for x in input().strip().split()]
    V = [int(x) for x in input().strip().split()]
    ans = -float('inf')
    for l in range(min(N, K)+1):
        for r in range(min(N, K)-l+1):
            vt = sorted(V[:l] + V[N-r:])
            c = 0
            i = 0
            for i in range(len(vt)):
                if c == (K-l-r):
                    break
                if vt[i] < 0:
                    vt[i] = 0
                    c += 1
                else:
                    break
            ans = max(ans, sum(vt))

    print(ans)

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

import statistics
import numpy as np

num_q = int(input())
x = 0
f = 0
a = []
c = False

for _ in range(num_q):
  q = input()
  if q[0] == '2':
    print(x, f)
  else:
    _, n, m = map(int, q.split(" "))
    a.append(n)
    if c:
      x_tmp = int(np.median(a + [-100000000]))
      f += m + np.abs(x - n)
    else:
      x_tmp = int(np.median(a))
      f += m + np.abs(x_tmp - n)
    c = ~c
    x = x_tmp


### FIXED CODE ###

from heapq import heappush, heappop

num_q = int(input())
x = 0
f = 0
low = []
high = []
odd = False
num_a = 0
 
for _ in range(num_q):
  q = input()
  if q[0] == '2':
    print(x, f)
  else:
    _, n, m = map(int, q.split(" "))
    if odd:
      if n >= -low[0]:
        f += m + n + low[0]
        heappush(high, n)
      else:
        f += m - n - low[0]
        heappush(low, -n)
        heappush(high, -heappop(low))
    else:
      f += m
      if num_a == 0:
        heappush(low, -n)
      else:
        if n > -low[0]:
          if n > high[0]:
            f += n - high[0]
          heappush(high, n)
          heappush(low, -heappop(high))
        else:
          f += - n - low[0]
          heappush(low, -n)

    odd = ~odd
    x = -low[0]
    num_a += 1
### BUGGY CODE ###

import math
k = int(input())
sum =0
for a in range(1, k+1):
     for b in range(1, k+1):
          for c in range(1, k+1):
               sum += math.gcd(math.gcd(a, b), c)
print(sum)

### FIXED CODE ###

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

def prod(X):
    val = 1
    for x in X:
        val = val * x
    return val

N, K = map(int, input().split())
A = list(map(int, input().split()))
G = [prod(A[:K])]
for i in range(K, N):
    val = G[-1] // A[i - K] * A[i]
    G.append(val)
# print(G)

for i in range(1, len(G)):
    print('Yes' if G[i] > G[i - 1] else 'No')

### FIXED CODE ###

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

for i in range(K, N):
    print('Yes' if A[i] > A[i - K] else 'No')
### BUGGY CODE ###

n,p = map(int,input().split())
s = input()
num = 0
s1 = [0]*(n+1)
ans = 0
if p == 2 or p == 5:
	for i in range(len(s)):
		if int(s[i])%p == 0:
			ans += i+1	
	print(ans)
	exit()
ans = 0
arr = [0]*p
for i in range(n+1):
	s1[i] = int(s)%(10**i)%p
	arr[s1[i]] += 1
for i in range(p):
	ans += arr[i] * (arr[i] - 1) // 2
print(ans)


### FIXED CODE ###

def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

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

import Queue
import copy

def main():
    H, W = map(int, raw_input().split())
    S = []

    for i in range(H):
        S.append(list(raw_input()))


    def bfs(start, goal, S2):
        q = Queue.Queue()
        q.put(start)
        candidates = [[0, 1], [1, 0], [0, -1], [-1, 0]]
        dist = [[0]*W for _ in range(H)]
        while not q.empty():
            i = q.get()
            x = i[0]
            y = i[1]
            S2[x][y] = "#"

            for candi in candidates:
                x_new = x + candi[0]
                y_new = y + candi[1]

                if x_new < H and x_new >= 0:
                    if y_new < W and y_new >=0:
                        if S2[x_new][y_new] == ".":
                            dist[x_new][y_new] = dist[x][y] + 1
                            q.put([x_new, y_new])

        return dist[goal[0]][goal[1]]


    ans = 0
    for i in range(H):
        for j in range(W):
            if S[i][j] == ".":
                for k in range(H):
                    for l in range(W):
                        if S[k][l] == ".":
                            S2 = copy.deepcopy(S)
                            ans = max(ans, bfs([i, j], [k, l], S2))

    print ans


if __name__ == "__main__":
    main()



### FIXED CODE ###

import Queue
import copy

def main():
    H, W = map(int, raw_input().split())
    S = []

    for i in range(H):
        S.append(list(raw_input()))


    def bfs(start, S2):
        q = Queue.Queue()
        q.put(start)
        candidates = [[0, 1], [1, 0], [0, -1], [-1, 0]]
        dist = [[0]*W for _ in range(H)]
        S2[start[0]][start[1]] = "#"
        while not q.empty():
            i = q.get()
            x = i[0]
            y = i[1]

            for candi in candidates:
                x_new = x + candi[0]
                y_new = y + candi[1]

                if x_new < H and x_new >= 0:
                    if y_new < W and y_new >=0:
                        if S2[x_new][y_new] == ".":
                            dist[x_new][y_new] = dist[x][y] + 1
                            S2[x_new][y_new] = "#"
                            q.put([x_new, y_new])

        ans = 0
        for i in range(H):
            ans = max(ans, max(dist[i]))

        return ans

    ans = 0
    for i in range(H):
        for j in range(W):
            if S[i][j] == ".":
                S2 = copy.deepcopy(S)
                ans_temp = bfs([i, j], S2)
#                print i, j, ans_temp                                                                                                                     
                ans = max(ans, ans_temp)


    print ans


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

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

dp = [0]*N
dp[1] = abs(l[0]-l[1])

for i in range(2,N):
  dp[i] = int(10e10)
  for j in range(1,K+1):
    if i-j >= 0:
      dp[i] = min(dp[i],dp[i-j]+abs(l[i]-l[i-j]))

print(dp[N-1])

### FIXED CODE ###

import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
l = np.array(IL())

dp = np.zeros(N,dtype=int)
dp[0] = 0

for i in range(1,N):
    if i-K < 0:
        dp[i] = np.min(dp[:i]+np.abs(l[i]-l[:i]))
    elif i-K >= 0:
        dp[i] = np.min(dp[i-K:i]+np.abs(l[i]-l[i-K:i]))

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

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

L = [[0 for _ in range(w)] for _ in range(h)]
R = [[0 for _ in range(w)] for _ in range(h)]
D = [[0 for _ in range(w)] for _ in range(h)]
U = [[0 for _ in range(w)] for _ in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j] == '.':
      if j == 0:
        L[i][j] = 1
      else:
        L[i][j] = L[i][j-1]+1     
      if i == 0:
        D[i][j] = 1
      else:
        D[i][j] = D[i-1][j]+1
      if i == h-1:
        U[i][j] = 1
      else:
        U[i][j] = U[i+1][j]+1
  for j in range(w-1, -1, -1):
    if s[i][j] == '.':
      if j == w-1:
        R[i][j] = 1
      else:
        R[i][j] = R[i][j+1]+1

for i in range(h-1, -1, -1):
  for j in range(w):
    if s[i][j] == '.':
      if i == h-1:
        U[i][j] = 1
      else:
        U[i][j] = U[i+1][j]+1
ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)

print(ans)



### FIXED CODE ###

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

L = [[0 for _ in range(w)] for _ in range(h)]
R = [[0 for _ in range(w)] for _ in range(h)]
D = [[0 for _ in range(w)] for _ in range(h)]
U = [[0 for _ in range(w)] for _ in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j] == '.':
      if j == 0:
        L[i][j] = 1
      else:
        L[i][j] = L[i][j-1]+1     
      if i == 0:
        D[i][j] = 1
      else:
        D[i][j] = D[i-1][j]+1
      if i == h-1:
        U[i][j] = 1
      else:
        U[i][j] = U[i+1][j]+1
  for j in range(w-1, -1, -1):
    if s[i][j] == '.':
      if j == w-1:
        R[i][j] = 1
      else:
        R[i][j] = R[i][j+1]+1

for i in range(h-1, -1, -1):
  for j in range(w):
    if s[i][j] == '.':
      if i == h-1:
        U[i][j] = 1
      else:
        U[i][j] = U[i+1][j]+1
ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)

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

from sys import stdin, setrecursionlimit


def main():
    mod = 10 ** 9 + 7
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0

    for i in range(n - 1):
        for j in range(i + 1, n):
            ans += a[i] ^ a[j]
            ans %= mod

    print(ans % mod)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


### FIXED CODE ###

from sys import stdin, setrecursionlimit


def main():
    mod = 10 ** 9 + 7
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))

    counts = [0] * 60

    for ai in a:
        for i in range(60):
            if ai & (1 << i):
                counts[i] += 1

    ans = 0
    base = 0

    for c in counts:
        ans += c * (n - c) * (1 << base)
        ans %= mod
        base += 1

    print(ans % mod)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
### BUGGY CODE ###

from itertools import accumulate

def main():
    n, k = map(int, input().strip().split())
    A = list(map(int, input().strip().split()))
    
    # for _ in range(min(k,int(math.log(n,2)))):
    
    
    def solve():
        nonlocal A
        for _ in range(k):
            b=[0]*(n+1)
            
            for i,v in enumerate(A):
                b[max(0,i-v)]+=1
                b[min(n,i+v+1)]-=1
            
            # update ans
            # cnt=0
            # for i in range(1,n):
            #     b[i]+=b[i-1]
            A=[*accumulate(b)][:-1]
            
            if min(A)==n:
                break
        print(*A)
    solve()

if '__main__' == __name__:
    main()

### FIXED CODE ###

import sys
from itertools import accumulate
input=sys.stdin.buffer.readline

def main():
    n, k = map(int, input().split())
    A = [*map(int, input().split())]
    
    # for _ in range(min(k,int(math.log(n,2)))):
    
    for _ in "_"*k:
        b=[0]*(n)
        
        for i,v in enumerate(A):
            if i>v:
            # b[max(0,i-v)]+=1
                b[i-v]+=1
            else:
                b[0]+=1
            j=i-~v
            # if i+v+1<n:
            #     b[i+v+1]-=1
            if j<n:
                b[j]-=1
        
        # update ans
        # cnt=0
        # for i in range(1,n):
        #     b[i]+=b[i-1]
        A=[*accumulate(b)]
        
        if min(A)==n:
            break
    print(" ".join(map(str,A)))

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

from sys import exit, stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
AB = []
for i in range(N):
    a, b = stdin.readline().rstrip().split()
    for j in range(int(b)):
        AB.append(int(a))
AB.sort()
print(AB[K-1])

### FIXED CODE ###

from sys import exit, stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
AB = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N)]
AB.sort(key=lambda ab:ab[0])
total_index = 0
for ab in AB:
    total_index += ab[1]
    if total_index >= K:
        print(ab[0])
        exit()
### BUGGY CODE ###

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

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


for i in range(k,n):
    end=start*a[i]//a[i-k]

#    print(end,start)

    if end>start:
        print('Yes')
    else:
        print('No')
    start=end

### 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 ###

n,m = map(int,input().split())
ab = [ list(map(int,input().split())) for i in range(n) ]
# print(ab)
ab.sort(reverse=True)
# print(ab)
import heapq
heap = []
ans = 0
for day in reversed(range(m)):
    for i in range(len(ab)):
        if ab[i][0]==(m-day):
            heapq.heappush(heap,-ab[i][1])
    if len(heap)>0:
        ans += abs(heapq.heappop(heap))
print(ans)

### FIXED CODE ###

n,m = map(int,input().split())
ab = [ list(map(int,input().split())) for i in range(n) ]
# print(ab)
ab.sort()
# print(ab)
import heapq
heap = []
ans = 0
i = 0
for day in reversed(range(m)):
    while i<n and ab[i][0]==(m-day):
        heapq.heappush(heap,-ab[i][1])
        i += 1
    if len(heap) > 0:
        ans += -heapq.heappop(heap)
    # print(day,i,ans,heap)
print(ans)
### BUGGY CODE ###

X, Y = map(int, input().split())


if X != Y:       
    for i in range(max(X,Y)+1, 10**18):
        if (i % X == 0) and (i % Y != 0):
            print(i)
            break
else:
    print(-1)

### FIXED CODE ###

X, Y = map(int, input().split())


if X % Y != 0:
    print(X)
else:
    print(-1)
### BUGGY CODE ###

import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for i in range(0,N-1):
    for j in range(i+1,N):
        D=L[i]+L[j]
        ans+=bisect.bisect_left(L[j+1:],D)
print(ans)

### FIXED CODE ###

N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for i in range(0,N-1):
    for j in range(i+1,N):
        D=L[i]+L[j]
        l=j
        r=N
        while r-l>1:
            med=(l+r)//2
            if D>L[med]:
                l=med
            else:
                r=med
        ans+=l-j
print(ans)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
a0=[ai for ai in a]
b=[-1]*(n+1)
base=0
for i in range(n):
    j=n-1-i
    if b[a[j]]==-1:
        c=a.count(a[j])
        b[a[j]]=c
        base+=c*(c-1)//2
        a.pop()
for i in range(n):
    d=b[a0[i]]
    print(base-d+1)


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
a0=[ai for ai in a]
a0.sort()
b=[-1]*(n+1)
base=0
i=0
while i < n:
    t=a0[i]
    c=1
    while i < n-1 and a0[i]==a0[i+1]:
        c+=1
        i+=1
    b[t]=c
    base+=c*(c-1)//2
    i+=1
for i in range(n):
    d=b[a[i]]
    print(base-d+1)
### BUGGY CODE ###

import sys

def main():
    N = int(input())
    A = [int(sys.stdin.readline()) for i in range(N)]


    for j in range(N):
        if j == 0:
            print(max(A[1:]))

        elif j == N-1:
            print(max(A[:N-1]))

        else:
            print(max(max(A[:j]), max(A[j+1:])))


main()

### FIXED CODE ###

import sys

def main():
    N = int(input())
    A = [int(sys.stdin.readline()) for i in range(N)]
    anoA = sorted(A)
    mA = max(A)

    for j in range(N):
        if mA == A[j]:
            print(anoA[-2])

        else:
            print(mA)


main()
### BUGGY CODE ###

# -*- coding: utf-8 -*-

# import sys

#_input = sys.stdin.readlines()
array_length = int(input())
array = list(map(int, input().split()))
# print(array_length, array)
assert array_length == len(array)


def merge(A, left, mid, right):
    global count

    n1 = mid - left
    n2 = right - mid
    L = [0 for _ in range(n1 + 1)]
    R = [0 for _ in range(n2 + 1)]

    for m in range(n1):
        L[m] = A[left + m]

    for n in range(n2):
        R[n] = A[mid + n]

    L[n1], R[n2] = [1e9] * 2
    i, j = [0] * 2

    for k in range(left, right):
        count += 1
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1

    return None


def merge_sort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)
    return A


if __name__ == '__main__':
    count = 0
    print(*merge_sort(A=array, left=0, right=array_length))
    print(count)

### FIXED CODE ###

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

import sys


def merge(A, left, mid, right):
    global count

    L = A[left:mid] + [1e9]
    R = A[mid:right] + [1e9]

    i, j = 0, 0

    for k in range(left, right):
        count += 1
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
    return None


def merge_sort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)
    return A


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    assert array_length == len(array)
    count = 0
    print(*merge_sort(A=array, left=0, right=array_length))
    print(count)
### BUGGY CODE ###

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

for i in range(m):
    b, c = map(int, input().split())
    
    if c > min(nums):
        nums.extend([c] * b)
    
nums = sorted(nums, reverse=True)
print(sum(nums[:length]))

### FIXED CODE ###

import numpy as np

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

arr = np.array([list(map(int, input().split())) for i in range(m)])
mask = arr.T[1].argsort()[::-1]
arr = arr[mask]

cnt = 0
for b, c in arr:
    nums.extend([int(c)]*b)
    cnt += b
    if cnt > length:
        break

nums = list(nums)
nums.sort(reverse=True)

print(sum(nums[:length]))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
  b.append(str(a[i]))
  b.reverse()
print(" ".join(b))


### FIXED CODE ###

n = int(input())
a = list(input().split())
b = []

if n % 2 == 0:
  for i in range(n//2):
    b.append(a[n-1-i*2])
  for i in range(n//2):
    b.append(a[i*2])
else:
  for i in range(n//2+1):
    b.append(a[n-i*2-1])
  for i in range(n//2):
    b.append(a[i*2+1])

print(" ".join(b))