Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
import sys
sys.setrecursionlimit(10**5)

def f(x, po, h, t, ple):
    if po=='h':
        ple+=B[x][1]*abs(B[x][0]-h)
        h+=1
    else:
        ple+=B[x][1]*abs(B[x][0]-t)
        t-=1
    if h>t:
        return ple
    
    ans1=f(x+1, 'h', h, t, ple)
    ans2=f(x+1, 't', h, t, ple)
    return max(ans1, ans2)

N=int(input())
A=list(map(int, input().split()))
B=[[i, A[i]] for i in range(N)]
B=sorted(B, key=itemgetter(1), reverse=True)
h=0
t=N-1
ans1=f(0, 'h', h, t, 0)
ans2=f(0, 't', h, t, 0)
print(max(ans1, ans2))

Fixed submission:
from operator import itemgetter
N=int(input())
A=list(map(int, input().split()))
B=[[i, A[i]] for i in range(N)] 
B=sorted(B, key=itemgetter(1), reverse=True)

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

for x in range(N):
    dp[x+1][0]=dp[x][0]+B[x][1]*abs(B[x][0]-x)
    
for y in range(N):
    dp[0][y+1]=dp[0][y]+B[y][1]*abs(N-1-y-B[y][0])
    
for z in range(2, N+1):
    for x in range(1, z):
        y=z-x
        dp[x][y]=max(dp[x][y-1]+B[z-1][1]*abs(N-y-B[z-1][0]), dp[x-1][y]+B[z-1][1]*abs(x-1-B[z-1][0]))
        
print(max([dp[x][N-x] for x in range(N+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
N=int(input())
A=list(map(int, input().split()))
B=[[i, A[i]] for i in range(N)] 
B=sorted(B, key=itemgetter(1), reverse=True)

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

for x in range(N):
    dp[x+1][0]=dp[x][0]+B[x][1]*abs(B[x][0]-x)
    
for y in range(N):
    dp[0][y+1]=dp[0][y]+B[y][1]*abs(N-1-y-B[y][0])
    
for z in range(2, N+1):
    for x in range(1, z):
        y=z-x
        dp[x][y]=max(dp[x][y-1]+B[z-1][1]*abs(N-y-B[z-1][0]), dp[x-1][y]+B[z-1][1]*abs(x-1-B[z-1][0]))
        
print(max([dp[x][N-x] for x in range(N+1)]))

Fixed submission:
from operator import itemgetter
N=int(input())
A=list(map(int, input().split()))
B=[[i, A[i]] for i in range(N)] 
B=sorted(B, key=itemgetter(1), reverse=True)

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

for x in range(N):
    dp[x+1][0]=dp[x][0]+B[x][1]*abs(B[x][0]-x)
    
for y in range(N):
    dp[0][y+1]=dp[0][y]+B[y][1]*abs(N-1-y-B[y][0])
    
for z in range(2, N+1):
    for x in range(1, z):
        y=z-x
        dp[x][y]=max(dp[x][y-1]+B[z-1][1]*abs(N-y-B[z-1][0]), dp[x-1][y]+B[z-1][1]*abs(x-1-B[z-1][0]))
        
print(max([dp[x][N-x] for x in range(N+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
import sys
sys.setrecursionlimit(10**5)

def f(x, po, h, t, ple):
    if po=='h':
        ple+=B[x][1]*abs(B[x][0]-h)
        h+=1
    else:
        ple+=B[x][1]*abs(B[x][0]-t)
        t-=1
    if h>t:
        return ple
    
    ans1=f(x+1, 'h', h, t, ple)
    ans2=f(x+1, 't', h, t, ple)
    return max(ans1, ans2)

N=int(input())
A=list(map(int, input().split()))
B=[[i, A[i]] for i in range(N)]
B=sorted(B, key=itemgetter(1), reverse=True)
h=0
t=N-1
ans1=f(0, 'h', h, t, 0)
ans2=f(0, 't', h, t, 0)
print(max(ans1, ans2))

Fixed submission:
from operator import itemgetter
N=int(input())
A=list(map(int, input().split()))
B=[[i, A[i]] for i in range(N)] 
B=sorted(B, key=itemgetter(1), reverse=True)

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

for x in range(N):
    dp[x+1][0]=dp[x][0]+B[x][1]*abs(B[x][0]-x)
    
for y in range(N):
    dp[0][y+1]=dp[0][y]+B[y][1]*abs(N-1-y-B[y][0])
    
for z in range(2, N+1):
    for x in range(1, z):
        y=z-x
        dp[x][y]=max(dp[x][y-1]+B[z-1][1]*abs(N-y-B[z-1][0]), dp[x-1][y]+B[z-1][1]*abs(x-1-B[z-1][0]))
        
print(max([dp[x][N-x] for x in range(N+1)]))