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

N, Y = map(int, input().split())
for x in range(0, N+1):
    for y in range(0, N+1):
        for z in range(0, N+1):
            if x + y + z == N and 10000 * x + 5000 * y + 1000 * z == Y:
                print(x, y, z)
                exit(0)
print(-1, -1, -1)


### FIXED CODE ###

N, Y = map(int, input().split())
for x in range(0, N+1):
    for y in range(0, N+1):
        z = N - x - y
        if z < 0:
            continue
        if x + y + z == N and 10000 * x + 5000 * y + 1000 * z == Y:
            print(x, y, z)
            exit(0)
print(-1, -1, -1)
### BUGGY CODE ###

N,K = map(int,input().split())
H = list(map(int,input().split()))
dp = [10**18]*N
dp[0] = 0
for i in range(1,N):
    for j in range(max(0,i-K),i):
        dp[i] = min(dp[i], dp[j] + abs(H[i]-H[j]))
print(dp[-1])


### FIXED CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  dp=[inf]*100010
  dp[0]=0
  for i in range(n):
    for j in range(1,k+1):
      if i+j<n:
        dp[i+j]=min(dp[i+j],dp[i]+abs(l[i+j]-l[i]))

  return dp[n-1]

print(main())
### BUGGY CODE ###

def isPrime(x):
    if x==2:
        return True
    if x<2 or x%2==0:
        return False
    if pow(2,x-1,x)==1:
        return True
    else:
        return False
while 1:
    try:
        n=input()
        ct=0
        for i in xrange(n+1):
            if isPrime(i):
                ct+=1
        print(ct)
    except:
        break

### FIXED CODE ###

def sieve(n):
    p=0
    prime=[0]*n
    is_prime=[0]*(n+1)
    for i in xrange(n+1):
        is_prime[i]=True
    is_prime[0]=is_prime[1]=False
    for i in xrange(2,n+1):
        if is_prime[i]:
            p+=1
            prime[p]=i
            for j in xrange(2*i,n+1,i):
                is_prime[j]=False
    return p
while 1:
    try:
        n=input()
        print(sieve(n))
    except:
        break
### BUGGY CODE ###

import bisect

n = int(input())
sl = []
for i in range(n):
    s = input()
    bisect.insort_left(sl, s)  # 2 最も左(前)の挿入箇所が返ってきている

# print(sl)
c = 0
anss = []
max_n = 0
for i, t in enumerate(sl):
    if i == 0:
        c += 1
        target = t
        continue

    if t == target:
        c += 1
    else:
        if max_n < c:
            anss = []
            anss.append(target)
            max_n = c
        elif max_n == c:
            anss.append(target)
            max_n = c
        target = t
        c = 1

if max_n < c:
    anss = []
    anss.append(t)
elif max_n == c:
    anss.append(t)


print(*anss)


### FIXED CODE ###

import bisect

n = int(input())
sl = ['']*n
for i in range(n):
    sl[i] = input()
sl = sorted(sl, key=str.lower)
# print(sl)
c = 0
anss = []
max_n = 0
for i, t in enumerate(sl):
    if i == 0:
        c += 1
        target = t
        continue

    if t == target:
        c += 1
    else:
        if max_n < c:
            anss = []
            anss.append(target)
            max_n = c
        elif max_n == c:
            anss.append(target)
            max_n = c
        target = t
        c = 1

if max_n < c:
    anss = []
    anss.append(t)
elif max_n == c:
    anss.append(t)


print(*anss)
### BUGGY CODE ###

def upheap(a,i):
    while i > 0:
        p = (i+1)/2-1
        if a[p] < a[i]:
            a[p],a[i]=a[i],a[p]
        i = p
def heap_insert(a,v):
    a.append(v)
    upheap(a,len(a)-1)
def extract(a):
    ret = a[0]
    a[0],a[len(a)-1]=a[len(a)-1],a[0]
    del a[len(a)-1]
    downheap(a,0)
    return ret
def downheap(a,i):
    lid = (i+1)*2-1
    rid = (i+1)*2
    maxc = -1
    if lid < len(a):
        maxc = lid
    if rid < len(a):
        if maxc == -1 or a[maxc] < a[rid]:
            maxc = rid
    if maxc != -1:
        if a[i] < a[maxc]:
            a[i],a[maxc]=a[maxc],a[i]
            downheap(a,maxc)


a = []
ret = []
while True:
    op = raw_input()
    if op[0] == 'e' and op[1] == 'n':
        break
    elif op[0] == 'i':
        num = int(op.split(' ')[1])
        heap_insert(a,num)
    else:
        ret.append(extract(a))

for r in ret:
    print r

### FIXED CODE ###

from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break
### BUGGY CODE ###

def gcd(m, n):
    while n:
        m, n = n, m % n
    return m


N = int(input())

A = list(map(int, input().split()))
max_A = max(A)
gcd_list = []
for i in range(N):
    tmp = A[i]
    A[i] = max_A
    hoge = gcd(A[0], A[1])
    for j in range(2, N):
        hoge = gcd(A[j], hoge)
    gcd_list.append(hoge)
    A[i] = tmp

print(max(gcd_list))
# 最大の値と変えてもいい


### FIXED CODE ###

def gcd(m, n):
    while n:
        m, n = n, m % n
    return m


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

left = [0]
right = [0]

for i in range(N):
    left.append(gcd(left[-1], A[i]))
    right.append(gcd(right[-1], A[(N-1)-i]))  # こっちのAは後ろからみる, iがN-1になった時はA[0]


right.reverse()  # indexが小さいほうが多くの数字を考慮した最大公約数になっている


gcd_list = [gcd(left[i], right[i+1]) for i in range(N)]

print(max(gcd_list))
### BUGGY CODE ###

LIM=200004
L=[0]*LIM

def bin_sum(Y):
    S=bin(Y)[2:]
    count=0
    for i in range(len(S)):
        count+=int(S[i])
    return count

def bin_sum2(Y):
    count=0
    for i in range(len(Y)):
        count+=int(Y[i])
    return count

for i in range(1,LIM):
    L[i]+=bin_sum(i)

def pop_num(N,b,L):
    if N==0:
        return 0
    v=N%b
    return pop_num(v,L[v],L)+1
    
M=[0]*200005
for i in range(1,200004):
    M[i]+=pop_num(i,L[i],L)
X=int(input())
Y=input()
d=bin_sum2(Y)
e=int(Y,2)%(d+1)
f=int(Y,2)%max(1,(d-1))
for i in range(X):
    if Y[i]=="0":
        b=d+1
        flag=1
        g=(e+2**(X-1-i))%b
    else:
        b=max(d-1,1)
        flag=max(0,d-1)
        g=(f-2**(X-1-i)%b+b)%b
    if flag==0:
        print(0)
    elif g==0:
        print(1)
    else:
        print(M[g]+1)

### FIXED CODE ###

from collections import deque
LIM=200004
L=[0]*LIM

def bin_sum(Y):
    S=bin(Y)[2:]
    count=0
    for i in range(len(S)):
        count+=int(S[i])
    return count

def bin_sum2(Y):
    count=0
    for i in range(len(Y)):
        count+=int(Y[i])
    return count

for i in range(1,LIM):
    L[i]+=bin_sum(i)

def pop_num(N,b,L):
    if N==0:
        return 0
    v=N%b
    return pop_num(v,L[v],L)+1
    
M=[0]*200005
for i in range(1,200004):
    M[i]+=pop_num(i,L[i],L)
X=int(input())
Y=input()
d=bin_sum2(Y)
e=int(Y,2)%(d+1)
f=int(Y,2)%max(1,(d-1))
O=[1]*(X+2)
P=[1]*(X+2)
q=max(d-1,1)
for i in range(1,X+1):
    O[i]=O[i-1]*2%q
    P[i]=P[i-1]*2%(d+1)
for i in range(X):
    
    if int(Y[i])==1:
        b=max(d-1,1)
        flag=max(0,d-1)
        g=(f-O[X-1-i]+b)%b
    else:
        b=d+1
        flag=1
        g=(e+P[X-1-i])%b

    if flag==0:
        print(0)
    elif g==0:
        print(1)
    else:
        print(M[g]+1)
### BUGGY CODE ###

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

gakki_sum = 1
gakki_sum_new = 1
for j in range(K):
        gakki_sum *= A[j]

for i in range( K+1, N+1):
    for j in range(K):
        gakki_sum_new *= A[i-K+j]
    if gakki_sum < gakki_sum_new:
        print("Yes")
    elif gakki_sum == gakki_sum_new:
        print("No")
    else:
        print("No")
    gakki_sum = gakki_sum_new
    gakki_sum_new = 1


### FIXED CODE ###

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

for i in range(K, N):
    
    if A[i-K] < A[i]:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

L = list(input())
N = [-1 for i in range(len(L)+1)]

def sounyu(L,N):    
    for i in range(len(N)-1):
        if L[i] == '<':
            N[i] = max(N[i],0)
            while(L[i] == '<'):
                N[i+1] = max(N[i+1],N[i]+1)
                i += 1
                if i == len(L):
                    break
def sounyu_aft(L,N):    
    for i in range(len(N)-1):
        if L[i] == '>':
            N[i] = max(N[i],0)
            while(L[i] == '>'):
                N[i+1] = max(N[i+1],N[i]+1)
                i += 1
                if i == len(L):
                    break

sounyu(L,N)
L.reverse()
N.reverse()
sounyu_aft(L,N)

print(sum(N))



### FIXED CODE ###

L = input()
L = list(L.split('><'))
if len(L) == 1:
    a = [L[0].count('<'),L[0].count('>')]
    a_max = max(a)
    ans = a_max*(a_max+1)/2
else:
    ans = 0
    for i in range(len(L)):
        if i == 0:
            a = [L[i].count('<'),L[i].count('>')+1]
        elif i == len(L)-1:
            a = [L[i].count('<')+1,L[i].count('>')]
        else:
            a = [L[i].count('<')+1,L[i].count('>')+1]
        a_max = max(a)
        a_min = min(a)

        ans += a_max*(a_max+1)/2
        ans += a_min*(a_min-1)/2

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

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [LI()for i in range(n)]
inf = 10**17
mod = 10**9 + 7

s=input().rstrip()
l=len(s)
dp=[[0 for i in range(13)] for i in  range(l)]
#dp[0]=[1 for i in range(13)]
i=0
u=s[0]
if u!="?":
    k=(int(u)*10**(l-1))%13
    dp[0][k]+=1
else:
    for g in range(10):
        k=(g*10**(l-1))%13
        dp[0][k]+=1
#print(dp)
for i in range(l-1):
    u=s[i+1]
    t=(10**(l-i-2))%13
    for j in range(13):
        if u!="?":
            k=(j+int(u)*t)%13
            dp[i+1][k]+=dp[i][j]
        
        else:
            for g in range(10):
                k=(j+g*t)%13
                dp[i+1][k]+=dp[i][j]
    
                
            
#print(dp)
print(dp[l-1][5]%mod)
        

### FIXED CODE ###

import sys,math,copy

def main():
    # import numpy as np
    input = sys.stdin.readline
    S = ''.join(reversed(input().strip()))

    # dp = np.zeros((13), dtype=int)
    # dp_ = np.zeros((13), dtype=int)
    dp = [0] * 13
    dp_ = [0] * 13
    mod13_list = [i % 13 for i in range(12 ** 2 + 1)]
    list10 = list(range(10))
    list13 = list(range(13))
    divisor = 10 ** 9 + 7
    dp_[0] = 1
    mul = 1
    for i in range(len(S)):
        s = S[i]
        if s == '?':
            for d in list10:
                m = mod13_list[(d * mul)]
                for pm in list13:
                    dp[mod13_list[(pm+m)]] += dp_[pm]

        else:
            si = int(s)
            m = mod13_list[(si * mul)]
            for pm in list13:
                dp[mod13_list[(pm+m)]] = dp_[pm]

        # dp_ = dp % divisor
        # dp = dp * 0 # np.zeros((13), dtype=int)
        for j in list13:
            dp_[j] = dp[j] % divisor
            dp[j] = 0
        mul = mod13_list[(mul * 10)]
    print(dp_[5])

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

from itertools import product
 
X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
 
AB = [a + b for a, b in product(A, B)]
AB.sort(reverse=True)
 
ABC = [ab + c for ab, c in product(AB[:min(3000, X*Y*Z)], C)]
ABC.sort(reverse=True)
 
for abc in ABC[:K]:
    print(abc)

### FIXED CODE ###

from itertools import product

X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = [a + b for a, b in product(A, B)]
AB.sort(reverse=True)

ABC = [ab + c for ab, c in product(AB[:K], C)]
ABC.sort(reverse=True)

for abc in ABC[:K]:
    print(abc)
### BUGGY CODE ###

N = int(input())
C=list(input())
type=set(C)
L=0
R=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      if L==N-1-R:
        break
      if C[i]=='W':
        type=set(C[i+1:N-R])
        if 'R' in type:
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              R+=ii+1
              break
            if L==N-1-R:
              break
        else:
          break
print(X)

### FIXED CODE ###

N = int(input())
C=list(input())
type=set(C)
L=0
R=0
RR=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      R=RR
      if L>=N-1-R:
        break
      if C[i]=='W':
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              break
            RR+=1
            if L>=N-1-R:
              break
print(X)
### BUGGY CODE ###

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

order = []
ans = A[0] - 1
start = 0
stop = 0


for i in range(K):
    order.append(ans)
    if len(set(order)) != i+1:
        stop = i - 1
        start = order.index(ans)
        new = order[start:stop+1]
        break
    else:
        if i == K - 1:
            print(ans+1)
            exit()
        else:
            ans = A[ans] - 1


print(new[(K-start) % (stop-start+1)-1]+1)


### FIXED CODE ###

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

n = 1
order = [n]
order_set = set([n])

for i in range(1, K + 1):
    n = A[n - 1]
    if n in order_set:
        Li = order.index(n)
        Ri = len(order)
        idx = Li + (K - Li) % (Ri - Li)
        print(order[idx])
        exit()
    else:
        order.append(n)
        order_set.add(n)
        continue
print(n)
### BUGGY CODE ###

N=int(input())
a=list(map(int,input().split()))
count=0
count2=0
for i in range(N):
  for j in range(N):
    if(a[j]==a[i]):
      count+=1
      count2+=1
#print((count-N)//2)

for i in range(N):
  count2=0
  for j in range(N):
    if(a[j]==a[i]):
      count2+=1
 # print(count)
  print((count-N)//2-count2+1)

### FIXED CODE ###

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

for i in range(N):
  sum+=b[i]*(b[i]-1)//2
for i in range(N):
  print(sum-b[a[i]-1]+1)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse = False)
cnt = 1

for i in range(N):
    if 0 in A:
        print (0)
        exit()
    else:
        cnt *= A[i]
        if cnt > 10 ** 18:
            print (-1)
            exit ()
print (cnt)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse = False)
cnt = 1


if 0 in A:
    print (0)
    exit()
else:
    for i in range(N):
        cnt *= A[i]
        if cnt > 10 ** 18:
            print (-1)
            exit ()
print (cnt)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)
H,W = map(int,input().split())
grid = []
for _ in range(H):
  grid.append(list(input()))

def dfs(i,j,direction):
  if i<0 or j<0 or i>=H or j>=W:
    return 0
  if grid[i][j] == "#":
    return 0

  if direction == "L":
    return 1 + dfs(i,j-1,direction)
  elif direction == "R":
    return 1 + dfs(i,j+1,direction)
  elif direction == "U":
    return 1 + dfs(i+1,j,direction)
  else:
    return 1 + dfs(i-1,j,direction)

res = 0
for i in range(H):
  for j in range(W):
    if grid[i][j] != "#":
      step = 1
      for d in ["L","R","U","D"]:
        ni,nj = i,j
        if d=="L": nj = j-1
        if d=="R": nj = j+1
        if d=="U": ni = i+1
        if d=="D": ni = i-1
        step += dfs(ni,nj,d)
      res = max(res, step)

print(res)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
H,W = map(int,input().split())
grid = []
for _ in range(H):
  grid.append(list(input()))

L = [ [0]*W for _ in range(H) ]
R = [ [0]*W for _ in range(H) ]
U = [ [0]*W for _ in range(H) ]
D = [ [0]*W for _ in range(H) ]

for i in range(H):
  for j in range(W):
    if j-1 >= 0:
      L[i][j] = L[i][j-1]
    if i-1 >= 0:
      D[i][j] = D[i-1][j]

    if grid[i][j] == ".":
      L[i][j] += 1
      D[i][j] += 1
    else:
      L[i][j] = 0
      D[i][j] = 0

for i in range(H-1,-1,-1):
  for j in range(W-1,-1,-1):
    if j+1 < W:
      R[i][j] = R[i][j+1] 
    if i+1 < H:
      U[i][j] = U[i+1][j]
    if grid[i][j] == ".":
      R[i][j] += 1
      U[i][j] += 1
    else:
      R[i][j] = 0
      U[i][j] = 0

res = 0
for i in range(H):
  for j in range(W):
    if grid[i][j] == ".":
      res = max(res, L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)

print(res)
### BUGGY CODE ###

n=int(input())
l=list(map(int,input().split()))
f=l[0]
from fractions import gcd

for i in l:
    f=f*i//gcd(f,i)
mod=10**9+7
ans=0
for i in l:
  ans+=f//i
print(ans%mod)


### FIXED CODE ###

def s():
  from sys import stdin
  read=stdin.buffer.readline
  n=int(read())
  l=list(map(int,read().split()))
  f=l[0]
  mod=10**9+7
  def gcd(s,d):
    s,d=max(s,d),min(s,d)
    while  d:
      s,d=d,s%d
    return s
  for i in l:
    f=i*f//gcd(i,f)
  ans=0
  for iu in l:
    b,u,v=mod,1,0
    while b:
      t=iu//b;iu,b=b,iu%b;u-=t*v;u,v=v,u
    ans+=(f*u%mod)%mod
    ans%=mod
  print(ans)
  
if __name__=="__main__":s()
### BUGGY CODE ###

import numpy as np
MOD = 10**9 + 7
def solve(s, p=13):
    s = s[::-1]
    n = len(s)
    a = np.zeros(p, dtype=int)
    b = np.zeros(p, dtype=int)
    if s[0] == "?":
        a[:10] = 1
    else:
        a[int(s[0])] = 1
    D = {str(i): [i] for i in range(10)}
    D["?"] = list(range(10))
    q = [(10**i) % p for i in range(6)]
    for i in range(1,n):
        b[:] = 0
        for s_i in D[s[i]]:
            for j in range(p):
                k = (j + s_i * q[i%6]) % p
                b[k] += a[j]
        a = b % MOD
    return a[5]

s = input()
print(solve(s))

### FIXED CODE ###

MOD = 10**9 + 7

def solve(s, p=13):
    a = [0] * p
    a[0] = 1
    for c in s:
        b = [0] * p
        for i in range(p):
            b[(i*10)%p] = a[i] % MOD
        b += b
        if c == "?":
            for i in range(p):
                a[i] = sum(b[i+4:i+p+1])
        else:
            for i in range(p):
                a[i] = b[i+p-int(c)]
    return a[5] % MOD

s = input()
print(solve(s))
### BUGGY CODE ###

N,M = map(int,input().split())
A=[int(x) for x in input().split()]
BC=[]
for i in range(M):
  BC +=[list(map(int,input().split()))]
BC = sorted(BC, key=lambda x: x[1],reverse = True)
A=sorted(A)
for i in range(M):
  for j in range(BC[i][0]):
    A[j]=max(A[j],BC[i][1])
  A=sorted(A)
print(sum(A))

### FIXED CODE ###

N,M=map(int,input().split())
A=[int(x) for x in input().split()]
CB=[]
for i in range(M):
  B,C = map(int,input().split())
  CB +=[[C,B]]

for j in range(N):
  CB +=[[A[j],1]]

CB=sorted(CB,reverse = True)
count = 0
result =0

i=0
while count<N:
  count +=CB[i][1]
  result +=CB[i][0]*CB[i][1]
  i +=1

result -= (count-N)*(CB[i-1][0])
print(result)
### BUGGY CODE ###

n, m = map(int, input().split())
value = []
for i in range(m):
    A, B = map(int, input().split())
    C = list(map(int, input().split()))
    binary = 0
    for c in C:
        binary += 1<<(c-1)
    value.append([A, binary])
    
dp = [float("inf") for _ in range(1<<n)]
dp[0] = 0
for s in range(1<<n):
    for i in range(m):
        t = s | value[i][1]
        cost = dp[s] + value[i][0]
        dp[t] = min(dp[t], cost)
if dp[(1<<n)-1] == float("inf"):
    print(-1)
else:
    print(dp[(1<<n)-1])

### FIXED CODE ###

#import sys
#input = sys.stdin.readline
 
n, m = map(int, input().split())
value = []
for i in range(m):
    A, B = map(int, input().split())
    c = sum(1<<(ci-1) for ci in map(int, input().split()))
    value.append([A, c])
    
dp = [100100100] * (1<<n)
dp[0] = 0
for s in range(1<<n):
    for i in range(m):
        t = s | value[i][1]
        cost = dp[s] + value[i][0]
        if dp[t] > cost:
            dp[t] = cost
        
if dp[(1<<n)-1] == 100100100:
    print(-1)
else:
    print(dp[(1<<n)-1])
### BUGGY CODE ###

import itertools

def main():
    N, M, R = map(int, input().split())
    RR = tuple(map(int, input().split()))
    ABC = {tuple(map(int, input().split())) for _ in range(M)}

    dist = [[100001 for i in range(N + 1)] for j in range(N + 1)]
    for a, b, c in ABC:
        dist[a][b] = dist[b][a] = c
    for k in range(N + 1):
        for i in range(N + 1):
            for j in range(N + 1):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

    ans = float('inf')
    for r in itertools.permutations(range(1, N + 1)):
        move = 0
        flag = {i: 1 for i in RR}
        for i in range(N - 1):
            a = r[i]
            b = r[i+1]
            move += dist[a][b]
            flag[a] = flag[b] = 0
            if sum(flag.values()) == 0: break
        ans = min(ans, move)

    print(ans)

main()


### FIXED CODE ###

import itertools

def main():
    N, M, R = map(int, input().split())
    RR = tuple(map(int, input().split()))
    ABC = {tuple(map(int, input().split())) for _ in range(M)}

    dist = [[float('inf')] * (N + 1) for j in range(N + 1)]
    for a, b, c in ABC:
        dist[a][b] = dist[b][a] = c
        dist[a][a] = dist[b][b] = 0
    for k in range(1, N + 1):
        for i in range(1, N + 1):
            for j in range(1, N + 1):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

    ans = float('inf')
    for r in itertools.permutations(RR):
        move = 0
        for i in range(R - 1):
            a = r[i]
            b = r[i+1]
            move += dist[a][b]
        ans = min(ans, move)

    print(ans)

main()
### BUGGY CODE ###

n=int(input())
l=list(map(int,input().split()))
dp=[[0 for j in range(n+1)] for i in range(n+1)]
e=[]
for i in range(n):
    e.append([l[i],i])
e.sort(reverse=True)
for i in range(1,n+1):
    p=e[i-1][0]
    q=e[i-1][1]
    for j in range(i+1):
        if j<i:
            a1=dp[i-1][j]+p*abs(n-(i-j)-q)
        else:
            a1=0
        if j>0:
            a2=dp[i-1][j-1]+p*abs(q-j+1)
        else:
            a2=0
        dp[i][j]=max(a1,a2)
print(max(dp[-1]))


### FIXED CODE ###

n=int(input())
l=list(map(int,input().split()))
dp=[[0 for j in range(n+1)] for i in range(n+1)]
e=[]
for i in range(n):
    e.append([l[i],i])
e.sort(reverse=True)
for i in range(1,n+1):
    p=e[i-1][0]
    q=e[i-1][1]
    for j in range(i+1):
        if j<i:
            a1=dp[i-1][j]+p*abs(n-(i-j)-q)
        else:
            a1=0
        if j>0:
            a2=dp[i-1][j-1]+p*abs(q-j+1)
        else:
            a2=0
        dp[i][j]=max(a1,a2)
print(max(dp[-1]))