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.
# Enter your code here. Read input from STDIN. Print output to STDOUTdeflongest_segment(A,L):n=len(A)C=[None]*(n+1)M=[None]*(n+1)M[0]=C[0]=x=y=0foriinrange(1,n+1):C[i]=C[i-1]+A[i-1]ifC[i-1]<C[M[i-1]]:M[i]=i-1else:M[i]=M[i-1]k=i-y+x-1whilek>0:ifC[i]-C[M[k]]>=L:k=M[k]else:breakx,y=k+1,ireturn0ify==0elsey-x+1defmax_prefix(s):m,t=0,0forvins:t+=vm=max(m,t)returnmdefnumeric(s):returnlist(map(lambdax:3ifx=='1'else-2,s))defsolve(k,s):n=len(s)s=numeric(s)d=sum(s)ifd>=0:returnk*nd=-dmh=max_prefix(s)mt=max_prefix(reversed(s))r=longest_segment(s,0)ifk>1:mult=min((mh+mt)// d, k - 2)r=max(r,mult*n+longest_segment(s*2,mult*d))returnrk,s=input().split()print(solve(int(k),s))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock Puzzle
You are viewing a single comment's thread. Return to all comments →
Python3 solution