from collections import defaultdict
def main(arr,k):
    
    # ps[j]-ps[i] max:==> ps[j]-ps[i] should be minimum
    
    n=len(arr)
    ps=[]
    
    for x in arr:
        if len(ps) == 0:
            ps.append(x)
        else:
            ps.append(x+ps[-1])
    
    hm=defaultdict(int)
    
    ans=0
    
    for j in range(n):
        p1= k + arr[j]
        p2= -1*k + arr[j]
        
        if p1 in hm:
            i=hm[p1]
            s= ps[j] if i==0 else ps[j]-ps[i-1]
            ans=max(ans,s)
        
        if p2 in hm:
            i=hm[p2]
            s= ps[j] if i==0 else ps[j]-ps[i-1]
            ans=max(ans,s)
        
        if arr[j] not in hm:
            hm[arr[j]]=j
        else:
            prevIndexPS=ps[hm[arr[j]]]
            currIndexPS=ps[j]
            if currIndexPS < prevIndexPS:
                hm[arr[j]]=j
        
    
    return ans
    

print(main([1,5,-5,8,8,8,10,15],5))