Harryxun/stf_run huggingface dataset:
N,K=map(int,(input().split()))
TO=list(map(int,input().split()))

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    for i in range((K-roop)%roop):
        next_town = TO[next_town-1]
    ans=next_town

print(ans)

### FIXED CODE ###

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

visit_flag=[[0]*2 for i in [1]*N]
visit_count=0
first_visit=0
last_visit=0
next_town=1
visit_flag[0][0]=1
visit_flag[0][1]=0

for i in range(K):
    visit_count+=1
    next_town = TO[next_town-1]
    if visit_flag[next_town-1][0]==0:
        visit_flag[next_town-1][0]=1
        visit_flag[next_town-1][1]=visit_count
    else:
        first_visit=visit_flag[next_town-1][1]
        last_visit=visit_count
        break

roop=last_visit-first_visit
span=visit_flag[next_town-1][1]

if K<=roop:
    next_town = 1
    for i in range(K):     
        next_town = TO[next_town-1]
    ans=next_town
else:
    if roop==0:
        ans=next_town
    else:
        for i in range((K-span)%roop):
            next_town = TO[next_town-1]
        ans=next_town

print(ans)
import sys
A=[list(map(int,input().split())) for i in range()]
N=int(input())
b=[list(map(int,input().split())) for i in range(N)]
flat=[j for i in A for j in i]
for i,value in enumerate(flat):
    if [value] in b:
         flat[i]=-1
for i in range(3):
    if flat[i]==flat[i+1]==flat[i+2]==-1:
        print('Yes')
        sys.exit()
    if flat[i]==flat[i+3]==flat[i+6]==-1:
        print('Yes')
        sys.exit()
if flat[0]==flat[4]==flat[8]==-1 or flat[2]==flat[4]==flat[6]==-1:
    print('Yes')
    sys.exit()
print('No')

### FIXED CODE ###

import sys
A=[list(map(int,input().split())) for i in range(3)]
N=int(input())
b=[list(map(int,input().split())) for i in range(N)]
flat=[j for i in A for j in i]
for i,value in enumerate(flat):
    if [value] in b:
         flat[i]=-1
for i in range(3):
    if flat[i*3]==flat[i*3+1]==flat[i*3+2]==-1:
        print('Yes')
        sys.exit()
    if flat[i]==flat[i+3]==flat[i+6]==-1:
        print('Yes')
        sys.exit()
if flat[0]==flat[4]==flat[8]==-1 or flat[2]==flat[4]==flat[6]==-1:
    print('Yes')
    sys.exit()
print('No')
N, M = [int(nm) for nm in input().split()]
for n in range(N):
  KA = [int(ka) for ka in input().split()]
  for ka in range(len(KA)-1):
    L.append(KA[ka+1])
like = 0
for m in range(M):
  if L.count(m+1) == N:
    like += 1
print(like)

### FIXED CODE ###

N, M = [int(nm) for nm in input().split()]
L = []
for n in range(N):
  KA = [int(ka) for ka in input().split()]
  for ka in range(len(KA)-1):
    L.append(KA[ka+1])
like = 0
for m in range(M):
  if L.count(m+1) == N:
    like += 1
print(like)
import sys
def input():
    return sys.stdin.readline()[:-1]

n = int(input())

if n > 81:
    print('No')
    sys.exit()
if n < 10:
  	print('Yes')
    sys.exit()

for i in range(1, 10):
    for j in range(1, 10):
        if n == i * j:
            print('Yes')
            sys.exit()

print('No')

### FIXED CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]

n = int(input())

if n > 81:
    print('No')
    sys.exit()


for i in range(1, 10):
    for j in range(1, 10):
        if n == i * j:
            print('Yes')
            sys.exit()

print('No')
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
m = 0
for i in range(N):
    m += B[a[i] - 1]
for t in range(N-1):
    if a[t] + 1 == a[t + 1]:
        m += C[a[t] - 1]
print(m)
         

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
m = 0
for i in range(N):
    m += B[A[i] - 1]
for t in range(N-1):
    if A[t] + 1 == A[t + 1]:
        m += C[A[t] - 1]
print(m)
s,w = int(input())

if s<=w:
  print("unsafe")
else:
  print("safe")

### FIXED CODE ###

s,w = map(int,input().split())

if s<=w:
  print("unsafe")
else:
  print("safe")
from collections import defaultdict
import heapq
inpl = lambda: list(map(int,input().split()))

X, Y, Z, K = inpl()
A = [ sorted(inpl(), reverse=True) for _ in range(3) ]
N = [X, Y, Z]

mem = defaultdict(int)
score = sum([A[i][0] for i in range(3)])
mem[(0,0,0)] = 1
candidates = []
heapq._heappush_max(candidates, (score,(0,0,0)))

for i in range(K):
    c = heapq._heappop_max(candidates)
    print(c[0])
    n = list(c[1])
    
    for p in range(3):
        if n[p] < N[p]-1:
            n[p] += 1
            if mem[tuple(n)] == 0:
                mem[tuple(n)] = 1
                score = sum([A[j][n[j]] for j in range(3)])
                heapq._heappush_max(candidates, (score,tuple(n)))
            n[p] -= 1

### FIXED CODE ###

from collections import defaultdict
inpl = lambda: list(map(int,input().split()))

X, Y, Z, K = inpl()
A = [ sorted(inpl(), key=lambda x: -x) for _ in range(3) ]
N = [X, Y, Z]

mem = defaultdict(int)
ans = sum([A[i][0] for i in range(3)])
mem[(0,0,0)] = 1
cand = [(ans,0,0,0)]

for i in range(K):
    cand.sort()
    c = cand[-1]
    print(c[0])
    n = [c[1],c[2],c[3]]
    cand.pop(-1)
    
    for p in range(3):
        if n[p] < N[p]-1:
            n[p] += 1
            if mem[(n[0],n[1],n[2])] == 0:
                mem[(n[0],n[1],n[2])] = 1
                cand.append((sum([A[j][n[j]] for j in range(3)]),n[0],n[1],n[2]))
            n[p] -= 1


### FIXED CODE ###

def main():
    (n,k),p,c=[[*map(int,t.split())]for t in open(0)]
    m=-10**18
    for i in range(n):
        s=i
        t=1
        x=c[s]
        while p[s]-1!=i:
            s=p[s]-1
            t+=1
            x+=c[s]
        if x>0:
            a=(k//t-1)*x
            if a>m:m=a
            s=i
            for _ in range(k%t+t):
                s=p[s]-1
                a+=c[s]
                if a>m:m=a
        else:
            a=0
            s=i
            for _ in range(min(k,t)):
                s=p[s]-1
                a+=c[s]
                if a>m:m=a
    print(m)
main()
S = input()
count1 = 0
count2 = 0
answer = "WA"

if S[0] == 'A':
    exception = [0]
    for i in range(2,len(S)-1):
        print(i)
        if S[i] == 'C':
            count1 += 1

if count1 == 1:
    C = S.index('C')
    exception.append(C)

if len(exception) > 1:
    for j in range(len(S)):
        if j not in exception and S[j].islower():
            answer = "AC"

print(answer)


### FIXED CODE ###

S = input()
count = 0
exception = [0]
answer = "WA"

if S[0] == 'A' and S[2:-1].count('C') == 1:
    C = S.index('C')
    exception.append(C)

if len(exception) == 2:
    for j in range(len(S)):
        if j not in exception and S[j].islower():
            count += 1

if count == len(S)-2:
    answer = "AC"

print(answer)
import math

def least_common_multiple(x,y):
    return (x * y) / math.gcd(x, y)
    
a,b = map(int,input().split(" "))

print(int(least_common_multiple(a,b)))


### FIXED CODE ###

a,b = map(int, input().split(" "))

import fractions
def lcm(x, y):
    return (x * y) / fractions.gcd(x,y)
    
print(int(lcm(a, b)))
# -*- coding: utf-8 -*-
import math
from functools import reduce

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = int(input())

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

print(gcd_list(A))

### FIXED CODE ###

# -*- coding: utf-8 -*-
from functools import reduce

def gcd(a, b):
    x = max(a, b)
    y = min(a, b)
    z = y
    while x%y != 0:
        z = x%y
        x = y
        y = z
    else:
        return z
            

def gcd_list(numbers):
    return reduce(gcd, numbers)


N = int(input())

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

print(gcd_list(A))
# じょえチャンネル
# 高評価・チャンネル登録宜しくお願いします！
# https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ

import sys
from fractions import gcd
from itertools import groupby as gb
from itertools import permutations as perm
from itertools import combinations as comb
from collections import Counter as C
from collections import defaultdict as dd
sys.setrecursionlimit(10**5)
def y(f):
    if f:
        print('Yes')
    else:
        print('No')
def Y(f):
    if f:
        print('YES')
    else:
        print('NO')
def Z(f):
    if f:
        print('Yay!')
    else:
        print(':(')
def ispow(n):
    if int(n**.5)**2==n:
        return True
    return False

n = int(input())
a = list(map(int,input().split()))

res = 10 ** 100
for i in range(-100, 200):
    tmp = 0
    for j in range(a):
        tmp += (i - j) ** 2
    res = min(res, tmp)
print(res)

### FIXED CODE ###

# じょえチャンネル
# 高評価・チャンネル登録宜しくお願いします！
# https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ

import sys
from fractions import gcd
from itertools import groupby as gb
from itertools import permutations as perm
from itertools import combinations as comb
from collections import Counter as C
from collections import defaultdict as dd
sys.setrecursionlimit(10**5)
def y(f):
    if f:
        print('Yes')
    else:
        print('No')
def Y(f):
    if f:
        print('YES')
    else:
        print('NO')
def Z(f):
    if f:
        print('Yay!')
    else:
        print(':(')
def ispow(n):
    if int(n**.5)**2==n:
        return True
    return False

n = int(input())
a = list(map(int,input().split()))

res = 10 ** 100
for i in range(-100, 200):
    tmp = 0
    for j in a:
        tmp += (i - j) ** 2
    res = min(res, tmp)
print(res)
n = int(input())
dict = {}
for i in range(n):
  a = input()
  if (not (a in list)):
    dict[a] = 0
  else:
    dict[a] += 1
b = max(dict.values())
for i in dict.keys():
  if dict[i] == b:
    print(i+"\n")
  

### FIXED CODE ###

n = int(input())
dict = {}
for i in range(n):
  a = input()
  if (not (a in dict.keys())):
    dict[a] = 1
  else:
    dict[a] += 1
b = max(dict.values())
list = []
for i in dict.keys():
  if dict[i] == b:
    list.append(i)
list.sort()
for i in list:
  print(i)
N,X=map(int,input().split())
L = [int(i) for i in input().split()] 

a=0
for i in range(N):
    a+=L[i]
    if a > X:
        ans=i+1
        break

print(ans)
        

### FIXED CODE ###

N,X=map(int,input().split())
L = [int(i) for i in input().split()] 
ans=N+1
a=0
for i in range(N):
    a+=L[i]
    if a > X:
        ans=i+1
        break

print(ans)
N = int(input())
A = list(map(int, input().split())) 
ans = 0
 
for n in range(N-1):
  t = A[n] - A[n+1]
  if t <= 0: 
    continue
  else:
    A[n+1] += t
    ans += t
 
print(ans)

### FIXED CODE ###

import math
N, X, T = map(int, input().split())

print(math.ceil(N / X)*T)
today_weather = int()

if today_weather = ‘Sunny’:
    tomorrow_weather = ‘Cloudy’

elif today_weather = ‘Cloudy’:
    tomorrow_weather = ‘Rainy’

elif today_weather = ‘Rainy’:
    tomorrow_weather = ‘Suuny’

print(tomorrow_weather)

### FIXED CODE ###

S = input()

if S == 'Sunny':
    print("Cloudy")

elif S == 'Cloudy':
    print("Rainy")

elif S == 'Rainy':
    print("Sunny")
import bisect
a,b=map(int,input().split())
c=list(map(int,input().split()))

if a not in c:
    print(a)
    exit()

n=[i for i in range(-1,101)]

m=sorted(list(set(n)^set(c)))
bisect.insort_right(m,a)

m=sorted(list(set(m)))
if len(m)=<3:
    print(m[m.index(a)-1])

elif b==0:
    print(a)

elif a==100:
    print(m[m.index(a)+1])

elif abs(a-m[m.index(a)-1])<abs(a-m[m.index(a)+1]):
    print(m[m.index(a)-1])

elif abs(a-m[m.index(a)-1])==abs(a-m[m.index(a)+1]):
    print(m[m.index(a)-1])

else:
    print(m[m.index(a)+1])

### FIXED CODE ###

a,b=map(int,input().split())
c=list(map(int,input().split()))
ans=0
for i in range(100):
    if a-i not in c:
        ans=a-i
        break
    elif a+i not in c:
        ans=a+i
        break
print(ans)
def main():
    n = int(input())
    nums = list(map(int, input().split()))
    print('{:.16g}'.format(1 / sum(1 / x for x in nums)))


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    n = int(input())
    nums = list(map(int, input().split()))
    print('{:.16g}'.format(1 / sum(1 / x for x in nums)))


if __name__ == '__main__':
    main()
input=list(input())
if(input[2]==input[3] and input[5]==input[6]):
  print("Yes")
else:
  print("No")

### FIXED CODE ###

string=str(input())
if(string[2]==string[3] and string[4]==string[5]):
    print("Yes")
else:
    print("No")
N, A, B, C = map(int, input().split())
ls = [int(input) for i in range(N)]

def dfs(cur, a, b, c):
  if cur==N:
    return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else 10**9
  ret0 = dfs(cur+1, a+ls[cur], b, c)
  ret1 = dfs(cur+1, a, b+ls[cur], c)
  ret2 = dfs(cur+1, a, b, c+ls[cur])
  ret3 = dfs(cur+1, a, b, c)
  return min(ret0, ret1, ret2, ret3)
print(dfs(0,0,0,0))

### FIXED CODE ###

N, A, B, C = map(int, input().split())
ls = [int(input()) for i in range(N)]

def dfs(cur, a, b, c):
  if cur==N:
    return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else 10**9
  ret0 = dfs(cur+1, a+ls[cur], b, c)+10
  ret1 = dfs(cur+1, a, b+ls[cur], c)+10
  ret2 = dfs(cur+1, a, b, c+ls[cur])+10
  ret3 = dfs(cur+1, a, b, c)
  return min(ret0, ret1, ret2, ret3)
print(dfs(0,0,0,0))
N,K = map(int,input().split())
h =[]
dif = []
for n in range(0,N):
    h.append(int(input()))

h.sort()

m = 10**10

for i in range(0,N-K+1):
    m = min(m,abs(h[i] - h[i+K-1]))

print(m)



### FIXED CODE ###

N = int(input())
pLs=[]
for n in range(0,N):
    pLs.append(int(input()))

pLs.sort(reverse = True)

sm = 0
pLs[0] = int(pLs[0]/2)
for n in range(0,N):
    sm += pLs[n]

print(sm)
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
if abs(b-a) - (v-w)*t <= 0:
    print("YES")
ekes:
    print("NO")

### FIXED CODE ###

a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
if abs(b-a) - (v-w)*t <= 0:
    print("YES")
else:
    print("NO")
def H(h, x, y, cx, cy):
    return h + abs(x-cx) + abs(y-cy)
def Hr(h, x, y, cx, cy):
    return max(h-abs(x-cx)-abs(y-cy),0)


N = int(input())
x = [list(map(int, input().split())) for i in range(N)]
for cx in range(0, 101):
    for cy in range(0, 101):
        judge = True
        m = [l for l in range(N) if x[l][2]!=0][0]
        H0 = H(x[m][2], x[m][0], x[m][1], cx, cy)
        for n in range(len(x)):
            Hn = H(x[n][2], x[n][0], x[n][1], cx, cy)
            #print(Hn)
            if(x[n][2] != 0):
                if(Hn == H0):
                    judge = judge and True
                else:
                    judge = judge and False
        if(judge):
            for n in range(len(x)):
                if(x[n][2] == 0):
                    Hr = Hr(H0, x[n][0], x[n][1], cx, cy)
                    if(Hr == 0):
                        judge = judge and True
                    else:
                        judge = judge and False
                
        if(judge):
            print(cx, cy, H0)


### FIXED CODE ###

def H(h, x, y, cx, cy):
    return h + abs(x-cx) + abs(y-cy)
def Hr(h, x, y, cx, cy):
    return max(h-abs(x-cx)-abs(y-cy),0)


N = int(input())
x = [list(map(int, input().split())) for i in range(N)]
for cx in range(0, 101):
    for cy in range(0, 101):
        judge = True
        m = [l for l in range(N) if x[l][2]!=0][0]
        H0 = H(x[m][2], x[m][0], x[m][1], cx, cy)
        for n in range(len(x)):
            Hn = H(x[n][2], x[n][0], x[n][1], cx, cy)
            #print(Hn)
            if(x[n][2] != 0):
                if(Hn == H0):
                    judge = judge and True
                else:
                    judge = judge and False
        if(judge):
            for n in range(len(x)):
                if(x[n][2] == 0):
                    Hm = Hr(H0, x[n][0], x[n][1], cx, cy)
                    if(Hm == 0):
                        judge = judge and True
                    else:
                        judge = judge and False
                
            if(judge):
                print(cx, cy, H0)
n = int(input())

qq = []
for i in range(1,10):
  for j in range(1,10):
    qq.append(i*j) if i*j not in qq

print('Yes') if n in qq else print('No')

### FIXED CODE ###

n = int(input())

lis = []
for i in range(10):
  for j in range(i,10):
    lis.append(i*j)
    
if n in lis: print('Yes')
else: print('No')
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
if v = w and a == b:
    print('YES')
elif v = w and a != b:
    print('NO')
elif a == b:
    print('YES')
else:
    if 0 <= ((b - a) / (v - w)) <= t:
        print('YES')
    else:
        print('NO')

### FIXED CODE ###

a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
if v <= w:
    print('NO')
else:
    if 1 <= (abs(b - a) / (v - w)) <= t:
        print('YES')
    else:
        print('NO')
import sys,math,collections,itertools,bisect
input = sys.stdin.readline

N=int(input())
A=list(map(int,input().split()))
process = []
#--最大最小を見つける--#
maxA = A.pop(A.index(max(A)))
minA = A.pop(A.index(min(A)))
A.append(minA-maxA)
process.append(A)

for i in range(len(A)-2):
    maxA = A.pop(A.index(max(A)))
    A[-1]-=maxA
    process.append(list(q))
print(A[0]-A[-1])
for pro in process:
    print(*pro)


### FIXED CODE ###

import sys,math,collections,itertools,bisect
input = sys.stdin.readline

N=int(input())
A=sorted(list(map(int,input().split())))
process = []
for i in range(N-2):
    tmp = A.pop()
    y=A.pop()
    A.append(tmp)
    if y>=0:
        process.append([A[0],y])
        A[0]-=y
    else:
        process.append([A[-1],y])
        A[-1]-=y
process.append([A[-1],A[0]])
print(A[-1]-A[0])
for pro in process:
    print(*pro)
list1 = [int(i) for i in input().split()]
print("{0}".format(list[0]))

### FIXED CODE ###

item = [int(i) for i in input().split()]
print(item[0]*item[1],2*(item[0]+item[1]))
a, b, c,k = map(int,input().split())

if a + b >= k :
    print(a)
elif a > k :
    print(k)
else :

    print(2*a - k + b)


### FIXED CODE ###

n=input()
m=input()

M=(m[:-1])
if n ==M :
  print("Yes")
else : 
  print("No")
# -*- coding: utf-8 -*-

inlist = input().split()
n = int(inlist[0])
m = int(inlist[1])
k = int(inlist[2])
mat = [[1 for i in range(n)] for j in range(m)]

def initialize(i, j, matrix):
    for k in range(i):
        for l in range(j):
            matrix[k][l] = 1
    return matrix

def sum_mat(i, j, matrix):
    sum = 0
    for k in range(i):
        for l in range(j):
            sum = sum + matrix[k][l]
    return sum

escape = False

for l in range(n + 1): #l = 0 -> n
    mat = initialize(n, m, mat)
    for i in range(l):
        for j in range(m):
            mat[i][j] = -mat[i][j]
    mat_save = mat
    for p in range(m + 1): # p = 0 -> m
        mat = mat_save
        for j in range(p):
            for i in range(n):
                mat[i][j] = -mat[i][j]
        sum = sum_mat(n, m, mat)
        if(sum == n * m - 2 * k):
            print("Yes")
            escape = True
            break;
        elif(l == n and p == m):
            print("No")
    if(escape):
        break;

### FIXED CODE ###

# -*- coding: utf-8 -*-

s = input()
l = s.split("I")
if(l[0] == "YAK" and len(s) > 3):
    print("Yes")
else:
    print("No")
n = int(input())
a = list(map(int, input().split()))
ans = []
for b in range(-(n // 2), n // 2):
  total = 0
  for i in range(n):
    total += abs(a[i] - (b + i + 1))
  ans.append(total)
print(min(ans))

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
B = []
ans = 0
for i in range(n):
  B.append(a[i] - i - 1)
b = sorted(B)[len(B) // 2]
for i in range(n):
  ans += abs(B[i] - b)
print(ans)
#s = input()
"""
s = '101'
s = '100000000'
s = '00001011'
"""
k = len(s)
for i in range(1, len(s)):
    if s[i-1] != s[i+1]:
        k = min(k, max(i, len(s)-i))
print(k)




### FIXED CODE ###

s = input()
"""
s = '101'
s = '100000000'
s = '00001011'
"""
k = len(s)
for i in range(1, len(s)):
    if s[i-1] != s[i]:
        k = min(k, max(i, len(s)-i))
print(k)
import sys
from bisect import bisect
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N = int(input())
    A = sorted(list(map(int, input().split())))
    B = sorted(list(map(int, input().split())))
    C = sorted(list(map(int, input().split())))

    Bn = [0] * N
    for i in range(N):
        b = B[i]
        m = bisect(C, b)
        Bn[i] = (N - m)

    Bn = list(accumulate(Bn[::-1]))[::-1]

    ans = 0
    for i in range(N):
        a = A[i]
        m = bisect(B, a)
        ans += Bn[m]

    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from bisect import bisect
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N = int(input())
    A = sorted(list(map(int, input().split())))
    B = sorted(list(map(int, input().split())))
    C = sorted(list(map(int, input().split())))

    Bn = [0] * N
    for i in range(N):
        b = B[i]
        m = bisect(C, b)
        Bn[i] = (N - m)

    Bn = list(accumulate(Bn[::-1]))[::-1]
    Bn += [0]

    ans = 0
    for i in range(N):
        a = A[i]
        m = bisect(B, a)
        ans += Bn[m]

    print(ans)


if __name__ == '__main__':
    main()
N, X = map(int, input().split())
L = list(map(int, input().split()))

s = 0
for i in range(N + 1):
    s += L[i]
    if s > X:
        break

print(i + 1)

### FIXED CODE ###

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

s = 0
for i in range(N):
    s += L[i]
    if s > X:
        print(i + 1)
        exit()

print(i + 2)
from collections import deque
n, m = list(map(int, input().split()))
al = [0] * n
bl = [0] * n
G = [[] for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
N = n
# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

dist = [-1]*N
tag = [-1]*N
que = deque([0])
dist[0] = 0
pre = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        tag[w] = v + 1
        que.append(w)

print('Yes')
for t in tag[1:]:
    print(t)


### FIXED CODE ###

from collections import deque
n, m = list(map(int, input().split()))
al = [0] * n
bl = [0] * n
G = [[] for _ in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
N = n
# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

dist = [-1]*N
tag = [-1]*N
que = deque([0])
dist[0] = 0
pre = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        tag[w] = v + 1
        que.append(w)

print('Yes')
for t in tag[1:]:
    print(t)
import sys
sys.setrecursionlimit(10000)

def rec(i,j,k):
    if k == N or i == N - 1 or j == N - 1:
        return
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - i)
    if tmp > dp[i+1][j]:
        dp[i+1][j] = tmp
        rec(i + 1, j, k + 1)
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - N + j + 1)
    if tmp > dp[i][j+1]:
        dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
        rec(i, j + 1, k + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

### FIXED CODE ###

from collections import deque

N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]
d = deque([(0,0)])
dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
while(len(d)>0):
    i, j = d.pop()
    if i + j > N - 1:
        continue
    tmp = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - i)
    if tmp > dp[i+1][j]:
        if dp[i+1][j] == 0:
            d.appendleft((i+1,j))
        dp[i+1][j] = tmp
    tmp = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - N + j + 1)
    if tmp > dp[i][j+1]:
        if dp[i][j+1] == 0:
            d.appendleft((i,j+1))
        dp[i][j+1] = tmp
        
ans = -1
for i in range(N):
    for j in range(N):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)