-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.py
More file actions
31 lines (26 loc) · 926 Bytes
/
Copy pathtriangle.py
File metadata and controls
31 lines (26 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# https://codeforces.com/contest/2074/problem/G
def _triangle(a):
_dp = [[0 for _ in range(len(a))] for _ in range(len(a))]
for k in range(2, len(a)):
for i in range(len(a) - k):
_max = 0
for j in range(i + 1, i + k):
_cost = (
a[i] * a[i + k] * a[j] + _dp[i + 1][j - 1] + _dp[j + 1][i + k - 1]
)
if _cost > _max:
_max = _cost
_cost = _dp[i][j - 1] + _dp[j][i + k]
if _cost > _max:
_max = _cost
_cost = _dp[i][i + k - 1] + _dp[i + k][i + k]
if _cost > _max:
_max = _cost
_dp[i][i + k] = _max
return _dp[0][-1]
q = int(input().strip())
for q_itr in range(q):
_ = input().rstrip()
a = tuple([int(x) for x in input().rstrip().split()])
result = _triangle(a)
print(result)