Chief's bot is playing an old DOS-based game. There are N+1 buildings in the game - indexed from 0 to N and are placed left-to-right. It is guaranteed that building with index 0 will be of height 0 unit. For buildings with index i (i∈[1,N] ) height will be hi units.
At beginning Chief's bot is at building with index0 . At each step, bot jumps to next (right) building. Suppose bot is at kth building and his current energy is botEnergy , then in next step he will jump to (k+1)th building. He will gain/lose energy equal in amount to difference between hk+1 and botEnergy
At beginning Chief's bot is at building with index
- If
hk+1>botEnergy , then he will losehk+1−botEnergy units of energy. - Otherwise, he will gain
botEnergy−hk+1 units of energy.
Goal is to reach Nth building, and during the course bot should never have negative energy units. What should be the minimum units of energy with which bot should start to successfully complete the game?
Input Format
The first line contains integer N . Next line contains N space separated integers h1,h2, ⋯,hN representing the heights of the buildings.
Output Format
Print a single number representing minimum units of energy required to complete the game.
Solution [Python]
h = [int(x) for x in input().split(" ")]
i=0
Energy=0
r=len(h)
while 1:
botEnergy=Energy
for x in range(0,len(h)):
if(h[x]>botEnergy):
botEnergy=botEnergy-(h[x]-botEnergy)
else:
botEnergy=botEnergy+(botEnergy-h[x])
if(botEnergy<0):
break
if(botEnergy>0):
print(Energy)
break
else:
Energy=Energy+1
No comments:
Post a Comment