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

N, *ABC = map(int, input().split())
l = [int(input()) for _ in range(N)]
#print(min(min(func(l, abc)) for abc in [[A,B,C], [A,C,B], [B,A,C], [B,C,A], [C,A,B], [C,B,A]]))

def func(l, ABC, point=0):
    if len(ABC)==0:
        yield point
    else:
        x = ABC[0]
        p = min(abs(x-y) for y in l)
        if x+p in l:
            lc = l.copy()
            lc.remove(x+p)
            yield from func(lc, ABC[1:], point+p)
        if x-p in l:
            lc = l.copy()
            lc.remove(x-p)
            yield from func(lc, ABC[1:], point+p)
from itertools import chain
def func2(l, p, A, B, C):
    # for abc in [[A,B,C], [A,C,B], [B,A,C], [B,C,A], [C,A,B], [C,B,A]]:
    #     print(min(func(l, abc)))
    return min(min(func(l, abc, p)) for abc in [[A,B,C], [A,C,B], [B,A,C], [B,C,A], [C,A,B], [C,B,A]])
def grouping(l, g, h, k):
    if all(len(x)>0 for x in [g, h, k]):
        yield g, h, k
    if len(l)>0:
        a, *l = l
        yield from grouping(l, g+[a], h, k)
        yield from grouping(l, g, h+[a], k)
        yield from grouping(l, g, h, k+[a])
        yield from grouping(l, g, h, k)
def func3(l, ABC):
    for g in grouping(l, [], [], []):
        p = sum(len(gg)-1 for gg in g)*10
        yield func2(list(map(sum, g)), p, *ABC)
print(min(func3(l, ABC)))

### FIXED CODE ###

N, *ABC = map(int, input().split())
l = [int(input()) for _ in range(N)]
def grouping(l, g, h, k):
    if all(len(x)>0 for x in [g, h, k]):
        yield g, h, k
    if len(l)>0:
        a, *l = l
        yield from grouping(l, g+[a], h, k)
        yield from grouping(l, g, h+[a], k)
        yield from grouping(l, g, h, k+[a])
        yield from grouping(l, g, h, k)
def func3(l, ABC):
    for g in grouping(l, [], [], []):
        p = sum(len(gg)-1 for gg in g)*10
        l = list(map(sum, g))
        yield sum(abs(x-y) for x,y in zip(l,ABC))+p
print(min(func3(l, ABC)))
### BUGGY CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

from collections import Counter, defaultdict
c = Counter(a)
n1, n2, n3 = c[1], c[2], c[3]
n0 = c[0]
dp = defaultdict(float)
dp[0,0,0] = 0
def sub(i1,i2,i3):
#     print(i1,i2,i3)
    if (i1,i2,i3) in dp:
        return dp[i1,i2,i3]
    ans = 1
    p = 0
    if i1>0:
        ans += i1/n * sub(i1-1,i2,i3)
        p += i1/n
    if i2>0:
        ans += i2/n * sub(i1+1,i2-1,i3)
        p += i2/n
    if i3>0:
        ans += i3/n * sub(i1,i2+1,i3-1)
        p += i3/n
    ans = ans/p
    dp[i1,i2,i3] = ans
    return ans
print(sub(n1,n2,n3))

### FIXED CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

from collections import Counter, defaultdict
c = Counter(a)
n1, n2, n3 = c[1], c[2], c[3]
n0 = c[0]
def glist(*l, fill=None):
    if len(l)==1:
        return [fill]*l[0]
    ll = l[1:]
    return [glist(*ll) for _ in range(l[0])]

count = 0
S = sum(a)
sub = glist(n1+n2+n3+1,n2+n3+1,n3+1)
sub[0][0][0] = 0
#     for i1 in range(min(n+1, s+1)):
#         for i2 in range(min(n+1, (s-i1)//2+1)):
for i3 in range(n3+1):
    for i2 in range(n2+n3+1-i3):
        for i1 in range(n1+n2+n3 - (i2+i3) + 1):
            if i1==i2==i3==0 or i1+i2+i3>n:
                continue
#             print(i1,i2,i3)
            ans = n
            p = 0
            if i1>0:
                ans += i1 * sub[i1-1][i2][i3]
                p += i1
            if i2>0:
                ans += i2 * sub[i1+1][i2-1][i3]
                p += i2
            if i3>0:
                ans += i3 * sub[i1][i2+1][i3-1]
                p += i3
            ans = ans/p
            sub[i1][i2][i3] = ans
print(sub[n1][n2][n3])
### BUGGY CODE ###

import sys
input = sys.stdin.buffer.readline


r, c, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(k)]
INF = 10 ** 18

item = [[0] * c for i in range(r)]
for i, j, v in info:
    i -= 1
    j -= 1
    item[i][j] = v

dp = [[[-INF] * 4 for i in range(c + 1)] for i in range(r + 1)]
for i in range(r + 1):
    dp[i][0][0] = 0
for j in range(c + 1):
    dp[0][j][0] = 0
    
for i in range(r):
    for j in range(c):
        val = item[i][j]
        dp[i + 1][j + 1][0] = max(dp[i + 1][j][0], dp[i][j + 1][0], dp[i][j + 1][1], dp[i][j + 1][2], dp[i][j + 1][3])
        dp[i + 1][j + 1][1] = max(dp[i + 1][j][1], dp[i + 1][j][0] + val,  dp[i][j + 1][0] + val, dp[i][j + 1][1] + val, dp[i][j + 1][2] + val, dp[i][j + 1][3] + val)
        dp[i + 1][j + 1][2] = max(dp[i + 1][j][2], dp[i + 1][j][1] + val)
        dp[i + 1][j + 1][3] = max(dp[i + 1][j][3], dp[i + 1][j][2] + val)
                                   
print(max(dp[-1][-1]))
                                   

### FIXED CODE ###

import sys
input = sys.stdin.buffer.readline


r, c, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(k)]
INF = 10 ** 18

item = [[0] * c for i in range(r)]
for i, j, v in info:
    i -= 1
    j -= 1
    item[i][j] = v

OFFSET = (r + 1)
dp = [[-INF] * ((c + 1) * (r + 1)) for i in range(4)]
for i in range(r + 1):
    dp[0][i] = 0
for j in range(c + 1):
    dp[0][OFFSET * j] = 0
    
for i in range(r):
    for j in range(c):
        val = item[i][j]
        tmp11 = i + 1 + OFFSET * (j + 1)
        tmp = i + 1 + OFFSET * j
        tmp01 = i + OFFSET * (j + 1)

        dp[0][tmp11] = max(dp[0][tmp], dp[0][tmp01], dp[1][tmp01], dp[2][tmp01], dp[3][tmp01])
        dp[1][tmp11] = max(dp[1][tmp], dp[0][tmp] + val,  dp[0][tmp01] + val, dp[1][tmp01] + val, dp[2][tmp01] + val, dp[3][tmp01] + val)
        dp[2][tmp11] = max(dp[2][tmp], dp[1][tmp] + val)
        dp[3][tmp11] = max(dp[3][tmp], dp[2][tmp] + val)


print(max(dp[0][(c + 1) * (r + 1) - 1], dp[1][(c + 1) * (r + 1) - 1], dp[2][(c + 1) * (r + 1) - 1], dp[3][(c + 1) * (r + 1) - 1]))
### BUGGY CODE ###

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

def kitai(p):
  return (p+1)/2

for i in range(N):
  Q.append(kitai(P[i]))

for i in range(N - K + 1):
  R.append(sum(Q[i:i+K]))

print(max(R))

### FIXED CODE ###

N,K = map(int,input().split())
P = list(map(int,input().split()))
Q = list()
R = [0]
result = 0

for i in P:
  Q.append((i+1)/2)

for i in range(N):
  R.append(R[i] + Q[i])

for i in range(N - K + 1):
  result = max(result,R[i+K]-R[i])

print(result)
### BUGGY CODE ###

import heapq
N,Q=map(int,input().split())

event_list=[]
for i in range(N):
  S,T,X=map(int,input().split())
  event_list.append((S-X-0.5,1,X))
  event_list.append((T-X-0.5,-1,X))
  
dlist=[]
for i in range(Q):
  D=int(input())
  event_list.append((D,0,X))  
  dlist.append(D)
event_list.sort()
#print(event_list)

answer_dic={}
stop_set=set()
hq=[]
for t,f,x in event_list:
  if f==1:
    stop_set.add(x)
    heapq.heappush(hq,x)
  elif f==-1:
    stop_set.remove(x)
  else:
    if len(stop_set)>0:
      while(True):
        if hq[0] in stop_set:
          answer_dic[t]=hq[0]
          break
        else:
          heapq.heappop(hq)
    else:
      answer_dic[t]=-1
#print(answer_dic)

for d in dlist:
  print(answer_dic[d])

### FIXED CODE ###

import heapq
import sys
input=sys.stdin.readline
N,Q=map(int,input().split())

event_list=[]
for i in range(N):
  S,T,X=map(int,input().split())
  event_list.append((S-X-0.5,1,X))
  event_list.append((T-X-0.5,-1,X))
  
dlist=[]
for i in range(Q):
  D=int(input())
  event_list.append((D,0,X))  
  dlist.append(D)
event_list.sort()
#print(event_list)

answer_dic={}
stop_set=set()
min_set=float("inf")
for t,f,x in event_list:
  if f==1:
    stop_set.add(x)
    if x<min_set:
      min_set=x
  elif f==-1:
    stop_set.remove(x)
    if x==min_set:
      if len(stop_set)>0:
        min_set=min(stop_set)
      else:
        min_set=float("inf")
  else:
    if len(stop_set)>0:
      answer_dic[t]=min_set
    else:
      answer_dic[t]=-1
#print(answer_dic)

for d in dlist:
  print(answer_dic[d])
### BUGGY CODE ###

# A
n,m = map(int, input().split())
s = str(input())
t = str(input())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def lcm(a,b):
    return a*b/gcd(a,b)
l=int(lcm(n,m))
ss = [i for i in range(1,l+1,int(l/n))]
# print(ss)
tt = [i for i in range(1,l+1,int(l/m))]
# print(tt)
ii = list(set(ss)&set(tt))
# print(ii)
for x in ii:
    if s[ss.index(x)] != t[tt.index(x)]:
        print('-1')
        break
else:
    print(l)

### FIXED CODE ###

from fractions import gcd
n,m = map(int, input().split())
s = input()
t = input()

u = gcd(n,m)
l = n*m//u
x = n//u
y = m//u
for i in range(u):
    # print(n, m, u, l, i, i*x, s[i*x], i*y, t[i*y])
    if s[i * x] != t[i * y]:
        print(-1)
        break
else:
    print(l)
### BUGGY CODE ###

s = str(input())
mod = [0]*2019
mod[0] += 1#右端から見たときに2019の倍数がある場合に対応
for i in range(len(s)-1, -1, -1):
  mod[int(s[i:])%2019] += 1
ans =  0
for i in mod:
  ans += i*(i-1)//2
print(ans)

### FIXED CODE ###

s = str(input())
p = 2019
mod = [0]*2019
mod[0] += 1#右端から見たときに2019の倍数がある場合に対応
now = 0
_10 = 1
for i in range(len(s)-1, -1, -1):
  now = (now + int(s[i])*_10) % p
  _10 *= 10
  _10 %= p
  mod[now] += 1
ans =  0
for i in mod:
  ans += i*(i-1)//2
print(ans)
### BUGGY CODE ###

k = int(input())
from fractions import gcd
from functools import reduce

x = 0
for a in range(1, k+1):
  for b in range(a, k+1):
    for c in range(b, k+1):
      if len({a, b, c})==1:
        x += reduce(gcd, [a,b,c])
      elif len({a, b, c})==2:
        x += 3*reduce(gcd, [a,b,c])
      else:
        x += 6*reduce(gcd, [a,b,c])
      
print(x)


### FIXED CODE ###

k = int(input())
from fractions import gcd
from functools import reduce

x = 3*k*k - 3*k +1
for a in range(2, k+1):
  for b in range(a, k+1):
    if gcd(a,b)==1:
      x += 6*(k-b)+3
    else:
      for c in range(b, k+1):
        if len({a, b, c})==1:
          x += reduce(gcd, [a,b,c])
        elif len({a, b, c})==2:
          x += 3*reduce(gcd, [a,b,c])
        else:
          x += 6*reduce(gcd, [a,b,c])
      
print(x)
### BUGGY CODE ###

from copy import deepcopy

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

def median(x, i):
    y = deepcopy(x)
    y.pop(i)
    y = sorted(y)

    return(y[(len(y) + 1) // 2 - 1])

for i in range(n):
    print(median(x, i))

### FIXED CODE ###

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

y = sorted(x)

for i in x:
    if i < y[len(y) // 2]:
        print(y[len(y) // 2])
    else:
        print(y[len(y) // 2 - 1])
### BUGGY CODE ###

n = int(input())
robots = []
for i in range(n):
    x_tmp, l_tmp = (int(i) for i in input().split())
    robots.append({'x': x_tmp, 'l': l_tmp})

robots_sorted = sorted(robots, key=lambda x: x['x'])
ans = n
i = 0
while(i < n-1):
    if((robots_sorted[i]['x'] + robots_sorted[i]['l']) > (robots_sorted[i+1]['x'] - robots_sorted[i+1]['l'])):
        if(i == 0):
            if(robots_sorted[0]['l'] > robots_sorted[1]['l'] * 2):
                robots_sorted.pop(i)
            else:
                robots_sorted.pop(i+1)
        elif(i == n-2):
            if(robots_sorted[n-2]['l'] * 2 >= robots_sorted[n-1]['l']):
                robots_sorted.pop(i)
            else:
                robots_sorted.pop(i+1)
        elif(robots_sorted[i]['l'] > robots_sorted[i+1]['l']):
            robots_sorted.pop(i)
        else:
            robots_sorted.pop(i+1)
        i += -1
        n += -1
    i += 1

print(n)


### FIXED CODE ###

n = int(input())
robots = []
for i in range(n):
    x_tmp, l_tmp = (int(i) for i in input().split())
    robots.append([x_tmp - l_tmp, x_tmp + l_tmp])

robots.sort()
ans = n
right = robots[0][1]
for i in range(n-1):
    if(robots[i+1][0] < right):
        ans -= 1
        right = min(right, robots[i+1][1])
    else:
        right = robots[i+1][1]
print(ans)
### BUGGY CODE ###

def main():
    s = input()
    r = [0]*len(s)
    r[0] = int(s[-1])
    for i in range(1,len(s)):
        r[i] = (int(s[-(1+i)])*10**i+ r[i-1]) % 2019
    
    ans = 0
    cts = [0]*2019
    for i in r:
        ans+=cts[i]
        cts[i]+=1
    
    if cts[0]>0:
        ans+=1
        
    print(ans)
main()

### FIXED CODE ###

def main():
    s = list(map(int, list(input())))
    r = [0]*len(s)
    r[0] = int(s[-1])
    for i in range(1,len(s)):
        temp = s[-(1+i)]*pow(10,i,2019)
        r[i] = (temp+r[i-1])%2019

    ans = 0
    cts = [0]*2019
    for i in r:
        ans+=cts[i]
        cts[i]+=1
    
    ans+=cts[0]

    print(ans)

main()
### BUGGY CODE ###

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

ans = 0

for i in range(60 + 1):
    count1 = 0
    for a in A:
        count1 += (a >> i) & 1

    count0 = len(A) - count1
    ans += count1*count0 * pow(2, i)
    ans%=1000000007

print(ans)

### FIXED CODE ###

import queue
import numpy as np
import math

n = int(input())
A = list(map(int, input().split()))
A = np.array(A,np.int64)
ans = 0

for i in range(60 + 1):
    a = (A >> i) & 1
    count1 = np.count_nonzero(a)
    count0 = len(A) - count1
    ans += count1*count0 * pow(2, i)
    ans%=1000000007

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

inpl = lambda: list(map(int,input().split()))
MOD = 10**9+7

def inv_mod(a, p=MOD):
    def inv_mod_sub(a, p):
        if a == 1:
            return 1, 0
        else:
            d, r = p//a, p%a
            x, y = inv_mod_sub(r, a)
            return y-d*x, x
    if p < 0: p = -p
    a %= p
    return inv_mod_sub(a,p)[0] % p

def comb_mod(n,k):
    if k < 0 or k > n:
        return 0
    else:
        return factorial_mod[n]*factorial_inv_mod[k]*factorial_inv_mod[n-k] % MOD

N, K = inpl()
inv = [1]*(N+1)
factorial_mod = [1]*(N+1)
factorial_inv_mod = [1]*(N+1)
for n in range(1,N+1):
    inv[n] = inv_mod(n)
    factorial_mod[n] = factorial_mod[n-1]*n
    factorial_inv_mod[n] = factorial_inv_mod[n-1]*inv[n]

for i in range(1,K+1):
    print((comb_mod(K-1,i-1)*comb_mod(N-K+1,i)) % MOD)

### FIXED CODE ###

import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))
MOD = 10**9+7

def inv_mod(a, p=MOD):
    def inv_mod_sub(a, p):
        if a == 1:
            return 1, 0
        else:
            d, r = p//a, p%a
            x, y = inv_mod_sub(r, a)
            return y-d*x, x
    if p < 0: p = -p
    a %= p
    return inv_mod_sub(a,p)[0] % p

def comb_mod(n,k,p=MOD):
    ans = 1
    k = min(k,n-k)
    for i in range(k):
        ans = ans * (n-i) * inv[i+1] % p
    return ans

N, K = inpl()
inv = [0]*(N+1)
for n in range(1,N+1):
    inv[n] = inv_mod(n)

for i in range(1,min(K+1,N-K+2)):
    print((comb_mod(K-1,i-1)*comb_mod(N-K+1,i)) % MOD)
for i in range(N-K+2,K+1):
    print(0)
### BUGGY CODE ###

MOD = 10**9 + 7
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
dp = [[0] * (1 << N) for _ in range(N+1)]
dp[0][0] = 1
prev = [0]
for i in range(N):
    temp = []
    for j in range(N):
        if A[i][j] != 1: continue
        bit = 1 << j
        for s in prev:
            if s & bit == 0:
                new_s = s | bit
                dp[i+1][new_s] += dp[i][s]
                dp[i+1][new_s] %= MOD
                temp.append(new_s)
    prev = list(set(temp))
print(dp[N][(1 << N) - 1])

### FIXED CODE ###

MOD = 10**9 + 7
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
dp = [0] * (1 << N)
dp[0] = 1
bits = [1 << i for i in range(N)]
cands = [[] for _ in range(N+1)]
for s in range(1 << N):
    temp = 0
    for n in range(N):
        if s & (1 << n): temp += 1
    cands[temp].append(s)
for i, a in enumerate(A):
    for j in [k for k, v in enumerate(a) if v == 1]:
        bit = bits[j]
        for s in cands[i]:
            if s & bit == 0:
                new_s = s | bit
                dp[new_s] += dp[s]
                dp[new_s] %= MOD
print(dp[-1])
### BUGGY CODE ###

# Problem D - String Formation

# input process
S = list(input())
Q = int(input()) # query number

# initialization
sento = 1

# process
for i in range(Q):
    query_num = input()
    # クエリが１の時
    if query_num[0]=="1":
        # 反転処理
        if sento==1:
            sento = 10
        elif sento==10:
            sento = 1
    elif query_num[0]=="2":
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                S.insert(0, c)
            elif sento==10:
                S.append(c)
        elif f==2: # 末尾追加処理
            if sento==1:
                S.append(c)
            elif sento==10:
                S.insert(0, c)

# output process
if sento==1:
    for s in S:
        print(s, end="")
else:
    for s in S[::-1]:
        print(s, end="")


### FIXED CODE ###

# Problem D - String Formation

from collections import deque

# input process
S = input().replace("\n", "")
S = deque(list(S))
Q = int(input()) # query number

# initialization
sento = 1

# process
for i in range(Q):
    query_num = input()
    # クエリが１の時
    if query_num[0]=="1":
        # 反転処理
        if sento==1:
            sento = 10
        elif sento==10:
            sento = 1
    elif query_num[0]=="2":
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                S.appendleft(c)
            elif sento==10:
                S.append(c)
        elif f==2: # 末尾追加処理
            if sento==1:
                S.append(c)
            elif sento==10:
                S.appendleft(c)

# output process
if sento==1:
    print("".join(S))
else:
    print("".join(list(S)[::-1]))
### BUGGY CODE ###

from collections import Counter

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

s = Counter(c)
t = list(s.items())

nag = len(t)
x = 0
rt = [0 for i in range(nag)]

for i in range(nag):
    x += (t[i][1]*(t[i][1]-1))//2
for f in range(nag):
    rt[f] = x - (t[f][1] -1)
  
for j in range(n):
    for k in range(nag):
        if c[j] == t[k][0]:
            print(rt[k])
            break

### FIXED CODE ###

from collections import Counter

n = int(input())
c = list(map(int,input().split()))
s = Counter(c)
t = list(s.items())
nag = len(t)
x = 0
ans = 0

for i in range(nag):
    x += (t[i][1]*(t[i][1]-1))//2
    
for j in range(n):
    ans = x - s[c[j]] + 1 
    print(ans)
### BUGGY CODE ###

N = int(input())

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

import fractions
import copy
from functools import reduce

def gcd(numbers):
    return reduce(fractions.gcd, numbers)

max_gcd = 0
for idx in range(N):
    cleaned = copy.copy(A_list)
    del cleaned[idx]
    gcd_value = gcd(tuple(cleaned))
    if max_gcd < gcd_value:
        max_gcd = gcd_value
print(max_gcd)



### FIXED CODE ###

import numpy as np

def gcd(a,b):    
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
    
N = int(input())
A_list = list(map(int, input().split()))

# =====
# initialize L and R
# =====
L = np.zeros(N+1)
R = np.zeros(N+1)

for idx, element in enumerate(A_list):
    if L[idx] == 0:
        L[idx+1] = element
    else:
        L[idx+1] = gcd(L[idx], element)

for idx, element in enumerate(A_list[-1::-1]):
    idx = N-idx
    if R[idx] == 0:
        R[idx-1] = element
    else:
        R[idx-1] = gcd(R[idx], element)

ans = 0
for i in range(N):
    if L[i] == 0:
        M = R[i+1]
    elif R[i+1] == 0:
        M = L[i]
    else:
        M = gcd(L[i], R[i+1])
    if ans < M:
        ans = M

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

from collections import defaultdict

n = int(input())
arr = map(int, input().split())
arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))

dp = defaultdict(int)
for i, (p, a) in enumerate(arr):
  for l in range(i + 1):
    dp[i + 1, l] = max(dp[i + 1, l], dp[i, l] + a * abs(p - (n - (i - l) - 1)))
    dp[i + 1, l + 1] = max(dp[i + 1, l + 1], dp[i, l] + a * abs(p - l))

print(max([dp[n, i] for i in range(n + 1)]))


### FIXED CODE ###


def main():

  n = int(input())
  arr = map(int, input().split())
  arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))

  dp = [[0] * (n + 1) for _ in range(n + 1)]
  for i, (p, a) in enumerate(arr):
    for l in range(i + 1):
      dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + a * abs(p - (n - (i - l) - 1)))
      dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + a * abs(p - l))

  print(max(dp[n]))

main()
### BUGGY CODE ###

n, k = map(int, input().split())

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


li_cost = [float("inf")] * n

li_cost[0] = 0

# i = 現在の足場
for i in range(n):
    #for a in range(1, k + 1):
    w = min(k + 1, n - i)
    for a in range(1, w):
        if i + a < n:
            li_cost[i + a] = min(abs(li_h[i] - li_h[i + a]) + li_cost[i], li_cost[i + a])

print(li_cost[-1])



### FIXED CODE ###

n, k = map(int, input().split())

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


dp = [float("inf")] * n

dp[0] = 0

for i in range(1, n):
    w = i - k if i - k > 0 else 0
    dp[i] = min([cost + abs(height - li_h[i]) for cost, height in zip(dp[w:i], li_h[w:i])])

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


def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(input())
    A = list(map(int, input().split()))

    d = dict()
    for x in A:
        if x in d:
            d[x] += 1
        else:
            d[x] = 1
    A.sort()

    ans = 0
    dp = [True]*N

    for i in range(N):
        if dp[i]:
            if d[A[i]] == 1:
                ans += 1
            dp[i] = False
            for j in range(i+1, N):
                if dp[j]:
                    if A[j] % A[i] == 0:
                        dp[j] = False
        else:
            continue

    print(ans)


main()


### FIXED CODE ###


def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(input())
    A = list(map(int, input().split()))

    d = dict()
    for x in A:
        if x in d:
            d[x] += 1
        else:
            d[x] = 1
    A.sort()

    ans = 0
    M = max(A)
    dp = [True]*(M+1)
    for x in A:
        for xx in range(x+x, M+1, x):
            dp[xx] = False
    for x in A:
        if d[x] == 1 and dp[x]:
            ans += 1
    print(ans)


main()
### BUGGY CODE ###

def main():
    N, K = map(int, input().split())
    h = [int(x) for x in input().split()]
    # memo = [None for i in range(N)]

    # def rec(n):
    #     if memo[n-1] == None:
    #         if n <= 1:
    #             memo[n-1] = 0
    #         elif n == 2:
    #             memo[n-1] = abs(h[0]-h[1])
    #         else:
    #             memo[n-1] = min(abs(h[n-1]-h[j-1]) + rec(j) for j in range(max(0,n-K),n))
    #     return memo[n-1]

    # print(rec(N))

    dp = [-1]*N
    dp[0] = 0
    dp[1] = abs(h[1]-h[0])

    for i in range(2,N):
        dp[i] = min(abs(h[i]-h[j]) + dp[j] for j in range(max(0,i-K),i))
    print(dp[N-1])

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    N, K = map(int, input().split())
    h = [int(x) for x in input().split()]
    # memo = [None for i in range(N)]

    # def rec(n):
    #     if memo[n-1] == None:
    #         if n <= 1:
    #             memo[n-1] = 0
    #         elif n == 2:
    #             memo[n-1] = abs(h[0]-h[1])
    #         else:
    #             memo[n-1] = min(abs(h[n-1]-h[j-1]) + rec(j) for j in range(max(0,n-K),n))
    #     return memo[n-1]

    # print(rec(N))

    dp = [-1]*N
    dp[0] = 0
    dp[1] = abs(h[1]-h[0])

    for i in range(2,N):
        dp[i] = min(abs(h[i]-h[j]) + dp[j] for j in range(max(0,i-K),i))
    print(dp[N-1])

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

n=int(input())
a=[int(input()) for _ in range(n)]

cnt=0
tmp=1
already=[]
while True:
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        break
    elif tmp in already:
        print(-1)
        break

### FIXED CODE ###

import sys
n=int(input())
a=[int(input()) for _ in range(n)]
       
cnt=0
tmp=1

for i in range(n):
    cnt+=1
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        sys.exit()
print(-1)
### BUGGY CODE ###

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

for i in range(1,N+1,1):
    print(arr.count(i))

### FIXED CODE ###

N = int(input())
arr = list(map(int, input().split()))
ans = [0]*N

for i in range(N-1):
    ans[(arr[i])-1] += 1

for i in range(len(ans)):
    print(ans[i])