CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
slist = [int(eval(input())) for _ in range(N)]
anslist = []
for bit in range(1<<(N)):
    sum = 0
    for i in range(N):
        if bit & (1<<i):
            sum += slist[i]
    if sum % 10 != 0:
        anslist.append(sum)
if len(anslist) == 0:
    print((0))
else:
    print((max(anslist)))

# Optimized solution
import sys
N = int(eval(input()))
slist = [int(eval(input())) for _ in range(N)]
ssum = sum(slist)
if ssum % 10 == 0:
    slist.sort()
    for s in slist:
        if  s % 10 != 0:
            print((ssum - s))
            sys.exit()
    print((0))
else:
    print(ssum)
# Original solution
import itertools
n = int(eval(input()))
l = [int(eval(input())) for i in range(n)]
combo = [list(itertools.combinations(l, i)) for i in range(1, n+1)]
ans = 0
for i in combo:
  for j in i:
    temp = sum(list(j))
    if temp > ans and not temp % 10 == 0:
      ans = temp

print(ans)

# Optimized solution
n = int(eval(input()))
l = [int(eval(input())) for i in range(n)]
def f():
  ans = sum(l)
  if ans % 10 == 0:
    for i in sorted(l):
      if not (ans - i) % 10 == 0:
        return ans-i
    return 0
  else:
    return ans

print((f()))
# Original solution
n = int(eval(input()))
l = [int(eval(input())) for i in range(n)]
ans = 0
def f(now, i):
  global ans
  if not now % 10 == 0 and now > ans:
    ans = now

  if i >= n:
    return
  
  f(now+l[i] ,i+1)
  f(now, i+1)
f(0, 0)

print(ans)

# Optimized solution
n = int(eval(input()))
l = [int(eval(input())) for i in range(n)]
ans = sum(l)

def f():
  if not ans % 10 == 0:
    return ans
  else:
    for i in sorted(l):
      if not i % 10 == 0:
        return ans - i
    return 0
print((f()))
# Original solution
N=int(eval(input()))
s=[int(eval(input())) for i in range(N)]
import collections
table=collections.defaultdict(int)
for x in s:
  if x%10!=0:
    table[x]+=1
if sum(s)%10==0:
  if len(table)==0:
    print((0))
  else:
    table=list(table.keys())
    table.sort()
    print((sum(s) - table[0]))
else:
  print((sum(s)))

# Optimized solution
N = int(eval(input()))
s = [int(eval(input())) for i in range(N)]

S = sum(s)
if S % 10 != 0:
    print(S)
else:
    s.sort()
    ans = S
    flag = False
    for x in s:
        if x % 10 != 0:
            ans -= x
            flag = True
            break
        else:
            continue
    if flag:
        print(ans)
    else:
        print((0))
# Original solution
n = int(eval(input()))
s = [int(eval(input())) for _ in range(n)]
notten = sorted([i for i in s if i%10]) 
ten = [i for i in s if i%10==0] 
if notten:
    for j in range(len(notten)):
        if sum(notten[j:])%10:
            print((sum(ten)+sum(notten[j:])))
            break
        
else:
    print((0))

# Optimized solution
n = int(eval(input()))
s = sorted([int(eval(input())) for _ in range(n)])
a = sum(s)
if a %10:
    print(a)
    exit()
for i in range(n):
    b = a - s[i]
    if b%10:
        print(b)
        break
else:
    print((0))
# Original solution
N = int(eval(input()))
S = []
for i in range(N):
    S.append(int(eval(input())))


def solve(ind, choice):
    # ending
    if ind == N:
        num = sum(choice)
        if num % 10 == 0:
            return 0
        else:
            return sum(choice)
    # transition
    return max(solve(ind=ind + 1, choice=choice + [S[ind]]), solve(ind=ind + 1, choice=choice))

print((solve(ind=0, choice=[])))

# Optimized solution
N = int(eval(input()))
S = []
for i in range(N):
    S.append(int(eval(input())))

memo = {}


def solve(ind, points):
    # ending
    if ind == N:
        if points % 10 == 0:
            return 0
        else:
            return points

    if memo.get((ind, points)) is not None:
        return memo[(ind, points)]

    # transition
    ret = max(solve(ind=ind + 1, points=points + S[ind]), solve(ind=ind + 1, points=points))
    memo[(ind, points)] = ret

    return ret

print((solve(ind=0, points=0)))
# Original solution
N = int(eval(input()))

s = [int(eval(input())) for x in range(N)]

fullmax = sum(s)
s_sorted = sorted(s)

cur_max = 0

if fullmax % 10 != 0 :
    print(fullmax)
else :
    # if everything divisable by zero
    div_flg = True
    for i in s :
        if i % 10 != 0 :
            div_flg = False
            
    if div_flg == True :
        print("0")
    else :
        for i in range(N) :
            if (fullmax - s_sorted[i]) % 10 != 0 :
                cur_max = max(fullmax - s_sorted[i], cur_max)
        
        print(cur_max)

# Optimized solution
N = int(eval(input()))

s = []
all_10_flg = True

for x in range(N) :
    n = int(eval(input()))
    s.append(n)
    
    if n % 10 != 0 :
        all_10_flg = False
    

sum_all = sum(s)

if sum_all % 10 != 0 :
    print(sum_all)
elif all_10_flg == True :
    print("0")
else :
    max_sum = 0
    
    for i in s :
        if (sum_all - i) % 10 != 0:
            max_sum = max(max_sum, sum_all - i)
        
    print(max_sum)
# Original solution
n = int(eval(input()))
s = [int(eval(input())) for i in range(n)]
ans=0
for i in range(2**len(s)):
    x=0
    for j in range(n):
        if (i>>j)&1:
            x+=s[j]
    if x%10!=0:
        ans=max(ans,x)
print(ans)

# Optimized solution
n = int(eval(input()))
s = [int(eval(input())) for i in range(n)]
ans=0
m = sum(s)
if m%10==0:
    f = True
    s.sort()
    for si in s:
        if si%10!=0:
            print((m-si))
            f = False
            break
    if f:
        print((0))
else:
    print(m)
# Original solution
# C - Bugged
N = int(eval(input()))
S = [int(eval(input())) for _ in range(N)]

lst = []
for i in range(2**N):
	ref = 0
	for j in range(N):
		if 1 & (i >> j):
			ref += S[j]
	if ref % 10 == 0:
		ref = 0
	lst.append(ref)

print((max(lst)))


# Optimized solution
# C - Bugged
N = int(eval(input()))
S = [int(eval(input())) for _ in range(N)]
S = sorted(S)

ans = sum(S)

if ans % 10 != 0:
	pass
else:
	for i in range(N):
		if S[i] % 10 != 0:
			ans = ans - S[i]
			break
	else:
		ans = 0

print(ans)
# Original solution
N = int(eval(input()))
s = [int(eval(input())) for i in range(N)]

def dfs(i, score):
    global ans
    if i == N:
        if score % 10 != 0:
            ans = max(ans, score)
            
    else:
        dfs(i+1, score)
        dfs(i+1, score+s[i])

ans = 0
dfs(0, 0)
print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(10**6)

N = int(eval(input()))
s = sorted([int(eval(input())) for i in range(N)])

correct = False
for n in s:
    if n % 10 != 0:
        correct = True
        break

if correct:
    ans_max = sum(s)
    if ans_max % 10 != 0:
        print(ans_max)
    else:
        for i in s:
            if i % 10 != 0:
                print((ans_max-i))
                break
else:
    print((0))
# Original solution
# -*- coding: utf-8 -*-

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

s.sort()
sm = sum(s)

if sm % 10:
    ans = sm
else:
    ans = 0
    for ele in s:
        if ele % 10:
            ans = sm - ele
            break

print(ans)


# Optimized solution
n = int(eval(input()))
s = [int(eval(input())) for _ in range(n)]

sm = sum(s)

if sm % 10:
    ans = sm
else:
    ans = 0
    for e in s:
        tmp = sm - e
        if tmp % 10:
            ans = max(ans, tmp)

print(ans)
# Original solution
# coding: utf-8
import os
import time
import re
import math


def main():
    N=int(eval(input()))
    A=[int(eval(input())) for  a in range(N)] 
    #start=time.time();
    totalList=[]
    for a in range(1<<N):
        bit=[]
        for b in range(N):
            if (a & (1<<b))>0:
                bit.insert(0,1)
            else:
                bit.insert(0,0)
        total=0
        for b in range(N):
                total+=A[b]*bit[b]
        totalList.append(total)
    totalList.sort(key=lambda x: -x)
    ans=0
    for a in totalList:
        if not a%10==0:
            ans=a
            break
    print(ans)

    #print("{}ms".format(math.floor(time.time()*1000-start*1000)))#


main()


# Optimized solution
# coding: utf-8
import time
import re
import math

def main():
    global N,A,memo
    N=int(eval(input()))
    A=[int(eval(input())) for  a in range(N)] 
    if sum(A)%10==0:
        nm0=[]
        for a in A:
            if not a%10==0:
                nm0.append(a)
        if len(nm0)==0:
            print((0))
        else:
            print((sum(A)-min(nm0)))

    else:
        print((sum(A)))

main()
# Original solution
import itertools
n=int(eval(input()))
d=[]
for i in range(n):
    d.append(int(eval(input())))
r=0
for i in range(1,n+1):
    for j in list(itertools.combinations(d,i)):
        x=sum(j)
        if r<x and x%10!=0:
            r=x
print(r)

# Optimized solution
data=[]
for i in range(int(eval(input()))):
    data.append(int(eval(input())))
un=[num for num in data if num%10==0]
data=[num for num in data if num%10!=0]
if len(data)==0:
    print((0))
else:
    if sum(un+data)%10!=0:
        print((sum(un+data)))
    else:
        print((sum(un+data)-min(data)))
# Original solution
# https://atcoder.jp/contests/abc063/tasks/arc075_a

n = int(eval(input()))
nums = []
for _ in range(n):
    nums.append(int(eval(input())))

total = sum(nums)
dp = [[False for _ in range(total + 1)] for _ in range(n + 1)]
dp[0][0] = True
for i in range(n):
    num = nums[i]
    dp[i + 1][num] = True
    for j in range(total + 1):
        if dp[i][j] and num + j <= total:
            dp[i + 1][num + j] = True

        dp[i + 1][j] = dp[i][j] or dp[i + 1][j]

for i in range(len(dp[0]))[::-1]:
    if dp[-1][i] and i % 10 != 0:
        print(i)
        break
else:
    print((0))

# Optimized solution
n = int(eval(input()))
nums = []
for _ in range(n):
    nums.append(int(eval(input())))
nums.sort()
ans = sum(nums)
if ans % 10 != 0:
    print(ans)
else:
    for num in nums:
        t = ans - num
        if t % 10 != 0:
            print(t)
            break
    else:
        print((0))
# Original solution
# ABC063 C - Bugged
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

L = []
s = sum(A)

for a in A:
    if a%10 != 0:
        L.append(a)
L.sort()

if not L:
    print((0))
else:
    if s%10 == 0:
        for l in L:
            s -= l
            if s%10 != 0:
                print(s)
                exit()
    else:
        print(s)

# Optimized solution
n=int(eval(input()))
s=[int(eval(input())) for i in range(n)]
tot=sum(s)
s.sort()
if tot%10!=0:
  print(tot)
  exit(0)

for ss in s:
  if ss%10!=0:
    tot-=ss
    print(tot)
    exit(0)
print((0))
# Original solution
n = int(eval(input()))
S = sorted([int(eval(input())) for i in range(n)])

if sum(S) % 10 != 0:
    print((sum(S)))
else:
    S = S + [-1]
    for i in range(n + 1):
        if S[i] == -1:
            print((0))
        elif S[i] % 10 != 0:
            del S[i]
            print((sum(S) + 1))
            break

# Optimized solution
N = int(eval(input()))
S = [int(eval(input())) for i in range(N)]
sumS = sum(S)
sortS = sorted(S)

if sumS % 10 != 0:
    ans = sumS
else:
    for i in range(N):
        if sortS[i] % 10 != 0:
            ans = sumS - sortS[i]
            break
    else:
        ans = 0

print(ans)
# Original solution
from sys import stdin
from itertools import accumulate
from itertools import permutations

n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip()) for _ in range(n)]

lin = list(permutations(li))

ma = 0

for i in lin:
    i = list(i)
    lis = list(accumulate(i))[::-1]
    for i in lis:
        if i % 10 == 0:
            continue
        ma = max(ma,i)
print(ma)

# Optimized solution
from sys import stdin
from itertools import accumulate

n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip()) for _ in range(n)]
li.sort()
lin = list(accumulate(li))
if lin[-1] %10 != 0:
    print((lin[-1]))
    exit()
else:
    for i in li:
        if i % 10 == 0:
            continue
        print((lin[-1]-i))
        exit()
print((0))
# Original solution
n = int(eval(input()))
li = [int(eval(input())) for _ in range(n)]
li.sort(reverse=True)
point = sum(li)
while True:
    if point % 10 != 0:
        print(point)
        break
    if not li:
        print((0))
        break
    a = li.pop()
    point = point-a
    if point%10 == 0:
        point += a

# Optimized solution
n = int(eval(input()))
s = [int(eval(input())) for _ in range(n)]

s.sort()

point = sum(s)

if point%10 == 0:
    for i in s:
        if i%10 != 0:
            point -= i
            break

if point%10 == 0:
    print((0))
else:
    print(point)
# Original solution
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    N=int(eval(input()))
    S=[int(eval(input())) for i in range(N)]
    ls=[]
    for i in range(1, N + 1):
        for j in combinations(S,i):
            ls.append(j)
    ans=0
    for i in ls:
        if sum(i)%10!=0:
            ans=max(ans,sum(i))
    print(ans)


resolve()

# Optimized solution
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    N=int(eval(input()))
    S=[int(eval(input())) for i in range(N)]
    ls=[]
    for i in range(1, N + 1):
        for j in combinations(S,i):
            val=sum(j)
            if val%10!=0:
                ls.append(val)

    print((max(ls,default=0)))


resolve()
# Original solution
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    N=int(eval(input()))
    S=[int(eval(input())) for i in range(N)]
    ls=[]
    for i in range(1, N + 1):
        for j in combinations(S,i):
            val=sum(j)
            if val%10!=0:
                ls.append(val)

    print((max(ls,default=0)))


resolve()

# Optimized solution
def resolve():
    N=int(eval(input()))
    S=sorted([int(eval(input())) for i in range(N)])
    total=sum(S)
    if total%10!=0:
        print(total)
    else:
        for i in range(N):
            if S[i]%10!=0:
                print((total-S[i]))
                break
            elif i==N-1:print((0))


resolve()
# Original solution
import sys

input = sys.stdin.buffer.readline
n = int(eval(input()))
S = []
for _ in range(n):
    S.append(int(eval(input())))
ans = 0
for i in range(1 << n):
    temp = 0
    for j in range(n):
        if (i >> j) & 1:
            temp += S[j]
    if temp % 10 == 0:
        temp = 0
    ans = max(temp, ans)
print(ans)


# Optimized solution
import sys

input = sys.stdin.buffer.readline
n = int(eval(input()))
S = []
for _ in range(n):
    S.append(int(eval(input())))
S.sort()
goukei = sum(S)
if goukei %10!=0:
    print(goukei)
    exit()
ans = 0
for i in range(n):
    temp = goukei - S[i]
    if temp % 10 == 0:
        temp = 0
    ans = max(ans, temp)

print(ans)
# Original solution

ans = float("inf")


def solve():

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

    if N % K:
        print((1))
    else:
        print((0))


if __name__ == "__main__":
    solve()


# Optimized solution
N, K = list(map(int, input().split()))

print((0 if N % K == 0 else 1))
# Original solution
print((int(eval(input().replace(' ','%'))!=0)))

# Optimized solution
print(((eval(input().replace(' ','%'))>0)+0))
# Original solution
a,b = list(map(int,input().split()))
if a % b == 0:
    print('0')
else:
    print('1')

# Optimized solution
n,k = list(map(int,input().split()))
if n%k == 0:
    print((0))
else:
    print((1))
# Original solution
N, K = list(map(int, input().split()))
print((0 if N % K == 0 else 1))


# Optimized solution
N, K = list(map(int, input().split()))
print((min(N % K, 1)))
# Original solution
n, k = list(map(int, input().split()))
if n%k > 0:
    print((1))
else:
    print((0))


# Optimized solution
n, k = list(map(int, input().split()))
if n%k == 0:
  print((0))
else:
  print((1))
# Original solution
n, k = list(map(int, input().split()))
if n % k == 0:
  print((0))
else:
  print((1))

# Optimized solution
n, k = list(map(int, input().split()))
print((0 if n % k == 0 else 1))
# Original solution
print((int(eval(input().replace(" ","%"))!=0)))

# Optimized solution
print(((eval(input().replace(" ","%"))>0)+0))
# Original solution
n,k=list(map(int,input().split()))
print((1 if n%k else 0))

# Optimized solution
N, K = list(map(int, input().split()))

print((int(N%K != 0)))
# Original solution
N, K = list(map(int, input().split() ))
if N%K == 0:
  print((0))
else:
  print((1))

# Optimized solution
n, k = list(map(int, input().split()))

if n%k == 0:
    print("0")
else:
    print("1")
# Original solution
import sys
rm = lambda: list(map(int, sys.stdin.buffer.readline().split()))

n, k = rm()
print((0 if n%k == 0 else 1))


# Optimized solution
import sys
rm = lambda: list(map(int, sys.stdin.readline().split()))

n, k = rm()
print((0 if n%k == 0 else 1))
# Original solution
N, K = [int(i) for i in input().split()]
tmp = [0] * K
while True:
  for i in range(K):
    tmp[i] += 1
    N -= 1
    if N == 0:
      break
  if N == 0:
    break
print((max(tmp) - min(tmp)))

# Optimized solution
N, K = [int(i) for i in input().split()]
print((0 if N % K == 0 else 1))
# Original solution
import sys
sys.setrecursionlimit(10000)

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y,wy = findset(y,element,weight)
    link(x,wx,y,wy,w,element)
def link(x,wx,y,wy,z,element):
    z = z+wy-wx

    element[x] = y
    weight[x] = z

n,q = list(map(int, input().split()));

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = list(map(int, input().split()));
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print((wx-wy))
        else:
            print('?')


# Optimized solution
import sys
sys.setrecursionlimit(10000)

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
        sumW += weight[x]
    else:
        resultPos = x
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y,wy = findset(y,element,weight)
    link(x,wx,y,wy,w,element)
def link(x,wx,y,wy,z,element):
    z = z+wy-wx

    element[x] = y
    weight[x] = z

n,q = list(map(int, input().split()));

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = list(map(int, input().split()));
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        x,wx = findset(x,element,weight)
        y,wy = findset(y,element,weight)
        if(x == y):
            print((wx-wy))
        else:
            print('?')
# Original solution

n = int(eval(input()))
arr = list(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])))


# Optimized solution

def main():

  n = int(eval(input()))
  arr = list(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()
# Original solution
import sys
sys.setrecursionlimit(10 ** 9)

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

b = []
for i in range(n):
  b.append((a[i],i))
b.sort(reverse = True)

ans = 0
def f(l,r,i,h):
  global ans
  if i == n:
    ans = max(ans,h)
    return
  x = b[i][0]
  y = b[i][1]
  f(l+1,r,i+1,h+x*abs(y-l))
  f(l,r-1,i+1,h+x*abs(y-r))

f(0,n-1,0,0)
print(ans)

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))

b = []
for i in range(n):
  b.append((a[i],i))
b.sort(reverse = True)

dp = [[0 for _ in range(n+1)] for _ in range(n+1)]
for i in range(n):
  x = b[i][0]
  y = b[i][1]
  for l in range(i+1):
    r = i-l
    dp[i+1][l+1] = max(dp[i+1][l+1],dp[i][l]+x*(y-l))
    dp[i+1][l] = max(dp[i+1][l],dp[i][l]+x*(n-1-r-y))

ans = 0
for i in range(n+1):
  ans = max(dp[n][i],ans)
print(ans)
#print(dp)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
A = sorted([(a, i) for i, a in enumerate(A)], reverse=True)

dp = [0] * (N + 1)
for i in range(N):
    dp2 = dp[:]
    for l in range(i + 1):
        a, pos = A[i]
        # add left side
        dp2[l + 1] = max(dp2[l + 1], dp[l] + (pos - l) * a)
        # add right side
        r = i - l + 1
        dp2[l] = max(dp2[l], dp[l] + (N - r - pos) * a)
    dp = dp2
print((max(dp)))

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A = sorted([(a, i) for i, a in enumerate(A)], reverse=True)

dp = [0] * (N + 1)
for i in range(N):
    dp2 = dp[:]
    for l in range(i + 1):
        a, pos = A[i]
        # add left side
        dp2[l + 1] = max(dp2[l + 1], dp[l] + abs(pos - l) * a)
        # add right side
        r = i - l + 1
        dp2[l] = max(dp2[l], dp[l] + abs(N - r - pos) * a)
    dp = dp2
print((max(dp)))
# Original solution
n = int(eval(input()))
A_l = [(A,i) for i, A in enumerate(map(int,input().split()))]
A_l.sort()
A_l.reverse()

dp = []
for _ in range(n+1):
    dp.append([0] * (n+1))
for j in range(1,n+1): # all go bigger
    yb = n - (j-1)
    x = A_l[j-1][1] + 1
#    if yb-i <0:
#        dp[j][0] = -float('inf')
#    else:
    dp[j][0] = dp[j-1][0] + A_l[j-1][0]*(yb-x)
for k in range(1,n+1): # all go smaller
    ys = 1 + (k-1)
    x = A_l[k-1][1] + 1
#    if i-ys <0:
#        dp[0][k] = -float('inf')
#    else:
    dp[0][k] = dp[0][k-1] + A_l[k-1][0]*(x-ys)

for j in range(1,n+1):
    for k in range(1,n-j+1):
        yb = n - (j-1)
        ys = 1 + (k-1)
        x = A_l[j+k-1][1] + 1
        j_plus = A_l[j+k-1][0]*(yb-x)
        k_plus = A_l[j+k-1][0]*(x-ys)
        if j_plus <0:
            j_plus = -float('inf')
        elif k_plus <0:
            k_plus = -float('inf')
        dp[j][k] = max(dp[j-1][k]+j_plus, dp[j][k-1]+k_plus)

result = 0
for j in range(0,n+1):
    k = n-j
    result = max(result, dp[j][k])
print(result)
#for l in range(n+1):
#    print(dp[l])

# Optimized solution
n = int(eval(input()))
A_l = [(A,i) for i, A in enumerate(map(int,input().split()))]
A_l.sort()
A_l.reverse()

dp = []
for _ in range(n+1):
    dp.append([0] * (n+1))
for j in range(1,n+1): # all go bigger
    yb = n - (j-1)
    a, i = A_l[j-1]
    dp[j][0] = dp[j-1][0] + a*(yb-(i+1))
for k in range(1,n+1): # all go smaller
    ys = 1 + (k-1)
    a, i = A_l[k-1]
    dp[0][k] = dp[0][k-1] + a*((i+1)-ys)

for j in range(1,n+1):
    for k in range(1,n-j+1):
        yb = n - (j-1)
        ys = 1 + (k-1)
        a, i = A_l[j+k-1]
        j_plus = a*(yb-(i+1))
        k_plus = a*((i+1)-ys)
        dp[j][k] = max(dp[j-1][k]+j_plus, dp[j][k-1]+k_plus)

result = 0
for j in range(0,n+1):
    k = n-j
    result = max(result, dp[j][k])
print(result)
#for l in range(n+1):
#    print(dp[l])