Skip to content

Instantly share code, notes, and snippets.

@mym0404
Created May 3, 2022 04:20
Show Gist options
  • Save mym0404/c233ed31c9d57ea109c1abaa682ed8fd to your computer and use it in GitHub Desktop.
Save mym0404/c233ed31c9d57ea109c1abaa682ed8fd to your computer and use it in GitHub Desktop.
n = int(input())
h = [int(input()) for _ in range(n)]
h.append(10 ** 20)
n += 1
answer = 0
stack = []
for i in range(n):
while len(stack) and stack[-1][0] <= h[i]:
answer += i - stack[-1][1] - 1
stack.pop()
stack.append([h[i], i])
print(answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment