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

import operator as op
from functools import reduce


def ncr(n, r):
    r = min(r, n - r)
    numer = reduce(op.mul, range(n, n - r, -1), 1)
    denom = reduce(op.mul, range(1, r + 1), 1)
    return numer // denom


def solve(string):
    n, m, k = map(int, string.split())
    base = n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 % (10**9 + 7)
    return str(base * ncr(m * n - 2, k - 2) % (10**9 + 7))


if __name__ == '__main__':
    print(solve(input()))


### FIXED CODE ###

mod = 10**9 + 7


def comb(n, k):
    x, y = 1, 1
    k = min(k, n - k)
    for i in range(k):
        x *= n - i
        x %= mod
        y *= i + 1
        y %= mod
    return x * pow(y, mod - 2, mod) % mod


def solve(string):
    n, m, k = map(int, string.split())
    base = n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 % mod
    return str(base * comb(n * m - 2, k - 2) % mod)


if __name__ == '__main__':
    print(solve(input()))
### BUGGY CODE ###

import math

def main():

    N, T = map(int, input().split())
    C = []
    for _ in range(N):
        A, B = map(int, input().split())
        C.append([B, A])
    C.sort()

    dp = [[0, 0] for _ in range(T)]
    ans = 0
    for i in range(T):
        for j in range(N):
            if (dp[i][1] >> j) % 2 == 0:
                if i+C[j][1] > T-1:
                    ans = max(ans, dp[i][0] + C[j][0])
                else:
                    if dp[i][0]+C[j][0] > dp[i+C[j][1]][0]:
                        dp[i+C[j][1]][0] = dp[i][0]+C[j][0]
                        dp[i+C[j][1]][1] = (1 << j) | dp[i][1]

    for i in range(T):
        ans = max(ans, dp[i][0])
    return ans



if __name__ == '__main__':
    print(main())


### FIXED CODE ###

def main():

    N, T = map(int, input().split())
    C = []
    for _ in range(N):
        A, B = map(int, input().split())
        C.append([B, A])

    dp = [[0, set()] for _ in range(T)]
    ans = 0
    for i in range(T):
        for j in range(N):
            if j not in dp[i][1]:
                k = i+C[j][1]
                if k > T-1:
                    ans = max(ans, dp[i][0] + C[j][0])
                else:
                    if dp[i][0]+C[j][0] > dp[k][0]:
                        dp[k][0] = dp[i][0]+C[j][0]
                        dp[k][1] = dp[i][1] | {j}
        ans = max(ans, dp[i][0])

    return ans

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

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


### FIXED CODE ###

n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = 10 ** 4 + 1
dp = [inf] * n
dp[0] = 0
for i in range(1,n):
    H = h[i]
    dp[i] = min([dp[j] + abs(h[j] - H) for j in range(max(0, i - k), i)])
print(dp[-1])
### BUGGY CODE ###

N = int(input())

XY = []
for _ in range(N):
    x, y = map(int, input().split())
    XY.append((x, y))
    
# BITの定義
import math
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    # index 1からiの値の和を返す
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    # index iの値にxを加算する
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
#######################
# 開始
p_list = XY[:]

# y座標の圧縮
p_list = sorted(p_list, key=lambda x: x[1])
#p_list = [ {'x': p[0], 'y': p[1], 'y_rank': i+1} for i, p in enumerate(p_list)]
p_list = [ (p[0], p[1], i+1) for i, p in enumerate(p_list)]

# xでソート
p_list= sorted(p_list)

a = [] # 左下
b = [] # 左上
c = [] # 右下
d = [] # 右上

# a,bを求める
bit = Bit(N)
for i, (x, y, r) in enumerate(p_list):
    tmp = bit.sum(r)
    a.append(tmp)
    b.append(i-tmp)
    bit.add(r, 1)

#print(p_list)
#print(a)
#print(b)

# c,dを求める
bit = Bit(N)
for i, (x, y, r) in enumerate(reversed(p_list)):
    tmp = bit.sum(r)
    c.append(tmp)
    d.append(i-tmp)
    bit.add(r, 1)
c = c[::-1]
d = d[::-1]
#print(c)
#print(d)

# ある点が四角に含まれる場合の総数を求めるメソッド
power = [1] * N
for i in range(1, N):
    power[i] = power[i-1] * 2 % 998244353
def p_comb(a, b, c, d):
    a = power[a] - 1
    b = power[b] - 1
    c = power[c] - 1
    d = power[d] - 1
    
    ret = (a+1) * (d+1) * c * b
    ret += (b+1) * (c+1) * a * d
 
    ret -= a*b*c*d
 
    ret += power[N-1]
    return ret % 998244353
 
ans = 0
for i in range(N):
    ans += p_comb(a[i], b[i], c[i], d[i])
    
print(ans % 998244353)   


### FIXED CODE ###

N = int(input())

XY = []
for _ in range(N):
    x, y = map(int, input().split())
    XY.append((x, y))
    
# BITの定義
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    # index 1からiの値の和を返す
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    # index iの値にxを加算する
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

#######################
# 開始
from operator import itemgetter
def solve():
    p_list = XY[:]

    # y座標の圧縮
    p_list = sorted(p_list, key=itemgetter(1))
    p_list = [ (x, y, i+1) for i, (x, y) in enumerate(p_list)]

    # xでソート
    p_list= sorted(p_list)

    # ある点が四角に含まれる場合の総数を求めるメソッド
    power = [1] * N
    for i in range(1, N):
        power[i] = power[i-1] * 2 % 998244353

    # a,bを求める
    bit = Bit(N)
    ans = 0
    for i, (x, y, r) in enumerate(p_list):
        tmp = bit.sum(r)
        a = tmp
        b = i-tmp
        c = r-tmp-1
        d = (N-r+1)-(i-tmp)-1
        bit.add(r, 1)
        _a = power[a] - 1
        _b = power[b] - 1
        _c = power[c] - 1
        _d = power[d] - 1

        ret = (_a+1) * (_d+1) * _c * _b
        ret += (_b+1) * (_c+1) * _a * _d
        ret -= _a*_b*_c*_d
        ret += power[N-1]
        ans += ret % 998244353

    print(ans % 998244353)
    
solve()
### BUGGY CODE ###

A,B,Q=map(int,input().split())
s=[int(input()) for _ in range(A)]
t=[int(input()) for _ in range(B)]

#二分探索関数
def bin_search(x,seq):
	width=[0,len(seq)-1]
	if len(seq)<=1:
		return [0,0]
	if x<=seq[0]:
		return [0,0]
	elif x>=seq[width[1]]:
		return [width[1],width[1]]
	else:
		while(width[1]-width[0]>1):
			center=(sum(width)//2)
			if x>=seq[center]:
				width=[center,width[1]]
			else:
				width=[width[0],center]
	if x==seq[width[0]]:
		return [width[0],width[0]]
	elif x==seq[width[1]]:
		return [width[1],width[1]]
	else:
		return width

for i in range(Q):
	x=int(input())
	Nb_s0=s[bin_search(x,s)[0]]
	Nb_s1=s[bin_search(x,s)[1]]
	Nb_t0=t[bin_search(x,t)[0]]
	Nb_t1=t[bin_search(x,t)[1]]

	k1=abs(Nb_s0-x)+abs(Nb_t0-Nb_s0)
	k2=abs(Nb_s1-x)+abs(Nb_t0-Nb_s1)
	k3=abs(Nb_s0-x)+abs(Nb_t1-Nb_s0)
	k4=abs(Nb_s1-x)+abs(Nb_t1-Nb_s1)
	k5=abs(Nb_t0-x)+abs(Nb_t0-Nb_s0)
	k6=abs(Nb_t1-x)+abs(Nb_t1-Nb_s1)
	k7=abs(Nb_t0-x)+abs(Nb_t0-Nb_s1)
	k8=abs(Nb_t1-x)+abs(Nb_t1-Nb_s0)
	print(min(k1,k2,k3,k4,k5,k6,k7,k8))

### FIXED CODE ###

inf=10**11
A,B,Q=map(int,input().split())
s=[-inf]+[int(input()) for _ in range(A)]+[inf]
t=[-inf]+[int(input()) for _ in range(B)]+[inf]
x=[[i,int(input())] for i in range(Q)]

x.sort(key=lambda x:x[1])
i,s_i,t_i=0,0,0
ans=[]

for i in range(Q):
    while(1):
        if x[i][1]>=s[s_i]:
            s_i+=1
        else:
            break
    while(1):
        if x[i][1]>=t[t_i]:
            t_i+=1
        else:
            break
    ans.append([x[i][0],x[i][1],s[s_i-1],s[s_i],t[t_i-1],t[t_i]])
ans.sort(key=lambda x:x[0])
for i in range(Q):
    xx=ans[i][1]
    Nb_s0=ans[i][2]
    Nb_s1=ans[i][3]
    Nb_t0=ans[i][4]
    Nb_t1=ans[i][5]

    p1=abs(Nb_s0-xx)
    p2=abs(Nb_s1-xx)
    p3=abs(Nb_t0-xx)
    p4=abs(Nb_t1-xx)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))
### BUGGY CODE ###

from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

def bfs():
    c = [[0]*n for i in range(n)]
    dist = [-1]*n
    que = deque([0])
    dist[0] = 0
    v_prev = 0
    k = 0
    while que:
        v = que.popleft()
        d = dist[v]
        color = set(range(1, n))
        used = []
        for w in g[v]:
            if dist[w] > -1:
                used.append(c[v][w])
        color.difference_update(used)
        color = deque(color)
        for w in g[v]:
            if dist[w] > -1:
                continue
            c[v][w] = c[w][v] = color.popleft()
            k = max(k, c[v][w])
            dist[w] = d + 1            
            que.append(w)
    return k, c

n = INT()
a = [0]*(n-1)
b = [0]*(n-1)
g = [[] for i in range(n)]
for i in range(n-1):
    a[i], b[i] = MAP()
    a[i] -= 1
    b[i] -= 1
    g[a[i]].append(b[i])
    g[b[i]].append(a[i])
k, c = bfs()
print(k)
for i in range(n-1):
    print(c[a[i]][b[i]])

### FIXED CODE ###

from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())

def solve():
    n = INT()
    Q = [tuple(MAP1()) for _ in range(n-1)]

    deg = [0]*n
    adj = [[] for _ in range(n)]
    color = dict()

    for a, b in Q:
        deg[a] += 1
        deg[b] += 1
        adj[a].append(b)
        adj[b].append(a)

    def dfs(s, p):
        nouse = -1
        if p > -1:
            nouse = color[(min(s,p),max(s,p))]
        c = 1
        for t in adj[s]:
            if t == p:
                continue
            if c == nouse:
                c += 1
            color[(min(s,t),max(s,t))] = c
            dfs(t,s)
            c += 1
    
    dfs(1,-1)
    
    print(max(deg))
    for a, b in Q:
        print(color[(a, b)])

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

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

cands = list(sorted(set(src)))

def enough(i):
    bit = [0] * (2*N+10)
    def bit_add(a,w):
        x = a
        while x <= (2*N+9):
            bit[x] += w
            x += (x & -x)
    def bit_sum(a):
        x = a
        ret = 0
        while x > 0:
            ret += bit[x]
            x -= (x & -x)
        return ret

    cums = [0]
    for a in src:
        tmp = 1 if a >= cands[i] else -1
        cums.append(cums[-1] + tmp)

    cnt = 0
    for i in range(N+1):
        a = cums[i] + N+5
        cnt += bit_sum(a)
        bit_add(a, 1)
    return cnt*2 >= N*(N+1)//2

ok = 0
ng = len(cands)
while ng - ok > 1:
    m = (ok+ng)//2
    if enough(m):
        ok = m
    else:
        ng = m
print(cands[ok])

### FIXED CODE ###

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

cands = list(sorted(set(src)))

def enough(i):
    bit = [0] * (2*N+10)
    def bit_add(a,w):
        x = a
        while x <= (2*N+9):
            bit[x] += w
            x += (x & -x)
    def bit_sum(a):
        x = a
        ret = 0
        while x > 0:
            ret += bit[x]
            x -= (x & -x)
        return ret

    cums = [0]
    for a in src:
        tmp = 1 if a >= cands[i] else -1
        cums.append(cums[-1] + tmp)

    cnt = 0
    for i in range(N+1):
        a = cums[i] + N+5
        cnt += bit_sum(a)
        bit_add(a, 1)
    return cnt*2 >= N*(N+1)//2

ok = 0
ng = len(cands)
while ng - ok > 1:
    m = (ok+ng)//2
    if enough(m):
        ok = m
    else:
        ng = m
print(cands[ok])
### BUGGY CODE ###

#template
def inputlist(): return [int(j) for j in input().split()]
#template

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}


N,M = inputlist()
uf = UnionFind(N+1)
for i in range(M):
    A,B = inputlist()
    uf.union(A,B)

ans = 0
lis = uf.all_group_members().values()
for i in lis:
    ans = max(ans,len(i))
print(ans)

### FIXED CODE ###

#template
def inputlist(): return [int(j) for j in input().split()]
#template

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}


N,M = inputlist()
uf = UnionFind(N+1)
for i in range(M):
    A,B = inputlist()
    uf.union(A,B)

ans = 0
for i in range(N+1):
    ans = max(ans,uf.size(i))
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import defaultdict,deque
from heapq import heappush,heappop,heapify

N=input()
AL=defaultdict(lambda: set())
IN=defaultdict(lambda: 0)
BEGIN=set()

for i in range(N):
	A=map(int, sys.stdin.readline().split())
	for j in range(N-2):
		x=i+1
		x_next=i+1
		y=A[j]
		y_next=A[j+1]
		#print x,y,x_next,y_next
		if x>y: x,y=y,x
		if x_next>y_next: x_next,y_next=y_next,x_next
		IN[(x_next,y_next)]+=1
		AL[(x,y)].add((x_next,y_next))

		if j==0:
			BEGIN.add((x,y))

q=deque()

for x,y in BEGIN:
	if IN[(x,y)]==0:
		q.append((x,y))


DAY=defaultdict(lambda: 1)	#距離

ans=-1
cnt=0
while q:
	fro=q.popleft()
	cnt+=1
	for to in AL[fro]:
		IN[to]-=1
		DAY[to]=max(DAY[to],DAY[fro]+1)
		if IN[to]==0:
			q.append((to[0],to[1]))

if cnt==N*(N-1)/2:
	print max(DAY.values())
else:
	print -1

### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
import copy
from collections import defaultdict,deque

N=input()
A=[[]]+[ map(int, sys.stdin.readline().split()) for _ in range(N) ]
C=defaultdict(lambda: 0)

Now=[ 0 for _ in range(N+1)]
game=set()
next_game=set()

for i in range(1,N+1):
	a=i
	b=A[i][0]
	if a>b: a,b=b,a
	game.add((a,b))

day=0

while game:
	next_game=set()
	Visit=defaultdict(lambda: None) 

	for x,y in game:
		now_x=Now[x]
		now_y=Now[y]

		xopp=A[x][now_x]
		yopp=A[y][now_y]

		if (x,y)==(yopp,xopp) and Visit[x] is None and Visit[y] is None:
			Visit[x]=1
			Visit[y]=1
			if now_x<N-2:
				now_x+=1
				Now[x]=now_x
				x_nextopp=A[x][now_x]
				if x_nextopp<x: x,x_nextopp=x_nextopp,x
				next_game.add((x,x_nextopp))

			if now_y<N-2:
				now_y+=1
				Now[y]=now_y
				y_nextopp=A[y][now_y]
				if y_nextopp<y: y,y_nextopp=y_nextopp,y
				next_game.add((y,y_nextopp))

	game=next_game
	day+=1


for i in range(1,N+1):
	if Now[i] != N-2:
		print -1
		quit()
else:
	print day
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]
ans = []
for i in range(n):
    ans.append(a.index(i + 1) + 1)
print(*ans)


### FIXED CODE ###

import sys


def main():
    n = int(input())
    a = [int(i) for i in sys.stdin.readline().split()]
    ans = [0] * n
    for i in range(n):
        ans[a[i] - 1] = i + 1
    print(*ans)


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

import sys
input=sys.stdin.readline
def main():
	R,C,K=map(int,input().split())
	lis=[[0]*(C) for i in range(R)]
	for i in range(K):
		a,b,c=map(int,input().split())
		lis[a-1][b-1]=c
	dp=[[[0]*4 for i in range(C+1)]for j in range(R+1)]
	for i in range(1,R+1):
		for j in range(1,C+1):
			for k in range(4):
				t=lis[i-1][j-1]
				dp[i][j][k]=max(dp[i-1][j][k],dp[i][j][k])
				dp[i][j][k]=max(dp[i][j-1][k],dp[i][j][k])
				if t!=0:
					
					dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][k]+t)
					if k>0:
						dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]+t)
	print(max(dp[-1][-1]))
if __name__ == '__main__':
	main()

### FIXED CODE ###

import sys
input=sys.stdin.readline
def main():
	R,C,K=map(int,input().split())
	lis=[[0]*(C) for i in range(R)]
	for i in range(K):
		a,b,c=map(int,input().split())
		lis[a-1][b-1]=c
	dp0=[[0]*(C+1) for i in range(R+1)]
	dp1=[[0]*(C+1) for i in range(R+1)]
	dp2=[[0]*(C+1) for i in range(R+1)]
	dp3=[[0]*(C+1) for i in range(R+1)]
	for i in range(1,R+1):
		for j in range(1,C+1):
			for x,y in ((dp0,0),(dp1,dp0),(dp2,dp1),(dp3,dp2)):
				t=lis[i-1][j-1]
				x[i][j]=max(x[i-1][j],x[i][j])
				x[i][j]=max(x[i][j-1],x[i][j])
				if t!=0:
					dp1[i][j]=max(dp1[i][j],x[i-1][j]+t)
					if y!=0:
						x[i][j]=max(x[i][j],y[i][j-1]+t)
	print(max(dp0[-1][-1],dp1[-1][-1],dp2[-1][-1],dp3[-1][-1]))
if __name__ == '__main__':
	main()
### BUGGY CODE ###


N = int(input())

a = []

for i in range(N):

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

    a.append(A)

day = 0
flag = True
end = 0

while end < N and day < N*(N-1)//2:

    day += 1

    Q = []
    flag = False

    for i in range(N):


        if len(a[i]) > 0  and a[ a[i][0]-1 ][0] == i + 1 and a[i][0] > i + 1:

            Q.append(a[i][0])
            Q.append(i + 1)

            flag = True

    if not flag:
        break

    #print (a,Q)

    for q in Q:
        del a[q-1][0]

        if len(a[q-1]) == 0:
            end += 1



if flag:
    print (day)
else:
    print (-1)
        
        


### FIXED CODE ###


N = int(input())

A = []

for i in range(N):

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

    A.append(a)

Q = []
for i in range(N):

    if A[ A[i][0]-1 ][0] == i + 1 and A[i][0]-1 > i:
        Q.append([min(A[i][0]-1,i),max(A[i][0]-1,i),1])

day = 0

while len(Q) > 0:

    q = Q.pop(0)

    del A[q[0]][0]
    del A[q[1]][0]




    if len(A[q[0]]) > 0 and A[ A[q[0]][0] - 1 ][0] == q[0] + 1:
        Q.append([min(A[q[0]][0] - 1,q[0]),max(A[q[0]][0] - 1,q[0]),q[2]+1])

    if len(A[q[1]]) > 0 and A[ A[q[1]][0] - 1 ][0] == q[1] + 1:
        Q.append([min(A[q[1]][0] - 1,q[1]),max(A[q[1]][0] - 1,q[1]),q[2]+1])

    day = max(day,q[2])

flag = True

for a in A:
    if len(a) > 0:
        flag = False
        
if flag:
    print (day)
else:
    print (-1)
### BUGGY CODE ###

def main():
    s = input()
    n = len(s)
    mod = 10 ** 9 + 7

    table1 = [0] * 13
    table2 = [0] * 13
    table1[0] = 1
    pre = [[(10 * i + j) % 13 for j in range(10)] for i in range(13)]
    r13 = range(13)
    for l in s:
        for i in r13:
            q = table1[i]
            if l == '?':
                for k in pre[i]:
                    table2[k] += q
            else:
                k = pre[i][int(l)]
                table2[k] += q

        table1 = table2
        table2 = [0] * 13

    print(table1[5] % mod)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    s = input()
    mod = 10 ** 9 + 7
    table1 = [0] * 13
    table2 = [0] * 13
    table1[0] = 1
    pre = [[((10 * i + j) % 13) for j in range(10)] for i in range(13)]

    for l in s:
        for i in range(13):
            q = table1[i] % mod
            if q == 0:
                continue
            if l == '?':
                for k in pre[i]:
                    table2[k] += q
            else:
                k = (10 * i + int(l)) % 13
                table2[k] += q

        table1 = table2
        table2 = [0] * 13

    print(table1[5] % mod)


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

import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
second = []
third = []
ans = 0
for i in range(N):
  second.append(N - bisect.bisect_right(B,A[i]))
  third.append(N - bisect.bisect_right(C,B[i]))
for i in range(0,N):
  if second[i] == 0:
    continue
  ans += sum(third[-second[i]:])
print(ans)

### FIXED CODE ###

import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
B = list(map(int,input().split()))
C = sorted(list(map(int,input().split())))
ans = 0
for i in range(N):
  ans += bisect.bisect_left(A,B[i]) * (N - bisect.bisect_right(C,B[i]))
print(ans)
### BUGGY CODE ###

import numpy as np

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

# [活発度, もとの番号] のリストを降順に並べる
A_list = [[A, index] for index, A in enumerate(A_list)]
A_list.sort(reverse=True)

# print(A_list)

dp = np.zeros((N+2, N+2), int)
# print(dp)
for left in range(0, N):
    for right in range(0, N - left):
        # print(f'left, righte: { left, right }')
        # print(A_list[left + right - 1][0])
        activity = A_list[left + right][0]
        distance_from_left = A_list[left + right][1] - left
        # print(f'distance_from_left: { distance_from_left }')
        distance_from_right = (N - 1 - right) - A_list[left + right][1]
        # print(f'distance_from_right: { distance_from_right }')
        dp[left + 1][right]\
            = max(dp[left + 1][right],
                  dp[left][right] + activity * distance_from_left)
        dp[left][right + 1]\
            = max(dp[left][right + 1],
                  dp[left][right] + activity * distance_from_right)
        # print('dp---')
        # print(dp)
        # print('---dp')
# print(dp)
print(np.max(dp))


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
table = []
 
for i, a in enumerate(A):
    table.append([a,i])
table.sort()
 
# DP[i][j] 左端に i 個, 右端に j 個貪欲に詰めたときのうれしさ
DP = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(1,N+1):
    # i 人目 table[i-1] の幼児を移動させる
    baby, pos = table.pop()
    
    DP[i][0] = DP[i-1][0] + baby * abs(pos-i+1)
    DP[0][i] = DP[0][i-1] + baby * abs(pos-(N-i))
    for x in range(1,i):
        y = i - x
 
        #左端に置く場合と右端に置く場合でmax
        DP[x][y] = max(DP[x-1][y] + baby * abs(pos-x+1),\
                     DP[x][y-1] + baby * abs(pos-(N-y)))
 
ans = 0
for i in range(N+1):
    ans = max(ans, DP[i][N-i])
print(ans)