TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
for ret in c.values():
    print(ret)
print(0)

import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
for i in range(1,n+1):
    print(c[i])

Verdicts: Wrong Answer

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
N = int(input())
ls = list(map(int, input()))
anls = [0]*(N+1)
for i in ls:
    anls[i] += 1
for i in range(1,N+1):
    print(anls[i])


N = int(input())
ls = list(map(int, input().split()))
anls = [0]*(N+1)
for i in ls:
    anls[i] += 1
for i in range(1,N+1):
    print(anls[i])



Verdicts: Runtime Error

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
N = int(input())
As = list(map(int, input().split()))
ans = [0] * N

for i in As:
    ans[i]+=1

for j in ans:
    print(j)

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

for i in As:
    ans[i-1]+=1

for j in ans:
    print(j)

Verdicts: Wrong Answer

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
import collections
import numpy as np

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

zero = np.zeros(n+1)
c = collections.Counter(a)

for val,key in c.items():
    zero[val] = key

for i in range(len(zero)):
    if i > 0:
        print(zero[i])

import numpy as np

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

all = np.zeros(n+1)

for j in range(1,n+1):
    for i,a_i in enumerate(a):
        if j == a_i:
            all[a_i] += 1
for i in range(len(all)):
    if i >= 1:
        print(all[i])

import numpy as np

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

all = np.zeros(n+1)

for j in range(1,n+1):         #Ai
    for i,a_i in enumerate(a):
        if i + 1 < a_i:
            pass
        else:
            if j == a_i:
                all[a_i] += 1

for i in range(1,len(all)):
        print(all[i])

import collections
import numpy as np

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

zero = np.zeros(n+1)
c = collections.Counter(a)

for val,key in c.items():
    zero[val] = key

for i in range(len(zero)):
    if i > 0:
        print(int(zero[i]))


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
import collections
n=int(input())
a=list(map(int, input().split()))
c=collections.Counter(a)
for i in range(n):
    print(c[i])

import collections
n=int(input())
a=list(map(int, input().split()))
c=collections.Counter(a)
for i in range(1,n+1):
    print(c[i])

Verdicts: Wrong Answer

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
b=[0]*(n)

for i in a:
  b[i]+=1

print(b for _ in range(n-1))

n=int(input())
a=list(map(int, input().split()))
b=[0]*n

for i in a:
  b[i-1] += 1

for i in range(n):
  print(b[i])

Verdicts: Wrong Answer

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
for i in range(n-1):
    ans[a[i]-1] += 1
for i in range(n-1):
    print(ans[i])

n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
for i in range(n):
    ans[a[i]-1] += 1
for i in range(n):
    print(ans[i])


n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
for i in range(n-1):
    ans[a[i]-1] += 1
for i in range(n):
    print(ans[i])

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
n = input()
l = list(map(int,input().split()))
for i in l:
    a = l.count(i+1)
    print(a)

from collections import Counter
n = input()
l = input().split()
lt = [int(i) for i in l]
f = Counter(l)
for i in lt:
    print(f[str(i+1)])


from collections import Counter
n = list(range(int(input())))
l = input().split()
lt = [int(i) for i in l]
f = Counter(l)
for i in n:
    print(f[str(i+1)])

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: A company has N members, who are assigned ID numbers 1, ..., N.  Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.  When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.  You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))

for i in range(N-1):
    count = A.count(i+1)
    print(count)

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

bukaList = []
for i in range(N-1):
    count = A.count(i+1)
    bukaList.append(count)


for k in range(N-1):
    print(bukaList[k])



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

count = 0
buka = [0 for _ in range(N)]

for i in range(N-1):
    buka[A[i]-1] += 1

for k in range(N):
    print(buka[k])


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
import sys
from collections import defaultdict

sys.setrecursionlimit(4000)

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

dp = {}
def cal(ln, rn):
  if ln + rn > n:
    return

  if ln > 0 or rn > 0:
    idx = ln + rn - 1
    p, a = arr[idx]
    val_l = dp[ln - 1, rn] + a * abs(p - (ln - 1)) if ln > 0 else 0
    val_r = dp[ln, rn - 1] + a * abs(p - (n - rn)) if rn > 0 else 0

    dp[ln, rn] = max(val_l, val_r)
  else:
    dp[ln, rn] = 0

  cal(ln + 1, rn)
  cal(ln, rn + 1)

cal(0, 0)

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



def main():

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

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

  print(max(dp[n]))

main()


Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
n = int(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, x = 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])

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

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

for j in range(1,n+1):
    for k in range(1,n-j+1):
        a, i = A_l[j+k-1]
        j_plus = a*(n-(j-1) - (i+1))
        k_plus = a*((i+1) - (1+(k-1)))
        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])

Verdicts: Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
# coding: utf-8
import numpy as np
N = int(input())
_A = sorted(enumerate(map(int, input().split()), 1), key=lambda x:x[1], reverse=True)
dp = np.zeros((N+1, N+1))

for i in range(1,N+1):
    k, Ak = _A[i-1]
    if (N-i-k) < 0:break
    dp[0,i] = dp[0,i-1] + (N-i+1-k) * Ak

for i in range(1,N+1):
    k, Ak = _A[i-1]
    if (k-i) < 0:break
    dp[i,0] = dp[i-1,0] + (k-i) * Ak

for x in range(1, N+1):
    for y in range(1, N-x+1):
        k, val = _A[x+y-1]
        dp[x,y]= max((k-x > 0)*(dp[x-1, y] + (k-x)*val),
                     (N-y-k+1 > 0)*(dp[x, y-1] + (N-y-k+1) * val))

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

# coding: utf-8
N = int(input())
_A = sorted(enumerate(map(int, input().split()), 1), key=lambda x:x[1], reverse=True)
dp = [[0] * (N+1) for i in range(N+1)]

for i in range(1,N+1):
    k, Ak = _A[i-1]
    if (N-i-k) < 0:break
    dp[0][i] = dp[0][i-1] + (N-i+1-k) * Ak

for i in range(1,N+1):
    k, Ak = _A[i-1]
    if (k-i) < 0:break
    dp[i][0] = dp[i-1][0] + (k-i) * Ak

for x in range(1, N+1):
    for y in range(1, N-x+1):
        k, val = _A[x+y-1]
        dp[x][y]= max(dp[x-1][y] + abs(k-x)*val,
                      dp[x][y-1] + abs(N-y-k+1) * val)

print(int(max(dp[i][N-i] for i in range(N+1))))


Verdicts: Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
import numpy as np

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

N = int(readline())
A = np.array(read().split(), np.int64)
I = np.argsort(A)[::-1].tolist()
A = A[I].tolist()

dp = np.zeros(1, np.int64)

for n, (i, x) in enumerate(zip(I, A)):
    newdp = np.zeros(len(dp) + 1, np.int64)
    left_ind = np.arange(len(dp))
    right_ind = N - 1 - n + left_ind
    left_cost = x * np.abs(left_ind - i)
    right_cost = x * np.abs(right_ind - i)
    np.maximum(newdp[:-1], dp + right_cost, out=newdp[:-1])
    np.maximum(newdp[1:], dp + left_cost, out=newdp[1:])
    dp = newdp

print(dp.max())


Verdicts: Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
AAwIdx = [[0]*2 for _ in range(N)]
for i in range(N):
    AAwIdx[i][0] = i
    AAwIdx[i][1] = AA[i]
AA_sorted = sorted(AAwIdx, key=lambda x: x[1], reverse=True)

Idx_i = 0
Idx_f = N-1
AnsIdx = [0]*N
#print(Idx_f)
answer = 0
for i in range(N):
    if Idx_f - AA_sorted[i][0] > AA_sorted[i][0] - Idx_i:
        AnsIdx[AA_sorted[i][0]] = Idx_f
        
        Idx_f -= 1
    else:
        AnsIdx[AA_sorted[i][0]] = Idx_i
        
        Idx_i += 1
    
for i in range(N):
    answer += abs(AAwIdx[i][0] - AnsIdx[i])*AAwIdx[i][1]
answerb = 0
Idx_i = 0
Idx_f = N-1
AnsIdx = [0]*N
if Idx_f - AA_sorted[0][0] > AA_sorted[0][0] - Idx_i:
    AnsIdx[AA_sorted[i][0]] = Idx_i
    
    Idx_i += 1
else:
    AnsIdx[AA_sorted[i][0]] = Idx_f
    
    Idx_f -= 1
for i in range(1,N):
    if Idx_f - AA_sorted[i][0] > AA_sorted[i][0] - Idx_i:
        AnsIdx[AA_sorted[i][0]] = Idx_f
        
        Idx_f -= 1
    else:
        AnsIdx[AA_sorted[i][0]] = Idx_i
        
        Idx_i += 1
    
for i in range(N):
    answerb += abs(AAwIdx[i][0] - AnsIdx[i])*AAwIdx[i][1]
#print(AnsIdx)
print(max(answer, answerb))

def solve():
    N = int(input())
    #AAwIdx = list(map(int,input().split()))
    AAwIdx = [(i,A) for i, A in enumerate(map(int, input().split()))]
    
    #    AAwIdx[i][0] = i
    #    AAwIdx[i][1] = AA[i]
    AA_sorted = sorted(AAwIdx, key=lambda x: x[1], reverse=True)
    

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

    ans_all = 0
    Idx_i = 0
    Idx_f = N-1
    for i in range(0,N):
        for no in range(i+1):
            idxi = i-no
            idxf = Idx_f-no
            ans = answer[i-no][no] + abs(AA_sorted[i][0]-idxi)*AA_sorted[i][1]
            if ans>answer[i+1-no][no]:
                answer[i+1-no][no] = ans
            ans = answer[i-no][no] + abs(AA_sorted[i][0]-idxf)*AA_sorted[i][1]
            if ans>answer[i-no][no+1]:
                answer[i-no][no+1] = ans
    for i in range(N,N+1):
        for no in range(i+1):
            if ans_all<answer[i-no][no]:
                ans_all = answer[i-no][no]

    print(ans_all)
solve()

Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
import sys
sys.setrecursionlimit(2 ** 2000)
n = int(input())
a = list(map(int, input().split()))
a = [(i, v) for i, v in enumerate(a)]
a.sort(key=lambda x: x[1], reverse=True)

dp = [[-1] * (m + 1) for m in range(n + 1)]

def f(m, l):
    if m == n: return 0
    if dp[m][l] != -1: return dp[m][l]
    r = n - m + l - 1
    i, v = a[m]
    if i >= l: dp[m + 1][l + 1] = f(m + 1, l + 1)
    if i <= r: dp[m + 1][l] = f(m + 1, l)
    return max(dp[m + 1][l + 1] + v * (i - l), dp[m + 1][l] + v * (r - i))
print(f(0, 0))

import sys
sys.setrecursionlimit(2000 * 2)
n = int(input())
a = list(map(int, input().split()))
a = [(i, v) for i, v in enumerate(a)]
a.sort(key=lambda x: x[1], reverse=True)

dp = [[-1] * (m + 1) for m in range(n + 1)]

def f(m, l):
    if m == n: return 0
    if dp[m][l] != -1: return dp[m][l]
    r = n - m + l - 1
    i, v = a[m]
    if i >= l: dp[m + 1][l + 1] = f(m + 1, l + 1)
    if i <= r: dp[m + 1][l] = f(m + 1, l)
    return max(dp[m + 1][l + 1] + v * (i - l), dp[m + 1][l] + v * (r - i))
print(f(0, 0))

Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
#import numpy as np
#import math
#from decimal import *
#from numba import njit


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

    A = sorted(enumerate(A), key = lambda x: x[1])[::-1]

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

    for i in range(1,len(A)+1):
        index,a = A[i-1]
        for j in range(i+1):
            if (i-j,j) == (0,0):
                dp[i-j][j] = 0
            elif i-j == 0:
                dp[i-j][j] = dp[i-j][j-1] + (N-j-index)*a
            elif j == 0:
                dp[i-j][j] = dp[i-j-1][j] + (abs(index-(i-j-1)))*a
            else:
                dp[i-j][j] = max(dp[i-j][j-1] + (N-j-index)*a, dp[i-j-1][j] + (abs(index-(i-j-1)))*a)

    #print(dp)
    #print(np.max(dp))

main()


#import numpy as np
#import math
#from decimal import *
#from numba import njit


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

    A = sorted(enumerate(A), key = lambda x: x[1])[::-1]

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

    m = 0
    for i in range(1,len(A)+1):
        index,a = A[i-1]
        for j in range(i+1):
            if (i-j,j) == (0,0):
                dp[i-j][j] = 0
            elif i-j == 0:
                dp[i-j][j] = dp[i-j][j-1] + (N-j-index)*a
            elif j == 0:
                dp[i-j][j] = dp[i-j-1][j] + (abs(index-(i-j-1)))*a
            else:
                dp[i-j][j] = max(dp[i-j][j-1] + (N-j-index)*a, dp[i-j-1][j] + (abs(index-(i-j-1)))*a)
            if i == len(A):
                m = max(m, dp[i-j][j])

    #print(dp)
    #print(np.max(dp))
    print(m)

main()


Verdicts: Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    AA = [(v, i) for i, v in enumerate(A)]
    AA.sort(reverse=True)
    dp = [[0] * (N+1) for _ in range(N+1)]
    for i, (a, b) in enumerate(AA, start=1):
        for x in range(i+1):
            y = i - x
            if y == 0:
                dp[x][y] = dp[x-1][y] + abs(b - x+1) * a
            elif x == 0:
                dp[x][y] = dp[x][y-1] + abs(N-y-b) * a
            else:
                dp[x][y] = max(dp[x-1][y] + abs(b - x + 1) * a, dp[x][y-1] + abs(N-y-b) * a)
    for i in range(N+1):
        r = max(r, dp[i][N-i])
    return r
print(main())


def main():
    N = int(input())
    A = list(map(int, input().split()))
    AA = [(v, i) for i, v in enumerate(A)]
    AA.sort(reverse=True)
    dp = [[0] * (N+1) for _ in range(N+1)]
    for i, (a, b) in enumerate(AA, start=1):
        for x in range(i+1):
            y = i - x
            if y == 0:
                dp[x][y] = dp[x-1][y] + abs(b - x+1) * a
            elif x == 0:
                dp[x][y] = dp[x][y-1] + abs(N-y-b) * a
            else:
                dp[x][y] = max(dp[x-1][y] + abs(b - x + 1) * a, dp[x][y-1] + abs(N-y-b) * a)
    r = 0
    for i in range(N+1):
        r = max(r, dp[i][N-i])
    return r
print(main())


Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
N,A=open(0);N=int(N);A=sorted((int(x),i)for i,x in enumerate(A.split()));t=[0]*N**2
for w in range(1,N):
 a,j=A[w]
 for l in range(N-w):
  r=l+w;t[l*N+r]=max(a*abs(j-l)+t[(l+1)*N+r],a*abs(j-r)+t[l*N+r-1])
print(t[N-1])

N=int(input());A=sorted((int(x),i)for i,x in enumerate(input().split()));t=[0]*N**2
for w in range(N):
 a,j=A[w]
 for l in range(N-w):
  r=l+w;p=a*abs(j-l);t[l*N+r]=max(p+t[(l+1)*N+r],a*abs(j-r)+t[l*N+r-1])if w else p
print(t[N-1])

Verdicts: Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
def main():
    N = int(input())
    A = list(map(int, input().split()))

    
    
    

    
    sortedA = sorted(A[[a,i] for i,a in enumerate(A)], reverse=True)
    # DP
    dp = [[0]*(N+1) for _ in range(N+1)]
    for i in range(N):
        for l in range(i+1):
            r = i-l
            dp[i+1][l] = max(dp[i+1][l], dp[i][l]+sortedA[i][0]*abs((N-r-1)-sortedA[i][1]))
            dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l]+sortedA[i][0]*abs(l-sortedA[i][1]))
    print(max(dp[N]))

main()


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

    
    
    

    
    Awithidx = [[a,i] for i,a in enumerate(A)]
    sortedA = sorted(Awithidx, reverse=True)
    # DP
    dp = [[0]*(N+1) for _ in range(N+1)]
    for i in range(N):
        a = sortedA[i][0]
        idx = sortedA[i][1]
        for l in range(i+1):
            r = i-l
            dp[i+1][l] = max(dp[i+1][l], dp[i][l]+a*abs((N-r-1)-idx))
            dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l]+a*abs(l-idx))
    print(max(dp[N]))

main()


Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))

a = [(i, ai) for i, ai in enumerate(A)]
a = sorted(key=lambda x: x[1], reverse=True)

# import numpy as np


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]))

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 = [[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]))

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    solve(N, A)


Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
from copy import deepcopy

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

index = [i for i in range(N)]
index = sorted(index, key=lambda i: A[i], reverse=True)

print(A)
print(index)


def LorR(index, res):
    i = index[0]
    dist = max(i, N - 1 - i)
    end = False
    cand1 = cand2 = 0
    while not end:
        j = i + dist
        if 0 <= j and j < N and not res[j]:
            r = deepcopy(res)
            r[j] = True
            cand1 = A[i] * dist + loop(index[1:], r)
            end = True

        j = i - dist
        if 0 <= j and j < N and not res[j]:
            r = deepcopy(res)
            r[j] = True
            cand2 = A[i] * dist + loop(index[1:], r)
            end = True

        dist -= 1
    return max(cand1, cand2)


def loop(index, res):
    if not index:
        return 0

    num = 0
    a = A[index[0]]
    while num < len(index) and A[index[num]] == a:
        num += 1

    cand = [0] * num
    for i in range(num):
        ind = deepcopy(index)
        head = ind.pop(i)
        ind.insert(0, head)
        cand[i] = LorR(ind, res)

    return max(cand)


print(loop(index, [False] * N))


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

A = sorted([(val, pos) for pos, val in enumerate(A)], reverse=True)

dp = [0]
for i, (val, pos) in enumerate(A):
    ldp = [e + val * abs(pos - (i - r)) for r, e in enumerate(dp)]
    rdp = [e + val * abs(N - 1 - r - pos) for r, e in enumerate(dp)]
    dp = [max(L, R) for L, R in zip(ldp + [0], [0] + rdp)]

print(max(dp))


Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
N = int(input())
 
ABI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True)
prev = [0]
for k, (a,i) in enumerate(ABI):
  curr = [0]*(k+2)
  for l in range(k+1):
    curr[l] = max(curr[l], prev[l]+abs(N-i-k+l)*a)
    curr[l+1] = prev[l]+abs(i-l-1)*a
    
  prev = curr
 
print(max(prev))

N = int(input())
 
A = [int(s) for s in input().split(" ")]
ABI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True)
prev = [0]
for k, (a,i) in enumerate(ABI):
  curr = [0]*(k+2)
  for l in range(k+1):
    curr[l] = max(curr[l], prev[l]+abs(N-i-k+l)*a)
    curr[l+1] = prev[l]+abs(i-l-1)*a
    print(curr,k,i)
  prev = curr
 
print(max(prev))

N = int(input())
A = [int(s) for s in input().split(" ")]
ABI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True)
prev = [0]
for k, (a,i) in enumerate(ABI):
  curr = [0]*(k+2)
  for l in range(k+1):
    curr[l] = max(curr[l], prev[l]+abs(N-i-k+l)*a)
    curr[l+1] = prev[l]+abs(i-l-1)*a
    
  prev = curr
 
print(max(prev))

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
table = []

for i, a in enumerate(A):
    table.append([a,i])
table.sort()


DP = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(1,N+1):
    
    baby, pos = table.pop()
    for x in range(i+1):
        y = i - x

        
        DP[x][0] = DP[x-1][0] + baby * abs(pos-x+1)
        DP[0][y] = DP[x][y-1] + baby * abs(pos-(N-y))
        if x & y:
            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)


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

for i, a in enumerate(A):
    table.append([a,i])
table.sort(reverse=True)


DP = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(1,N+1):
    
    baby, pos = table.pop()
    for x in range(i+1):
        y = i - x

        
        DP[x][y] = max((DP[x-1][y] + baby * abs(pos-x+1)) * (x>0),\
                     (DP[x][y-1] + baby * abs(pos-(N-y)) * (y>0)))

ans = 0
for i in range(N+1):
    ans = max(ans, DP[i][N-i])
print(ans)


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

for i, a in enumerate(A):
    table.append([a,i])
table.sort()


DP = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(1,N+1):
    
    baby, pos = table.pop()
    for x in range(i+1):
        y = i - x

        
        DP[x][0] = DP[x-1][0] + baby * abs(pos-x+1))
        DP[0][y] = DP[x][y-1] + baby * abs(pos-(N-y))
        if x & y:
            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)


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

for i, a in enumerate(A):
    table.append([a,i])
table.sort()


DP = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(1,N+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

        
        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)


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
#!python3

# input
N = int(input())
A = list(map(int, input().split()))
N = 2000
A = [i for i in range(1, N + 1)]


def main():
    P = [(A[i], i + 1) for i in range(N)]
    P.sort(reverse=True)

    dp = [[0] * (N + 1) for _ in range(N + 1)]
    for n, p in zip(range(1, N + 1), P):
        x, y = p
        for i in range(n):
            j = n - 1 - i
            left = dp[i][j] + x * abs(y - (i + 1))
            dp[i + 1][j] = max(dp[i + 1][j], left)
            right = dp[i][j] + x * abs(y - (N - j))
            dp[i][j + 1] = max(dp[i][j + 1], right)

    ans = max([dp[i][N - i] for i in range(N)])
    print(ans)


if __name__ == "__main__":
    main()


#!python3

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



def main():
    P = [(A[i], i + 1) for i in range(N)]
    P.sort(reverse=True)

    dp = [[0] * (N + 1) for _ in range(N + 1)]
    for n, p in zip(range(1, N + 1), P):
        x, y = p
        for i in range(n):
            j = n - 1 - i
            left = dp[i][j] + x * abs(y - (i + 1))
            dp[i + 1][j] = max(dp[i + 1][j], left)
            right = dp[i][j] + x * abs(y - (N - j))
            dp[i][j + 1] = max(dp[i][j + 1], right)

    ans = max([dp[i][N - i] for i in range(N)])
    print(ans)


if __name__ == "__main__":
    main()


Verdicts: Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
from collections import defaultdict
#n=int(input())
#a_=list(map(int,input().split()))
import numpy as np
n=2000
a_=range(1,n+1)
a=[]
for i,ai in enumerate(a_):
      a.append([1+i,ai])
a.sort(key=lambda x:x[1],reverse=True)
dp=np.zeros((n+1,n+1),np.int64) 
dp[1,1]=a[0][1]*(n-a[0][0])
dp[1,0]=a[0][1]*(a[0][0]-1)
for i in range(2,n+1):
      dp[i,0]=dp[i-1,0]+a[i-1][1]*abs((a[i-1][0]-1-(i-1))) 
      dp[i,i]=dp[i-1,i-1]+a[i-1][1]*abs((n-a[i-1][0]-(i-1))) 
      
      l=dp[i-1,1:i]+[a[i-1][1]*abs((a[i-1][0]-1-j))for j in range(i-2,-1,-1)] 
      
      r=dp[i-1,:i-1]+[a[i-1][1]*abs((n-a[i-1][0]-j)) for j in range(i-1)] 
      dp[i,1:i]=np.maximum(l,r)
print(max(dp[n]))


import sys
import numpy as np
def f(n,a_):
      a=[]
      for i,ai in enumerate(a_):
            a.append([1+i,ai])
      a.sort(key=lambda x:x[1],reverse=True)
      dp=np.zeros((n+1,n+1),np.int64) 
      dp[1,1]=a[0][1]*(n-a[0][0])
      dp[1,0]=a[0][1]*(a[0][0]-1)
      for i in range(2,n+1):
            dp[i,0]=dp[i-1,0]+a[i-1][1]*abs((a[i-1][0]-1-(i-1))) 
            dp[i,i]=dp[i-1,i-1]+a[i-1][1]*abs((n-a[i-1][0]-(i-1))) 
            
            l=dp[i-1,1:i]+[a[i-1][1]*abs((a[i-1][0]-1-j))for j in range(i-2,-1,-1)] 
            
            r=dp[i-1,:i-1]+[a[i-1][1]*abs((n-a[i-1][0]-j)) for j in range(i-1)] 
            dp[i,1:i]=np.maximum(l,r)
      return max(dp[n])

if __name__ == '__main__':
      input = sys.stdin.readline
      n=int(input())
      a_=list(map(int,input().split()))
      print(f(n,a_))


Verdicts: Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
from collections import *
from itertools import *

N = int(input())

A = map(int, input().split())
SA = SB = 0
ABI = sorted((((b:=max(N-i, i-1)*a), (SA:=SA+a), (SB:=SB+b)) and (a, b, i) for i, a in enumerate(A, 1)), reverse=True)


print(1)


N = int(input())

def solve(a, i, prev):
  r = N - len(prev) - i + 1
  p = -i*a
  for j, s in enumerate(prev):
    yield max(p+abs(j-i)*a, s+abs(j+r)*a)
    p = s
  yield s+abs(len(prev)-i)*a

pd = [0]
A = map(int, input().split())
for a,i in sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True):
  pd = [*solve(a,i, prev)]

print(max(prev))


from collections import *
from itertools import *

N = int(input())

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

ABI = sorted(((a, max(N-i, i-1)*a, i) for i, a in enumerate(A, 1)), reverse=True)
SA, SB = map(sum, islice(zip(*ABI), 0, 2))

prev = {(0,0):0}
prev_max = 0
for a, b, i in ABI:
  curr = defaultdict(int)
  curr_max = 0
  for (l,r), p in prev.items():
    if p + SB - min(l,r)*SA < prev_max:
      continue
    curr[l,r+1] = max(curr[l,r+1], p+abs(N-i-r)*a)
    curr[l+1,r] = max(curr[l+1,r], p+abs(i-l-1)*a)
    curr_max = max(curr_max, curr[l,r+1], curr[l+1,r])
  SA -= a
  SB -= b
  prev = curr
  prev_max = curr_max
  print(prev)

print(curr_max)

from collections import *
from itertools import *

N = int(input())

A = map(int, input().split())
SA = SB = 0
ABI = sorted(((b:=max(N-i, i-1)*a, SA:=SA+a, SB:=SB+b) and (a, b, i) for i, a in enumerate(A, 1)), reverse=True)

prev = {0:0}
prev_max = 0
for k, (a, b, i) in enumerate(ABI, 1):
  curr = defaultdict(int)
  curr_max = 0
  for l, p in prev.items():
    r = k-l
    if p + SB - min(l,r)*SA < prev_max:
      continue
    curr[l] = max(curr[l], p+abs(N-i-r)*a)
    curr[l+1] = p+abs(i-l-1)*a
    curr_max = max(curr_max, curr[l], curr[l+1])
  SA -= a
  SB -= b
  prev = curr
  prev_max = curr_max

print(curr_max)


N = int(input())

def solve(a, i, prev):
  r = N - len(prev) - i + 1
  p = -i*a
  for j, s in enumerate(prev):
    yield max(p+abs(j-i)*a, s+abs(j+r)*a)
    p = s
  yield s+abs(len(prev)-i)*a

pd = [0]
A = map(int, input().split())
for a,i in sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True):
  pd = [*solve(a,i, pd)]

print(max(prev))


N = int(input())

def solve(a, i, prev):
  r = N - len(prev) - i + 1
  p = -i*a
  for j, s in enumerate(prev):
    yield p+abs(j-i)*a, s+abs(j+r)*a
    p = s
  yield s+abs(len(prev)-i)*a,

pd = [0]
A = map(int, input().split())
for a,i in sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True):
  pd = [*map(max, solve(a,i, pd))]

print(max(pd))


Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
N=int(input())
actlist=list(map(int,input().split()))
As = sorted([(A, i) for i, A in enumerate(actlist)])[::-1]

INF = float('inf')
left=1
right=N
maxnum=0

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

dp[0][0]=0
for r in range(N+1):
    for l in range(N-r+1):
        dp[l][r]=max(dp[l][r-1]+As[l+r-1][0]*abs(N-r-As[l+r-1][1]),\
          dp[l-1][r]+As[l+r-1][0]*abs(l-1-As[l+r-1][1]))
        maxnum=dp[l][r] if maxnum<dp[l][r] else maxnum

    
print(maxnum)

import itertools

N=int(input())
actlist=list(map(int,input().split()))
alllist=list(itertools.permutations(range(1,N+1)))

count=1

hapsum=0
for i in range(N-1):
    for j in (i+1,N):
        maxnum=0
        for m,n in zip(alllist[i],alllist[j]):
            hapsum+=actlist[i]*abs(m-n)
        if maxnum<hapsum:
            maxnum=hapsum
        

print(maxnum)

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

As = sorted([(A, i) for i, A in enumerate(actlist)])[::-1]

left=1
right=N
sumnum=0

for i in As:
    move=0
    moveleft=abs(i[1]+1-left)
    moveright=abs(i[1]+1-right)
    if moveleft>moveright:
        move=moveleft
        left+=1
    else:
        move=moveright
        right=right-1
    sumnum+=move*i[0]
    
print(sumnum+1)

def main():
  N = int(input())
  INF = float('inf')
  A = [(i+1, a) for i, a in enumerate(map(int, input().split()))]
  A = sorted(A, key=lambda x: x[1], reverse = True)
 
  dp = [[-INF] * (N+1) for _ in range(N+1)]
  dp[0][0] = 0
 
  for s in range(1, N+1):
    for l in range(s+1):
      r = s - l
      dp[l][r] = max(dp[l-1][r] + A[s-1][1] * abs(A[s-1][0]-l), dp[l][r-1] + A[s-1][1] * abs(N-r+1-A[s-1][0]))
    
  ans = 0
  for m in range(N):
    if(dp[m][N-m] > ans):
      ans = dp[m][N-m]
    
  print(ans)
 
main()

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
acts = [(x, i) for i, x in enumerate(A)]
acts.sort()
print(acts)
dp = [[-1]*N for _ in range(N)]

def solve(l, r):
    if r < l:
        return 0
    if dp[l][r] >= 0:
        return dp[l][r]
    k = r - l
    act, init_pos = acts[k]
    gain_l = act * abs(l - init_pos)
    gain_r = act * abs(r - init_pos)
    res = dp[l][r] = max(gain_l + solve(l+1, r), gain_r + solve(l, r-1))
    return res

print(solve(0, N-1))


import sys
sys.setrecursionlimit(10**6)
N = int(input())
A = list(map(int, input().split()))
acts = [(x, i) for i, x in enumerate(A)]
acts.sort()
dp = [[-1]*N for _ in range(N)]
 
def solve(l, r):
    if r < l:
        return 0
    if dp[l][r] >= 0:
        return dp[l][r]
    k = r - l
    act, init_pos = acts[k]
    gain_l = act * abs(l - init_pos)
    gain_r = act * abs(r - init_pos)
    res = dp[l][r] = max(gain_l + solve(l+1, r), gain_r + solve(l, r-1))
    return res
 
print(solve(0, N-1))

Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
import sys
sys.setrecursionlimit(10**6)
N = int(input())
A = list(map(int, input().split()))
acts = [(x, i) for i, x in enumerate(A)]
acts.sort()
dp = [[-1]*N for _ in range(N)]
 
def rec(l, r):
    if r < l:
        return 0
    if dp[l][r] >= 0:
        return dp[l][r]
    k = r - l
    act, init_pos = acts[k]
    gain_l = act * abs(l - init_pos)
    gain_r = act * abs(r - init_pos)
    res = dp[l][r] = max(gain_l + rec(l+1, r), gain_r + rec(l, r-1))
    return res
 
print(solve(0, N-1))

def solve():
        
    N = int(input())
    A = [(a, i+1) for i, a in enumerate(map(int, input().split()))]

    A.sort(reverse=True)
    INF = float('inf')
    dp = [[-INF] * (N+1) for _ in range(N+1)]
    dp[0][0] = 0

    for s in range(1, N+1):
        for l in range(s+1):
            r = s - l
            dp[l][r] = max(dp[l-1][r] + A[s-1][0] * abs(A[s-1][1]-l), dp[l][r-1] + A[s-1][0] * abs(N-r+1-A[s-1][1]))

    ans = 0
    for m in range(N):
        if dp[m][N-m] > ans:
            ans = dp[m][N-m]

    print(ans)
solve()

Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
n = int(input())
a = list(map(int, input().split()))

a = list(enumerate(a))
a.sort(key = lambda x: x[1], reverse=True)

for i in range(n):
    a[i][0] = a[i][0] + 1

DP = [[0 for i in range(n+1)] for j in range(n+1)]
DP[0][0] = 0
for i in range(1, n+1):
    DP[0][i] = DP[0][i-1] + abs(a[i-1][1] * (n - i + 1 - a[i-1][0]))
    DP[i][0] = DP[i-1][0] + abs(a[i-1][1] * (a[i-1][0] - i))
    for x in range(1, i):
        y = i - x
        DP[x][y] = max(DP[x-1][y] + abs(a[i-1][1] * (a[i-1][0] - x)),  \
            DP[x][y-1] + abs(a[i-1][1] * (n - y + 1 - a[i-1][0])))

ans = 0
for i in range(n+1):
    ans = max(ans, DP[i][n-i])

print(ans) 

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

a = list(enumerate(a))
a.sort(key = lambda x: x[1])

DP = [[0 for i in range(n+1)] for j in range(n+1)]
DP[0][0] = 0
for i in range(1, n+1):
    pos, val = a.pop()
    pos = pos + 1

    DP[0][i] = DP[0][i-1] + abs(val * (n - i + 1 - pos))
    DP[i][0] = DP[i-1][0] + abs(val * (pos - i))
    for x in range(1, i):
        y = i - x
        DP[x][y] = max(DP[x-1][y] + abs(val * (pos - x)),  \
            DP[x][y-1] + abs(val * (n - y + 1 - pos)))

ans = 0
for i in range(n+1):
    ans = max(ans, DP[i][n-i])

print(ans) 

Verdicts: Runtime Error

Accepted
Problem: There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.  You can rearrange these children just one time in any order you like.  When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.  Find the maximum total happiness points the children can earn.

Submission attempts (in order):
N = int(input())
A = np.array(list(map(int,input().split())))
ABI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True)
prev = [0]
for k, (a, i) in enumerate(ABI)
    curr = [0]*(k+2)
    for l in range(k+1):
        curr[l] = max(curr[l], prev[l] + a*abs(N-i-(k-l)))
        curr[l+1] = prev[l]+a*abs(i-l-1)
    prev = curr
print (max(prev))

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

junban = list(range(N))

max_happness = 0
for subset in permutations(junban):
    happness = 0
    newjunban = (list(subset))
    for i in range(len(junban)):
        happness +=stuff[i]*abs(newjunban[i]-i)
    if happness>max_happness:
        max_happness = happness
print (max_happness)

N = int(input())
A = list(map(int,input().split()))
ABI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True)
prev = [0]
for k, (a, i) in enumerate(ABI):
    curr = [0]*(k+2)
    for l in range(k+1):
        curr[l] = max(curr[l], prev[l] + a*abs(N-i-(k-l)))
        curr[l+1] = prev[l]+a*abs(i-l-1)
    prev = curr
print (max(prev))

Verdicts: Runtime Error

Runtime Error

Accepted