Problem p02709 (python)

Fixed submission:

def main():

  n = int(input())
  arr = map(int, input().split())
  arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))

  dp = [[0] * (n + 1) for _ in range(n + 1)]
  for i, (p, a) in enumerate(arr):
    for l in range(i + 1):
      dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + a * abs(p - (n - (i - l) - 1)))
      dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + a * abs(p - l))

  print(max(dp[n]))

main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict

sys.setrecursionlimit(4000)

n = int(input())
arr = map(int, input().split())
arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))

dp = {}
def cal(ln, rn):
  if ln + rn > n:
    return

  if ln > 0 or rn > 0:
    idx = ln + rn - 1
    p, a = arr[idx]
    val_l = dp[ln - 1, rn] + a * abs(p - (ln - 1)) if ln > 0 else 0
    val_r = dp[ln, rn - 1] + a * abs(p - (n - rn)) if rn > 0 else 0

    dp[ln, rn] = max(val_l, val_r)
  else:
    dp[ln, rn] = 0

  cal(ln + 1, rn)
  cal(ln, rn + 1)

cal(0, 0)

print(max([dp[i, n - i] for i in range(n + 1)]))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

n = int(input())
arr = map(int, input().split())
arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))

dp = defaultdict(int)

def cal(ln, rn):
  if ln + rn > n:
    return

  if ln > 0 or rn > 0:
    idx = ln + rn - 1
    p, a = arr[idx]
    val_l = dp[ln - 1, rn] + a * abs(p - (ln - 1)) if ln > 0 else 0
    val_r = dp[ln, rn - 1] + a * abs(p - (n - rn)) if rn > 0 else 0

    dp[ln, rn] = max(val_l, val_r)

  cal(ln + 1, rn)
  cal(ln, rn + 1)

cal(0, 0)

print(max([dp[i, n - i] for i in range(n + 1)]))

Buggy submission (verdict: Runtime Error):

n = int(input())
arr = tuple(map(int, input().split()))

def cal(i, dst):
  if i == -1:
    return 0

  result = None
  for j in dst:
    next_dst = [v for v in dst if v != j]
    val = arr[i] * abs(i - j) + cal(i - 1, next_dst)
    result = max(result, val) if result else val

  return result

print(cal(n - 1, [i for i in range(n)]))

Buggy submission (verdict: Runtime Error):
import copy

n = int(input())
arr = tuple(map(int, input().split()))


def cal(i, dst):
  if i == -1:
    return 0

  result = None
  for j in range(n):
    if dst[j]:
      continue
    dst[j] = i
    val = arr[i] * abs(i - j) + cal(i - 1, dst)
    dst[j] = None
    result = max(result, val) if result else val

  return result

print(cal(n - 1, [None for _ in range(n)]))

Buggy submission (verdict: Runtime Error):
import copy

n = int(input())
arr = tuple(map(int, input().split()))


def cal(i, dst):
  if i == -1:
    return 0

  results = []
  for j in range(n):
    if dst[j]:
      continue
    dst[j] = i
    val = arr[i] * abs(i - j)
    results.append(val + cal(i - 1, dst))
    dst[j] = None

  return max(results)

print(cal(n - 1, [None for _ in range(n)]))

Buggy submission (verdict: Runtime Error):
import copy

n = int(input())
arr = tuple(map(int, input().split()))


def cal(i, dst):
  if i == -1:
    return 0

  results = []
  for j in range(n):
    if dst[j]:
      continue
    next_dst = copy.deepcopy(dst)
    next_dst[j] = i
    val = arr[i] * abs(i - j)
    results.append(val + cal(i - 1, next_dst))

  return max(results)

print(cal(n - 1, [None for _ in range(n)]))