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

n, q = map(int, input().split())
n = [0]*(n+1)
maxv = winner = 0
for _ in range(q):
    a, v = map(int, input().split())
    n[a] += v
    if v < 0:
        maxv = max(n)
        winner = n.index(maxv)
    elif n[a] > maxv:
            maxv, winner = n[a], a
    elif n[a] == maxv:
            winner = min(winner, a)
    print(winner, maxv)

### FIXED CODE ###

n, q = map(int, input().split())
n = [0]*(n+1)
maxv = winner = 0
for _ in range(q):
    a, v = map(int, input().split())
    n[a] += v
    if v < 0 and a == winner:
        maxv = max(n)
        winner = n.index(maxv)
    elif n[a] > maxv:
            maxv, winner = n[a], a
    elif n[a] == maxv:
            winner = min(winner, a)
    print(winner, maxv)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(500*500)

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

for i in range(w):
    for j in range(h):
        for di in [-1,0,1]:
            for dj in [-1,0,1]:
                if abs(di)+abs(dj)!=1:
                    continue
                if 0<=i+di<w and 0<=j+dj<h:
                    if s[j][i]!=s[j+dj][i+di]:
                        g[j*w+i].append(i+di+(j+dj)*w)

visit=[0]*(h*w)
ans=0
black=[0]*(h*w)
white=[0]*(h*w)

def rec(j,i,rj,ri):
    if s[j][i]=='#':
        black[rj*w+ri]+=1
    else:
        white[rj*w+ri]+=1
    
    for n in g[j*w+i]:
        if visit[n]==1:
            continue

        nj=n//w
        ni=n%w

        visit[n]=1

        rec(nj,ni,rj,ri)

for i in range(w):
    for j in range(h):
        if visit[w*j+i]:
            continue
        visit[w*j+i]=1

        rec(j,i,j,i)
        
        ans+=black[w*j+i]*white[w*j+i]

print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(500*500)

h,w=list(map(int,input().split()))
s=[[1 if c=='#' else 0 for c in input()] for _ in range(h)]
g=[[] for _ in range(h*w)]

for i in range(w):
    for j in range(h):
        for di in [-1,0,1]:
            for dj in [-1,0,1]:
                if abs(di)+abs(dj)!=1:
                    continue
                if 0<=i+di<w and 0<=j+dj<h:
                    if s[j][i]!=s[j+dj][i+di]:
                        g[j*w+i].append(i+di+(j+dj)*w)

visit=[0]*(h*w)
black=[0]*(h*w)
white=[0]*(h*w)

def rec(j,i,rj,ri):
    if s[j][i]:
        black[rj*w+ri]+=1
    else:
        white[rj*w+ri]+=1
    
    for n in g[j*w+i]:
        if visit[n]:
            continue

        visit[n]=1

        rec(n//w,n%w,rj,ri)

ans=0

for i in range(w):
    for j in range(h):
        if visit[w*j+i]:
            continue
        visit[w*j+i]=1

        rec(j,i,j,i)
        
        ans+=black[w*j+i]*white[w*j+i]

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

h, w = map(int, input().split())
maze = [['#'] * (w+2)]
cou = 0
for i in range(h):
    a = ['#'] + list(input()) + ['#']
    maze.append(a)
x = ['#'] * (w+2)
maze.append(x)

for i in range(1, h+1):
    for j in range(1, w+1):
        if maze[i][j] == '#':
            cou += 1

ng = True
pos = [[1, 1, 0]]
while len(pos) > 0:
    x, y, depth = pos.pop(0)
    if ([x, y] == [h, w]) & (maze[x][y] != '#'):
        ans = h * w - cou - depth - 1
        #print(cou)
        #print(depth)
        print(ans)
        ng = False
        break
    maze[x][y] = 'f'

    if maze[x+1][y] == '.':
        pos.append([x+1, y, depth+1])
    if maze[x][y+1] == '.':
        pos.append([x, y+1, depth+1])
    if maze[x-1][y] == '.':
        pos.append([x-1, y, depth+1])
    if maze[x][y-1] == '.':
        pos.append([x, y-1, depth+1])
    #print(pos)
    #print(maze)


if (len(pos) == 0) & ng:
    print(-1)

### FIXED CODE ###

h, w = map(int, input().split())
maze = [['#'] * (w+2)]
cou = 0
for i in range(h):
    a = ['#'] + list(input()) + ['#']
    maze.append(a)
x = ['#'] * (w+2)
maze.append(x)

for i in range(1, h+1):
    for j in range(1, w+1):
        if maze[i][j] == '#':
            cou += 1

ng = True
pos = [[1, 1, 0]]
checklist = [[0] * (w+2) for _ in range(h+2)]
while len(pos) > 0:
    x, y, depth = pos.pop(0)
    if ([x, y] == [h, w]) & (maze[x][y] != '#'):
        ans = h * w - cou - depth - 1
        #print(cou)
        #print(depth)
        print(ans)
        ng = False
        break
    maze[x][y] = 'f'

    if (maze[x+1][y] == '.') & (checklist[x+1][y] == 0):
        pos.append([x+1, y, depth+1])
        checklist[x+1][y] = 1
    if (maze[x][y+1] == '.') & (checklist[x][y+1] == 0):
        pos.append([x, y+1, depth+1])
        checklist[x][y+1] = 1
    if (maze[x-1][y] == '.') & (checklist[x-1][y] == 0):
        pos.append([x-1, y, depth+1])
        checklist[x-1][y] = 1
    if (maze[x][y-1] == '.') & (checklist[x][y-1] == 0):
        pos.append([x, y-1, depth+1])
        checklist[x][y-1] = 1
    #print(pos)
    #print(maze)


if (len(pos) == 0) & ng:
    print(-1)
### BUGGY CODE ###

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


if n == 1:
    ans = a[0]//(2**m)
    print(ans)

else:
    for i in range(m):
        l = max(a)
        c = a.index(l)
        a[c] = a[c]//2
    print(sum(a))

### FIXED CODE ###

import heapq
n, m = map(int, input().split())
a = []
for i in list(map(int, input().split())):
    heapq.heappush(a,-i)

for _ in range(m):
    p = heapq.heappop(a)
    heapq.heappush(a, int(p/2))

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

N = int(input())
S = str(input())
R = []
G = []
B = []
for i in range(N):
    if S[i] == 'R':
        R.append(i+1)
    elif S[i] == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
ans = 0
count = 0
for k in R:
    for j in G:
        for i in B:
            fr = 0
            ab = [k,j,i]
            ab.sort()
            if ab[2] - ab[1] == ab[1] - ab[0]:
                fr += 1

            if fr == 0:
                ans += 1
print(ans)

### FIXED CODE ###

N = int(input())
N -= 1
S = str(input())
count = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
    for j in range(i,N+1):
        k = 2*j - i
        if k > N:
            pass
        elif S[i] == S[j] or S[i] == S[k] or S[j] == S[k]:
            pass
        else:
            count -= 1
print(count)
### BUGGY CODE ###

n=int(input())

robotLEFT=[]
robotRIGHT={}

for _ in range(n):
    x,l=map(int,input().split())
    if x-l not in robotLEFT:
        robotLEFT.extend([x-l])
        robotRIGHT[x-l]=x+l
    else:
        robotRIGHT[x-l]=min(robotRIGHT[x-l],x+l)

robotLEFT.sort()
#print(robotRIGHT)
#print(robotLEFT)

temp=robotLEFT[0]
LL=temp-2
RR=temp-1
cnt=0

for item in robotLEFT:
    #print(LL,RR,cnt)
    if RR<=item:
        cnt+=1
        LL=item
        RR=robotRIGHT[LL]
    else:
        if RR>=robotRIGHT[item]:
            RR=robotRIGHT[item]
            LL=item
print(cnt)




### FIXED CODE ###

n=int(input())

robot=[]

for _ in range(n):
    x,l=map(int,input().split())
    robot.append([x-l,x+l])
robot.sort()



RR=robot[0][0]-1
cnt=0

for item in robot:
    if RR<=item[0]:
        cnt+=1
        RR=item[1]
    elif RR>=item[1]:
        RR=item[1]

print(cnt)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
assert len(A) == N
total = 0
for i in range(0, N):
  for j in range(i+1, N):
    total += A[i] * A[j]
#    print(A[i], A[j])
print(total % (10 ** 9 + 7))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
assert len(A) == N
total = 0
sum_A = sum(A)
for i,ai in enumerate(A):
  sum_A -= ai
  total += ai * sum_A
print(total % (10 ** 9 + 7))
### BUGGY CODE ###

from collections import deque

s=input()
s=s.replace("BC","D")
s=deque(list( s ))
t=deque([]) #
cnt=0

#print(t,s)

while s!=deque([]):
  x=s.popleft()
  if t==deque([]):
    t.append(x)
  else:
    if t[-1]=="A" and x=="D":
      tmp=t.pop()
      s.appendleft(tmp)
      s.appendleft(x)
      cnt += 1
    else:
      t.append(x)
      
#print(t,s)        
print(cnt)

### FIXED CODE ###

from collections import deque

s=input()
s=s.replace("BC","D")
N=len(s)

cnt=0
cntA=0
cntD=0

for i in range(N):
  if s[i]=="A":
    cntA += 1
  elif s[i]=="D":
    cnt += cntA
  else:
    cntA=0
    cntD=0
    
print(cnt)
### BUGGY CODE ###

n, m = map(int, input().split())
S = input()
T = input()

def gcd(x, y):
    while y > 0:
        x, y = y, x%y
    return x

def lcm(x, y):
    return (x * y) // gcd(x, y)

l = lcm(n, m)
numS = [1]
numT = [1]

for i in range(1, n):
    num = i * l // n + 1
    numS.append(num)

for i in range(1, m):
    num = i * l // m + 1
    numT.append(num)
    
dictS = {}
dictT = {}

for i in range(n):
    dictS[numS[i]] = S[i]

for i in range(m):
    dictT[numT[i]] = T[i]
    
ans = l
for i in dictS:
    for j in dictT:
        if i == j:
            if dictS[i] != dictT[j]:
                ans = -1
                
print(ans)

### FIXED CODE ###

def gcd(x, y):
    while y != 0:
        x, y = y, x%y
    return x

def lcm(x, y):
    return (x * y) // gcd(x, y)

n, m = map(int, input().split())
S = input()
T = input()

l = lcm(n, m)
        
if S[::n // gcd(n, m)] == T[::m // gcd(n, m)]:
    print(l)
else:
    print(-1)
### BUGGY CODE ###

from sys import stdin
N,P=[int(x) for x in stdin.readline().rstrip().split()]
for i in reversed(range(int(pow(P,(1/N)))+2)):
    tmp=i**N
    if P==1:
        ans=1
        break
    if N==1:
        ans=P
        break
    if P%tmp==0:
        ans=i
        break
print(ans)

### FIXED CODE ###

import math
from sys import stdin
N,P=[int(x) for x in stdin.readline().rstrip().split()]
for i in reversed(range(round(math.pow(P,(1/N)))+1)):
    tmp=math.pow(i,N)
    if P==1:
        ans=1
        break
    if N==1:
        ans=P
        break
    if P%tmp==0:
        ans=i
        break
print(ans)
### BUGGY CODE ###

from itertools import permutations
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**6
path=[[INF]*n for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  path[a-1][b-1]=c
  path[b-1][a-1]=c
for i in range(n):
  for j in range(n):
    for k in range(n):
      if i!=j and j!=k and k!=i:
        path[i][j]=min(path[i][j],path[i][k]+path[k][j])
        path[j][i]=path[i][j]
ans=10**18
for root in permutations(vis):
  cnt=0
  for j in range(1,r):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(ans)

### FIXED CODE ###

from itertools import permutations
from scipy.sparse.csgraph import floyd_warshall
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**18
path=[[INF]*n for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  path[a-1][b-1]=c
  path[b-1][a-1]=c
path=floyd_warshall(path)
ans=10**18
for root in permutations(vis):
  cnt=0
  for j in range(1,r):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(int(ans))
### BUGGY CODE ###

def main(N, Q, S, lr_list):

    s_list = [0]
    for i in range(N):
        if i+1 < N and list(S)[i] == 'A' and list(S)[i+1] == 'C':
            s_list.append(s_list[i] + 1)
        else:
            s_list.append(s_list[i])

    res_list = []
    for lr in lr_list:
        l, r = lr
        l -= 1
        r -= 1
        res_list.append(s_list[r] - s_list[l])

    return res_list


if __name__ == "__main__":
    N, Q = list(map(int, input().split()))
    S = input()
    lr_list = []
    for _ in range(Q):
        lr_list.append(tuple(map(int, input().split())))
    res = main(N, Q, S, lr_list)
    for r in res:
        print(r)


### FIXED CODE ###

def main(N, Q, S, lr_list):
    chars = list(S)

    s_list = [0]
    for i in range(N):
        if i+1 < N and chars[i] == 'A' and chars[i+1] == 'C':
            s_list.append(s_list[i] + 1)
        else:
            s_list.append(s_list[i])

    res_list = []
    for lr in lr_list:
        l, r = lr
        l -= 1
        r -= 1
        res_list.append(s_list[r] - s_list[l])

    return res_list


if __name__ == "__main__":
    N, Q = list(map(int, input().split()))
    S = input()
    lr_list = []
    for _ in range(Q):
        lr_list.append(tuple(map(int, input().split())))
    res = main(N, Q, S, lr_list)
    for r in res:
        print(r)
### BUGGY CODE ###

from bisect import bisect
import sys
input=sys.stdin.readline

N,Q=map(int,input().split())
XST=[]
for i in range(N):
    S,T,X=map(int,input().split())
    XST.append([X,S,T])
XST.sort()
D=[[int(input()),i] for i in range(Q)]

ans=[-1]*Q
for X,S,T in XST:
    s,t=S-X,T-X
    left=bisect(D,[s])
    right=bisect(D,[t])
    
    for i in range(left,right):
        ans[D[i][1]]=X
    D=D[:left]+D[right:]
    
for u in ans:
    print(u)

### FIXED CODE ###

from bisect import bisect,bisect_left
import sys
input=sys.stdin.readline

def main():
    n,q=map(int,input().split())
    xst=[]
    for i in range(n):
        s,t,x=map(int,input().split())
        xst.append([x,s-x,t-x])
    xst.sort()
    d=[None]*q
    for i in range(q):
        d[i]=int(input())
    ans=[-1]*q
    skip=[-1]*q
    for u in xst:
        x ,left ,right = u
        a=bisect_left(d,left)
        b=bisect_left(d,right)
        while a < b:
            if skip[a] == -1:
                ans[a]=x
                skip[a]=b
                a+=1
            else:
                a=skip[a]
    for u in ans:
        print(u)

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

#163C
#management

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

for i in range(1,n+1):
    print(a.count(i))

### FIXED CODE ###

#163C
#management

n=int(input())
a=list(map(int,input().split()))
l=[0]*n
for i in range(n-1):
    l[a[i]-1]+=1
for i in range(n):
    print(l[i])
### BUGGY CODE ###

N,C,K = map(int,input().split())
T = []
for i in range(N):
    T.append(int(input()))
flag = 0
ans = 1
T = sorted(T)

while flag == 0:
    if C < len(T) or T[-1]-T[0]>=K:
        if C < len(T):
            if T[0]+K<T[C-1]:
                T = [i for i in T if i > T[0]+K]
                ans += 1
            else:
                T = T[C:]
                ans += 1
        else:
            T = [i for i in T if i > T[0]+K]
            ans += 1
    else:
        flag = 1
print(ans)

### FIXED CODE ###

N,C,K=map(int,input().split())
T=[]
for i in range(N):
    p=int(input())
    T.append(p)
  
ans = 1
T = sorted(T)
ind = 0
 
for i in range(len(T)):
    if T[i] > T[ind]+K:
        ans += 1
        ind = i
    elif i >= ind + C:
        ans += 1
        ind = i
print(ans)
### BUGGY CODE ###

N = int(input())
S = input()
from collections import deque
C =[deque([]),deque([]),deque([])]
for i in range(N):
    if S[i] == 'R':
        C[0].append(i)
    elif S[i] == 'G':
        C[1].append(i)
    else:
        C[2].append(i)

count = 0
l = len(C[2])
for i in C[0]:
    for j in C[1]:
        count += l
        if max(i,j) + abs(j-i) in C[2]:
            count -= 1
        if min(i,j) - abs(j-i) in C[2]:
            count -= 1
        if (i + j)%2 == 0 and (i + j)//2 in C[2]:
            count -= 1


print(count)


### FIXED CODE ###

N = int(input())
S = input()
from collections import deque
C =[deque([]),deque([]),deque([])]
for i in range(N):
    if S[i] == 'R':
        C[0].append(i)
    elif S[i] == 'G':
        C[1].append(i)
    else:
        C[2].append(i)

count = len(C[0])*len(C[1])*len(C[2])

for i in C[0]:
    for j in C[1]:
        if max(i,j) + abs(j-i)<N and S[max(i,j) + abs(j-i)] == "B":
            count -= 1
        if min(i,j) - abs(j-i)>=0 and S[min(i,j) - abs(j-i)] == "B":
            count -= 1
        if (i + j)%2 == 0 and S[(i + j)//2] == "B":
            count -= 1


print(count)
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
A = [0 for i in range(N)]
for i in range(N-1):
  if S[i] == 'A' and S[i+1] == 'C':
    A[i] = 1
for i in range(Q):
  l, r = map(int, input().split())
  T = A[(l-1):(r-1)]
  ans = sum(T)
  print(ans)

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
A = []
a = 0
for i in range(N-1):
  if S[i] == 'A' and S[i+1] == 'C':
    a += 1
    A.append(a)
  else:
    A.append(a)
for i in range(Q):
  l, r = map(int, input().split())
  if l>=2:
    ans = A[r-2]-A[l-2]
    print(ans)
  else:
    ans = A[r-2]
    print(ans)
### BUGGY CODE ###

def main():
  s = ''.join(reversed(input()))
  multiplier = 1
  divisor = 10 ** 9 + 7
  p = [0 for i in range(13)]
  np = [0 for i in range(13)]
  if s[0] != '?':
    p[int(s[0])] = 1
  else:
    for j in range(10):
      p[j] = 1
  for i in range(1, len(s)):
    multiplier = (10 * multiplier) % 13
    if s[i] != '?':
      r = int(s[i]) * multiplier
      for j in range(13):
        np[(j + r) % 13] = p[j]
    else:
      for j in range(10):
        r = multiplier * j
        for k in range(13):
          np[(k + r) % 13] += p[k]
    for j in range(13):
      p[j] = np[j] % divisor
      np[j] = 0
  print(p[5] % divisor)
main()

### FIXED CODE ###

s = ''.join(reversed(input()))
multiplier = 1
divisor = 10 ** 9 + 7
p = [0 for i in range(13)]
if s[0] != '?':
  p[int(s[0])] = 1
else:
  for j in range(10):
    p[j] = 1
for i in range(1, len(s)):
  multiplier = (10 * multiplier) % 13
  np = [0 for j in range(13)]
  if s[i] != '?':
    r = int(s[i]) * multiplier
    for j in range(13):
      np[(j + r) % 13] = p[j]
  else:
    for j in range(10):
      r = j * multiplier
      for k in range(13):
        np[(k + r) % 13] += p[k]
  for j in range(13):
    p[j] = np[j] % divisor
print(p[5] % divisor)
### BUGGY CODE ###

N = int(input())

# initial
p_t, p_a = 1, 1

for i in range(N) :
    t, a = map(int, input().split())
    t_up, a_up = t, a
    
    if t >= p_t and a >= p_a : 
        p_t, p_a = t, a
    else :
        while t_up < p_t or a_up < p_a :
            t_up += t
            a_up += a
            # print("DBG2 : ", t, a)
            
        p_t, p_a = t_up, a_up
        
    # print(p_t, p_a)

print(p_t + p_a)
    
    
    
    

### FIXED CODE ###

N = int(input())

# initial
p_t, p_a = 1, 1

for i in range(N) :
    t, a = map(int, input().split())
    t_up, a_up = t, a
    
    if t >= p_t and a >= p_a : 
        p_t, p_a = t, a
    else :
        f = max(
            p_t // t if p_t % t == 0 else p_t // t + 1,  
            p_a // a if p_a % a == 0 else p_a // a + 1
        )
        
        p_t, p_a = t*f, a*f

print(p_t + p_a)
### BUGGY CODE ###

import copy
n,m=map(int,input().split())

cost = [[float("inf") for i in range(n)] for i in range(n)] 

for i in range(m):
    x,y = map(int,input().split())
    cost[x-1][y-1] = 1
    cost[y-1][x-1] = 1

dist=[0]*n    
flag=[False]*n
flag[0]=True
que=[]
que.append(0)
while(False in flag):
    v=que.pop(0)
    tmp=copy.deepcopy(que)
    for i in range(n):
        if(cost[v][i]==1 and flag[i]==False):
            dist[i]=v+1
            flag[i]=True
            que.append(i)
    if(tmp==que):
        print("No")
        exit()
print("Yes")
for i in dist[1:]:
    print(i)    


### FIXED CODE ###

from collections import deque
n,m=map(int,input().split())
to=[[] for i in range(n)]
for i in range(m):
    x,y = map(int,input().split())
    to[x-1].append(y-1)
    to[y-1].append(x-1)
dist=[None]*n    

que=deque([0])
while que:
    v=que.pop()
    for i in to[v]:
        if(dist[i] is None):
            dist[i]=v+1
            que.appendleft(i)
        
print("Yes")
for i in dist[1:]:
    print(i)
### BUGGY CODE ###

import sys
from collections import namedtuple
from heapq import heappush, heappop

INF = sys.maxsize

P = namedtuple('P', 'cost v')
Edage = namedtuple('Edge', 'to cost')

n, m, s, t = map(int, input().split())

s, t = s - 1, t - 1

# 円とスヌークそれぞれグラフを構成する
Yen = [[] for _ in range(n)]
Snuuk = [[] for _ in range(n)]

for _ in range(m):
  u, v, a, b = map(int, input().split())
  
  u, v = u - 1, v - 1
  
  Yen[u].append(Edage(v, a))
  Yen[v].append(Edage(u, a))
  Snuuk[u].append(Edage(v, b))
  Snuuk[v].append(Edage(u, b))

  
def min_costs(graph, start):
  costs = [INF] * n
  costs[start] = 0
  
  q = [P(0, start)]
  
  while q:
    cost, v = heappop(q)
    if cost > costs[v]:
      continue
    for to, cost in graph[v]:
      if costs[to] > costs[v] + cost:
        costs[to] = costs[v] + cost
        heappush(q, P(costs[to], to))        
        
  return costs
  
total_costs = [
  min_costs(Yen, s)[i] + min_costs(Snuuk, t)[i] 
  for i in range(n)
]

# 両替所が閉鎖されていく
for i in range(n - 1, 0, -1):
  total_costs[i - 1] = min(total_costs[i - 1], total_costs[i])
                      
    
for i in range(n):
  print(10 ** 15 - total_costs[i])
  
  

### FIXED CODE ###

import sys
from collections import namedtuple
from heapq import heappush, heappop


INF = sys.maxsize
Edge = namedtuple('Edge', 'to yen snuuk')
Vertex = namedtuple('Vertex', 'cost id')

n, m, s, t = map(int, input().split())

edges = [[] for _ in range(n + 1)]

for _ in range(m):
  u, v, a, b = map(int, input().split())
  edges[u].append(Edge(v, a, b))
  edges[v].append(Edge(u, a, b))

costs_in_yen = [INF] * (n + 1)
costs_in_snuuk = [INF] * (n + 1)

q = []

heappush(q, Vertex(0, s))
costs_in_yen[s] = 0

while q:
  v = heappop(q)
  if v.cost > costs_in_yen[v.id]:
    continue
  for edge in edges[v.id]:
    if v.cost + edge.yen < costs_in_yen[edge.to]:
      costs_in_yen[edge.to] = v.cost + edge.yen
      heappush(q, Vertex(costs_in_yen[edge.to], edge.to))
      
heappush(q, Vertex(0, t))
costs_in_snuuk[t] = 0

while q:
  v = heappop(q)
  if v.cost > costs_in_snuuk[v.id]:
    continue
  for edge in edges[v.id]:
    if v.cost + edge.snuuk < costs_in_snuuk[edge.to]:
      costs_in_snuuk[edge.to] = v.cost + edge.snuuk
      heappush(q, Vertex(costs_in_snuuk[edge.to], edge.to))      

# n 年後の手持ち snuuk の最大額
snuuk_in_hand = [0] * (n + 1)

for i in range(n - 1, -1, -1):
  snuuk_in_hand[i] = max(
    snuuk_in_hand[i + 1],
    10**15 - costs_in_yen[i + 1] - costs_in_snuuk[i + 1]
  )

for i in range(0, n):
  print(snuuk_in_hand[i])
### BUGGY CODE ###

import itertools

MOD = 10**9 + 7
N = int(input())
A = [int(i) for i in input().split()]


def main():
    ans = sum([a ^ b for (a, b) in itertools.combinations(A, 2)]) % MOD
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import itertools

MOD = 10**9 + 7
N = int(input())
A = [int(i) for i in input().split()]


def main():
    count_1 = [0] * 61
    for i in range(60):
        tmp = 0
        for j, a in enumerate(A):
            tmp += (a >> i) & 1
        count_1[i] += tmp
    ans = 0
    for i, c1 in enumerate(count_1):
        c0 = len(A) - c1
        ans += c0 * c1 * 2**i
    ans %= MOD
    print(ans)


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

import sys
sys.setrecursionlimit(10**6)

n = int(input())
readline = sys.stdin.readline
a = [int(input()) for _ in range(n)]

from collections import deque
a = deque(a)

ans = []

for _ in range(n):
    tmp = a.popleft()
    ans.append(max(a))
    a.append(tmp)

print(*ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)

n = int(input())
readline = sys.stdin.readline
A = [int(input()) for _ in range(n)]

import heapq

Q = [-a for a in A]
heapq.heapify(Q)

ans = []

for i in range(n):
    if -Q[0] == A[i]:
        tmp = heapq.heappop(Q)
        ans.append(-Q[0])
        heapq.heappush(Q, tmp)
    else:
        ans.append(-Q[0])

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

def main():
    N,M = map(int,input().split())
    A =[int(x) for x in input().split()]
    A.sort()
    for _ in range(M):
        b,c = map(int,input().split())
        for i in range(b):
            if A[i]<c:
                A[i]=c
        A.sort()
    print(sum(A))
main()    

### FIXED CODE ###

from operator import itemgetter

def main():
    N,M = map(int, input().split())
    A = [[1,int(x)] for x in input().split()]
    for _ in range(M):
        b,c = map(int, input().split())
        A.append([b,c])
    A.sort(key=itemgetter(1))
    A.reverse()
    n=0
    s=0
    for a in A:
        n+=a[0]
        if n<=N:
            s+=a[1]*a[0]
        elif n>N:
            d=a[0]
            e=a[1]
            break
    s+=(N-n+d)*e
    print(s)
    
main()
### BUGGY CODE ###

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

b = sorted(a)
for i in range(N):
    if i == a.index(max(a)):
        print(b[-2])
    else:
        print(b[-1])

### FIXED CODE ###

n=int(input())
a = [int(input()) for i in range(n)]
b = sorted(a)
for i in a:
    print(b[-2] if i==b[-1] else b[-1])