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

# どう考えてもK>5000でループが起きるので，
# dp[k][i]:= k回まで，移動したときの最大の値とすればよい
# -infで初期化
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))

dp = [[-float('inf') for _ in range(N)] for __ in range(5000)]
for i,c in enumerate(C):
    dp[0][i] = 0
max_val = -float('inf')
if K<(5000-1):
    for i in range(K):
        for j in range(N):
            next_cell = P[j] - 1
            dp[i + 1][next_cell] = max(dp[i][j] + C[next_cell], dp[i + 1][next_cell])
            max_val = max(dp[i + 1][next_cell], max_val)
    print(max_val)
    exit()

for i in range(5000-1):
    for j in range(N):
        next_cell = P[j]-1
        dp[i+1][next_cell] = max(dp[i+1][next_cell] + C[next_cell], dp[i+1][next_cell])
        max_val = max(dp[i+1][next_cell],max_val)
print(max_val)



### FIXED CODE ###

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

LC = [-1] * N
# Pが順列なので，結局それぞれのマスはループのどれかにいる事がわかる．あとは，ループの長さとループ1回分のコスト(a->a)を計算しておく
# ループ1回分のコストが正の場合はループをなるべく回す．負の場合は1回もループしないほうがいい．ただし最初のa->aは考慮する.
# 1回は最低でも動かないといけないから
max_score = -float('inf')
for i in range(N):
    Loop_len = 0
    Loop_cost =[]
    Loop_cost_sum = 0
    now = i
    while True:
        now = P[now] - 1
        Loop_len += 1
        Loop_cost_sum += C[now]
        Loop_cost.append(Loop_cost_sum)
        if now == i:
            break
    # それぞれのマスへのループによるコストのゲインと,最短でそこへいくまでの移動コストを計算してコストを決定する
    for i,lc in enumerate(Loop_cost):
        if K-(i+1)<0: # 行けないので
            continue
        possible_loop_num = (K-(i+1))//Loop_len
        cost = lc + max(0,Loop_cost_sum) * possible_loop_num
        max_score = max(cost,max_score)

print(max_score)
### BUGGY CODE ###

s = list(input())
def move_children(RL, li):
    newli = li.copy()
    for n, rl in enumerate(RL):
        if rl == "R":
            newli[n]-=li[n]
            newli[n+1]+=li[n]
        elif rl=="L":
            newli[n]-=li[n]
            newli[n-1]+=li[n]
    return newli
nextli = [1 for i in range(len(s))]
 
for f in range(10**3):
    nextli=move_children(s,nextli)
print(" ".join(map(str,nextli)))

### FIXED CODE ###

s = list(input())
output = [0 for i in s]
ruiseki = 0
for p in range(len(s)):
    if s[p] == "R":
        if s[p+1] == "R":
            ruiseki+=1
        else:
            ruiseki+=1
            output[p] += ruiseki//2 + ruiseki%2
            output[p+1] += ruiseki//2
            ruiseki=0
    
ruiseki = 0
for q in range(len(s)-1, -1, -1):
    if s[q] == "L":
        if s[q-1]=="L":
            ruiseki+=1
        else:
            ruiseki+=1
            output[q] += ruiseki//2 + ruiseki%2
            output[q-1] += ruiseki//2
            ruiseki=0
print(" ".join(map(str,output)))
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
 
 
for k in range(K):
  B = [0]*N
  for i in range(N): 
    start = max(0, i-A[i])
    end = min(N, i+A[i]+1)
    B[start] += 1
    if end <= N - 1:
      B[end] -= 1
 
  for i in range(1, N):
    B[i] = B[i-1] + B[i] 
  A = B
  if min(A) == N:
    break
 
print(*A)

### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
 
 
for k in range(K):
  B = [0]*N
  for i in range(N): 
    start = max(0, i-A[i])
    end = min(N, i+A[i]+1)
    B[start] += 1
    if end <= N - 1:
      B[end] -= 1
 
  for i in range(1, N):
    B[i] = B[i-1] + B[i] 
  A = B
  if min(A) == N:
    break
 
print(*A)
### BUGGY CODE ###

N = int(input())
L = list(map(int, input().split()))
L.sort()
 
from bisect import bisect_left 
cnt = 0
for i in range(0,N-2):
  for j in range(i+1,N-1):
    t = L[i]+L[j]
    idx = bisect_left(L,t,lo=j+1)
    cnt += max(0,idx-j-1)
print(cnt)

### FIXED CODE ###

N = int(input())
L = list(map(int, input().split()))
L.sort()

from bisect import bisect_left 
cnt = 0
for i in range(0,N):
  for j in range(i+1,N):
    t = L[i]+L[j]
    idx = bisect_left(L,t)
    cnt += max(0,idx-j-1)
print(cnt)
### BUGGY CODE ###

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

for i in range(m):
    A[i][2] = - (A[i][2]-p)

#import numpy as np
# A = np.full(np.inf, (n+1, n+1))

# scipy.sparse.csgraph.bellman_ford


S = [[10**10, 0, 0] for i in range(n+1)]

S[1][0]=0
S[1][1]=1
S[n][2]=1

for i in range(m):
    if A[i][0] == 1:
        S[A[i][1]][0] = A[i][2]

for j in range(n-1):
    for i in range(m):
        S[A[i][1]][0] = min(S[A[i][1]][0], S[A[i][0]][0] + A[i][2])
        S[A[i][1]][1] = max(S[A[i][1]][1], S[A[i][0]][1])
        S[A[i][0]][2] = max(S[A[i][1]][2], S[A[i][0]][2])

ANS0 = [S[i][0] for i in range(len(S))]

for j in range(n-1):
    for i in range(m):
        S[A[i][1]][0] = min(S[A[i][1]][0], S[A[i][0]][0] + A[i][2])
        # S[A[i][1]][1] = max(S[A[i][1]][1], S[A[i][0]][1])
        # S[A[i][0]][2] = max(S[A[i][1]][2], S[A[i][0]][2])

ANS1 = [S[i][0] for i in range(len(S))]

#print(ANS0, ANS1)

imp = 0

for i in range(len(S)):
    if S[i][1] == 1 and S[i][2] == 1:
        if ANS0[i] != ANS1[i]:
            imp = 1

if imp == 1:
    print(-1)
else:
    print(max(0, -ANS0[n]))

### FIXED CODE ###

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

for i in range(m):
    A[i][2] = - (A[i][2]-p)

#import numpy as np
# A = np.full(np.inf, (n+1, n+1))

# scipy.sparse.csgraph.bellman_ford


S = [10**13 for i in range(n+1)]

S[1]=0
for i in range(m):
    if A[i][0] == 1:
        S[A[i][1]]=A[i][2]

for j in range(n-1):
    for i in range(m):
        S[A[i][1]] = min(S[A[i][1]], S[A[i][0]]+A[i][2])

#print(S)
ans1 = -S[n]

for j in range(n):
    for i in range(m):
        S[A[i][1]] = min(S[A[i][1]], S[A[i][0]]+A[i][2])
#print(S)
ans2 = -S[n]

if ans1 != ans2:
    print(-1)
else:
    print(max(0, ans1))
### BUGGY CODE ###

N = int(input())
S = input()
ans = N-1
for i in range(N):
    ans = min(ans,S[:i].count("W")+S[i+1:].count("E"))
print(ans)

### FIXED CODE ###

N = int(input())
S = input()
left = [0]
for i in range(N):
    if S[i] == "W":
        left.append(left[-1]+1)
    else:
        left.append(left[-1])
right = [0]
for i in range(N):
    if S[-i-1] == "E":
        right.append(right[-1]+1)
    else:
        right.append(right[-1])
ans = N-1
for i in range(N):
    ans = min(ans,left[i]+right[N-i-1])
print(ans)
### BUGGY CODE ###

N,K=map(int,input().split())
D=[]
X=[]
Y=[]

for i in range(N):
    x,y=map(int,input().split())
    D.append([x,y])
    X.append(x)
    Y.append(y)
    
X.sort()
Y.sort()
ans=10**20
for a in range(len(X)):
    for b in range(len(X)-a):
        if len(X)-b-a<K:
            break
        for c in range(len(Y)):
            for d in range(len(Y)-c):
                cnt=0
                for e in range(N):
                    if X[a]<=D[e][0]<=X[len(X)-1-b] and Y[c]<=D[e][1]<=Y[len(Y)-1-d]:
                        cnt+=1
                if cnt>=K and ans>abs(X[a]-X[len(X)-1-b])*abs(Y[c]-Y[len(Y)-1-d]):
                    ans=abs(X[a]-X[len(X)-1-b])*abs(Y[c]-Y[len(Y)-1-d])
print(ans)

### FIXED CODE ###

N,K=map(int,input().split())
D=[]
X=[]
Y=[]

for i in range(N):
    x,y=map(int,input().split())
    D.append([x,y])
    X.append(x)
    Y.append(y)
    
X.sort()
Y.sort()
D.sort()
ans=10**20
for a in range(len(X)):
    for b in range(len(X)-a):
        if len(X)-b-a<K:
            break
        for c in range(len(Y)):
            for d in range(len(Y)-c):
                cnt=0
                for e in range(a,len(X)-b):
                    if X[a]<=D[e][0]<=X[len(X)-1-b] and Y[c]<=D[e][1]<=Y[len(Y)-1-d]:
                        cnt+=1
                if cnt>=K and ans>abs(X[a]-X[len(X)-1-b])*abs(Y[c]-Y[len(Y)-1-d]):
                    ans=abs(X[a]-X[len(X)-1-b])*abs(Y[c]-Y[len(Y)-1-d])
print(ans)
### BUGGY CODE ###

H, W, K = map(int, input().split())
S = list(input() for i in range(H))

sum = [[0] * (W+1) for i in range((H+1))]
for i in range(H):
    for j in range(W):
        sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + (1 if S[i][j] == '1' else 0)

ans = H + W - 2

for ptn in range(1<<H-1): # 2^H - 1 通り
    cand = 0

    sep = [0] # 横方向の分割リスト、ここ天才
    for i in range(H-1):
        if((ptn >> i) & 1): # & を取ることで、上の桁を消せる
            sep.append(i+1)
            cand += 1
    sep.append(H)

    left = 0 # left は直前の分割
    for pos in range(W):
    	cur = [] # cur は今見てるホワイトチョコの個数のリスト

    	# 横方向に分割されている各部分について
    	for i in range(len(sep) - 1):
    		cur.append(sum[sep[i+1]][pos+1] - sum[sep[i+1]][left] - sum[sep[i]][pos+1] + sum[sep[i]][left])

    	if max(cur) > K:
    		if left == pos: # 縦に分割できない場合(一列しかない場合)
    			cand = H * W
    			break
    		else:
    			cand += 1
    			left = pos

    ans = min(ans, cand)

print(ans)


### FIXED CODE ###

H, W, K = map(int, input().split())
S = list(input() for i in range(H))

sum = [[0] * (W+1) for i in range((H+1))]
for i in range(H):
    for j in range(W):
        sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + (1 if S[i][j] == '1' else 0)

ans = H + W - 2

for ptn in range(1<<H-1): # 2^H - 1 通り
    cand = 0

    sep = [0] # 横方向の分割リスト、ここ天才
    for i in range(H-1):
        if((ptn >> i) & 1): # & を取ることで、上の桁を消せる
            sep.append(i+1)
            cand += 1
    sep.append(H)

    left = 0 # left は直前の分割
    for pos in range(W):
    	cur = [] # cur は今見てるホワイトチョコの個数のリスト

    	# 横方向に分割されている各部分について
    	for i in range(len(sep) - 1):
    		cur.append(sum[sep[i+1]][pos+1] - sum[sep[i+1]][left] - sum[sep[i]][pos+1] + sum[sep[i]][left])

    	if max(cur) > K:
    		if left == pos: # 縦に分割できない場合(一列しかない場合)
    			cand = H * W
    			break
    		else:
    			cand += 1
    			left = pos

    ans = min(ans, cand)

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

import numpy as np

X=int(input())
ans=[]
for i in range(2*X):
  A=i-X
  for j in range(2*X):
    B=j-X
    x=A**5-B**5
    if x == X:
      ans.append(A)
      ans.append(B)
print("{} {}".format(ans[0],ans[1]))
      

### FIXED CODE ###

import numpy as np

X=int(input())
ans=[0,0]

for i in range(-120,120):
  a=i
  for j in range(-120 , 120):
    b=j
    if (a**5 - b**5) ==X:
      ans[0]=a
      ans[1]=b

print("{} {}".format(ans[0],ans[1]))
### BUGGY CODE ###

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

listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(map(int,input().split())))
    except:
        break;
        #または、quit(),os.exit()をして止める。

#print(listA)
#[[1, 2, 2, 3, 1], [4, 5, 3, 4, 1, 2, 3, 4], [2, 3, 5, 6, 0, 2]]
    
count = 0
for i in range(1,N+1):
  count1 = 0
  for j in range(M):
    if listA[j][0] <= i <= listA[j][1]:
      count1 += 1
  if count1 == M:
  	count += 1
  count1 = 0
print(count)

### FIXED CODE ###

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

listA=[]
while True:
    try:
        listA.append(list(map(int,input().split())))
    except:
        break;
L=[]
R=[]
for i in listA:
  L.append(i[0])
  R.append(i[1])
maxL = max(L)
minR = min(R)

count = 0
for i in range(1,N+1):
  if maxL <= i <= minR:
    count+=1
print(count)
### BUGGY CODE ###

n = int(input())
lst = [int(input()) for _ in range(n)]
lst_rev = lst[::-1]
visited = [False]*n
cnt = 0
for i in range(n):
    if visited[i]:
        continue
    else:
        visited[i] = True
        cnt += 1
        tmp = lst_rev[i]
        for j in range(i+1, n):
            if not visited[j] and lst_rev[j] < tmp:
                visited[j] = True
                tmp = lst_rev[j]
print(cnt)

### FIXED CODE ###

import bisect
from collections import deque

n = int(input())
la = [int(input()) for _ in range(n)]
result = deque([])

for a in la:
    idx = bisect.bisect_left(result, a)
    if idx==0:
        result.appendleft(a)
    else:
        result[idx - 1] = a
print(len(result))
### BUGGY CODE ###

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

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

uf = UnionFind(n)

for i in range(m):
    x , y = map(int, input().split())
    x-=1
    y-=1
    uf.union(x,y)
ans = 0

ro=uf.roots()

for i in ro:
    mem = uf.members(i)
    for j in mem:
        if p[j]-1 in mem:
            ans+=1
print(ans)


### FIXED CODE ###

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

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)


uf = UnionFind(n)

for i in range(m):
    x , y = map(int, input().split())
    x-=1
    y-=1
    uf.union(x,y)
ans = 0
for i in range(n):
    if uf.same(i,p[i]-1):
        ans+=1
print(ans)
### BUGGY CODE ###

H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            A[i][j] = 0
        else:
            A[i][j] = -1


def chg(i, j, c):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] >= 0:
        return False

    A[i][j] = c
    return True


def f(c):
    edit = 0
    for i in range(H):
        for j in range(W):
            if 0 <= A[i][j] < c:
                A[i][j] = c
                edit |= chg(i - 1, j, c)
                edit |= chg(i + 1, j, c)
                edit |= chg(i, j - 1, c)
                edit |= chg(i, j + 1, c)
    return edit


ans = 0
while 1:
    if f(ans + 1):
        ans += 1
    else:
        break
print(ans)


### FIXED CODE ###

H, W = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]

from collections import deque

lst = deque()

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            lst.append((i, j, 0))


def chg(i, j):
    if i < 0 or j < 0 or H <= i or W <= j: return False
    if A[i][j] == '.':
        A[i][j] = '#'
        return True
    return False


ans = 0
while lst:
    h, w, v = lst.popleft()
    ans = max(v, ans)
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i != 0 and j != 0: continue

            if chg(h + i, w + j):
                lst.append((h + i, w + j, v + 1))
print(ans)
### BUGGY CODE ###

import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
X = list(map(int, sys.stdin.readline().split()))
L = int(sys.stdin.readline())
Q = int(sys.stdin.readline())
AB = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

N += 2
X = [X[0]] + X + [X[-1]]
X = np.array(X, dtype=int)
# dp[d][i]: i番のホテルから(2^d)日で何個右までいけるか
dp = np.zeros((N.bit_length() + 1, N), dtype=int)
dp[0] = np.searchsorted(X, X + L, side="right") - np.arange(N) - 1
for d in range(1, N.bit_length() + 1):
    dp[d] = dp[d - 1] + dp[d - 1][np.arange(N) + dp[d - 1]]
dp = dp.T


def solve(l, r):
    ret = 0
    while l + 1 < r:
        d = max(0, np.searchsorted(dp[l], r - l) - 1)
        ret += 2 ** d
        l += dp[l, d]
    if l < r:
        ret += 1
    return ret


ans = []
for a, b in AB:
    ans.append(solve(min(a, b), max(a, b)))
print(*ans, sep='\n')


### FIXED CODE ###

import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
X = list(map(int, sys.stdin.readline().split()))
L = int(sys.stdin.readline())
Q = int(sys.stdin.readline())
AB = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

N += 2
X = [X[0]] + X + [X[-1]]
X = np.array(X, dtype=int)
# dp[d][i]: i番のホテルから(2^d)日で何個右までいけるか
dp = np.zeros((N.bit_length() + 1, N), dtype=int)
dp[0] = np.searchsorted(X, X + L, side="right") - 1
for d in range(1, N.bit_length() + 1):
    dp[d] = dp[d - 1][dp[d - 1]]
dp = dp.tolist()


def solve(l, r):
    ret = 0
    d = len(dp) - 1
    while l + 1 < r:
        while d > 0 and dp[d][l] >= r:
            d -= 1
        ret += 1 << d
        l = dp[d][l]
    if l < r:
        ret += 1
    return ret


ans = []
for a, b in AB:
    ans.append(solve(min(a, b), max(a, b)))
print(*ans, sep='\n')
### BUGGY CODE ###

import sys
def mincost(n,h):
    dp=[-1 for i in range(n)]
    dp[n-1]=0
    dp[n-2]=abs(h[n-2]-h[n-1])
    for i in range(n-3,-1,-1):
        mn=min(i+k+1,n)
        mnm=sys.maxsize
        for j in range(i+1,mn):
            mnm=min(dp[j]+abs(h[j]-h[i]),mnm)
        dp[i]=mnm    
    return dp[0]

n,k=map(int,input().split())
h=[int(x) for x in input().split()]
print(mincost(n,h))

### FIXED CODE ###

from sys import stdin, gettrace
import sys
 
if not gettrace():
    def input():
        return next(stdin)[:-1]
 
 
# def input():
#    return stdin.buffer.readline()
 
def mincost(n,h,k):
  dp=[-1 for i in range(n)]
  dp[n-1]=0
  dp[n-2]=abs(h[n-2]-h[n-1])
  for i in range(n-3,-1,-1):
    mn=min(i+k+1,n)
    mnm=sys.maxsize
    for j in range(i+1,mn):
      mnm=min(dp[j]+abs(h[j]-h[i]),mnm)
    dp[i]=mnm    
  return dp[0]
 
def main():
  n,k=map(int,input().split())
  h=[int(x) for x in input().split()]
  print(mincost(n,h,k)) 
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

inf = 10**9 + 7
n, k = map(int, input().split())

nodes = list(map(int, input().split()))
dp = [inf] * n

dp[0] = 0

for i in range(n):
    for j in range(i+1, min(i+k+1, n)):
        dp[j] = min(dp[j], dp[i] + abs(nodes[i] - nodes[j]))
print(dp[n-1])


### FIXED CODE ###

inf = 10**9 + 7
n, k = map(int, input().split())

nodes = list(map(int, input().split()))
dp = [inf] * n

dp[0] = 0

for i in range(n):
    for j in range(i+1, min(i+k+1, n)):
        dp[j] = min(dp[j], dp[i] + abs(nodes[i] - nodes[j]))
# for i in range(n):
#     for j in range(1, min(k+1, n-i)):
#         dp[i+j] = min(dp[i+j], dp[i]+abs(nodes[i]-nodes[i+j]))
print(dp[n-1])
### BUGGY CODE ###

import sys
HK,WK,M = map(int, input().split())
hw_s = [list(map(int, input().split())) for _ in range(M)]
W = [0 for _ in range(WK+1)]
H = [0 for _ in range(HK+1)]
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
# 3*10^5

for i, hw in enumerate(hw_s):
    h=hw[0]
    w=hw[1]

    H[h] += 1
    W[w] += 1

    HW[h][w] = 1

answer = 0
HMAX = max(H)
WMAX = max(W)

HEMAX = H.count(HMAX)
WEMAX = W.count(WMAX)
if HEMAX * WEMAX > M:
    print(HMAX + WMAX)
    sys.exit(0)

HMAXES = [i for i, _ in enumerate(H) if _ == HMAX]
WMAXES = [i for i, _ in enumerate(W) if _ == WMAX]

# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if HW[h][w] == 0:
            print(answer)
            sys.exit(0)
print(answer-1)

### FIXED CODE ###

import sys
HK,WK,M = map(int, input().split())
W = [0] * (WK + 1)
H = [0] * (HK + 1)
HW = {}
# 3*10^5

for i in range(M):
    h, w = map(int, input().split())

    H[h] += 1
    W[w] += 1

    HW[(h,w)] = 1

answer = 0
HMAX = max(H)
WMAX = max(W)

HEMAX = H.count(HMAX)
WEMAX = W.count(WMAX)
if HEMAX * WEMAX > M:
    print(HMAX + WMAX)
    sys.exit(0)

HMAXES = [i for i, _ in enumerate(H) if _ == HMAX]
WMAXES = [i for i, _ in enumerate(W) if _ == WMAX]

# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if not (h,w) in HW:
            print(answer)
            sys.exit(0)
print(answer-1)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
ans=1
for i in range(N):
    ans=ans*A[i]
if ans>1e+18:
     print(-1)
else:
    print(ans)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
ans=1
for i in range(N):
    if (A[i]==0):
        ans=0
if (ans!=0):
    for i in range(N):
        ans=ans*A[i]
        if ans>1e+18:
            ans=-1
            break
       
print(ans)
### BUGGY CODE ###

S=input()
N=len(S)
childs=[1]*N
result=[0]*N

for i in range(N):
  if S[i]=="R": 
    j = i
    count = 0
    for j in range(j+1,N):
      count = count + 1
      if S[j] == "L":
        if count & 1 == 0:
          result[j] = result[j] + 1
        else:
          result[j-1] = result[j-1] + 1
        break
  else:
    j = i
    count = 0
    for j in range(j-1,-1,-1):
      count = count + 1
      if S[j] == "R":
        if count & 1 == 0:
          result[j] = result[j] + 1
        else:
          result[j+1] = result[j+1] + 1
        break

result_str = [str(n) for n in result]
print(" ".join(result_str))


### FIXED CODE ###

s=input()
ans=[0]*len(s)
import itertools
i=0
for key,g in itertools.groupby(s):
  num=len(list(g))
  if key=="R":
    i+=num
    ans[i-1]+=(num//2+(num%2>0))
    ans[i]+=num//2
  elif key=="L":
    ans[i-1]+=num//2
    ans[i]+=(num//2+(num%2>0))
    i+=num
  if i>=len(ans):
    break
print(*ans)
### BUGGY CODE ###

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

a=N
while True:
    if a-K>=0:
        a=a-K
    elif K-a>=a:
        a=a
        break
    else:
        a=K-a
print(a)

### FIXED CODE ###

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

a=N
while True:
    if a-10**16*K>=0:
        a=a-10**16*K
    elif a-10**14*K>=0:
        a=a-10**14*K
    elif a-10**12*K>=0:
        a=a-10**12*K
    elif a-10**10*K>=0:
        a=a-10**10*K
    elif a-10**8*K>=0:
        a=a-10**8*K
    elif a-10**6*K>=0:
        a=a-10**6*K
    elif a-10**4*K>=0:
        a=a-10**4*K
    elif a-10**2*K>=0:
        a=a-10**2*K
    elif a-K>=0:
        a=a-K
    elif K-a>=a:
        a=a
        break
    else:
        a=K-a
print(a)
### BUGGY CODE ###

N = int(input())
D = list(map(int, input().split()))
M = int(input())
T = list(map(int, input().split()))
judge = "YES"
for i in range(M):
	if D.count(T[i]) >= 1:
		D.remove(T[i])
	else:
		judge = "NO"
		break

print(judge)

### FIXED CODE ###

from collections import defaultdict

d = defaultdict(int)

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

for key in D:
	d[key] += 1

M = int(input())
T = list(map(int, input().split()))

judge = "YES"
for key in T:
	if d[key] >= 1:
		d[key] -= 1
	else:
		judge = "NO"
		break

print(judge)
### BUGGY CODE ###

A, B, K = map(int, input().split())
list = [i for i in range(A, B+1)]
list1 = list[:K]
list2 = list[(B-K-A+1):]
for i in list1:
    print(i)
for i in list2:
    if i not in list1:
        print(i)

### FIXED CODE ###

A, B, K = map(int, input().split())
list = [i for i in range(A, A+K)]
list.extend(i for i in range(B+1-K, B+1))
list = sorted(set(list))
for i in list:
    if A<=i<=B:
        print(i)