-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_task2.py
63 lines (43 loc) · 1.37 KB
/
sub_task2.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# import time
#Utilities start
class Container :
def __init__(self, state, answer) :
self.state = state
self.answer = answer
def deep_copy(selected_index, pr) :
return [pr[i] for i in range(len(pr)) if i != selected_index]
def hasher(arr) :
return "".join(map(str, arr))
def is_present(cache, ar_a) :
hashed_val = hasher(ar_a)
if hashed_val not in cache:
return False
return True
def dp(cache, pr) :
hashed_val = hasher(pr)
if is_present(cache, pr) :
return cache[hashed_val].answer
if len(pr) == 1 :
return pr[0]
answers = [-1]*len(pr)
for i in range(len(pr)) :
if i == 0 :
answers[i] = dp(cache, deep_copy(i, pr)) + (pr[i] * pr[i + 1])
elif i == (len(pr) - 1) :
answers[i] = dp(cache, deep_copy(i, pr)) + (pr[i] * pr[i - 1])
else :
answers[i] = dp(cache, deep_copy(i, pr)) + (pr[i] * pr[i + 1] * pr[i - 1])
ans = max(answers)
cache[hashed_val] = Container(pr, ans)
return ans
#Utilities end
#code starts executing
_tc = int(input())
for cs in range(_tc) :
n = int(input())
prices = list(map(int, input().split()))
cache = dict()
# start_time = time.time()
ans = dp(cache, prices)
print("Case " + str(cs + 1) + ": " + str(ans))
# print("--- %s seconds ---" % (time.time() - start_time))