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

N = int(input())
As = tuple(map(int, input().split()))

memo = set()

def dfs(tupleA):
    if tupleA in memo:
        return 0

    vs = [sum(tupleA[x::x + 1]) for x in range(N)]
    vMin = min(vs)
    if vMin >= 0:
        return vs[0]

    ans = 0
    for i in range(N):
        if vs[i] >= 0:
            continue
        listA = list(tupleA)
        for x in range(i, N, i + 1):
            listA[x] = 0
        tupleA2 = tuple(listA)
        ans = max(ans, dfs(tupleA2))
        memo.add(tupleA2)

    return ans

print(dfs(As))


### FIXED CODE ###

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

def func(Bs):
    for x in range(N):
        if Bs[x] >= 0: continue
        if max(Bs[x::x + 1]) <= 0:
            Bs[x] = 0

func(As)

zs = [27, 29, 31, 33] + list(range(34, 51))
for x in zs:
    if sum(As[x - 1::x]) < 0:
        for i in range(x - 1, N, x):
            As[i] = 0

ns = [1, 2, 4, 8, 16, 32] + [3, 6, 9, 12, 18, 24, 27, 36, 48] \
    + [5, 10, 15, 20, 25, 30, 40, 45, 50] + [7, 14, 21, 28, 35, 42, 49] \
    + [11, 22, 33, 44] + [13, 26, 39] + [17, 34] + [19, 38] + [23, 46] \
    + [29, 31, 37, 41, 43, 47]

memo = {}
memo[tuple(As)] = sum(As)

cs = [n - 1 for n in ns if n <= N and n not in zs and As[n - 1] < 0]
for c in cs:
    for tupleA in list(memo.keys()):
        if tupleA[c] == 0:
            continue

        listA = list(tupleA)
        for x in range(c, N, c + 1):
            listA[x] = 0

        func(listA)
        memo[tuple(listA)] = sum(listA)

print(max(memo.values()))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(5 * 10**5)

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

M = [[0] * N for _ in range(N)]
for _ in range(N-1):
    a, b = list(map(int, input().split()))
    M[a-1][b-1] = M[b-1][a-1] = 1

X = [0] * N
for _ in range(Q):
    p, x = list(map(int, input().split()))
    X[p-1] += x

ans = [0] * N

def setAnswer(parent, number, value):
    ans[number] = value

    for i in range(N):
        if i != parent and M[number][i] == 1:
            setAnswer(number, i, value + X[i])

setAnswer(0, 0, X[0])

print(' '.join(map(str, ans)))


### FIXED CODE ###

import sys
sys.setrecursionlimit(5 * 10**5)
input=sys.stdin.readline
 
N, Q = list(map(int, input().split()))
 
M = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = list(map(int, input().split()))
    M[a-1].append(b-1)
    M[b-1].append(a-1)
 
ans = [0] * N
for _ in range(Q):
    p, x = list(map(int, input().split()))
    ans[p-1] += x
 
def setAnswer(parent, number):
    ans[number] += ans[parent]
 
    for i in M[number]:
        if i != parent:
            setAnswer(number, i)

ans.append(0)
setAnswer(N, 0)
ans.pop(N)
 
print(' '.join(map(str, ans)))
### BUGGY CODE ###

import math

inn = [int(x) for x in input().split()]
aa = []
bb = []
for i in range(inn[0]):
    tmp = [int(x) for x in input().split()]
    aa.append(tmp[0])
    bb.append(tmp[1])

count = 0
tot = 0
for b in sorted(bb, reverse=True):
    if b <= max(aa):
        count += math.ceil((inn[1]-tot)/max(aa))
        tot = inn[1]
        print(count)
        break
    tot += b
    count += 1
    if tot >= inn[1]:
        print(count)
        break

if tot < inn[1]:
    count = inn[0] + math.ceil((inn[1]-sum(bb))/max(aa))
    print(count)


### FIXED CODE ###

import math

inn = [int(x) for x in input().split()]
aa = []
bb = []
for i in range(inn[0]):
    tmp = [int(x) for x in input().split()]
    aa.append(tmp[0])
    bb.append(tmp[1])

maxaa = max(aa)
ba = [x for x in bb if x > maxaa]
sumba = sum(ba)
lenba = len(ba)
if sumba == inn[1]:
    print(lenba)
    exit()
elif sumba > inn[1]:
    count = lenba
    tot = sumba
    for b in sorted(ba):
        tot -= b
        if tot < inn[1]:
            print(count)
            exit()
        count -= 1
else:
    count = lenba + math.ceil((inn[1]-sumba)/max(aa))
    print(count)
### BUGGY CODE ###

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

#偶数のaだけ取り出す
a_even = []
for a in a_list:
	if a % 2 == 0:
		a_even.append(a)

times = 0

while len(a_even) > 0:
	a_even[0] /= 2
	times += 1

	if a_even[0] % 2 == 1:
		a_even.pop(0)
	else:
		a = [a_even[0]] + [i*2 for i in a_even[1:]]

print(times)

### FIXED CODE ###

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

#偶数のaだけ取り出す
a_even = []
for a in a_list:
	if a % 2 == 0:
		a_even.append(a)

times = 0

while len(a_even) > 0:
	a_even[0] /= 2
	times += 1

	if a_even[0] % 2 == 1:
		a_even.pop(0)

print(times)
### BUGGY CODE ###

H,W,M=map(int,input().split())
height=[0]*H
width=[0]*W
#bomb=[0]*M
bset=set()
#for k in range(M):
    #bomb[k]=[0]*2
for j in range(M):
    h,w=map(int,input().split())
    bset.add((h-1,w-1))
    #bomb[j][0]=h-1
    #bomb[j][1]=w-1
    height[h-1]+=1
    width[w-1]+=1
#print(bomb)
#print(height)
#print(width)
hmax=max(height)
wmax=max(width)
flag=0
for i in range(H):
    if height[i]==hmax:
        for h in range(W):
            if width[h]==wmax:
                if (i,h) not in bset:
                    flag=1
                    break
if flag==1:
    print(hmax+wmax)
else:
    print(hmax+wmax-1)

### FIXED CODE ###

H,W,M=map(int,input().split())
height=[0]*H
width=[0]*W
#bomb=[0]*M
bset=set()
#for k in range(M):
    #bomb[k]=[0]*2
for j in range(M):
    h,w=map(int,input().split())
    bset.add((h-1,w-1))
    #bomb[j][0]=h-1
    #bomb[j][1]=w-1
    height[h-1]+=1
    width[w-1]+=1
#print(bomb)
#print(height)
#print(width)
hmax=max(height)
wmax=max(width)
hindex=0
windex=0
for i in range(H):
    if height[i]==hmax:
        #print(i)
        hindex+=1
for h in range(W):
    if width[h]==wmax:
        #print(h)
        windex+=1
kouho=hindex*windex
equalbomb=0
for p in bset:
    if height[p[0]]==hmax and width[p[1]]==wmax:
        #print(p)
        equalbomb+=1

if kouho==equalbomb:
    print(hmax+wmax-1)
else:
    print(hmax+wmax)
### BUGGY CODE ###

N = int(input())

M = N//6 #x,y,zがとりうる最大の値を計測
#print(M)

for n in range(1,N+1):
	count = 0
	for x in range(1,M+1):
		#P=x**2+y**2+z**2+x*y+y*z+z*x
		for y in range(1,M+1):
			#Q=x**2+y**2+z**2+x*y+y*z+z*x
			for z in range(1,M+1):
				#R = x**2+y**2+z**2+x*y+y*z+z*x
				if x**2+y**2+z**2+x*y+y*z+z*x == n:
					count = count + 1
				else:
					pass

	print(count)		


### FIXED CODE ###

N = int(input())
lst = [0]*(N+1)

for x in range(1,101):
    for y in range(1,101):
        for z in range(1,101):
            p = (x+y)*(x+z)+y**2+z**2
            if p > N:
                break
            else:
                lst[p] += 1
for i in range(1,N+1):
    print(lst[i])
### BUGGY CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
K = int(input())
total = 0
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      total += gcd(i,j,k)
print(total)

### FIXED CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
total = K*K
for i in range(2,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      total += gcd(i,j,k)
print(total)
### BUGGY CODE ###

import sys


def main():
    input = sys.stdin.buffer.readline
    h, n = map(int, input().split())
    a = [0] * n
    b = [0] * n
    for i in range(n):
        a[i], b[i] = map(int, input().split())
    max_a = max(a)
    # dp[j]:jダメージ与える魔力の最小値
    dp = [1e9] * (h + max_a + 1)
    dp[0] = 0
    for i in range(n):
        for j in range(1, h + max_a + 1):
            dp[j] = min(dp[j], min(dp[max(j - a[i], 0) : j]) + b[i])
    print(min(dp[h:]))


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys


def main():
    input = sys.stdin.buffer.readline
    h, n = map(int, input().split())
    a = [0] * n
    b = [0] * n
    for i in range(n):
        a[i], b[i] = map(int, input().split())
    max_a = max(a)
    # dp[j]:jダメージ与える魔力の最小値
    dp = [1e9] * (h + max_a + 1)
    dp[0] = 0
    for i in range(n):
        for j in range(1, h + max_a + 1):
            dp[j] = min(dp[j], dp[max(j - a[i], 0)] + b[i])
    print(min(dp[h:]))


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

import sys

sys.setrecursionlimit(10 ** 4)

MOD = 998244353


def solve(A, B, C, D):
    cache = {}

    def numWays(rows, cols):
        if rows == A and cols == B:
            return 1

        key = rows * 4096 + cols
        if key not in cache:

            assert rows >= A and cols >= B
            ways = 0
            if rows > A:
                ways += cols * numWays(rows - 1, cols) % MOD
            if cols > B:
                ways += rows * numWays(rows, cols - 1) % MOD

            if rows > A and cols > B:
                ways -= (cols - 1) * (rows - 1) * numWays(rows - 1, cols - 1) % MOD
            cache[key] = ways % MOD
        return cache[key]

    return numWays(C, D) % MOD


A, B, C, D = [int(x) for x in input().split()]
print(solve(A, B, C, D))


### FIXED CODE ###

MOD = 998244353


def solve(A, B, C, D):
    numWays = [[None for c in range(D + 1)] for r in range(C + 1)]

    numWays[A][B] = 1

    for rows in range(A, C + 1):
        for cols in range(B, D + 1):
            if rows == A and cols == B:
                continue
            ways = 0
            if rows > A:
                ways += cols * numWays[rows - 1][cols] % MOD
            if cols > B:
                ways += rows * numWays[rows][cols - 1] % MOD
            if rows > A and cols > B:
                ways -= (cols - 1) * (rows - 1) * numWays[rows - 1][cols - 1] % MOD
            numWays[rows][cols] = ways % MOD

    return numWays[C][D] % MOD


A, B, C, D = [int(x) for x in input().split()]
print(solve(A, B, C, D))
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = 1
for i in A:
    ans = ans * i
if ans > pow(10, 18):
    print(-1)
else:
    print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
if 0 in A:
    print(0)
    exit(0)
ans = 1
for i in A:
    ans = ans * i
    if ans > pow(10, 18):
        print(-1)
        exit(0)
else:
    print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
ans = a[0]

if 0 in a:
    ans = 0
else:
    for i in range(1, n):
        ans *= a[i]

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

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
ans = a[0]

if 0 in a:
    ans = 0
else:
    for i in range(1, n):
        if ans <= 10 ** 18:
            ans *= a[i]
        else:
            break

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

N = int(input())
S = []
count = 0
SCount = [0]*N
new = True

for i in range(N):
  new = True
  sj = input()
  sj = list(sj)
  sj.sort()
  for i in range(len(S)):
    if S[i] == sj:
      SCount[i]+=1
      count+=SCount[i]
      new = False
  if new == True:
      S.append(sj)
print(count)

### FIXED CODE ###

#n,*sl, = map(lambda x:''.join(sorted(x)),open(0).read().split())
n = int(input())
sl = []
for i in range(n):
  S = ''.join(sorted(input()))
  sl.append(S)
count = 0
dict ={}

for s in sl:
  if s in dict:
    count += dict[s]
    dict[s] += 1
  else:
    dict[s] = 1
print(count)
### BUGGY CODE ###

n = int(input())
A = [int(input()) for _ in range(n)]
for i in range(n):
  print(max(A[:i] + A[i+1:]))

### FIXED CODE ###

n = int(input())
A = [int(input()) for _ in range(n)]
max_num, second_num = sorted(A, reverse=True)[:2]

for a in A:
  if a==max_num: print(second_num)
  else: print(max_num)
### BUGGY CODE ###

import math

while True:
    try:
        n = int(raw_input())
        prime=[]
        if n == 1:
            print 0
        elif n == 2:
            print 1
        else:
            for i in range(3, n+1, 2):
                isprime = 1
                sqrt = int(math.sqrt(i))
                for j in prime:
                    if i % j == 0:
                        isprime = 0
                        break
                    if j > sqrt:
                        break
                if isprime:
                    prime.append(i)
            print len(prime)+1
    except:
        break

### FIXED CODE ###

import math
n = []
while True:
    try:
        n.append(int(raw_input()))
    except:
        break
  
r = max(n)+1
sqrt = int(math.sqrt(r))
p = [1]*r
p[0] = 0
p[3::2] = [0 for x in range(3,r,2)]
for i in range(2,sqrt,2):
    if p[i]:
        p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)] 
  
for i in n:
     print sum(p[:i])
### BUGGY CODE ###

N=int(input())
S=input()
list=[S[i] for i in range(N)]
for i in range(N-1):
    for j in range(i+1,N):
        if list[i]==list[j]:
            list[j]="0"
        else:
            break
list=[s for s in list if s != '0']
print(len(list))


### FIXED CODE ###

N=int(input())
S=input()
list=[S[i] for i in range(N)]
for i in range(N-1):
        if list[i]==list[i+1]:
            list[i]="0"
list=[s for s in list if s != '0']
print(len(list))
### BUGGY CODE ###

import sys
import collections


def solve():
    sys.setrecursionlimit(2000)
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    h, w = list(map(int, input().rstrip('\n').split()))
    a = [list(str(input().rstrip('\n'))) for _ in range(h)]
    ql = []
    ql = collections.deque(ql)
    fq = collections.defaultdict(list)
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                ql.append([0, i, j])
                fq[i, j]

    mt = 0
    while True:
        if len(ql) != 0:
            cost, xv, yv = ql.popleft()
            mt = max(mt, cost)
            for xv, yv in [[xv + 1, yv], [xv - 1, yv], [xv, yv + 1], [xv, yv - 1]]:
                if 0 <= xv < h and 0 <= yv < w:
                    if (xv, yv) not in fq:
                        ql.append([cost + 1, xv, yv])
                        fq[xv, yv]
        else:
            break
    print(mt)


if __name__ == '__main__':
    solve()


### FIXED CODE ###

import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    h, w = list(map(int, readline().split()))
    a = [[True] * w for _ in range(h)]
    ql = collections.deque()
    for i in range(h):
        s = list(str(readline().rstrip().decode('utf-8')))
        for j in range(w):
            if s[j] == "#":
                ql.append([0, i, j])
                a[i][j] = False
    while ql:
        cost, x, y = ql.popleft()
        for xv, yv in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
            if 0 <= xv < h and 0 <= yv < w:
                if a[xv][yv]:
                    ql.append([cost + 1, xv, yv])
                    a[xv][yv] = False
    print(cost)


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

s = list(input())
def change(a):
	global turn
	if [s[a], s[a+1], s[a+2]] == ["A", "B", "C"]:
		s[a], s[a+1], s[a+2] = s[a+1], s[a+2], s[a]
		turn += 1
		if a > 0:
			return a - 1
		else:
			return a + 2 
	else:
		return a + 1

pointer = 0
turn = 0
while pointer < len(s)-2:
	pointer = change(pointer)
print(turn)

### FIXED CODE ###


s = list(input())
ls = len(s)
p = 0
status = 0
a = 0
ans = 0
while p < ls:
    if status == 0:
        if s[p] == "A":
            status = 1
            a = 1

    elif status == 1:
        if s[p] == "A":
            a += 1
        elif s[p] == "B":
            status = 2
        else:
            status = 0
            a = 0

    elif status == 2:
        if s[p] == "C":
            ans += a
            status = 1

        elif s[p] == "A":
            status = 1
            a = 1
        elif s[p] == "B":
            status = 0
    p += 1

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

import sys
sys.setrecursionlimit(100001)

N = int(input())
memo = [[-1]*(N-1) for i in range(N)]

def frog(h,h2,h1):
    if h1 <= 0:
        if memo[h2][0]  == -1:
            memo[h2][0] = abs(h[h2] - h[0])
        return memo[h2][0]
    elif memo[h2][h1] == -1:
        memo[h2][h1] = min(frog(h,h1,h1-1)  + abs(h[h2]-h[h1]),frog(h,h1,h1-2)+abs(h[h2] - h[h1]))
        return memo[h2][h1]
    else:
        return memo[h2][h1]


s = input().split()

h = []
for i in range(N):
    h.append(int(s[i]))
print(min(frog(h,N-1,N-2),frog(h,N-1,N-3)))
#print(memo)

### FIXED CODE ###

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

if N ==2:
    print(abs(h[1] - h[0]))
else:
    dp = [10**4+1]*N
    dp[0] = 0
    dp[1] = abs(h[0]-h[1])
    for i in range(2,N):
        dp[i] = min(dp[i-2]+abs(h[i] - h[i-2]), dp[i-2] + abs(h[i-2]-h[i-1]) + abs(h[i-1] - h[i]), dp[i-1]+abs(h[i-1]-h[i]))
        
    print(dp[N-1])
### BUGGY CODE ###

N ,Q = map(int, input().split())
S = input()

pos = []
for i in range(Q):
    pos.append(list(map(int, input().split())))
    
def countAC(S, r, l):
    data = S[r-1:l]
    cnt = 0
    for i in range(len(data)-1):
        if data[i]=='A':
            if data[i+1] == 'C':
                cnt+=1
    return cnt
  
ret = []
for it in pos:
    cnt = countAC(S, it[0], it[1])
    ret.append(cnt)
_ = [print(it) for it in ret]

### FIXED CODE ###

N ,Q = map(int, input().split())
S = input()

cumul = [0]*len(S)
cnt = 0
for i in range(len(S)-1):
    cumul[i] = cnt
    if S[i]=='A':
        if S[i+1] == 'C':
            cnt+=1
cumul[-1]=cnt

ret = []
for i in range(Q):
    l, r = map(int, input().split())
    cnt = cumul[r-1] - cumul[l-1]
    ret.append(str(cnt))
print('\n'.join(ret))
### BUGGY CODE ###

#!/usr/bin/env python
# coding: utf-8

# In[15]:


N = int(input())
P = list(map(int, input().split()))
print(sum([1 if x == min(P[:i+1]) else 0 for i,x in enumerate(P)]))


# In[ ]:






### FIXED CODE ###

#!/usr/bin/env python
# coding: utf-8

# In[22]:


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


# In[23]:


ans = 0
tmp = P[0]
for i in range(N):
    if P[i] <= tmp:
        ans += 1
        tmp = P[i]
print(ans)


# In[ ]:
### BUGGY CODE ###

N,K = map(int,input().split())
p = list(map(int,input().split()))
E = [(1+i)/2 for i in p]
ans = 0
i = 0
while N-K-i >= 0:
  A = sum(E[N-K-i:N-i])
  ans = max(ans,A)
  i += 1
print(ans)

### FIXED CODE ###

N,K = map(int,input().split())
p = list(map(int,input().split()))
E = [(1+i)/2 for i in p]
S = [0]*(N+1)
sum = 0
for i in range(N):
  sum += E[i]
  S[i+1] = sum
ans = 0
i = 0
while N-K-i >= 0:
  A = S[K+i] -S[i]
  ans = max(ans,A)
  i += 1
print(ans)
### BUGGY CODE ###

N, M = list(map(int,input().split()))
 
d = int(M/N)

def solve():
    # ans = 1
    # for i in range(M//N+1, 0, -1):
    #     if M % i == 0:
    #         if (M // i) >= N:
    #             print(i)
    #             break
    for i in range(d):
    	if M%(d-i) == 0:
    		print(d-i)
    		break

	# i = 0
	# while True:
	# 	if M%(N+i+1) == 0:
	# 		print(int(M/(N+i+1)))
	# 		break
	# 	i += 1

if M%N == 0:
	print(d)
else:
	solve()

### FIXED CODE ###

import math, sys, random

N, M = list(map(int,input().split()))
# N, M = 1111, 1000000000

def is_prime(n):
    if n == 2: return True
    if n == 1 or n & 1 == 0: return False

    d = (n - 1) >> 1
    while d & 1 == 0:
        d >>= 1

    for k in range(100):
        a = random.randint(1, n - 1)
        t = d
        y = pow(a, t, n)

        while t != n - 1 and y != 1 and y != n - 1:
            y = (y * y) % n
            t <<= 1

        if y != n - 1 and t & 1 == 0:
            return False

    return True

d = int(math.floor(M/N))

if M%N == 0:
	print(d)
	sys.exit()

i = 0
f = False
while True:
	if M%(N+i+1) == 0:
		print(int(M/(N+i+1)))
		sys.exit()
	i += 1
	if i == 100000 and is_prime(M):
		print(1)
		sys.exit()
### BUGGY CODE ###

def solve():
    N = int(input())
    T = []
    A = []
    for _ in range(N):
        t, a = map(int,input().split())
        T.append(t)
        A.append(a)
    
    now_t = T[0]
    now_a = A[0]

    for i in range(1,N):
        mul_t = T[i]
        mul_a = A[i]
        j = 2
        while now_t > mul_t or now_a > mul_a:
            mul_t = T[i] * j
            mul_a = A[i] * j
            j += 1
        else:
            now_t = mul_t
            now_a = mul_a
    
    print(now_t + now_a)


if __name__ == '__main__':
    solve()

### FIXED CODE ###

from math import ceil
def solve():
    N = int(input())
    T = []
    A = []
    for _ in range(N):
        t, a = map(int,input().split())
        T.append(t)
        A.append(a)
    
    now_t = T[0]
    now_a = A[0]

    for i in range(1,N):
        mul = f(now_t,now_a,T[i],A[i])
        now_t = T[i] * mul
        now_a = A[i] * mul
    
    print(now_t + now_a)

def f(now_t, now_a, t, a):
    left = 0
    right = 10 ** 18
    while right - left > 1:
        mid = (right + left) // 2
        if now_t > t * mid or now_a > a * mid:
            left = mid
        else:
            right = mid
    
    return right

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

n,k=map(int,input().split())
p=[int(i)-1for i in input().split()]
c=list(map(int,input().split()))
ans=-10**9
for i in range(n):
    start=i
    cycle=[0]
    for _ in range(n):
        i=p[i]
        cycle+=[cycle[-1]+c[i]]
        if i==start:break
    S,L=cycle[-1],len(cycle)-1
    if S<=0:ans=max(ans,max(cycle[1:k+1]))
    else:ans=max(ans,max(0,k//L-1)*S+max(max(cycle[:k+1]),max(cycle[:k%L+1])+S*(k>L))) 
print(ans)

### FIXED CODE ###

n,k=map(int,input().split())
p=[int(i)-1for i in input().split()]
c=list(map(int,input().split()))
ans=-10**9
for i in range(n):
    start=i
    cycle=[0]
    for _ in range(n):
        i=p[i]
        cycle+=[cycle[-1]+c[i]]
        if i==start:break
    S,L=cycle[-1],len(cycle)-1
    if S<=0:ans=max(ans,max(cycle[1:k+1]))
    else:
        if k>L:ans=max(ans,(k//L-1)*S+max(max(cycle),max(cycle[:k%L+1])+S)) 
        else:ans=max(ans,max(cycle[:k+1]))
print(ans)
### BUGGY CODE ###

#LとRが変わる点に着目
#その点でどちらに行くかを繰り返す
#lの連続しているところの一番左に行く、rの連続しているところの右に行く
#偶数と奇数の時で変わりそう
#二つ入れた時にその繰り返しになったら止めれば良さそう
#二回を繰り返しの単位としてその二回でできたものが同じになっていれば終了
#deepcopyは使うなあほ
import copy
s=input()
n=len(s)

def make_new_list(a_1,a_2):
    for i in range(n):
        if s[i]=="L":
            a_2[i-1]+=a_1[i]
        else:
            a_2[i+1]+=a_1[i]



a0=[1]*n
a1=[0]*n
make_new_list(a0,a1)
a2=[0]*n
make_new_list(a1,a2)
a3=[0]*n
make_new_list(a2,a3)
a4=[0]*n
make_new_list(a3,a4)

while a1!=a3 or a2!=a4:
    a1=copy.deepcopy(a3)
    a2=copy.deepcopy(a4)
    a3=[0]*n
    make_new_list(a2,a3)
    a4=[0]*n
    make_new_list(a3,a4)

print(" ".join(list(map(str,a4))))


### FIXED CODE ###

#こういう問題で実際に動かすやつはあほ、普通にチェンジするとこまで行って、どっちか(偶数だけ離れてるとこ)に集まるやろ
s=input()
n=len(s)
#変わる点というより変わる点にくる集まりを考えるべき
d=0
a=[]
ax=[0]

for i in range(n):
    l=len(ax)
    if l==1:
        if s[i]=="L":
            ax.append(i-1)
            ax.append(i)
        if i==n-1:
            ax.append(i)
            a.append(ax)
    elif l==3:
        if s[i]=="R":
            ax.append(i-1)
            a.append(ax)
            ax=[i]
        elif i==n-1:
            ax.append(i)
            a.append(ax)
m=len(a)
c=[0]*n
for i in range(m):
    #print(type(a[i][1]))
    #print(type(c[a[i][1]]))
    c[a[i][1]]=(a[i][1]-a[i][0])//2+(a[i][3]-a[i][1])//2+1
    c[a[i][2]]=(a[i][2]-a[i][0])//2+(a[i][3]-a[i][2])//2+1

print(" ".join(list(map(str,c))))
### BUGGY CODE ###

# ABC151 C
n, m = list(map(int, input().split()))
ac = []
wa = {}
cnt_ac = 0
cnt_wa = 0
for _ in range(1, m):
    res = input().split()
    if res[1] == "AC":
        if not res[0] in ac:
            cnt_ac += 1
            ac.append(res[0])
            if res[0] in wa.keys():
                cnt_wa += wa[res[0]]
    else:
        if res[0] in wa.keys():
            wa[res[0]] += 1
        else:
            wa[res[0]] = 1
print(cnt_ac, cnt_wa)

### FIXED CODE ###

N,M=map(int,input().split())
AC=[0]*N
WA=[0]*N
wa=0
for i in range(M):
  p,s=input().split()
  p=int(p)-1
  if AC[p]==0 and s=="AC":
  	AC[p]=1
  	wa+=WA[p]
  WA[p]+=1
print(sum(AC),wa)