fork download
  1. def main(arr, k):
  2. # max element is k
  3. # x_harmless_element anchor harmless_element_y
  4.  
  5. x = 0
  6. y = 0
  7. anchor = 0
  8. ans = 0
  9.  
  10. n = len(arr)
  11.  
  12. for i in range(n):
  13. # 1. THE WALL
  14. if arr[i] > k:
  15. if anchor != 0:
  16. ans += (x + y + 1 + x * y)
  17. x = 0
  18. y = 0
  19. anchor = 0
  20.  
  21. else:
  22. # 2. THE ANCHOR
  23. if arr[i] == k:
  24. if anchor == 1: # Shift the old anchor
  25. ans += (x + y + 1 + x * y)
  26. x = x + 1 + y
  27. y = 0
  28. else: # First anchor lock-in
  29. anchor = 1
  30.  
  31. # 3. THE HARMLESS PADDING
  32. elif anchor == 0: # <-- Fixed 'else if' to 'elif'
  33. x += 1
  34. else:
  35. y += 1
  36.  
  37. # 4. FINAL TALLY (Protected!)
  38. if anchor != 0: # <-- Added protection check
  39. ans += (x + y + 1 + x * y)
  40.  
  41. return ans
Success #stdin #stdout 0.07s 14188KB
stdin
Standard input is empty
stdout
Standard output is empty