Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

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

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

dp = np.full((n, n), -1)
for i2 in range(n-1, -1, -1):
    a, i = a2[i2]

    for s in range(i2+1):
        if i2 == n-1:
            dp[i2][s] = a * abs(s-i)
        else:
            dp[i2][s] = max(dp[i2+1][s+1] + a * abs(s - i),
                            dp[i2+1][s] + a * abs(n-1-i2+s - i))

print(dp[0][0])

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

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

dp = [[-1] * n for _ in range(n)]
for i2 in range(n-1, -1, -1):
    a, i = a2[i2]

    for s in range(i2+1):
        if i2 == n-1:
            dp[i2][s] = a * abs(s-i)
        else:
            dp[i2][s] = max(dp[i2+1][s+1] + a * abs(s - i),
                            dp[i2+1][s] + a * abs(n-1-i2+s - i))

print(dp[0][0])

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
sys.setrecursionlimit(10**9)

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

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

dp = np.full((n, n, n), -1)


def solve(i2, s, e):
    if dp[i2][s][e] != -1:
        return dp[i2][s][e]

    a, i = a2[i2]

    if i2 == n-1:
        dp[i2][s][e] = a * abs(s-i)
        return dp[i2][s][e]

    dp[i2][s][e] = max(solve(i2+1, s+1, e) + a * abs(s - i),
                       solve(i2+1, s, e-1) + a * abs(e - i))

    return dp[i2][s][e]


print(solve(0, 0, n-1))


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

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

dp = [[-1] * n for _ in range(n)]
for i2 in range(n-1, -1, -1):
    a, i = a2[i2]

    for s in range(i2+1):
        if i2 == n-1:
            dp[i2][s] = a * abs(s-i)
        else:
            dp[i2][s] = max(dp[i2+1][s+1] + a * abs(s - i),
                            dp[i2+1][s] + a * abs(n-1-i2+s - i))

print(dp[0][0])

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
sys.setrecursionlimit(10**10)

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

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

dp = np.full((n, n, n), -1)


def solve(i2, s, e):
    if dp[i2][s][e] != -1:
        return dp[i2][s][e]

    a, i = a2[i2]

    if i2 == n-1:
        dp[i2][s][e] = a * abs(s-i)
        return dp[i2][s][e]

    dp[i2][s][e] = max(solve(i2+1, s+1, e) + a * abs(s - i),
                       solve(i2+1, s, e-1) + a * abs(e - i))

    return dp[i2][s][e]


print(solve(0, 0, n-1))


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

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

dp = [[-1] * n for _ in range(n)]
for i2 in range(n-1, -1, -1):
    a, i = a2[i2]

    for s in range(i2+1):
        if i2 == n-1:
            dp[i2][s] = a * abs(s-i)
        else:
            dp[i2][s] = max(dp[i2+1][s+1] + a * abs(s - i),
                            dp[i2+1][s] + a * abs(n-1-i2+s - i))

print(dp[0][0])

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
sys.setrecursionlimit(10**8)

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

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

dp = np.full((n, n, n), -1)


def solve(i2, s, e):
    if dp[i2][s][e] != -1:
        return dp[i2][s][e]

    a, i = a2[i2]

    if i2 == n-1:
        dp[i2][s][e] = a * abs(s-i)
        return dp[i2][s][e]

    dp[i2][s][e] = max(solve(i2+1, s+1, e) + a * abs(s - i),
                       solve(i2+1, s, e-1) + a * abs(e - i))

    return dp[i2][s][e]


print(solve(0, 0, n-1))


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

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

dp = [[-1] * n for _ in range(n)]
for i2 in range(n-1, -1, -1):
    a, i = a2[i2]

    for s in range(i2+1):
        if i2 == n-1:
            dp[i2][s] = a * abs(s-i)
        else:
            dp[i2][s] = max(dp[i2+1][s+1] + a * abs(s - i),
                            dp[i2+1][s] + a * abs(n-1-i2+s - i))

print(dp[0][0])