Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 15:28:15
Task:Buy Low, Sell High
Sender:El Numero Uno
Submission time:2018-05-26 15:28:15
Status:READY
Result:TIME LIMIT EXCEEDED

Show test data

Code

def parseM(m, v, reverse):
	n = []
	for pair in reversed(m):
		added = False
		if (v < pair[0] and not reverse) or (v > pair[0] and reverse):
			added = True
			n.append([v, v, 0])
			if pair[2] > 0:
				n.append(pair)
		if (v > pair[1] and not reverse) or (v < pair[1] and reverse):
			added = True
			n.append([pair[0], v, abs(v - pair[0])])
		if not added:
			n.append(pair)
		while len(n) > 1 and n[-1][2] <= n[0][2]:
			n.pop()
	return list(reversed(n))
	
 
def getAns(l, reverse=False):
	m = [[[l[0],l[0],0]]]
	for i in range(1, len(l)):
		v = l[i]
		m.append(parseM(m[-1], v, reverse))
	return m
			


input()
l = list(map(int, input().split()))
d1 = getAns(l)
d2 = list(reversed(getAns(list(reversed(l)), True)))
m = []
for i in range(len(l)):
	m.append(d1[i][0][2] + d2[i][0][2])
print(max(m))