We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importosimportsysdefbest_total_score(config):N,V,P=configroutes=[(0,0)]#startstateisstep=1(implied),energy=0,accumulatedscore=0forstepinrange(1,N):#step=1throughstep=N-1:2cases,loseifenergybecomes0#all routes fail - will happen if P has 0 at beginning, or too many 0 in a row.ifnotroutes:print("all routes failed")returnNonethis_score=V[step-1]this_energy=P[step-1]# all "take score" cases which are not better than each other continue to not be better than each other# V can have - then the "take energy" branch loses immediately.ifthis_energy>0:#"take energy" option: energy becomes the same whatever it was before. so all states going to it become one, score is max of their scores.all_max_score=max([route_max_scorefor(energy,route_max_score)inroutes])takeEnergy=(this_energy-1,all_max_score)#then add "pick score" ones - only if they dont lose (e > 0) and are better than the "take energy" case.newRoutes=[(energy-1,route_max_score+this_score)for(energy,route_max_score)inroutesif(energy>this_energyor(energy>0androute_max_score+this_score>all_max_score))]newRoutes.append(takeEnergy)else:newRoutes=[(energy-1,route_max_score+this_score)for(energy,route_max_score)inroutesifenergy>0]routes=newRoutes#step = N: only the plus score case, no energy >0 checkthis_score=V[N-1]returnmax(route_max_scorefor(energy,route_max_score)inroutes)+this_scoredefrobot(vp):N=len(vp)V=[]P=[]for[vi,pi]invp:V.append(vi)P.append(pi)config=(N,V,P)returnbest_total_score(config)if__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')n=int(input())vp=[]for_inrange(n):vp.append(list(map(int,input().rstrip().split())))result=robot(vp)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Robot
You are viewing a single comment's thread. Return to all comments →