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

s = input()
q = int(input())

flg = 1
for i in range(q):
    t = input()
    if t == "1":
        if flg:
            flg = 0
        else:
            flg = 1
    elif t[0] == "2":
        f = t[2]
        if f == "1":
            c = t[4]
            if flg:
                s = c + s
            else:
                s += c
        elif f == "2":
            c = t[4]
            if flg:
                s += c
            else:
                s = c + s

if flg:
    print(s)
else:
    print(s[::-1])


### FIXED CODE ###

from collections import deque

s = deque(input())
q = int(input())

flg = 1
for _ in range(q):
    t = input()
    if t == "1":
        flg = not flg
    elif t[0] == "2":
        f = t[2]
        if f == "1":
            c = t[4]
            if flg:
                s.appendleft(c)
            else:
                s.append(c)
        elif f == "2":
            c = t[4]
            if flg:
                s.append(c)
            else:
                s.appendleft(c)

if flg:
    print(''.join(s))
else:
    print(''.join(reversed(s)))
### BUGGY CODE ###

n,m=map(int,input().split())
ls=[int(input()) for i in range(m)]

if 1 in ls:
    steps=[1,0]
else:
    steps=[1,1]

for this_step in range(2,n+1):
    if this_step in ls:
        ap=0
    else:
        ap=steps[-1]+steps[-2]
    steps.append(ap)

print(steps[-1]%1000000007)

### FIXED CODE ###

n,m = map(int,input().split())
a = [int(input()) for _ in range(m)]
a = set(a)
mod = 10**9+7
dp = [0]*(n+1)
dp[0] = 1
if 1 not in a:
    dp[1] = 1
for i in range(2,n+1):
    if i not in a:
        dp[i] = dp[i-1]+dp[i-2]
print(dp[-1]%mod)
### BUGGY CODE ###

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    start = time.time()
    n,m = LI()
    a = [LI() for _ in range(m)]
    e = [set() for _ in range(n)]
    for l,r,d in a:
        e[l-1].add((r-1,d))
        e[r-1].add((l-1,-d))

    b = [False] * n
    def g(i,c):
        gt = time.time() - start
        if gt > 1.0:
            return True
        if c == False:
            c = 0
        b[i] = c
        for r,d in e[i]:
            if b[r] != False:
                if b[r] != c+d:
                    return False
            else:
                if not g(r,c+d):
                    return False
        return True

    for i in range(n):
        if b[i] != False:
            continue
        if not g(i,False):
            return 'No'
    return 'Yes'


print(main())





### FIXED CODE ###

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    e = [[] for _ in range(n)]
    if m > 0:
        for _ in range(m):
            l,r,d = LI()
            e[l-1].append((r-1,d))
            e[r-1].append((l-1,-d))

    b = [None] * n

    for i in range(n):
        if not b[i] is None:
            continue
        b[i] = 0
        q = [i]
        qi = 0
        nq = []
        while True:
            if len(q) == qi:
                if not nq:
                    break
                q = nq
                qi = 0
                nq = []
            j = q[qi]
            qi += 1
            c = b[j]
            for r,d in e[j]:
                if b[r] is None:
                    b[r] = c+d
                    nq.append(r)
                else:
                    if b[r] != c+d:
                        return 'No'

    return 'Yes'


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

import copy
import statistics
N = int(input())
X = list(map(int, input().split()))
for i in range(N):
    med = copy.deepcopy(X)
    med.pop(i)
    print(statistics.median(med))

### FIXED CODE ###

N = int(input())
X = list(map(int, input().split()))
Xsort = sorted(X)
med = (Xsort[N // 2] + Xsort[N // 2 - 1]) / 2
for i in range(N):
    if X[i] < med:
        print(Xsort[N // 2])
    else:
        print(Xsort[N // 2-1])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)

n = int(input())

from collections import defaultdict
d = defaultdict(list)

for _ in range(n-1):
    a,b,c = map(int,input().split())
    d[a-1].append((b-1, c))
    d[b-1].append((a-1, c))

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

global dist
dist = [-1 for _ in range(n)]
def dfs(l, pos):
    dist[pos] = l
    for to, c in d[pos]:
        if dist[to] == -1:
            dfs(l+c,to)

dfs(0, k-1)

for _ in range(q):
    x,y = map(int,input().split())
    print(dist[x-1] + dist[y-1])

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)

n = int(input())

from collections import defaultdict
d = defaultdict(list)

for _ in range(n-1):
    a,b,c = map(int,input().split())
    d[a-1].append((b-1, c))
    d[b-1].append((a-1, c))

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

global dist
dist = [-1 for _ in range(n)]
def dfs(l, pos):
    dist[pos] = l
    for to, c in d[pos]:
        if dist[to] == -1:
            dfs(l+c,to)

dfs(0, k-1)

for _ in range(q):
    x,y = map(int,input().split())
    print(dist[x-1] + dist[y-1])
### BUGGY CODE ###

input()
a = map(int, input().split())
count = 0
while all(i%2 == 0 for i in a):
  a = [i//2 for i in a]
  count += 1
print(count)


### FIXED CODE ###

n = int(input())
a = map(int, input().split())
m = float('inf')
for i in a:
  count = 0
  while i%2 == 0:
    i = i//2
    count += 1
  m = min(m, count)
  if m==0:
    break
print(m)
### BUGGY CODE ###

import sys


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    S = input()
    N = len(S)
    dp = [[0] * 13 for _ in range(N)]

    if S[-1] == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(S[-1])] = 1

    for i in range(1, N):
        if S[N - i - 1] != "?":
            for k in range(13):
                j = int(S[N - i - 1])
                idx = (k + j * (10 ** i)) % 13
                dp[i][idx] += dp[i - 1][k]
                dp[i][idx] %= MOD
        else:
            for j in range(10):
                for k in range(13):
                    idx = (k + j * (10 ** i)) % 13
                    dp[i][idx] += dp[i - 1][k]
                    dp[i][idx] %= MOD

    print(dp[-1][5])


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    S = input()
    N = len(S)
    dp = [[0] * 13 for _ in range(N)]

    if S[-1] == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(S[-1])] = 1

    tenfactor = 1
    for i in range(1, N):
        tenfactor *= 10
        tenfactor %= 13

        if S[N - i - 1] != "?":
            for k in range(13):
                j = int(S[N - i - 1])
                idx = (k + j * tenfactor) % 13
                dp[i][idx] += dp[i - 1][k]
                dp[i][idx] %= MOD
        else:
            for j in range(10):
                for k in range(13):
                    idx = (k + j * tenfactor) % 13
                    dp[i][idx] += dp[i - 1][k]
                    dp[i][idx] %= MOD

    print(dp[-1][5])


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

N = 10**9


def f(x):
    return x


M = 10**5
arr = list(range(M))
for i in range(N):
    t = (i + i * i - i) % M  # 四則演算
    b = arr[t]  # 配列ランダムアクセス
    b = f(b)  # 関数呼び出し

# 答え
a = int(input())
print(a + a**2 + a**3)


### FIXED CODE ###

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    a = in_n()
    print(a + a**2 + a**3)


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

import math
N, P = map(int,input().split())
if N == 1:
    print(P)
else:
    l = 1/N
    check = math.ceil(P**l)
    while check >1:
        if P%(check**N) == 0:
            break
        check-=1
    print(check)

### FIXED CODE ###

import math
N, P = map(int,input().split())
if N == 1:
    print(P)
else:
    l = 1/N
    check = round(P**l)
    while check >1:
        if P%(check**N) == 0:
            break
        check-=1
    print(check)
### BUGGY CODE ###

from itertools import chain, combinations
import copy

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


def powerset(iterable):
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))


def is_mono(a):
    if len(a) < 2:
        return True
    for i in range(len(a) - 1):
        if a[i + 1] < a[i]:
            return False
    return True


def can_mono(a):
    for i in range(len(a) - 2):
        if a[i + 1] < a[i] and a[i + 2] < a[i + 1]:
            return False
    for m in powerset(range(len(a))):
        b = copy.deepcopy(a)
        for i in m:
            b[i] -= 1
            if is_mono(b):
                return True
    return False


if can_mono(a):
    result = 'Yes'
else:
    result = 'No'
print("{}".format(result))

### FIXED CODE ###

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


def can_mono(a):
    len_a = len(a)
    for i in range(len_a - 2):
        if a[i + 1] < a[i] - 1:
            return False
        if a[i + 1] < a[i]:
            for j in range(i + 1, len_a - 1):
                if a[j + 1] < a[i + 1]:
                    return False
                if a[j + 1] > a[i + 1]:
                    break
                if a[j + 1] == a[i + 1]:
                    continue
    return True


if can_mono(a):
    result = 'Yes'
else:
    result = 'No'
print("{}".format(result))
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
list = [[0 for i in range(2)] for j in range(Q)]
#print(list)

for i in range(Q):
    l,r = map(int, input().split())
    list[i][0] = l
    list[i][1] = r

for i in range(Q):
    sub_S = S[list[i][0]-1:list[i][1]]
    count = 0
    for j in range(len(sub_S)-1):
        if sub_S[j] == 'A':
            if sub_S[j:j+2] == 'AC':
                count += 1
    print(count)

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
list = [[0 for i in range(2)] for j in range(Q)]

b = [0] * (N+1)
for i in range(N):
    if S[i:i+2] == 'AC':
        b[i+1] = b[i] + 1
    else:
        b[i+1] = b[i]
for i in range(Q):
    l,r = map(int, input().split())
    print(b[r-1]-b[l-1])
### BUGGY CODE ###

n = 10**5
is_prime = [True] * (n + 1)
is_prime[0] = False
is_prime[1] = False

for i in range(2, int(n**0.5) + 1):
    if not is_prime[i]:
        continue
    for j in range(i * 2, n + 1, i):
        is_prime[j] = False

is_simular_2017 = is_prime.copy()

for i in range(n + 1):
    if is_prime[i] and (i + 1) // 2 > 0:
        if not is_prime[(i + 1) // 2]:
            is_simular_2017[i] = False


simular_2017 = [i for i in range(len(is_simular_2017)) if is_simular_2017[i]]


Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]
for l, r in LR:
    count = 0
    for s in simular_2017:
        if l <= s <= r:
            count += 1
    print(count)


### FIXED CODE ###

import bisect

n = 10**5
is_prime = [True] * (n + 1)
is_prime[0] = False
is_prime[1] = False

for i in range(2, int(n**0.5) + 1):
    if not is_prime[i]:
        continue
    for j in range(i * 2, n + 1, i):
        is_prime[j] = False

num = [0] * (n + 1)

cnt = 0
for i in range(n + 1):
    if is_prime[i] and (i + 1) // 2 > 0:
        if is_prime[(i + 1) // 2]:
            cnt += 1
    num[i] = cnt

Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

for l, r in LR:
    print(num[r] - num[l - 1])
### BUGGY CODE ###

N, X, Y = map(int, input().split())

distance = [[abs(i-j) for j in range(N)] for i in range(N)]
ans = [0] * N

X -= 1
Y -= 1
for i in range(N):
    for j in range(i+1, N):
        # X Y i j
        if Y <= i:
            pass
        # i j X Y
        elif j <= X:
            pass
        # X i Y j
        elif X <= i < Y <= j:
            dis = (i-X) + 1 + (j-Y)
            distance[i][j] = min(distance[i][j], dis)
        # i X j Y
        elif i <= X < j <= Y:
            dis = (X-i) + 1 + (Y-j)
            distance[i][j] = min(distance[i][j], dis)
        # i X Y j
        elif i <= X < Y <= j:
            dis = (X-i) + 1 + (j-Y)
            distance[i][j] = min(distance[i][j], dis)
        # X i j Y
        elif X <= i < j <= Y:
            dis = (i-X) + 1 + (Y-j)
            distance[i][j] = min(distance[i][j], dis)

        ans[distance[i][j]] += 1

for k in range(1, N):
    print(ans[k])


### FIXED CODE ###

N, X, Y = map(int, input().split())

ans = [0] * N

X -= 1
Y -= 1
for i in range(N):
    for j in range(i+1, N):
        distance = min(abs(i-j), abs(i-X) + 1 + abs(j-Y))
        ans[distance] += 1

for k in range(1, N):
    print(ans[k])
### BUGGY CODE ###

def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

n,k = iim()
A = iil()
'''
A = [0]*200000
上記でtryしても41回で全て明るさ200000になる
'''

def imos(l):
    n = len(l)
    tmp = [0]*n
    for i,item in enumerate(l):
        l = max(0,i-item)
        r = min(n-1,i+item)
        tmp[l] += 1
        if r+1 < n:
            tmp[r+1] -= 1
    for i in range(1,n):
        tmp[i] += tmp[i-1]
    return tmp

for _ in range(min(k,100)):
    A = imos(A)
#    print(_,min(A))

print(*A)

### FIXED CODE ###

def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

n,k = iim()
A = iil()
'''
A = [0]*200000
上記でtryしても41回で全て明るさ200000になる
'''

def imos(l):
    n = len(l)
    tmp = [0]*n
    for i,item in enumerate(l):
        l = max(0,i-item)
        r = min(n-1,i+item)
        tmp[l] += 1
        if r+1 < n:
            tmp[r+1] -= 1
    for i in range(1,n):
        tmp[i] += tmp[i-1]
    return tmp

for _ in range(min(k,100)):
    A = imos(A)
#    print(_,min(A))

print(*A)
### BUGGY CODE ###

n = int(input())
s = []
for i in range(n):
  s.append(input())

words = []
cnt = dict()
for i in range(n):
  if s[i] not in words:
    words.append(s[i])
    cnt[s[i]] = 1
  else:
    cnt[s[i]] += 1

top_words = []
max_val = 0
for k, v in cnt.items():
  if v > max_val:
    max_val = v
    top_words = [k]
  elif v == max_val:
    top_words.append(k)

top_words =sorted(top_words)
for i in range(len(top_words)):
  print(top_words[i])

### FIXED CODE ###

n = int(input())
s = []
for i in range(n):
  s.append(input())

words = set()
cnt = dict()
for i in range(n):
  if s[i] not in words:
    words.add(s[i])
    cnt[s[i]] = 1
  else:
    cnt[s[i]] += 1

top_words = []
max_val = 0
for k, v in cnt.items():
  if v > max_val:
    max_val = v
    top_words = [k]
  elif v == max_val:
    top_words.append(k)

top_words =sorted(top_words)
for i in range(len(top_words)):
  print(top_words[i])
### BUGGY CODE ###

if __name__ == '__main__':
    (n,a,b) = [int(e) for e in input().split()]
    bs = ['b' for e in range(a)]
    rs = ['a' for a in range(b)]
    unit = []
    while len(unit) < n:
        unit.extend(bs)
        unit.extend(rs)
    print(len([e for e in unit[:n] if e == 'b']))

### FIXED CODE ###

if __name__ == '__main__':
    (n, a, b) = map(int, input().split())
    ans = (n//(a+b)) * a
    m = min(n%(a+b), a)
    print(ans+m)
### BUGGY CODE ###

s=input()[::-1]
n=len(s)
p=2019
ans=[0]*p

x10=1
S=0
for i in s:
  S+=x10*int(i)
  S%=p
  x10*=10%p
  ans[S]+=1

cnt=ans[0]
for a in ans:
  cnt+=(a*(a-1))//2
  
print(cnt)


### FIXED CODE ###

s=input()[::-1]
p=2019
ans=[0]*p

x10, S=1, 0
for i in s:
  S+=x10*int(i)
  S%=p
  x10*=10
  x10%=p
  ans[S]+=1

cnt=ans[0]
for a in ans:
  cnt+=(a*(a-1))//2
  
print(cnt)
### BUGGY CODE ###

from itertools import combinations
from collections import Counter
n = int(input())
A = list(map(int,input().split()))
c = Counter(A)
hs = []
for k,v in c.items():
  if v == 1:
    hs.append(k)
  elif v >= 2:
    hs.append(k)
    hs.append(k)
    
    
ts = []    
for cmb in combinations(hs,2):
  ts.append(sum(cmb))
  
ts = list(set(ts))

ans = 0
for h in ts:
  for i in range(n-h):    
    if abs(A[i]+A[i+h]) == h:
      ans += 1
      
print(ans)

### FIXED CODE ###

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

X = {}
ans = 0
for i in range(n):
  L = i+A[i]
  R = i-A[i]
  
  if L not in X:
    X[L] = 0
    
  X[L]+=1
  
  if R in X:
    ans += X[R]
    
print(ans)
### BUGGY CODE ###

def main():
    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
    import math

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    a = [list(map(int, input().split())) for _ in range(n)]
    b = [0]*n
    cnt = 0
    while True:
        f = 0
        f2 = 0
        sumi = [0]*n
        for i in range(n):
            if sumi[i] == 1 or b[i] == n-1:
                f2 += 1
                continue
            aite = a[i][b[i]] - 1
            if a[aite][b[aite]]-1 == i and sumi[aite]==0:
                f = 1
                b[i] += 1
                b[aite] += 1
                sumi[i] = 1
                sumi[aite] = 1
        if f2==n:
            break
        elif f:
            cnt += 1
        else:
            print(-1)
            exit()
    print(cnt)

if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    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
    import math

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    a = []
    for _ in range(n):
        a.append(deque(list(map(int, input().split()))))
    
    siai = n*(n-1)
    d = [0]*n

    while siai:
        f = 0
        for i in range(n):
            while a[i] and a[a[i][0]-1][0] == i+1:
                f = 1
                day = max(d[i], d[a[i][0]-1])
                d[i] = day + 1
                d[a[i][0]-1] = day + 1
                siai -= 2
                a[a[i][0]-1].popleft()
                a[i].popleft()
        if not f:
            print(-1)
            break
    else:
        print(max(d))

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

n, K = map(int, input().split())
h = [int(x) for x in input().split()]
inf = float('inf')
dp = [inf] * n  # initialize dp
dp[0] = 0
for i in range(1, n):
    for k in range(1, K+1):
        if i - k >= 0:
            dp[i] = min(dp[i-k] + abs(h[i] - h[i-k]), dp[i])
        else:
            break
print(dp[-1])

### FIXED CODE ###

n, K = map(int, input().split())
h = [int(x) for x in input().split()]
inf = float('inf')
dp = [inf] * n  # initialize dp
dp[0] = 0
for i in range(1, n):
    for k in range(1, K+1):
        if i - k >= 0:
            kouho = dp[i-k] + abs(h[i] - h[i-k])
            dp[i] = min(kouho, dp[i])
        else:
            break
print(dp[-1])
### BUGGY CODE ###

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

while k > 0:
    if a >= 1:
        a -= 1
        k -= 1
    elif a == 0 and b >= 1:
        b -= 1
        k -= 1
    elif a == 0 and b == 0:
        k -= 1

print(a, b)

### FIXED CODE ###

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

if a <= k:
    k -= a
    a = 0
    if b <= k:
        b =0
    else:
        b -= k
else:
    a -= k

print(a, b)
### BUGGY CODE ###

n, y = list(map(int, input().split(' ')))
for ten_th in range(min(n, int(y / 10000)), -1, -1):
    if y < ten_th * 10000 or n < ten_th:
        continue
    for five_th in range(min(n - ten_th, int((y - ten_th * 10000) / 5000)), -1, -1):
        if y < ten_th * 10000 + five_th * 5000 or n < ten_th + five_th:
            continue
        for th in range(min(n - ten_th - five_th, int((y - ten_th * 10000 - five_th * 5000) / 1000)), -1, -1):
            if y == ten_th * 10000 + five_th * 5000 + th * 1000 and n == ten_th + five_th + th:
                print(' '.join(str(s) for s in [ten_th, five_th, th]))
                exit()
print('-1 -1 -1')
            

### FIXED CODE ###

n, y = list(map(int, input().split(' ')))
if y % 1000 != 0:
    print('-1 -1 -1')
    exit()
for ten_th in range(min(n, int(y / 10000)), -1, -1):
    if y < ten_th * 10000 or n < ten_th:
        continue
    for five_th in range(min(n - ten_th, int((y - ten_th * 10000) / 5000)), -1, -1):
        if y < ten_th * 10000 + five_th * 5000 or n < ten_th + five_th:
            continue
        if (y - ten_th * 10000 - five_th * 5000) % 1000 == 0:
            th = int((y - ten_th * 10000 - five_th * 5000) / 1000)
            if n == ten_th + five_th + th:
                print('{} {} {}'.format(ten_th, five_th, th))
                exit()
print('-1 -1 -1')
### BUGGY CODE ###

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

from itertools import permutations

lis = [0]*N
for i in range(N):
    lis[i] = i

scores = []
for i, v in enumerate(permutations(lis, N)):
    score = 0
    for j in range(N): 
        score += abs(v.index(j)-j) * A[j]
    scores.append(score)

print(max(scores))

### FIXED CODE ###

def solve(N, A):
    a = [(i, ai) for i, ai in enumerate(A)]
    a.sort(key=lambda x: x[1], reverse=True)

    # import numpy as np
    # dp = np.zeros((N+1, N+1), np.int8)

    dp = [[0] * (N + 1) for _ in range(N + 1)]

    dp[0][0] = 0
    for i, ai in enumerate(a):
        for l in range(i+1):
            r = i - l
            dp[i+1][l] = max(dp[i+1][l], dp[i][l] + ai[1] * ((N-1-r) - ai[0]))
            dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l] + ai[1] * (ai[0] - l))

    print(max(dp[N]))

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