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

str_s = input()
int_k = int(input())

result = list()
for f in range(len(str_s)):
    pattern = set()
    if len(result) == int_k and str_s[f] > result[-1][0]:
        continue
    for t in range(f + 1, len(str_s) + 1):
        pattern.add(str_s[f:t])
        result = sorted(set(result + sorted(list(pattern))[:int_k]))[:int_k]

print(result[-1])


### FIXED CODE ###

str_s = input()
int_k = int(input())

result = list()
for f in range(len(str_s)):
    pattern = set()
    for t in range(f + 1, f + 1 + int_k):
        pattern.add(str_s[f:t])
        result = sorted(set(result + list(pattern)))[:int_k]

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

from collections import deque
import sys
input = sys.stdin.readline

N = int(input())
A = [None] + [deque(map(int, input().split())) for _ in range(N)]
ans = 0
while any(A):
    used = set()
    for i in range(1, N+1):
        if not A[i] or i in used:
            continue
        j = A[i][0]
        if j in used:
            continue
        if A[j][0] == i:
            used.add(i)
            used.add(j)
            A[i].popleft()
            A[j].popleft()
    if not used:
        ans = -1
        break
    ans += 1
print(ans)


### FIXED CODE ###

from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


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

from scipy.misc import comb
r1, c1, r2, c2 = map(int, input().split())
mod = 1000000007
hole = (comb(r2 + c2 + 2, r2 + 1, exact=True) - 1) % mod
rect1 = (comb(r2 + c1 - 1 + 2, r2 + 1, exact=True) - 1) % mod
rect2 = (comb(r1 - 1 + c2 + 2, r1, exact=True) - 1) % mod
duplicate = (comb(r1 + c1, r1, exact=True) - 1) % mod
ans = (hole - rect1 - rect2 + duplicate) % mod
print(ans)

### FIXED CODE ###

r1, c1, r2, c2 = map(int, input().split())

p = 10 ** 9 + 7
size = 1010000 * 2
f_list = [1] * size
for i in range(1, size):
    f_list[i] = (f_list[i - 1] * i) % p

def cmb(n, r):
    return f_list[n + r] * pow(f_list[n], p-2, p) * pow(f_list[r], p-2, p) % p

hole = cmb(r2 + 1, c2 + 1) - 1
rect1 = cmb(r2 + 1, c1) - 1
rect2 = cmb(r1, c2 + 1) - 1
duplicate = cmb(r1, c1) - 1
ans = (hole - rect1 - rect2 + duplicate) % p
print(ans)
### BUGGY CODE ###

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

#x=dict(zip(range(1,N+1),a))
#print(x)
route=[1,]
for _ in range(N):
#while 2 not in route:
  #print(x[route[-1]])
  if 2 in route:
    break
  elif x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print(-1)
    exit()
        
print(len(route)-1)

### FIXED CODE ###

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

on=1
ans=0
for _ in range(N):
  on=x[on-1]
  ans=ans+1
  if on==2:
    print(ans)
    exit()
  
print(-1)
### BUGGY CODE ###

n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
for i in range(n):
    b=a[i]
    a.pop(i)
    print(max(a))
    a.insert(i,b)

### FIXED CODE ###

n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
m=max(a)
midx=a.index(max(a))
l=sorted(a)[-2]
for i in range(n):
    if i==midx:
        print(l)
    else:
        print(m)
### BUGGY CODE ###

from pprint import pprint
import numpy as np
n,w=map(int, input().split())
wv=[list(map(int,input().split()))  for i in  range(n)]

dp=[[0]*(w+1) for i in range(n+1)]

wv.sort(key=lambda x:x[1])
#print(wv)
for i in range(n):
  #print("i={}".format(i))    
  for wsum in range(1,w+1):
    #print("重さ：{}".format(wsum))
    if wsum >= wv[i][0]:
        if dp[i+1][wsum] < dp[i][wsum-wv[i][0]]+wv[i][1]:
          #print("選ぶ, (重さ：{}, 価値：{})".format(wv[i][0], wv[i][1]))
          dp[i+1][wsum] = dp[i][wsum-wv[i][0]]+wv[i][1]
    else:
      dp[i+1][wsum] = dp[i][wsum]
    #pprint(dp)


print(dp[-1][-1])
      


### FIXED CODE ###

from pprint import pprint
import numpy as np
import numba as nb
@nb.njit
def Knapsack1(n,w,wl,vl):
  dp = np.zeros(shape=(n + 1, w + 1), dtype=np.int64)
  for wsum in range(1,w+1):
  	#print("i={}".format(i))    
    for i in range(n):
      #print("重さ：{}".format(wsum))
      if wsum >= wl[i]:
        if dp[i][wsum] < dp[i][wsum-wl[i]]+vl[i]:
          #print("選ぶ, (重さ：{}, 価値：{})".format(wv[i][0], wv[i][1]))
          dp[i+1][wsum] = dp[i][wsum-wl[i]]+vl[i]
        else:
          dp[i+1][wsum] = dp[i][wsum]
          
      else:
        dp[i+1][wsum] = dp[i][wsum]
  #print(dp)
  print(dp[-1][-1])

n,w=map(int, input().split())
wv=[list(map(int,input().split()))  for i in  range(n)]
wv.sort(key=lambda x:(x[1],x[0]))
wv=np.array(wv)
wv=wv.T
wl=wv[0]
vl=wv[1]
#print(wl)
#print(vl)
Knapsack1(n,w,wl,vl)
### BUGGY CODE ###

r,g,b,n=map(int,input().split())

ans=0
for i in [i*r for i in range(n//r+1)]:
    for j in [i*g for i in range(n//g+1)]:
        A=n-i-j
        if A%b==0 and A>=0:

            ans+=1

print(ans)

### FIXED CODE ###

r,g,b,n=map(int,input().split())

ans=0
for i in [i*r for i in range(n//r+1)]:
    for j in [i*g for i in range(n//g+1)]:
        A=n-i-j
        if A%b==0 and A>=0:

            ans+=1

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

#!/usr/bin/env python3
import sys
import collections


def solve(N: int, A: "List[int]"):
    for k in range(len(A)):
        a = A[:]
        del a[k]
        c = collections.Counter(a)
        values, counts = zip(*c.most_common())
        ans = 0
        for i in counts:
            ans += int(i * (i-1) / 2)
        print(ans)
    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
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
import collections
import copy


def solve(N: int, A: "List[int]"):
    C = collections.Counter(A)
    S = sum([int(i * (i-1)) / 2 for i in C.values()])
    for k in range(len(A)):
        c = C[A[k]]
        print(int(S - (c * (c-1) / 2) + ((c-1) * (c-2) / 2)))
    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
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

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

N, K = map(int, input().split())
A = list(map(int, input().split()))
C = list(set(A))
B = [0 for i in range(len(C))]
for a in A:
    B[C.index(a)] += 1
print(sum(sorted(B)[:-K]))

### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
B = {i:0 for i in set(A)}
for a in A:
    B[a] += 1
print(sum(sorted(B.values())[:-K]))
### BUGGY CODE ###

import itertools

n,m,q=map(int,input().split())

lis=[]
for i in range(q):
    lis.append(list(map(int,input().split())))

M=0
for l in map(list,itertools.combinations(range(1,n+m),n)):
    s=0
    for j in range(q):
        aj=lis[j][0]
        bj=lis[j][1]
        cj=lis[j][2]
        dj=lis[j][3]
        if l[bj-1]-bj-l[aj-1]+aj==cj:
            s+=dj
    M=max(M,s)
print(M)

### FIXED CODE ###

import itertools

n,m,q=map(int,input().split())

lis=[]
for i in range(q):
    lis.append(list(map(int,input().split())))

M=0
for l in map(list,itertools.combinations(range(1,n+m),n)):
    s=0
    for j in range(q):
        aj=lis[j][0]
        bj=lis[j][1]
        cj=lis[j][2]
        dj=lis[j][3]
        if l[bj-1]-bj-l[aj-1]+aj==cj:
            s+=dj
    M=max(M,s)
print(M)
### BUGGY CODE ###

while 1:
	try:
		stack = []
		for s in map(str, raw_input().split()):
			if s == "-":
				stack.append(stack.pop(-2) - stack.pop())
			elif s == "+":
				stack.append(stack.pop(-2) + stack.pop())
			elif s == "/":
				stack.append(stack.pop(-2) / stack.pop())
			elif s == "*":
				stack.append(stack.pop(-2) * stack.pop())
			elif s == "%":
				stack.append(stack.pop(-2) % stack.pop())
			else:
				stack.append(float(s))
	
		print stack[0]
	except:
		pass

### FIXED CODE ###

while 1:
	try:
		stack = []
		for s in raw_input().split():
			if s == "-":
				stack.append(stack.pop(-2) - stack.pop())
			elif s == "+":
				stack.append(stack.pop(-2) + stack.pop())
			elif s == "/":
				stack.append(stack.pop(-2) / stack.pop())
			elif s == "*":
				stack.append(stack.pop(-2) * stack.pop())
			else:
				stack.append(float(s))
	
		print "%.6f" % (stack[0])
	except:
		break
### BUGGY CODE ###

def popcount(x):
    ans = 0   
    while x > 0:
        if x & 1:
            ans += 1
        x >>= 1
    return ans
              
def f(x, pc, memo=None):
    if memo and (x, pc) in memo:
        return memo[(x, pc)]
    r = x % pc
    if memo:  
        memo[(x, pc)] = r
    return r  
              
N = int(input())
X = input()   
              
x = int(X, 2) 
              
memo = {}     
pc_x = popcount(x)
              
for i in range(N):
    if X[i] == '1':
        pc = pc_x - 1
    else:     
        pc = pc_x + 1
    if pc == 0:
        print('0')
        continue
    f_x = f(x, pc, memo)
    f_res = f(1<<(N-i-1), pc, memo)
    if X[i] == '1':
        f_i = f_x - f_res
    else:     
        f_i = f_x + f_res
    f_i = (f_i + 2 * pc) % pc
    ans = 1   
    while f_i > 0:
        ans += 1
        pc = popcount(f_i)
        f_i = f(f_i, pc)
    print(ans)
              

### FIXED CODE ###

N = int(input())
X = input()   
              
x_val = int(X, 2)
pc_x = X.count('1')
              
x_mod_pc_x_plus_1 = x_val % (pc_x + 1)
x_mod_pc_x_minus_1 = x_val % max(pc_x - 1, 1)
              
for i in range(N):
    if X[i] == '1':
        pc = pc_x - 1
        if pc == 0:
            print(0)
            continue
        val = (x_mod_pc_x_minus_1 - pow(2, N-i-1, pc)) % pc
    else:     
        pc = pc_x + 1
        val = (x_mod_pc_x_plus_1 + pow(2, N-i-1, pc)) % pc
    ans = 1   
    while val > 0:
        val %= bin(val).count('1')
        ans += 1
    print(ans)
### BUGGY CODE ###

from copy import deepcopy
h,w,n = (int(i) for i in input().split())
c1,ans = [[] for i in range(w)],0
for i in range(h):
	s = input()
	for j in range(w): c1[j].append(int(s[j]))
if n<4:
	if h==50: z = 40
	else: z = h//2
	for i in range(z,h):
		for j in range(w):
			c,m,a = deepcopy(c1),0,0
			del c[j][i]
			c[j].insert(0,0)
			while 1:
				d = []
				for k in range(i-h,-h,-1):
					k2 = 0
					while k2<w-n+1:
						if c[k2][k] and all(c[k2][k]==c[k2+k3][k] for k3 in range(1,n)):
							b = n
							for k3 in range(k2+n,w):
								if c[k2][k]==c[k3][k]: b+=1
								else: break
							d.append((h+k,k2,b))
							k2+=b
						else: k2+=1
				if d:
					for k,k2,k3 in d:
						a+=(2**m)*c[k2][k]*k3
						for k4 in range(k2,k2+k3): del c[k4][k]
					for k,k2,k3 in d:
						for k4 in range(k2,k2+k3): c[k4].insert(0,0)
				else: break
				m+=1
			ans = max(ans,a)
print(ans)

### FIXED CODE ###

from copy import deepcopy
h,w,n = (int(i) for i in input().split())
c1,ans = [[] for i in range(w)],0
for i in range(h):
	s = input()
	for j in range(w): c1[j].append(int(s[j]))
if n<4:
	if h==30: z = 25
	else: z = h//2
	for i in range(z,h):
		for j in range(w):
			c,m,a = deepcopy(c1),0,0
			del c[j][i]
			c[j].insert(0,0)
			while 1:
				d = []
				for k in range(i-h,-h,-1):
					k2 = 0
					while k2<w-n+1:
						if c[k2][k] and all(c[k2][k]==c[k2+k3][k] for k3 in range(1,n)):
							b = n
							for k3 in range(k2+n,w):
								if c[k2][k]==c[k3][k]: b+=1
								else: break
							d.append((h+k,k2,b))
							k2+=b
						else: k2+=1
				if d:
					for k,k2,k3 in d:
						a+=(2**m)*c[k2][k]*k3
						for k4 in range(k2,k2+k3): del c[k4][k]
					for k,k2,k3 in d:
						for k4 in range(k2,k2+k3): c[k4].insert(0,0)
				else: break
				m+=1
			ans = max(ans,a)
print(ans)
### BUGGY CODE ###

from heapq import heappop, heappush

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = []
for i in range(H):
    c.append(input().rstrip())

h = [(0, x1-1, y1-1)]

dp = [[False] * W for _ in range(H)]


def check(step, x, y):
    if c[x][y] == '@':
        return False

    if not dp[x][y]:
        dp[x][y] = True
        heappush(h, (step + 1, x, y))

    return True


while h:
    step, x, y = heappop(h)
    if x == x2-1 and y == y2-1:
        print(step)
        break

    for i in range(x+1, min(x+K+1, H)):
        if not check(step, i, y):
            break

    for i in range(x-1, max(-1, x-K-1), -1):
        if not check(step, i, y):
            break

    for j in range(y+1, min(y+K+1, W)):
        if not check(step, x, j):
            break

    for j in range(y-1, max(-1, y-K-1), -1):
        if not check(step, x, j):
            break
else:
    print(-1)


### FIXED CODE ###

from collections import deque

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

c = [input().rstrip() for _ in range(H)]

d = deque([(x1-1, y1-1)])

dp = [[-1] * W for _ in range(H)]
dp[x1-1][y1-1] = 0

while d:
    x, y = d.popleft()
    if x == x2-1 and y == y2-1:
        print(dp[x][y])
        break

    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        for i in range(1, K+1):
            nx = x + dx * i
            ny = y + dy * i

            if 0 > nx or nx >= H or 0 > ny or ny >= W or c[nx][ny] == '@':
                break

            if dp[nx][ny] == -1:
                dp[nx][ny] = dp[x][y] + 1
                d.append((nx, ny))
            elif dp[nx][ny] <= dp[x][y]:
                break
else:
    print(-1)
### BUGGY CODE ###

n = int(input())
p = [int(_) for _ in input().split()]

ans = 1
for i in range(1,len(p)):
    p_min = min(p[0:i+1])
    if p_min == p[i]:
        ans += 1
print(ans)

### FIXED CODE ###

n = int(input())
p = [int(_) for _ in input().split()]

t = 200001
ans = 0
for i in range(n):
    if p[i] < t:
        ans += 1
        t = p[i]
print(ans)
### BUGGY CODE ###

import itertools as it

n, x, y = list(map(int, input().split(' ')))

dist_list = []
for i, j in it.combinations(list(range(n)), 2):
    warp_dist = abs(i - (x - 1)) + 1 + abs(j - (y - 1))
    if abs(j - i) < warp_dist:
        dist_list.append(abs(j - i))
    else:
        dist_list.append(warp_dist)

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


### FIXED CODE ###

import itertools as it
import collections
 
n, x, y = list(map(int, input().split(' ')))
 
dist_list = []
dist_a = dist_list.append
for i, j in it.combinations(list(range(n)), 2):
    warp_dist = abs(i - (x - 1)) + 1 + abs(j - (y - 1))
    if abs(j - i) < warp_dist:
        dist_a(abs(j - i))
    else:
        dist_a(warp_dist)
 
dist_c = collections.Counter(dist_list)
 
for i in range(1, n):
    print(dist_c[i])
### BUGGY CODE ###

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

cnt = 0
pos = 0

while True:
    if pos == 1:
        print(cnt)
    if cnt >= N:
        print(-1)

    pos = A[pos] - 1
    cnt += 1

### FIXED CODE ###

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

ans = 0

for i in range(N):
    if i == 0:
        ans += 1
        next_pos = A[i]
        if next_pos == 2:
            break
        else:
            continue
        
    ans += 1
    next_pos = A[next_pos-1]

    if next_pos == 2:
        break
    else:
        continue

if ans < N:
    print(ans)
else:
    print(-1)
### BUGGY CODE ###

import bisect
import math
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
temp = 0
for j, i in enumerate(a):
    k = bisect.bisect_left(a[:j], (i / 2) + 1) - 1
    if (i - a[k]) > 0:
        if math.factorial(i) // (math.factorial(a[k]) * (math.factorial(i - a[k]))) > temp:
            ans = [i, a[k]]
        else:
            continue
    else:
        continue
print(*ans)

### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]
a.sort()
max_a = a[-1]
temp = a[0]
for i in a[1:-1]:
    if abs(i - max_a / 2) < abs(temp - max_a / 2):
        temp = i
print(*[max_a, temp])
### BUGGY CODE ###

N = input()
A = [0]*N
tmp = [0]*N

for i in range(0,N):
  tmp[i]=int(raw_input())
for i in range(0,N):
  A = tmp
  x = A.pop(i)
  print max(A)
  A.insert(i,x)

### FIXED CODE ###

N = int(raw_input())
A = []
for i in range(0,N):
  A.append(int(raw_input()))
A = list(enumerate(A))
A = sorted(A, key=lambda x: x[1])
max_A = A[N-1][1]
second_max_A = A[N-2][1]
A = sorted(A,key=lambda x: x[0])
for i in range(0,N):
  if A[i][1] == max_A:
    print second_max_A
  else:
    print max_A
### BUGGY CODE ###

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

here = 1
log = [1]
i = K
while i > 0:
    here = A[here-1]

    if here in log:
        loopuntil = log.index(here)
        mod = len(log[loopuntil:])
        i = (K - loopuntil) % mod
        log = []


    else:
        log.append(here)
        i = i - 1

print(here)

### FIXED CODE ###

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

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

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

# 初期入力
N = int(input())
k = list(map(int, input().split()))

# 書かれている数字のぼーるがいくつあるか、辞書
from collections import Counter
A =Counter(k)

from math import factorial
def kumiawase_num(n, r): # 組み合わせ n「C」r
    if  n<r:
        return 0
    return factorial(n) // (factorial(n - r) * factorial(r))

# 順番付きの辞書
from collections import OrderedDict
D = OrderedDict()
D2 = OrderedDict()

# 各数字を連続でとる場合の数
futatu_onaji =0
for i,v in A.items():
    futatu_onaji += kumiawase_num(v,2)


#D =[0]*N
D =[]
for i,v in enumerate(k):
    for ij,vj in A.items():
        if v == ij :
            x = kumiawase_num(vj-1,1) #最初のボールと同じ番号を次に取る
            D.append(futatu_onaji - x ) #全体から引いたものをリストに追加
print(*D,sep="\n")

### FIXED CODE ###

from collections import Counter 
N = int(input())
A = list(map(int, input().split()))
c_A = Counter(A)
all =sum( [i*(i-1)//2 for i in c_A.values()] )

for i in range(N):
    ans =all -( c_A[A[i]] -1)
    print(ans)
### BUGGY CODE ###

N,K = map(int,input().split())
S = list(input())
cnt = 1
k = {}
if S[0] == "0":
    k[cnt] = [0]
    cnt += 1


flg = False
for i in range(N):
    if S[i] == '1':
        if cnt not in k.keys() :
            k[cnt] = []
        k[cnt].append(i)
        flg = True
    else:
        if flg == True:
            flg = False
            cnt += 1

if S[-1] == '0':
    k[cnt] = [N-1]


if '0' not in S:
    print(len(S))
    exit()
if N == 1:
    print(1)
    exit()

if cnt - K + 1 < 0:
    print(len(S))
    exit()

ans = 0
for i in range(1,cnt - K+1):
    ans = max(ans , k[i+K][-1]-k[i][0] + 1)
print(ans)


### FIXED CODE ###

N,K = map(int,input().split())
S = list(input())
cnt = 1
k = {}
if S[0] == "0":
    k[cnt] = [0]
    cnt += 1


flg = False
for i in range(N):
    if S[i] == '1':
        if flg == False :
            k[cnt] = []
        k[cnt].append(i)
        flg = True
    else:
        if flg == True:
            flg = False
            cnt += 1

if S[-1] == '0':
    k[cnt] = [N-1]

if '0' not in S:
    print(len(S))
    exit()
if N == 1:
    print(1)
    exit()
if cnt - K + 1 < 0:
    print(len(S))
    exit()

ans = 0
for i in range(1,cnt - K+1):
    ans = max(ans , k[i+K][-1]-k[i][0] + 1)
print(ans)
### BUGGY CODE ###

import itertools
d,g=map(int,input().split())
pc=[list(map(int,input().split())) for _ in range(d)]
num=list(itertools.permutations(list(range(1,d+1))))
ans=100000
for i in range(d):
    pc[i][1]=pc[i][1]//100
g//=100
for i in range(len(num)):
    cnt=g
    cnt2=0
    for j in range(d):
        num3=num[i][j]
        num5=pc[num3-1][0]
        num4=num3*num5
        if num4>=cnt:
            cnt2+=((cnt-1)//num3)+1
            break
        elif num4+pc[num3-1][1]>=cnt:
            cnt2+=num5
            break
        else:
            cnt-=num4+pc[num3-1][1]
            cnt2+=num5
    ans=min(ans,cnt2)
print(ans)


### FIXED CODE ###

import itertools
d,g=map(int,input().split())
p=[]
c=[]
for i in range(d):
    p1,c1=map(int,input().split())
    p.append(p1)
    c.append(c1//100)
n=list(itertools.permutations(list(range(1,d+1))))
ans=1024
g//=100
for i in range(len(n)):
    cnt=g
    cnt2=0
    for j in range(d):
        num3=n[i][j]
        num4=num3*p[num3-1]
        if num4>=cnt:
            cnt2+=((cnt-1)//num3)+1
            break
        elif num4+c[num3-1]>=cnt:
            cnt2+=p[num3-1]
            break
        else:
            cnt-=num4+c[num3-1]
            cnt2+=p[num3-1]
    ans=min(ans,cnt2)
print(ans)
### BUGGY CODE ###

n, m = map(int, input().split())
s = input()
for _ in range(m):
    a, b = map(int, input().split())
    print(s.count("AC", a-1, b))

### FIXED CODE ###

n, m = map(int, input().split())
s = input()
t = [0] * (n+1)
for i in range(n):
    if s[i:i + 2] == "AC":
        t[i + 1] = t[i] + 1
    else:
        t[i + 1] = t[i]
for i in range(m):
    a, b = map(int, input().split())
    print(t[b-1] - t[a-1])
### BUGGY CODE ###

n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(m)]

b_sum=0
for b,c in l:
  a+=[c]*b
  b_sum+=b

a=sorted(a)[b_sum:]

print(sum(a))

### FIXED CODE ###

from sys import stdin
nii=lambda:map(int,stdin.readline().split())

n,m=map(int,input().split())
a=sorted(list(nii()))
l=[list(nii()) for i in range(m)]
l=sorted(l,key=lambda x:x[1],reverse=True)

man=[]
num=0
for b,c in l:
  man+=[c]*b
  num+=b
  if num>n:
    break

ans=0
for i in range(n):
  if i<num:
    ans+=max(a[i],man[i])
  else:
    ans+=a[i]
print(ans)
### BUGGY CODE ###

N = int(input())
a = [int(input()) for _ in range(N)]
for i in range(len(a)):
  num = a[:]
  num.pop(i)
  print(max(num))

### FIXED CODE ###

N = int(input())
a = [int(input()) for _ in range(N)]
max_num = sorted(a, reverse=True)[0]
max_num2 = sorted(a, reverse=True)[1]
[print(max_num) if i!=max_num else print(max_num2) for i in a]