Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a = [(a[i], i) for i in range(n)]
a.sort(key=lambda x: -x[0])
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]

for x in range(n+1):
    for y in range(n+1):
        if x + y > n:
            break
        active, idx = a[x+y-1]
        if x > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x-1][y] + active * abs(idx - (x-1)))

        if y > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x][y-1] + active * abs(idx - (n-y)))

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

print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a = [(a[i], i) for i in range(n)]
a.sort(key=lambda x: -x[0])
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]

for x in range(n+1):
    for y in range(n+1):
        if x + y > n:
            break
        active, idx = a[x+y-1]
        if x > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x-1][y] + active * abs(idx - (x-1)))

        if y > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x][y-1] + active * abs(idx - (n-y)))

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

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
s = [0 for _ in range(n)]

score = 0
for i in range(n):
    l = s.index(0)
    r = n - 1 -s[::-1].index(0)
    #right
    score_r, score_l = score, score
    sr, sl = s.copy(), s.copy()
    if r >= i:
        sr[r] = a[i]
        score_r += a[i] * (r - i)
        while r < n-1 and sr[r] > sr[r+1]:
            score_r += sr[r] - sr[r+1]
            sr[r], sr[r+1] = sr[r+1], sr[r]
            r += 1

    if l <= i:
        sl[l] = a[i]
        score_l += a[i] * (i - l)
        while l > 0 and sl[l-1] < sl[l]:
            score_l += sl[l] - sl[l-1]
            sl[l-1], sl[l] = sl[l], sl[l-1]
            l += 1

    if score_r >= score_l:
        score = score_r
        s = sr

    else:
        score = score_l
        s = sl


print(score)
#print(s)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a = [(a[i], i) for i in range(n)]
a.sort(key=lambda x: -x[0])
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]

for x in range(n+1):
    for y in range(n+1):
        if x + y > n:
            break
        active, idx = a[x+y-1]
        if x > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x-1][y] + active * abs(idx - (x-1)))

        if y > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x][y-1] + active * abs(idx - (n-y)))

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

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
s = [0 for _ in range(n)]

score = 0
for i in range(n):
    l = s.index(0)
    r = s[::-1].index(0)
    #right
    score_r, score_l = score, score
    sr, sl = s.copy(), s.copy()
    if r >= i:
        sr[r] = a[i]
        score_r += a[i] * (r - i)
        while r < n-1 and sr[r] > sr[r+1]:
            score_r += sr[r] - sr[r+1]
            sr[r], sr[r+1] = sr[r+1], sr[r]
            r += 1

    if l <= i:
        sl[l] = a[i]
        score_l += a[i] * (i - l)
        while l > 0 and sl[l-1] < sl[l]:
            score_l += sl[l] - sl[l-1]
            sl[l-1], sl[l] = sl[l], sl[l-1]
            l += 1

    if score_r >= score_l:
        score = score_r
        s = sr

    else:
        score = score_l
        s = sl


print(score)
#print(s)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a = [(a[i], i) for i in range(n)]
a.sort(key=lambda x: -x[0])
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]

for x in range(n+1):
    for y in range(n+1):
        if x + y > n:
            break
        active, idx = a[x+y-1]
        if x > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x-1][y] + active * abs(idx - (x-1)))

        if y > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x][y-1] + active * abs(idx - (n-y)))

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

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
s = [0 for _ in range(n)]

score = 0
for i in range(n):
    l = s.index(0)
    r = s[::-1].index(0)
    #right
    score_r, score_l = score, score
    if r >= i:
        sr = s.copy()
        sr[r] = a[i]
        score_r += a[i] * (r - i)
        while r < n-1 and sr[r] > sr[r+1]:
            score_r += sr[r] - sr[r+1]
            sr[r], sr[r+1] = sr[r+1], sr[r]
            r += 1

    if l <= i:
        sl = s.copy()
        sl[l] = a[i]
        score_l += a[i] * (i - l)
        while l > 0 and sl[l-1] < sl[l]:
            score_l += sl[l] - sl[l-1]
            sl[l-1], sl[l] = sl[l], sl[l-1]
            l += 1

    if score_r >= score_l:
        score = score_r
        s = sr

    else:
        score = score_l
        s = sl


print(score)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a = [(a[i], i) for i in range(n)]
a.sort(key=lambda x: -x[0])
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]

for x in range(n+1):
    for y in range(n+1):
        if x + y > n:
            break
        active, idx = a[x+y-1]
        if x > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x-1][y] + active * abs(idx - (x-1)))

        if y > 0:
            dp[x][y] = max(dp[x][y],
                           dp[x][y-1] + active * abs(idx - (n-y)))

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

print(ans)