Buggy python submission (verdict: Time Limit Exceeded):
#def solve():
INF = float('inf')

def max2(x, y): return x if x >= y else y

N = int(input())
As = [(A, i) for i, A in enumerate(map(int, input().split()))]

As.sort(reverse=True)

dp = [[-INF]*(N+1) for _ in range(N+1)]
dp[0][0] = 0
for no, (A, pos) in enumerate(As):
    for i in range(no+1):
        j = no-i
        dp[i+1][j] = max2(dp[i+1][j], dp[i][j] + A*abs(pos-i))
        dp[i][j+1] = max2(dp[i][j+1], dp[i][j] + A*abs(pos-(N-1-j)))

ans = 0
for i in range(N+1):
    j = N-i
    ans = max2(ans, dp[i][j])

print(ans)
 
 
#solve()

Fixed submission:
def solve():
    INF = float('inf')
 
    def max2(x, y): return x if x >= y else y
 
    N = int(input())
    As = [(A, i) for i, A in enumerate(map(int, input().split()))]
 
    As.sort(reverse=True)
 
    dp = [[-INF]*(N+1) for _ in range(N+1)]
    dp[0][0] = 0
    for no, (A, pos) in enumerate(As):
        for i in range(no+1):
            j = no-i
            dp[i+1][j] = max2(dp[i+1][j], dp[i][j] + A*abs(pos-i))
            dp[i][j+1] = max2(dp[i][j+1], dp[i][j] + A*abs(pos-(N-1-j)))
 
    ans = 0
    for i in range(N+1):
        j = N-i
        ans = max2(ans, dp[i][j])
 
    print(ans)
 
 
solve()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
AAwIdx = [(i,A) for i, A in enumerate(map(int, input().split()))]
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)

Fixed submission:
def solve():
    INF = float('inf')
 
    def max2(x, y): return x if x >= y else y
 
    N = int(input())
    As = [(A, i) for i, A in enumerate(map(int, input().split()))]
 
    As.sort(reverse=True)
 
    dp = [[-INF]*(N+1) for _ in range(N+1)]
    dp[0][0] = 0
    for no, (A, pos) in enumerate(As):
        for i in range(no+1):
            j = no-i
            dp[i+1][j] = max2(dp[i+1][j], dp[i][j] + A*abs(pos-i))
            dp[i][j+1] = max2(dp[i][j+1], dp[i][j] + A*abs(pos-(N-1-j)))
 
    ans = 0
    for i in range(N+1):
        j = N-i
        ans = max2(ans, dp[i][j])
 
    print(ans)
 
 
solve()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
AA = list(map(int,input().split()))
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)
#print(AA_sorted)

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)

Fixed submission:
def solve():
    INF = float('inf')
 
    def max2(x, y): return x if x >= y else y
 
    N = int(input())
    As = [(A, i) for i, A in enumerate(map(int, input().split()))]
 
    As.sort(reverse=True)
 
    dp = [[-INF]*(N+1) for _ in range(N+1)]
    dp[0][0] = 0
    for no, (A, pos) in enumerate(As):
        for i in range(no+1):
            j = no-i
            dp[i+1][j] = max2(dp[i+1][j], dp[i][j] + A*abs(pos-i))
            dp[i][j+1] = max2(dp[i][j+1], dp[i][j] + A*abs(pos-(N-1-j)))
 
    ans = 0
    for i in range(N+1):
        j = N-i
        ans = max2(ans, dp[i][j])
 
    print(ans)
 
 
solve()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
AA = list(map(int,input().split()))
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)
#print(AA_sorted)

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
        answer[i+1-no][no] = max(answer[i+1-no][no],answer[i-no][no] + abs(AA_sorted[i][0]-idxi)*AA_sorted[i][1])
        answer[i-no][no+1] = max(answer[i-no][no+1],answer[i-no][no] + abs(AA_sorted[i][0]-idxf)*AA_sorted[i][1])
        #print('idx:',AA_sorted[i][0], 'val:',AA_sorted[i][1],'idx_i:',idxi, 'idx_f:',idxf)
        #print('idx:',i-no,no,'ans:',answer[i-no][no])
        #print('idx:',i+1-no,no,'ans:',abs(AA_sorted[i][0]-idxi)*AA_sorted[i][1])
        #print('idx:',i-no,no+1,'ans:',abs(AA_sorted[i][0]-idxf)*AA_sorted[i][1])
        ans_all = max(answer[i+1-no][no], ans_all)
        ans_all = max(answer[i-no][no+1], ans_all)
#print(np.array(answer))
#    print(AAs)
#print(AnsIdx)
print(ans_all)

Fixed submission:
def solve():
    INF = float('inf')
 
    def max2(x, y): return x if x >= y else y
 
    N = int(input())
    As = [(A, i) for i, A in enumerate(map(int, input().split()))]
 
    As.sort(reverse=True)
 
    dp = [[-INF]*(N+1) for _ in range(N+1)]
    dp[0][0] = 0
    for no, (A, pos) in enumerate(As):
        for i in range(no+1):
            j = no-i
            dp[i+1][j] = max2(dp[i+1][j], dp[i][j] + A*abs(pos-i))
            dp[i][j+1] = max2(dp[i][j+1], dp[i][j] + A*abs(pos-(N-1-j)))
 
    ans = 0
    for i in range(N+1):
        j = N-i
        ans = max2(ans, dp[i][j])
 
    print(ans)
 
 
solve()

Buggy python submission (verdict: Runtime Error):
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)
#print(AA_sorted)
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
        #answer += abs(AA_sorted[i][0]-Idx_f)*AA_sorted[i][1]
        Idx_f -= 1
    else:
        AnsIdx[AA_sorted[i][0]] = Idx_i
        #answer += abs(AA_sorted[i][0]-Idx_i)*AA_sorted[i][1]
        Idx_i += 1
    #print(AA_sorted[i][0],Idx_i,Idx_f)
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
    #answer += abs(AA_sorted[i][0]-Idx_i)*AA_sorted[i][1]
    Idx_i += 1
else:
    AnsIdx[AA_sorted[i][0]] = Idx_f
    #answer += abs(AA_sorted[i][0]-Idx_f)*AA_sorted[i][1]
    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
        #answer += abs(AA_sorted[i][0]-Idx_f)*AA_sorted[i][1]
        Idx_f -= 1
    else:
        AnsIdx[AA_sorted[i][0]] = Idx_i
        #answer += abs(AA_sorted[i][0]-Idx_i)*AA_sorted[i][1]
        Idx_i += 1
    #print(AA_sorted[i][0],Idx_i,Idx_f)
for i in range(N):
    answerb += abs(AAwIdx[i][0] - AnsIdx[i])*AAwIdx[i][1]
#print(AnsIdx)
print(max(answer, answerb))

Fixed submission:
def solve():
    INF = float('inf')
 
    def max2(x, y): return x if x >= y else y
 
    N = int(input())
    As = [(A, i) for i, A in enumerate(map(int, input().split()))]
 
    As.sort(reverse=True)
 
    dp = [[-INF]*(N+1) for _ in range(N+1)]
    dp[0][0] = 0
    for no, (A, pos) in enumerate(As):
        for i in range(no+1):
            j = no-i
            dp[i+1][j] = max2(dp[i+1][j], dp[i][j] + A*abs(pos-i))
            dp[i][j+1] = max2(dp[i][j+1], dp[i][j] + A*abs(pos-(N-1-j)))
 
    ans = 0
    for i in range(N+1):
        j = N-i
        ans = max2(ans, dp[i][j])
 
    print(ans)
 
 
solve()