Problem p02709 (python)

Fixed submission:
from collections import deque

N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]
d = deque([(0,0)])
dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
while(len(d)>0):
    i, j = d.pop()
    if i + j > N - 1:
        continue
    tmp = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - i)
    if tmp > dp[i+1][j]:
        if dp[i+1][j] == 0:
            d.appendleft((i+1,j))
        dp[i+1][j] = tmp
    tmp = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - N + j + 1)
    if tmp > dp[i][j+1]:
        if dp[i][j+1] == 0:
            d.appendleft((i,j+1))
        dp[i][j+1] = tmp
        
ans = -1
for i in range(N):
    for j in range(N):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

Buggy submission (verdict: Runtime Error):
def rec(i, j):
    flag = False
    flag_ = False
    tmp = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - i)
    if tmp > dp[i+1][j]:
        dp[i+1][j] = tmp
        flag = True
    tmp_ = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - N + j + 1)
    if tmp_ > dp[i][j+1]:
        dp[i][j+1] = tmp_ if tmp_ > dp[i][j+1] else dp[i][j+1]
        flag_ = True
    if i + j == N - 1:
        return
    if flag:
        rec(i + 1, j)
    if flag_:
        rec(i, j + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
rec(0, 0)
ans = -1
for i in range(N):
    for j in range(N):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

Buggy submission (verdict: Runtime Error):
def rec(i,j):
    if i + j == N:
        return
    tmp = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - i)
    if tmp > dp[i+1][j]:
        dp[i+1][j] = tmp
    tmp_ = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - N + j + 1)
    if tmp_ > dp[i][j+1]:
        dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
    if tmp > dp[i+1][j]:
        rec(i + 1, j)
    if tmp_ > dp[i][j+1]:
        rec(i, j + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)

def rec(i,j,k):
    if k == N or i == N - 1 or j == N - 1:
        return
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - i)
    if tmp > dp[i+1][j]:
        dp[i+1][j] = tmp
        rec(i + 1, j, k + 1)
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - N + j + 1)
    if tmp > dp[i][j+1]:
        dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
        rec(i, j + 1, k + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

Buggy submission (verdict: Runtime Error):
def rec(i,j,k):
    if k == N or i == N - 1 or j == N - 1:
        return
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - i)
    if tmp > dp[i+1][j]:
        dp[i+1][j] = tmp
        rec(i + 1, j, k + 1)
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - N + j + 1)
    if tmp > dp[i][j+1]:
        dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
        rec(i, j + 1, k + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

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

sys.setrecursionlimit(10000)
@lru_cache(maxsize = None)
def rec(i,j,k):
    if k == N or i == N - 1 or j == N - 1:
        return
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - i)
    dp[i+1][j] = tmp if tmp > dp[i+1][j] else dp[i+1][j]
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - N + j + 1)
    dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
    rec(i + 1, j, k + 1)
    rec(i, j + 1, k + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)

def rec(i,j,k):
    if k == N or i == N - 1 or j == N - 1:
        return
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - i)
    dp[i+1][j] = tmp if tmp > dp[i+1][j] else dp[i+1][j]
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - N + j + 1)
    dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
    rec(i + 1, j, k + 1)
    rec(i, j + 1, k + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)

Buggy submission (verdict: Runtime Error):
def rec(i,j,k):
    if k == N or i == N - 1 or j == N - 1:
        return
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - i)
    dp[i+1][j] = tmp if tmp > dp[i+1][j] else dp[i+1][j]
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - N + j + 1)
    dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
    rec(i + 1, j, k + 1)
    rec(i, j + 1, k + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)