forked from anubhab91/CodeForces-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximums.py
More file actions
54 lines (34 loc) · 1.14 KB
/
Maximums.py
File metadata and controls
54 lines (34 loc) · 1.14 KB
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
__author__ = 'Devesh Bajpai'
'''
https://codeforces.com/problemset/problem/1326/B
Solution: Let's solve this mathematically.
x_0 = 0
b_0 = a_0 - x_0
=> b_0 + x_0 = a_0
=> b_0 = a_0
b_1 = a_1 - x_1
=> b_1 + x_1 = a_1
=> b_1 + max(0,a_0) = a_1
b_2 = a_2 - x_2
=> b_2 + x_2 = a_2
=> b_2 + max(0,a_0,a_1) = a_2
Hence extrapolating it further, for ith term in the series, we have
b_i + max(0,a_0,a_1,....a_(i-1)) = a_i
Hence the current value of x would be maximum of all the values of a from 0 to i-1. That can be kept as a running
variable. Once a_i is calculated, the current value of x can be updated by checking its value against a_i and picking
the maximum.
Return the string version of the list (separated by spaces)
'''
def solve(n, b):
a = [None] * n
a[0] = b[0]
curr_x = max(0, a[0])
for i in xrange(1,n):
a_i = b[i] + curr_x
a[i] = a_i
curr_x = max(curr_x,a_i)
return ' '.join(str(a_i) for a_i in a) #join requires an iterable, hence need to loop and convert each element as string
if __name__ == "__main__":
n = int(raw_input())
b = map(int, raw_input().split(" "))
print solve(n, b)