Buggy python submission (verdict: Time Limit Exceeded):
import functools
import sys

sys.setrecursionlimit(10 ** 9)

input()

arr = list(sorted([(v, idx) for idx, v in enumerate(map(int, input().split()))], reverse=True))


@functools.lru_cache(maxsize=None)
def dp(l, r, idx):
    if idx == len(arr):
        return 0

    left = abs(arr[idx][1] - l) * arr[idx][0] + dp(l + 1, r, idx + 1)
    right = abs(arr[idx][1] - r) * arr[idx][0] + dp(l, r - 1, idx + 1)

    return max(left, right)


print(dp(0, len(arr) - 1, 0))


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

input()

arr = list(sorted([(v, idx) for idx, v in enumerate(map(int, input().split()))], reverse=True))

d = [[-1 for _ in range(len(arr))] for _ in range(len(arr))]


def dp(l, r, idx):
    if idx == len(arr):
        return 0

    if d[l][r] != -1:
        return d[l][r]

    left = abs(arr[idx][1] - l) * arr[idx][0] + dp(l + 1, r, idx + 1)
    right = abs(arr[idx][1] - r) * arr[idx][0] + dp(l, r - 1, idx + 1)

    d[l][r] = max(left, right)
    return max(left, right)


print(dp(0, len(arr) - 1, 0))

Buggy python submission (verdict: Runtime Error):
import functools

input()

arr = list(sorted([(v, idx) for idx, v in enumerate(map(int, input().split()))], reverse=True))


@functools.lru_cache(maxsize=None)
def dp(l, r, idx):
    if idx == len(arr):
        return 0

    left = abs(arr[idx][1] - l) * arr[idx][0] + dp(l + 1, r, idx + 1)
    right = abs(arr[idx][1] - r) * arr[idx][0] + dp(l, r - 1, idx + 1)

    return max(left, right)


print(dp(0, len(arr) - 1, 0))


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

input()

arr = list(sorted([(v, idx) for idx, v in enumerate(map(int, input().split()))], reverse=True))

d = [[-1 for _ in range(len(arr))] for _ in range(len(arr))]


def dp(l, r, idx):
    if idx == len(arr):
        return 0

    if d[l][r] != -1:
        return d[l][r]

    left = abs(arr[idx][1] - l) * arr[idx][0] + dp(l + 1, r, idx + 1)
    right = abs(arr[idx][1] - r) * arr[idx][0] + dp(l, r - 1, idx + 1)

    d[l][r] = max(left, right)
    return max(left, right)


print(dp(0, len(arr) - 1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 9)

input()
arr = [(v, idx) for idx, v in enumerate(map(int, input().split()))]

dp = [-1 for _ in range(len(arr))]


def maximize(global_idx, temp_arr):
    if len(temp_arr) == 0:
        return 0

    # if dp[global_idx] != -1:
    #     return dp[global_idx]

    mx = 0

    for idx, child in enumerate(temp_arr):
        v, local_idx = child
        mx = max(mx, abs(global_idx - local_idx) * v + maximize(global_idx + 1, temp_arr[:idx] + temp_arr[idx + 1:]))

    dp[global_idx] = mx
    return dp[global_idx]


print(maximize(0, arr[:]))


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

input()

arr = list(sorted([(v, idx) for idx, v in enumerate(map(int, input().split()))], reverse=True))

d = [[-1 for _ in range(len(arr))] for _ in range(len(arr))]


def dp(l, r, idx):
    if idx == len(arr):
        return 0

    if d[l][r] != -1:
        return d[l][r]

    left = abs(arr[idx][1] - l) * arr[idx][0] + dp(l + 1, r, idx + 1)
    right = abs(arr[idx][1] - r) * arr[idx][0] + dp(l, r - 1, idx + 1)

    d[l][r] = max(left, right)
    return max(left, right)


print(dp(0, len(arr) - 1, 0))

Buggy python submission (verdict: Runtime Error):
from functools import lru_cache

input()
arr = [(v, idx) for idx, v in enumerate(map(int, input().split()))]

dp = [-1 for _ in range(len(arr))]


def maximize(global_idx, temp_arr):
    if len(temp_arr) == 0:
        return 0

    # if dp[global_idx] != -1:
    #     return dp[global_idx]

    mx = 0

    for idx, child in enumerate(temp_arr):
        v, local_idx = child
        mx = max(mx, abs(global_idx - local_idx) * v + maximize(global_idx + 1, temp_arr[:idx] + temp_arr[idx + 1:]))

    dp[global_idx] = mx
    return dp[global_idx]


print(maximize(0, arr[:]))


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

input()

arr = list(sorted([(v, idx) for idx, v in enumerate(map(int, input().split()))], reverse=True))

d = [[-1 for _ in range(len(arr))] for _ in range(len(arr))]


def dp(l, r, idx):
    if idx == len(arr):
        return 0

    if d[l][r] != -1:
        return d[l][r]

    left = abs(arr[idx][1] - l) * arr[idx][0] + dp(l + 1, r, idx + 1)
    right = abs(arr[idx][1] - r) * arr[idx][0] + dp(l, r - 1, idx + 1)

    d[l][r] = max(left, right)
    return max(left, right)


print(dp(0, len(arr) - 1, 0))