Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
B = [[int(x), i] for i, x in enumerate(input().split())]
B.sort()
C = [0] * N
cnt = 0
def lsearch(s, t):
  if s > t:
    return -1
  if s == t:
    return s if C[s] == 1 else -1
  else:
    m = (s + t) // 2
    return max(lsearch(s, m), lsearch(m + 1, t))
def rsearch(s, t):
  if s > t:
    return len(C)
  if s == t:
    return s if C[s] == 1 else len(C)
  else:
    m = (s + t) // 2
    return min(rsearch(s, m), rsearch(m + 1, t))
for b, i in B:
  l = i - lsearch(0, i - 1)
  r = rsearch(i + 1, N - 1) - i
  C[i] = 1
  cnt += b * l * r
print(cnt)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = [0] * (N + 1)
for i in range(N):
  B[A[i]] = i + 1
L = list(range(N + 2))
R = list(range(N + 2))
cnt = 0
for i in range(N, 0, -1):
  l = L[B[i]]
  r = R[B[i]]
  cnt += i * (B[i] - l + 1) * (r - B[i] + 1)
  L[r + 1] = l
  R[l - 1] = r
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(x) for x in input().split()]
B = [[A[i], i] for i in range(N)]
B.sort()
C = [0] * N
cnt = 0
def lsearch(s, t, C):
  if s > t:
    return -1
  if s == t:
    return s if C[s] == 1 else -1
  else:
    m = (s + t) // 2
    return max(lsearch(s, m, C), lsearch(m + 1, t, C))
def rsearch(s, t, C):
  if s > t:
    return len(C)
  if s == t:
    return s if C[s] == 1 else len(C)
  else:
    m = (s + t) // 2
    return min(rsearch(s, m, C), rsearch(m + 1, t, C))
for b, i in B:
  l = i - lsearch(0, i - 1, C)
  r = rsearch(i + 1, N - 1, C) - i
  C[i] = 1
  cnt += b * l * r
print(cnt)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = [0] * (N + 1)
for i in range(N):
  B[A[i]] = i + 1
L = list(range(N + 2))
R = list(range(N + 2))
cnt = 0
for i in range(N, 0, -1):
  l = L[B[i]]
  r = R[B[i]]
  cnt += i * (B[i] - l + 1) * (r - B[i] + 1)
  L[r + 1] = l
  R[l - 1] = r
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(x) for x in input().split()]
B = [[A[i], i] for i in range(N)]
B.sort()
C = [0] * N
cnt = 0
def lsearch(s, t, C):
  if s > t:
    return -1
  if s == t:
    return s if C[s] == 1 else -1
  else:
    m = (s + t) // 2
    return max(lsearch(s, m, C), lsearch(m + 1, t, C))
def rsearch(s, t, C):
  if s > t:
    return len(C)
  if s == t:
    return s if C[s] == 1 else len(C)
  else:
    m = (s + t) // 2
    return min(rsearch(s, m, C), rsearch(m + 1, t, C))
for b, i in B:
  l = i - lsearch(0, i - 1, C)
  r = rsearch(i + 1, N - 1, C) - i
  C[i] = 1
  cnt += b * l * r
print(cnt)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = [0] * (N + 1)
for i in range(N):
  B[A[i]] = i + 1
L = list(range(N + 2))
R = list(range(N + 2))
cnt = 0
for i in range(N, 0, -1):
  l = L[B[i]]
  r = R[B[i]]
  cnt += i * (B[i] - l + 1) * (r - B[i] + 1)
  L[r + 1] = l
  R[l - 1] = r
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N = int(input())
A = [int(x) for x in input().split()]
B = [[A[i], i] for i in range(N)]
B.sort()
C = []
cnt = 0
for b, i in B:
  p = bisect.bisect(C, i)
  l = (i - C[p - 1]) if p else (i + 1)
  r = (C[p] - i) if p < len(C) else (N - i)
  C = C[:p] + [i] + C[p:]
  cnt += b * l * r
print(cnt)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = [0] * (N + 1)
for i in range(N):
  B[A[i]] = i + 1
L = list(range(N + 2))
R = list(range(N + 2))
cnt = 0
for i in range(N, 0, -1):
  l = L[B[i]]
  r = R[B[i]]
  cnt += i * (B[i] - l + 1) * (r - B[i] + 1)
  L[r + 1] = l
  R[l - 1] = r
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N = int(input())
A = [int(x) for x in input().split()]
B = [[A[i], i] for i in range(N)]
B.sort()
C = []
cnt = 0
for b, i in B:
  p = bisect.bisect(C, i)
  l = (i - C[p - 1]) if p else (i + 1)
  r = (C[p] - i) if p < len(C) else (N - i)
  C = C[:p] + [i] + C[p:]
  cnt += b * l * r
print(cnt)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = [0] * (N + 1)
for i in range(N):
  B[A[i]] = i + 1
L = list(range(N + 2))
R = list(range(N + 2))
cnt = 0
for i in range(N, 0, -1):
  l = L[B[i]]
  r = R[B[i]]
  cnt += i * (B[i] - l + 1) * (r - B[i] + 1)
  L[r + 1] = l
  R[l - 1] = r
print(cnt)