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

import sys
import math
import heapq
mod=10**9+7
inf=float("inf")
from math import sqrt
from collections import deque
from collections import Counter
from math import ceil
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########
import sys
import math
import heapq
mod=10**9+7
inf=float("inf")
from math import sqrt
from collections import deque
from collections import Counter
from math import ceil
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########
n=int(input())
A=[]
for i in range(n):
  A.append(input())
  
PM=[[0,0] for i in range(n)]
for i in range(n):
    now=0
    mini=0
    for j in A[i]:
        if j=="(": now+=1 
        else:
            now-=1 ; mini=min(mini,now)
    PM[i]=[mini,now]

if sum([PM[i][1] for i in range(n)]  )!=0 : 
    print("No")
    exit()

MINI=0
NOW=0
PMf=[PM[i] for i in range(n) if PM[i][1]>=0]
PMf.sort()
for i in range(len(PMf)):
    MINI=min(MINI , NOW+PMf[-i-1][0] )
    NOW+=PMf[-i-1][1]
    if MINI<0 : print("No") ; exit()

PMs=[PM[i] for i in range(n) if PM[i][1]<0]
PMs=sorted(PMs , key=lambda x : x[1]-x[0])
for i in range(len(PMs)):
    MINI=min(MINI , NOW+PMs[-i-1][0] )
    NOW+=PMs[-i-1][1]
    if MINI<0 : print("No") ; exit()

print("Yes")


### FIXED CODE ###

import sys
input=lambda: sys.stdin.readline().strip()


n=int(input())
A=[]
#
PM=[[0,0] for i in range(n)]
for i in range(n):
    now=0
    mini=0
    for j in input():
        if j=="(": now+=1 
        else:
            now-=1 ; mini=min(mini,now)
    PM[i]=[mini,now]

if sum( [PM[i][1] for i in range(n)]  )!=0 : 
    print("No")
    exit()

MINI=0
NOW=0
PMf=[PM[i] for i in range(n) if PM[i][1]>=0]
PMf.sort()
for i in range(len(PMf)):
    MINI=min(MINI , NOW+PMf[-i-1][0] )
    NOW+=PMf[-i-1][1]
    if MINI<0 : print("No") ; exit()

PMs=[PM[i] for i in range(n) if PM[i][1]<0]
PMs=sorted(PMs , key=lambda x : x[1]-x[0])
for i in range(len(PMs)):
    MINI=min(MINI , NOW+PMs[-i-1][0] )
    NOW+=PMs[-i-1][1]
    if MINI<0 : print("No") ; exit()

print("Yes")
### BUGGY CODE ###

import sys;sys.setrecursionlimit(9**9);*A,=map(int,open(0))
def f(i,t):
 if i==2:return len(t)
 if i in t:return -1
 return f(A[i],t+[i])
print(f(1,[]))

### FIXED CODE ###

*A,=map(int,open(0));i=1;t=[0]*A[0]+[0]
while i-2and t[i]==0:t[i]=1;i=A[i]
print(-1if i-2else sum(t))
### BUGGY CODE ###

# 入力
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

n, ma, mb = li()
a,b,c = list(), list(), list()
for _ in range(n):
    ai,bi,ci = li()
    a.append(ai)
    b.append(bi)
    c.append(ci)
    
INF = 10**5
dp = [[[INF for _ in range(10*n+1)] for _ in range(10*n+1)] for _ in range(n+1)]
dp[0][0][0] = 0

for dr in range(n):
    for wa in range(10*n+1):
        for wb in range(10*n+1):
            dp[dr+1][wa][wb] = min(dp[dr][wa][wb], dp[dr][wa-a[dr]][wb-b[dr]] + c[dr])
            
ans = INF
k = 1
while k*ma <= 10*n and k*mb <= 10*n:
    ans = min(ans, dp[n][k*ma][k*mb])
    k += 1
    
if ans == INF:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

import sys
sdin = sys.stdin.readline

N,Ma,Mb = map(int, sdin().split())
ABC = []
for i in range(N):
    ABC.append(tuple(map(int, sdin().split())))


sum_a = [0]
sum_b = [0]
for a,b,c in ABC:
    sum_a.append(sum_a[-1]+a)
    sum_b.append(sum_b[-1]+b)
    
INF = float("inf")
MAX_W = 400
dp = [[[INF for _ in range(sum_b[-1]+1)] for _ in range(sum_a[-1]+1)] for _ in range(N+1)]
for i in range(N+1):
    dp[i][0][0] = 0

for i, (ai, bi, ci) in enumerate(ABC):
    for a in range(sum_a[i+1]+1):
        for b in range(sum_b[i+1]+1):
            if a-ai>=0 and b-bi>=0:
                dp[i+1][a][b] = min(dp[i][a][b], dp[i][a-ai][b-bi]+ci)
            else:
                dp[i+1][a][b] = dp[i][a][b]
        
k = 1           
ans = INF
while (k*Ma<=sum_a[-1] and k*Mb<=sum_b[-1]):
    ans = min(ans, dp[N][k*Ma][k*Mb])
    k += 1
    
if ans == INF:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

import heapq
N,K=[int(s) for s in input().split()]
ls1=[int(s) for s in input().split()]
ls2=[int(s) for s in input().split()]
ls1.sort()
ls2.sort()
cost=[]

ls3=[ls1[i]*ls2[N-1-i] for i in range(N)]

for i in range(N):
  heapq.heappush(cost,(-ls1[i]*ls2[N-1-i],ls1[i],ls2[N-i-1]))
for i in range(K):
  xy, x, y = heapq.heappop(cost)
  if x==0:
    heapq.heappush(cost,(-1*x*y,x,y))
    break
  else:
    heapq.heappush(cost,(-1*(x-1)*y,x-1,y))
xy,x,y=heapq.heappop(cost)
print(-1*xy)  


### FIXED CODE ###

import math
N,K=[int(s) for s in input().split()]
ls1=[int(s) for s in input().split()]
ls2=[int(s) for s in input().split()]
ls1.sort()
ls2.sort()
M=max([ls1[i]*ls2[N-1-i] for i in range(N)])
#print(M)
###aまで下げることが可能か？
def pos(a):
  s=0
  for i in range(N):
    s+=max([0,math.ceil(ls1[i]-a/ls2[N-1-i])])
  if s>K:
    return 0
  else:
    return 1
x=0
y=M//2
z=M
while z-x>1:
  if pos(y)==1:
    z=y
  else:
    x=y
  y=(x+z)//2
#  print(x,y,z)
if pos(y)==1:
  print(y)
else:
  print(z)
### BUGGY CODE ###

N = int(input())

ls = [input()]
count = 1
for i in range(N-1):
    if input() in ls:
        pass
    else:
        ls.append(i)
        count += 1
        
print(count)

### FIXED CODE ###

N = int(input())
ls = []
for i in range(N):
    ls.append(input())

print(len(set(ls)))
### BUGGY CODE ###

n,k = map(int,input().split())
H = list(map(int,input().split()))
H = sorted(H,reverse = True)
for i in range(k):
  if H == []:
    print(0)
    exit()
  else:
    H.pop(0)
print(sum(H))

### FIXED CODE ###

n,k = map(int,input().split())
H = list(map(int,input().split()))
H = sorted(H,reverse = True)
if k >= n:
  print(0)
else:
  del H[0:k]
  print(sum(H))
### BUGGY CODE ###

N = int(input())
A = [0]+list(map(int,input().split()))
A.sort()
B = [0]+list(map(int,input().split()))
B.sort()
C = [0]+list(map(int,input().split()))
C.sort()

bko = [0 for i in range(N+1)]
for i in range(1,N+1):
  b = B[i]
  cnt = 1
  for j in range(cnt,N+1):
    if A[j] >= b:
      bko[i] = j-1
      cnt = j-1
      break
    if j == N:
      bko[i] = N

#bkoを累積和にする
for i in range(N):     
  bko[i+1] += bko[i]

cko = [0 for i in range(N+1)]
for i in range(1,N+1):
  c = C[i]
  cnt = 0
  for j in range(cnt,N+1):
    if B[j] >= c:
      cko[i] = bko[j-1]
      cnt = j-1
      break
    if j == N:
      cko[i] = bko[N]
###
print(sum(cko))        

### FIXED CODE ###

N = int(input())
A = [0]+list(map(int,input().split()))
A.sort()
B = [0]+list(map(int,input().split()))
B.sort()
C = [0]+list(map(int,input().split()))
C.sort()

bko = [0 for i in range(N+1)]
cnt = 1
for i in range(1,N+1):
  b = B[i]
  for j in range(cnt,N+1):
    if A[j] >= b:
      bko[i] = j-1
      cnt = j
      break
    if j == N:
      bko[i] = N
      cnt = N

#bkoを累積和にする
for i in range(N):     
  bko[i+1] += bko[i]

cko = [0 for i in range(N+1)]
cnt = 1
for i in range(1,N+1):
  c = C[i]
  for j in range(cnt,N+1):
    if B[j] >= c:
      cko[i] = bko[j-1]
      cnt = j
      break
    if j == N:
      cko[i] = bko[N]
      cnt = N
###
print(sum(cko))
### BUGGY CODE ###

import sys
H, W, M = map(int, input().split())
hList = {i+1: 0 for i in range(H)}
wList = {i+1: 0 for i in range(W)}
pList = set()
#print(hList, wList, pList)
for _ in range(M):
  h, w = list(map(int, input().split()))
  #print(h, w)
  hList[h] += 1
  wList[w] += 1
  pList.add((h, w))
#print(hList, wList, pList)

maxCount = max(hList.values())
maxHList = [h for h, count in hList.items() if count==maxCount]
if maxCount==0:
	print(0)
	sys.exit()


maxCount = max(wList.values())
maxWList = [w for w, count in wList.items() if count==maxCount]

counterList = []
for maxH in maxHList:
  for maxW in maxWList:
    counterList.append(hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0))
print(max(counterList))


### FIXED CODE ###

import sys
H, W, M = map(int, input().split())
hList = {i+1: 0 for i in range(H)}
wList = {i+1: 0 for i in range(W)}
pList = set()
#print(hList, wList, pList)
for _ in range(M):
  h, w = list(map(int, input().split()))
  #print(h, w)
  hList[h] += 1
  wList[w] += 1
  pList.add((h, w))
#print(hList, wList, pList)

maxHCount = max(hList.values())
maxHList = [h for h, count in hList.items() if count==maxHCount]


maxWCount = max(wList.values())
maxWList = [w for w, count in wList.items() if count==maxWCount]

#counterList = []
#for maxH in maxHList:
#  for maxW in maxWList:
#    counterList.append(hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0))
#print(max(counterList))
#print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))
#print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))

#if M < len(maxHList)*len(maxWList):
#	print(maxHCount + maxWCount)
#	sys.exit(0)

for maxH in maxHList:
  for maxW in maxWList:
    if (maxH, maxW) not in pList:
      print(maxHCount + maxWCount)
      sys.exit(0)

print(maxHCount + maxWCount - 1)
### BUGGY CODE ###

def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    left = [[0] * W for _ in range(H)]
    right = [[0] * W for _ in range(H)]
    up = [[0] * W for _ in range(H)]
    down = [[0] * W for _ in range(H)]
    for i in range(H):
        cnt = 0
        for j in range(W):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            left[i][j] = cnt
    
    for i in range(H):
        cnt = 0
        for j in range(W-1,-1,-1):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            right[i][j] = cnt
    
    for j in range(W):
        cnt = 0
        for i in range(H):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            up[i][j] = cnt
    
    for j in range(W):
        cnt = 0
        for i in range(H-1,-1,-1):
            if S[i][j] == '#':
                cnt = 0
            else:
                cnt += 1
            down[i][j] = cnt
    
    ans = 0
    for i in range(H):
        for j in range(W):
            tmp = up[i][j] + down[i][j] + left[i][j] + right[i][j] - 3
            ans = max(ans, tmp)


    print(ans)
    


if __name__ == "__main__":
    main()


### FIXED CODE ###

# https://atcoder.jp/contests/abc129/tasks/abc129_d
from copy import deepcopy
def main():
    h, w = map(int, input().split())
    matrix = []
    for _ in range(h):
        row = list(input())
        matrix.append(row)

    left = [[0] * w for _ in range(h)] # 左から右
    right = [[0] * w for _ in range(h)] # 右から左
    up = [[0] * w for _ in range(h)] # 上から下
    down = [[0] * w for _ in range(h)] # 下から上

    for i in range(h):
        c = 0
        for j in range(w):
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            left[i][j] = c
    for i in range(h):
        c = 0
        for j in range(w)[::-1]:
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            right[i][j] = c
    for j in range(w):
        c = 0
        for i in range(h):
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            up[i][j] = c
    for j in range(w):
        c = 0
        for i in range(h)[::-1]:
            if matrix[i][j] == '.':
                c += 1
            else:
                c = 0
            down[i][j] = c
    ans = 1
    for i in range(h):
        for j in range(w):
            t = left[i][j] + right[i][j] + up[i][j] + down[i][j] - 3
            ans = max(ans, t)
    print(ans)

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

from operator import mul
from functools import reduce
n=int(input())
a=list(map(int,input().split()))
if reduce(mul, a)<=10**18:
    print(reduce(mul, a))
else:
    print(-1)

### FIXED CODE ###

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

n = int(input())
s = str(input())
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r * g * b
for i in range(2001):
    for j in range(n-2):
        if j + (2*i) <= n-1:
            if s[j] != s[j+i] and s[j+i] != s[j+(2*i)] and s[j+(2*i)] != s[j]:
                total -= 1
print(total)

### FIXED CODE ###

n = int(input())
s = str(input())
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r * g * b
for i in range(2000):
    for j in range(n-2*i):
        if j + (2*i) <= n-1:
            if s[j] != s[j+i] and s[j+i] != s[j+(2*i)] and s[j+(2*i)] != s[j]:
                total -= 1
print(total)
### BUGGY CODE ###

import collections
import itertools

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

check = [[0] * N for _ in range(N)]


cnt = 0
for i in range(N):
    c = []
    x = collections.Counter(moji[i])

    c.append(i)

    for j in range(i + 1, N):
        if check[i][j] == 1:
            continue

        y = collections.Counter(moji[j])

        if x == y:
            c.append(j)

    if len(c) > 1:
        for u, v in itertools.combinations(c, 2):
            check[u][v] = 1
            cnt += 1

print(cnt)


### FIXED CODE ###

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

dic = {}
cnt = 0

for moji in moji_lst:
    x = ''.join(sorted(moji))

    if x in dic:
        dic[x] += 1
        cnt += dic[x]
    else:
        dic[x] = 0


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

s = input()
q = int(input())
for i in range(q) :
    t = input()
    if len(t) == 5 :
        f = t[2]
        c = t[4]
        if f == "1" :
            s = c + s
        else :
            s = s + c
    else :
        s = s[::-1]
print(s)

### FIXED CODE ###

right = input()
Q = int(input())
flag = 0b1
left = ""
for i in range(Q):
    q = input()
    if q == "1":
        flag ^= 1
        continue
    m,f,c = q.split(" ")
    f = int(f)
    left += c*(1-flag)*(f-1)
    right += c*flag*(f-1)
    left += c*flag*(2-f)
    right += c*(1-flag)*(2-f)
if flag == 1:
    print(left[::-1]+right)
else:
    print(right[::-1]+left)
### BUGGY CODE ###

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

d = []
for i in range(M):
    L,R = map(int,input().split())
    d += list(range(L,R+1))

res = [x for x in set(d) if d.count(x) == M]
print(len(res))

### FIXED CODE ###

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

L_list = []
R_list = []

for i in range(M):
    L,R = map(int,input().split())
    L_list.append(L)
    R_list.append(R)

l = max(L_list)
r = min(R_list)

if(l > r):
    print(0)
else:
    print(r-l+1)
### BUGGY CODE ###

n = int(input())
xl = [[int(i) for i in input().split(' ')] for j in range(n)]
import numpy as np

ls = np.array([[i-j, i+j] for i, j in xl])
ls = ls[ls[:, 0].argsort()]

i, dr = 0, 0
while i in range(len(ls)-1):
    a, b = ls[i], ls[i+1]
    if a[1]>b[0]:
        dr += 1
        ls = np.delete(ls, i+1, 0)
    else:
        i += 1
        if i >= len(ls):
            break
print(len(ls))

### FIXED CODE ###

n = int(input())
xl = [[int(i) for i in input().split(' ')] for j in range(n)]

ls = sorted([[i-j, i+j] for i, j in xl], key=lambda x: x[1])

i, dr = 0, 0
while True:
    if (len(ls)<=1) | (i+1 >= len(ls)):
        break
    a, b = ls[i], ls[i+1]
    if a[1]>b[0]:
        dr += 1
        ls.pop(i+1)
    else:
        i += 1

print(len(ls))
### BUGGY CODE ###

N = int(input())
n = [x for x in range(1, N+1)]
print(sum(n[:-1]))

### FIXED CODE ###

N = int(input())
print((N+1) * N // 2 - N)
### BUGGY CODE ###

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

for i in range(n):
    memory = a[0]
    a.remove(a[0])
    print(max(a))
    a.append(memory)

### FIXED CODE ###

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

m = max(a)
count = a.count(m)

if count != 1:
    for i in range(n):
        print(m)

else:
    switch = 0
    for i in range(n-1):
        if a[i] == m:
            a.remove(m)
            m2max = max(a)
            print(m2max)
            a.append(m)
            switch = 1
        else:
            print(m)
    
    if switch == 1:
        print(m)
    else:
        a.remove(m)
        m2max = max(a)
        print(m2max)
### BUGGY CODE ###

import sys
import bisect
h, w, d = [int(i) for i in sys.stdin.readline().split()]
a_ls = []
for y in range(h):
    a = [int(i) for i in sys.stdin.readline().split()]
    for x in range(w):
        bisect.insort_left(a_ls, (a[x], x, y))

#a_ls.sort(key=lambda x:x[0])
q = int(input())
for j in range(q):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    _sum = 0
    for ind in range(l, r, d):
        ind -= 1
        start = a_ls[ind]
        goal = a_ls[ind+d]
        _sum += abs(start[2] - goal[2]) + abs(start[1] - goal[1])
    print(_sum)

### FIXED CODE ###

import sys
import bisect
h, w, d = [int(i) for i in sys.stdin.readline().split()]
a_ls = []
for y in range(h):
    a = [int(i) for i in sys.stdin.readline().split()]
    for x in range(w):
        a_ls += [(a[x], x, y)]

a_ls.sort(key=lambda x:x[0])
diff_ls = []
for ind in range(h*w):
    if ind < d:
        diff_ls.append(0)
    else:
        start = a_ls[ind-d]
        goal = a_ls[ind]
        diff = diff_ls[ind-d] + abs(start[2] - goal[2]) + abs(start[1] - goal[1])
        diff_ls.append(diff)
q = int(input())
for j in range(q):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    l -= 1
    r -= 1
    print(diff_ls[r] - diff_ls[l])
### BUGGY CODE ###

# coding: utf-8
# Your code here!
n,a,b=map(int,input().split())
s=list(input())
t=[]
count=1
for i in range(n):
    if s[i]=='a':
        if count<=a+b:
            count+=1
            print('Yes')
        else:
            print('No')
    elif s[i]=='b':
        t.append(s[i])
        if count<=a+b and t.count('b')<=b:
            count+=1
            print('Yes')
        else:
            print('No')
    else:
        print('No')

### FIXED CODE ###

# coding: utf-8
# Your code here!
n,a,b=map(int,input().split())
s=list(input())
count=1
c=1
for i in range(n):
    if s[i]=='a':
        if count<=a+b:
            count+=1
            print('Yes')
        else:
            print('No')
    elif s[i]=='b':
        if count<=a+b and c<=b:
            count+=1
            c+=1
            print('Yes')
        else:
            print('No')
    else:
        print('No')
### BUGGY CODE ###

n,ma,mb=map(int,input().split())
ab=[[[10**6]*401 for _ in range(401)] for _ in range(n+1)]
ab[0][0][0]=0

for p in range(1,n+1):
    a,b,c=map(int,input().split())
    for i in range(400):
        for j in range(400):
            if i+a<=400 and j+b<=400:
                ab[p][i+a][j+b]=min(ab[p][i+a][j+b],ab[p-1][i][j]+c)

ans=10**6-1
for p in range(1,n+1):
    for i in range(401):
        for j in range(401):
            if i!=0 and j!=0:
                if i*mb==j*ma:
                    ans=min(ans,ab[p][i][j])

if ans==10**6-1:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

n,ma,mb=map(int,input().split())
ab=[[[10**6]*401 for _ in range(401)] for _ in range(n+1)]
ab[0][0][0]=0

for p in range(1,n+1):
    a,b,c=map(int,input().split())
    for i in range(400):
        for j in range(400):
            ab[p][i][j]=min(ab[p][i][j],ab[p-1][i][j])
            if i+a<=400 and j+b<=400:
                ab[p][i+a][j+b]=min(ab[p][i+a][j+b],ab[p-1][i][j]+c)

ans=10**6-1
for i in range(401):
    for j in range(401):
        if i!=0 and j!=0:
            if i*mb==j*ma:
                ans=min(ans,ab[n][i][j])

if ans==10**6-1:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

n=int(input())
a=[]
m=0
for i in range(n):
    a.append(int(input()))
for i in range(n):
    if a[i]!=max(a):
        print(max(a))
    else:
        m=a[i]
        a[i]=1
        print(max(a))
        a[i]=m

### FIXED CODE ###

n=int(input())
a=[]
m1=0
m2=0
for i in range(n):
    a.append(int(input()))
    A=a[i]
    if a[i]>m1:
        m2=m1
        m1=a[i]
        index=i
    elif m1==a[i]:
        m2=a[i]
    elif a[i]>m2:
        m2=a[i]
for i in range(n):
    if i==index:
        print(m2)
    else:
        print(m1)
### BUGGY CODE ###

import math
n=int(input())
a=list(map(int,input().split()))
m=math.ceil(math.log(max(1,max(a)),2))

ans=0
for i in range(m):
  sum0=0
  sum1=0
  for j in range(n):
    if (a[j]>>i)&1:
      sum1+=1
    else:
      sum0+=1
  ans+=2**i*sum1*sum0
print(ans%(10**9+7))

### FIXED CODE ###

import math
n=int(input())
al=list(map(int,input().split()))
a=['{:060b}'.format(x) for x in al]
a=''.join(a)

m=math.ceil(math.log(max(1,max(al)),2))

ans=0
for i in range(m):
  sum1=a[59-i::60].count('1')
  sum0=n-sum1
  ans+=2**i*sum1*sum0

print(ans%(10**9+7))
### BUGGY CODE ###

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

A=[int(x) for x in input().split()]
dp=[[float('inf') for x in range(N)] for y in range(N)]

#dp[0]=min(A)を捕まえる最短時間を調べる。
m=A.index(min(A))
A=A[m:]+A[:m]

#dp[i][j]=A[j]のスライムをi回魔法を唱えて捕まえるための最小時間。
#dp[0][0]=A[0],dp[0][1]=A[1],dp[0][2]=A[2]...で初期化。
for i in range(N):
  dp[0][i]=A[i]

#dp[1][1]=min(A[1],dp[0][0]),dp[1][2]=min(A[2],dp[0][1])...
#dp[i][j]=min(A[i],dp[i-1][j-1])
#最終的には回数ごとのsum+k*xの最小値が答え。
for i in range(1,N):
  for j in range(N):
    dp[i][j]=min(A[j],dp[i-1][j-1])

ans=float('inf')
for i in range(N):
  ans=min(ans,sum(dp[i])+i*x)

print(ans)

### FIXED CODE ###

N,x=map(int,input().split())
A=[int(x) for x in input().split()]
dp=A[:]

#dp[i]:k回魔法を唱えた時にA[i]を手に入れる最小の時間。
#dp=Aで初期化。その後、１〜Nまでの各ステップでdp[i]はmin(dp[i],A[i-k])で順に更新されていく。
#各回におけるsum(dp)+k*xを算出して最小値が答え。

#ans初期値は魔法を唱えず、全てを入手した数字（sum(A))
ans=sum(A)
for k in range(1,N):
  for i in range(N):
    if dp[i]>A[i-k]:
      dp[i]=A[i-k]
  ans=min(ans,sum(dp)+x*k)

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

import math

A,B,Q = map(int, input().split())
S=[0]*A
T=[0]*B
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
S.append(10**11)
S.insert(0,(-1)*(10**11))
T.append(10**11)
T.insert(0,(-1)*(10**11))
for i in range(Q):
  x = int(input())
  floor = 0
  ceil = len(S)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    s = S[half]
    if s < x:
      floor = half
    else:
      ceil = half
  nearest_x_S = [S[floor]-x, S[ceil]-x]
        
  floor = 0
  ceil = len(T)-1
  while ceil-floor > 1:
    half = math.floor((floor+ceil)/2)
    t = T[half]
    if t < x:
      floor = half
    else:
      ceil = half
  nearest_x_T = [T[floor]-x, T[ceil]-x]
  
  ans_cand = []
  for x_S in nearest_x_S:
    for x_T in nearest_x_T:
      if x_S*x_T > 0:
        ans_cand.append(max(abs(x_S),abs(x_T)))
      else:
        ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))
  
  


### FIXED CODE ###

mod = 1000000007
eps = 10**-9
inf = 10**15


def main():
    import sys
    from bisect import bisect_left
    input = sys.stdin.buffer.readline

    A, B, Q = map(int, input().split())
    S = [-inf]
    for _ in range(A):
        S.append(int(input()))
    S.append(inf)
    T = [-inf]
    for _ in range(B):
        T.append(int(input()))
    T.append(inf)

    for _ in range(Q):
        x = int(input())
        i = bisect_left(S, x)
        j = bisect_left(T, x)
        ans = []
        ans.append(x - S[i-1] + abs(S[i-1] - T[j-1]))
        ans.append(x - S[i-1] + abs(S[i-1] - T[j]))
        ans.append(x - T[j-1] + abs(T[j-1] - S[i-1]))
        ans.append(x - T[j-1] + abs(T[j-1] - S[i]))
        ans.append(S[i] - x + abs(S[i] - T[j-1]))
        ans.append(S[i] - x + abs(S[i] - T[j]))
        ans.append(T[j] - x + abs(T[j] - S[i-1]))
        ans.append(T[j] - x + abs(T[j] - S[i]))
        print(min(ans))


if __name__ == '__main__':
    main()