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

h,w = map(int,input().split())
if h % 3 == 0 or w % 3 == 0:
  print(0)
  exit()
ans = 1000000000
for change in range(2): #heightとwidthを入れ替えてもう一回実行する
  for ox in range(1,w):
    if not ox == w - 1:
      for tx in range(ox+1, w):
        ans = min(ans, h * (max(ox, tx-ox, w-tx) - min(ox, tx-ox, w-tx)))
    for ty in range(1, h):
      ans = min(ans, max(ty*w, ox*(h-ty), (w-ox)*(h-ty)) - min(ty*w, ox*(h-ty), (w-ox)*(h-ty)))
  temp = h
  h = w
  w = temp
print(ans)

### FIXED CODE ###

h,w = map(int,input().split())
if h % 3 == 0 or w % 3 == 0:
  print(0)
  exit()
ans = 10000000000
for change in range(2): #heightとwidthを入れ替えてもう一回実行する
  #二分割
  for ty in range(1,h):
    ans = min(ans, max(ty*w, int(w/2)*(h-ty), (w-int(w/2))*(h-ty)) - min(ty*w, int(w/2)*(h-ty), (w-int(w/2))*(h-ty)))
  a = round(w/3 + 0.1)
  ans = min(ans, h*(max(a, w-a*2) - min(a, w-a*2)))
  temp = h
  h = w
  w = temp
print(ans)
### BUGGY CODE ###

def main():
    # rankを出力、modを使って実装
    def matrix_rank(a, mod=2):
        n, m = len(a), len(a[0])
        M = [[j for j in i] for i in a]
        rank = min(n, m)
        for i in range(min(n, m)):
            if M[i][i] == 0:
                for j in range(i+1, n):
                    if M[j][i] != 0:
                        for k in range(i, m):
                            M[i][k] = (M[i][k]+M[j][k]) % mod
                        break
                else:
                    for j in range(i+1, m):
                        if M[i][j] != 0:
                            for k in range(i, n):
                                M[k][i] = (M[k][i]+M[k][j]) % mod
                            break
                    else:
                        rank -= 1
            pinv = pow(M[i][i], mod-2, mod)
            for j in range(m):
                M[i][j] = M[i][j]*pinv % mod
            M2 = M[i]
            for j in range(i+1, n):
                M3 = M[j][i]
                M[j][i] = 0
                for k in range(i+1, m):
                    M[j][k] = (M[j][k]-M3*M2[k]) % mod
        return rank

    t = int(input())
    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        s = input()
        r = 0
        m = []
        m2 = []
        for i in range(n-1, -1, -1):
            ai = a[i]
            b = [[(ai >> j) & 1 for j in range(64)]]
            newr = matrix_rank(m+b)
            if s[i] == "0":
                if newr > r:
                    r += 1
                    m += b
                    m2 += [ai]
            else:
                if newr > r:
                    print(1)
                    break
        else:
            print(0)


main()


### FIXED CODE ###

def main():
    t = int(input())
    for _ in range(t):
        nn, aa, s, r, mm = int(input()), list(map(int, input().split())), input(), 0, []
        for ii in range(nn-1, -1, -1):
            ai = aa[ii]
            mm += [[(ai >> jj) & 1 for jj in range(64)]]
            n, i, j = r+1, 0, 0
            while i < n and j < 64:
                if mm[i][j] == 1 and mm[n-1][j] == 0:
                    i += 1
                    j += 1
                    continue
                if mm[i][j] == 0:
                    if mm[n-1][j] != 0:
                        for l in range(j, 64):
                            mm[i][l] ^= mm[n-1][l]
                    else:
                        j += 1
                        continue
                M2 = mm[i]
                for k in range(i+1, n):
                    M3 = mm[k][j]
                    mm[k][j] = 0
                    for l in range(j+1, 64):
                        mm[k][l] ^= M3*M2[l]
                i += 1
                j += 1
    
            if s[ii] == "0":
                if i > r:
                    r += 1
                else:
                    mm.pop()
            else:
                mm.pop()
                if i > r:
                    print(1)
                    break
        else:
            print(0)


main()
### BUGGY CODE ###

s = input()
q = int(input())
o = 1
for i in range(q):
    x = input().split()
    if x[0] == "1":
        o = o*-1
    else:
        if (x[1] == "1" and o == 1) or (x[1] == "2" and o == -1):
            s = x[2] + s
        else:
            s = s + x[2]

s = s[::o]
print(s)

### FIXED CODE ###

from collections import deque
 
S = input()
L = deque([])
for c in S:
    L.append(c)
Q = int(input())
flag = 1
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag *= -1
    else:
        F = q[1]
        if F == "1":
            if flag == 1:
                L.appendleft(q[2])
            else:
                L.append(q[2])
        else:
            if flag == 1:
                L.append(q[2])
            else:
                L.appendleft(q[2])
if flag == 1:
    print("".join(map(str, L)))
else:
    L.reverse()
    print("".join(map(str, L)))
### BUGGY CODE ###

import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))



##### main #####

class BIT(object):

	def __init__(self,l):
		self.size = l
		self.bit = [0]* (self.size+1)

	def sum(self, i):
		s = 0
		while i > 0:
			s += self.bit[i]
			i -= i & -i
		return s

	def add(self, i, x):
		while i < self.size:
			self.bit[i] += x
			i += i & -i

	def __str__(self):
		return str(self.bit)


def main():
	N,M = LI()

	bit = BIT(M+2)
	L = []
	R = []
	b = [[] for i in range(M+1)]


	for i in range(N):
		l,r = LI()
		L.append(l); R.append(r)
		b[r-l +1].append(i)


	gone = 0
	for d in range(1, M+1):
		for i in b[d]:
			gone+=1
			bit.add(L[i], 1)
			bit.add(R[i]+1, -1)


		ans = N-gone

		for m in range(d, M+1, d):

			ans += bit.sum(m)
		print(ans)




if __name__ == '__main__':
	main()

### FIXED CODE ###

import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque
 
 
# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")
 
 
##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)
 
##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))
 
 
 
##### main #####
 
class BIT(object):
 
	def __init__(self,l):
		self.size = l
		self.bit = [0]* (self.size+1)
 
	def sum(self, i):
		s = 0
		while i > 0:
			s += self.bit[i]
			i -= i & -i
		return s
 
	def add(self, i, x):
		while i < self.size:
			self.bit[i] += x
			i += i & -i
 
	def __str__(self):
		return str(self.bit)
 
 
def main():
	N,M = LI()
 
	bit = BIT(M+2)
	L = []
	R = []
	b = [[] for i in range(M+1)]
 
 
	for i in range(N):
		l,r = LI()
		L.append(l); R.append(r)
		b[r-l +1].append(i)
 
 
	gone = 0
	for d in range(1, M+1):
		for i in b[d]:
			gone+=1
			bit.add(L[i], 1)
			bit.add(R[i]+1, -1)
 
 
		ans = N-gone
 
		for m in range(d, M+1, d):
 
			ans += bit.sum(m)
		print(ans)
 
 
 
 
if __name__ == '__main__':
	main()
### BUGGY CODE ###

N, K = map(int, input().split())
h = [int(i) for i in input().split()]


dp = [float("inf") for i in range(N)]
dp[0] = 0

for i in range(1, N):
    for k in range(1, K+1):
        if i < k:
            continue

        dp[i] = min(dp[i], dp[i-k]+abs(h[i]-h[i-k]))


print(dp[N-1])

### FIXED CODE ###

def main():
	N, K = map(int, input().split())
	h = list(map(int, input().split()))

	dp=[0]*N
 
	for i in range(1,N):
		dp[i] = min(
			dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i)
		)
	print(dp[N-1])
main()
### BUGGY CODE ###

L=input()
N=len(L)

bit=[]
for i in range(0,N):
    if L[N-1-i]=='1':
        bit.append(i+1)

dp=[0 for i in range(0,100001)]
m=bit[0]
if m!=1:
    dp[m]=(2+3**(m-1))%(10**9+7)
else:
    dp[m]=3
for i in range(1,len(bit)):
    dp[bit[i]]=2*dp[bit[i-1]]+3**(bit[i]-1)
print(dp[N]%(10**9+7))


### FIXED CODE ###

import time

def pow_k(x, n):
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K = (K*x)%(10**9+7)
        x = (x*x)%(10**9+7)
        n //= 2

    return (K*x)%(10**9+7)

L=input()
N=len(L)


bit=[]
for i in range(0,N):
    if L[N-1-i]=='1':
        bit.append(i+1)

dp=[0 for i in range(0,100001)]

m=bit[0]
if m!=1:
    dp[m]=(2+3**(m-1))%(10**9+7)
else:
    dp[m]=3
time0=time.time()
for i in range(1,len(bit)):
    s=bit[i]
    t=bit[i-1]
    dp[s]=(2*dp[t]+pow_k(3,s-1))
time1=time.time()
print(dp[N]%(10**9+7))
### BUGGY CODE ###

import math
import sys
import numpy as np
import collections

#n, a, b= map(int, input().split())
n, y  = map(int, input().split())
#s = list(str(input()))
#n = int(input())

max1 = int(y/10000) + 1

for i in reversed(range(max1)):
  rest1 = y-i*10000
  max2 = int((rest1)/5000) + 1
  for ii in range(max2):
    rest2 = y-i*10000-ii*5000
    mod = rest2%1000
    iii = int(rest2/1000)
    tot = i+ii+iii
    if(mod == 0 and tot == n):
      print(i, ii, iii)
      sys.exit()

print(-1,-1,-1)

### FIXED CODE ###

import math
import sys
import numpy as np
import collections

#n, a, b= map(int, input().split())
n, y  = map(int, input().split())
#s = list(str(input()))
#n = int(input())

max1 = int(y/1000) + 1

for i in range(max1):
  rest = y-i*9000-n*1000
  mod = rest%4000
  ii  = int((rest)/4000)
  iii = n-i-ii
  if(mod == 0 and iii>=0 and ii>=0):
    print(i, ii, iii)
    sys.exit()
    
print(-1,-1,-1)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
 
n, k, q =map(int, input().split())
a = []
 
for i in range(q):
  a.append(int(input()))
  
m = list([k]*n)
 
for i in a:
  for j in range(n):
    m[j]-=1
  m[i-1]+=1
  
for i in range(n):
  if m[i]>0:
    print("Yes\n")
  else:
    print("No\n")

### FIXED CODE ###

# -*- coding: utf-8 -*-
 
n, k, q =map(int, input().split())
a = [int(input()) for i in range(q)]

p = [0]*(n+1)

for i in range(q):
  p[a[i]] += 1
  

for i in range(1,n+1):
  if p[i]>q-k:
    print("Yes")
  else:
    print("No")
### BUGGY CODE ###

s = input()
t = input()

def get_yes(s, t):
    s_dup_lists = []
    t_dup_lists = []

    for i, (s1, t1) in enumerate(zip(s, t)):
        for j, (s2, t2) in enumerate(zip(s, t)):
            if (i != j) and (s1 == s2):
                s_dup_lists.append([i, j])
            if (i != j) and (t1 == t2):
                t_dup_lists.append([i, j])
            if s_dup_lists != t_dup_lists:
                return 'No'

    if s_dup_lists == t_dup_lists:
        return 'Yes'
    else:
        return 'No'
print(get_yes(s, t))

### FIXED CODE ###

s = input()
t = input()

s_dict = {}
t_dict = {}
s_drdp = sorted(set(s), key=s.index)
t_drdp = sorted(set(t), key=t.index)
for i, ss in enumerate(s_drdp):
    s_dict[ss] = i
for i, tt in enumerate(t_drdp):
    t_dict[tt] = i

import numpy as np

def s2vec(d, d_dict):
    return d_dict[d]

vecf = np.vectorize(s2vec)
if list(vecf(list(s), s_dict)) == list(vecf(list(t), t_dict)):
    print('Yes')
else:
    print('No')
### BUGGY CODE ###

s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=[list(map(int,input().split())) for i in range(s[1])]
a=0
for i in range(s[1]):
  for j in range(u[i][0]):
    t.append(u[i][1])
t.sort()
t.reverse()
for l in range(s[0]):
  a = a+t[l]
print(a)

### FIXED CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[list(map(int,input().split())) for _ in range(M)]
B.sort(key=lambda x:-x[1])
i,j=0,0
while i<=N-1:
  for l in range(B[j][0]):
    if A[i]>=B[j][1]:
      print(sum(A))
      exit()
    A[i]=B[j][1]
    i+=1
    if i==N:
      print(sum(A))
      exit()
  j+=1
  if j==M:
    print(sum(A))
    exit()
print(sum(A))
### BUGGY CODE ###

import itertools

N = int(input())
A = map(int, input().split())
s = 0
for a, b, c, d in itertools.combinations(A, 4):
    l = [a, b, c, d]
    l = sorted(l)
    if l[0] == l[1] and l[2] == l[3]:
        s = max(s, l[0]*l[2])
print(s)

### FIXED CODE ###

import itertools

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

num = dict()

for a in A:
    if a not in num:
        num[a] = 0
    num[a] += 1

m1 = 0
m2 = 0

for a in sorted(num.keys(), reverse=True):
    if num[a] >= 2:
        m1 = a
        num[a] = num[a] - 2
        break
for a in sorted(num.keys(), reverse=True):
    if num[a] >= 2:
        m2 = a
        num[a] = num[a] - 2
        break
print(m1*m2)
### BUGGY CODE ###

import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(500000)

N = 0
colors = []

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def used_colors(assignment, u, v):
    return {color for e, color in assignment.items() if e[0] in (u, v) or e[1] in (u, v)}

def assign(assignment, edges):
    if not edges:
        return assignment
    
    local_assignment = assignment.copy()
    colored = used_colors(local_assignment, *edges[0])
    local_assignment[edges[0]] = [color for color in colors if color not in colored][0]
    return assign(local_assignment, edges[1:] if len(edges) > 1 else [])

def main():
    global N, colors
    N = int(input())
    colors = list(range(1,N+1))
    edges = list(readlines(N-1))
    ans = assign({}, edges)
    print(max(ans.values()))
    for e in edges:
        print(ans[e])

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

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

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
#import bisect
#from collections import deque
import math

sys.setrecursionlimit(10**5)

def input():
    return sys.stdin.readline().rstrip('\n')

N = int(input())
peer_nodes = [[] for _ in range(N)]
edge_colors = [[0]*(N) for _ in range(N)]
pairs = []
for _ in range(N-1):
    a,b = list(map(int, input().split()))
    a -= 1
    b -= 1
    pairs.append([a,b])
    peer_nodes[a].append(b)
    if not a in peer_nodes[b]:
        peer_nodes[b].append(a)
    edge_colors[a][b] = 0

#print(peer_nodes,edge_colors)

def draw_color_return_max_colors(node):
    max_colors = 0
    peers = peer_nodes[node]
    if len(peers) == 0:
        return 0
    used_colors = []
    for p in peers:
        c = edge_colors[node][p]
        if c != 0:
            used_colors.append(c)
    #print('----', node, peers,used_colors)
    if len(used_colors) == len(peers):
        return 0
    color = 1
    for p in peers:
        #print(node, p, edge_colors[node][p])
        while(color in used_colors):
            color += 1
        if edge_colors[node][p] == 0:
            used_colors.append(color)
            edge_colors[node][p] = color
            edge_colors[p][node] = color
    max_colors = max(used_colors)
    for p in peers:
        max_colors = max(max_colors, draw_color_return_max_colors(p))

    return max_colors


print(draw_color_return_max_colors(pairs[0][0]))

for a,b in pairs:
    print(edge_colors[a][b])


### FIXED CODE ###

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
#import bisect
#from collections import deque
import math

sys.setrecursionlimit(10**5)

def input():
    return sys.stdin.readline().rstrip('\n')

N = int(input())
peers = [[] for _ in range(N)]
colors = [{} for _ in range(N)]
pairs = []
for _ in range(N-1):
    a,b = list(map(int, input().split()))
    a -= 1
    b -= 1
    pairs.append([a,b])
    peers[a].append(b)
    colors[a][b] = 0


def draw(node, parent_edge_color):
    _peers = peers[node]
    if len(_peers) == 0:
        return 0
    color = 1
    max_nrof_color = 1
    for peer in _peers:
        #print(node,peer,color)
        if color == parent_edge_color:
            color +=1
        colors[node][peer] = color
        max_nrof_color = max(max_nrof_color, color, draw(peer, color))
        color += 1

    return max_nrof_color


print(draw(0,0))
for a,b in pairs:
    print(colors[a][b])
### BUGGY CODE ###

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

for i in range(K+1, N+1):
  s = x[i-1]
  t = x[i-2]
  for j in range(1, K):
    s *= x[i-1-j]
    t *= x[i-2-j]
  if t<s:
    print("Yes")
  else:
    print("No")

### FIXED CODE ###

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


for i in range(K+1, N+1):
  

  if x[i-K-1]<x[i-1]:
    print("Yes")
  else:
    print("No")
### BUGGY CODE ###

import itertools

def main():
    N, K = map(int, input().split())
    ans = 0
    for a in range(1, N + 1):
        vals = []
        for i in range(a//K + 1, (a + N)//K + 1):
            vals.append(K * i - a)
        ans += len([1 for comb in itertools.product(vals, repeat=2) if (comb[0] + comb[1]) % K == 0])
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    N, K = map(int, input().split())
    ans = 0
    if K % 2 == 1:
        ans = (N // K) ** 3
    else:
        ans = (N // K) ** 3
        r = K // 2
        ans += len([1 for v in range(1, N+1) if v % K == r]) ** 3
    print(ans)


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

H,W = map(int,input().split())
s = [list(map(lambda x: x == '.', list(input()))) for i in range(H)]
v = [[0]*W for i in range(H)]
h = [[0]*W for i in range(H)]

for i in range(H):
  for j in range(W):
    if not s[i][j]:
      continue
    h[i][j] = v[i][j] = s[i][j]
    if j:
      v[i][j] += v[i][j-1]
    if i:
      h[i][j] += h[i-1][j]
hx, hy = W, 0
vx, vy = 0, H
cv, ch = 0, 0
fh, fv = 0, 0
for _ in range(H*W):
  hy -= 1
  if hy < 0:
    hx -= 1
    hy += H
    fh = 1
  vx -= 1
  if vx < 0:
    vy -= 1
    vx += W
    fv = 1
  if fh or not ch or not h[hy][hx]:
    ch = h[hy][hx]
    fh = 0
  else:
    h[hy][hx] = ch
  if fv or not cv or not v[vy][vx]:
    cv = v[vy][vx]
    fv = 0
  else:
    v[vy][vx] = cv
print(max(h[i][j] + v[i][j] for i in range(H) for j in range(W)) - 1)

### FIXED CODE ###

h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
u = [[0]*w for _ in range(h)]
d = [[0]*w for _ in range(h)]
l = [[0]*w for _ in range(h)]
r = [[0]*w for _ in range(h)]
for i in range(h):
  l[i][0] = int(s[i][0] != "#")
  for j in range(1,w):
    if s[i][j] == "#":
      l[i][j] = 0
    else:
      l[i][j] = l[i][j-1] + 1
  r[i][w-1] = int(s[i][w-1] != "#")
  for j in range(w-2,-1,-1):
    if s[i][j] == "#":
      r[i][j] = 0
    else:
      r[i][j] = r[i][j+1] + 1 
for i in range(w):
  d[0][i] = int(s[0][i] != "#")
  for j in range(1,h):
    if s[j][i] == "#":
      d[j][i] = 0
    else:
      d[j][i] = d[j-1][i] + 1
  u[h-1][i] = int(s[h-1][i] != "#")
  for j in range(h-2,-1,-1):
    if s[j][i] == "#":
      u[j][i] = 0
    else:
      u[j][i] = u[j+1][i] + 1
ans = 0
for i in range(h):
  for j in range(w):
    cur = l[i][j] + r[i][j] + u[i][j] + d[i][j]
    if cur>ans:
      ans = cur
print(ans - 3)
### BUGGY CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]
import heapq
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
ans=[]
for i in range(n):
    ans.append([f[i]*a[n-i-1]*-1,a[n-i-1]*-1])
heapq.heapify(ans)
tmp=[]
for i in range(k):
    if i==0:
        tmp=heapq.heappop(ans)
    # print(tmp)
    if tmp[1]==0:
        heapq.heappush(ans,tmp)
        break
    tmp[0]=tmp[0]//tmp[1]
    tmp[1]+=1
    tmp[0]=tmp[0]*tmp[1]
    if i != k-1:
        tmp=heapq.heappushpop(ans,tmp)
    else:
        ans.append(tmp)

print(ans[0][0]*-1)

### FIXED CODE ###

import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
left,right=0,10**12+1
a.sort()
f.sort()
# print(a)
while right-left>0:
    mid=(right+left)//2
    tmp=0
    for i in range(n):
        tmp+=max(0,a[i]-mid//f[-1-i])
    
    if tmp<=k:
        right=mid-(mid==right)
    else:
        left=mid+(mid==left)
print(right)
### BUGGY CODE ###

n, k, l = map(int, input().split())
load = [-1] * (n + 1)
train = [-1] * (n + 1)

def find_load(x, u = load):
    if u[x] < 0 :
        return x
    else:
        return find_load(u[x])

def find_train(x, u = train):
    if u[x] < 0 :
        return x
    else:
        return find_train(u[x])

for i in range(k):
    p, q = map(int, input().split())
    p, q = map(find_load, [p, q])
    p, q = sorted((p, q))
    if p != q:
        load[p] += load[q]
        load[q] = p

for i in range(l):
    r, s = map(int, input().split())
    r, s = map(find_train, [r, s])
    r, s = sorted((r, s))
    if r != s:
        train[r] += train[s]
        train[s] = r

load = [i if x < 0 else find_load(x) for i, x in enumerate(load)]

train = [i if x < 0 else find_train(x) for i, x in enumerate(train)]

ans = []
for x, y in zip(load, train):
    if x == -1 or y == -1:
        ans.append(1)
    else:
        ans.append(sum([(x == X and y == Y) for X, Y in zip(load,train)]))

print(*ans[1:])

### FIXED CODE ###

n, k, l = map(int, input().split())
load = [-1] * (n + 1)
train = [-1] * (n + 1)

def find_load(x, u = load):
    if u[x] < 0 :
        return x
    else:
        u[x] = find_load(u[x])
        return u[x]

def find_train(x, u = train):
    if u[x] < 0 :
        return x
    else:
        u[x] = find_train(u[x])
        return u[x]

for i in range(k):

    p, q = map(int, input().split())
    p, q = map(find_load, [p, q])
    p, q = sorted((p, q))
    if p != q:
        load[q] = p

load = [i if x < 0 else x for i, x in enumerate(load)]

for i in range(l):
    r, s = map(int, input().split())
    r, s = map(find_train, [r, s])
    r, s = sorted((r, s))
    if r != s:
        train[s] = r
train = [i if x < 0 else x for i, x in enumerate(train)]

keys = []
dic  = {}

for node in range(n):
    key = (find_load(load[node + 1]), find_train(train[node + 1]))
    keys.append(key)
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1

ans = [str(dic[key]) for key in keys]
print(" ".join(ans))
### BUGGY CODE ###

import sys
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
vis=[0]
now=0
for i in range(1,k+1):
  now=a[now]-1
  if now in vis:
    bgn=vis.index(now)
    print(vis[bgn+((k-bgn)%(len(vis)-bgn))]+1)
    sys.exit()
  else:
    vis.append(now)
print(vis[-1]+1)

### FIXED CODE ###

import sys
n,k=map(int,input().split())
a=[int(x) for x in input().split()]
vis_ti=[-1]*n
vis_at=[0]
now=0
for i in range(1,k+1):
  now=a[now]-1
  if vis_ti[now]>=0:
    bgn=vis_ti[now]
    print(vis_at[bgn+(k-bgn)%(i-bgn)]+1)
    sys.exit()
  vis_ti[now]=i
  vis_at.append(now)
print(vis_at[-1]+1)