Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
p = list(map(int,input().split()))
q = [(v,i) for i,v in enumerate(p)]
q.sort(reverse=True)
flg = np.array([-1,-1,n,n])
ans = 0

for v,i in q:
    if flg.size==0:
        flg = np.append(flg,i)
        continue
    else:
        flg_arr = np.array(flg)
        flg_low = flg_arr[flg_arr<i]
        flg_upp = flg_arr[flg_arr>i]
        ans += v*(  (flg_low[-1]-flg_low[-2])*(flg_upp[0]-i)+
                    (flg_upp[1]-flg_upp[0])*(i-flg_low[-1]))
        flg = np.append(flg,i)
        flg = np.sort(flg)
print(ans)


Fixed submission:
#!/usr/bin/env python3
N=int(input())
p=list(map(int,input().split()))
LL = [0] + [i for i in range(N+1)]
RR = [i+1 for i in range(N+1)] + [N+1]
E = [(v, i+1) for i, v in enumerate(p)]
E.sort()
ans=0
for v, i in E:
  r0 = RR[i]
  r1 = RR[r0]
  l0 = LL[i]
  l1 = LL[l0]
  RR[l0] = r0
  LL[r0] = l0

  ans += ((r1 - r0)*(i-l0)+(r0-i)*(l0 - l1))*v

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
p = list(map(int,input().split()))
q = [(v,i) for i,v in enumerate(p)]
q.sort(reverse=True)
# print(q)
flg = [-1,-1,n,n]
ans = 0

for v,i in q:
    # print("(v,i)=({},{})".format(v,i))
    if not flg:
        flg.append(i)
        # print(flg)
        continue
    else:
        flg_arr = np.array(flg)
        flg_low = flg_arr[flg_arr<i]
        # print("flg_low:{}".format(flg_low))
        flg_upp = flg_arr[flg_arr>i]
        # print("flg_upp:{}".format(flg_upp))

        # if (flg_low.size >= 2) and (flg_upp.size >= 2):
        ans += v*(  (flg_low[-1]-flg_low[-2])*(flg_upp[0]-i)+
                    (flg_upp[1]-flg_upp[0])*(i-flg_low[-1]))
        # elif flg_low.size >= 2 and

        flg = list(flg_arr)
        flg.append(i)
        flg.sort()
        # print(flg)
print(ans)


Fixed submission:
#!/usr/bin/env python3
N=int(input())
p=list(map(int,input().split()))
LL = [0] + [i for i in range(N+1)]
RR = [i+1 for i in range(N+1)] + [N+1]
E = [(v, i+1) for i, v in enumerate(p)]
E.sort()
ans=0
for v, i in E:
  r0 = RR[i]
  r1 = RR[r0]
  l0 = LL[i]
  l1 = LL[l0]
  RR[l0] = r0
  LL[r0] = l0

  ans += ((r1 - r0)*(i-l0)+(r0-i)*(l0 - l1))*v

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# import numpy as np

# def second_largest(numbers):
#     count = 0
#     m1 = m2 = float('-inf')
#     for x in numbers:
#         count += 1
#         if x > m2:
#             if x >= m1:
#                 m1, m2 = x, m1            
#             else:
#                 m2 = x
#     return m2 if count >= 2 else None

def second_largest(q):
    q.sort()
    return q[-2]

    

n = int(input())
p = list(map(int,input().split()))

cnt = 0
for i in range(len(p)):
    for j in range(i+1,len(p)):
        cnt += second_largest(p[i:j+1])
print(cnt)


Fixed submission:
#!/usr/bin/env python3
N=int(input())
p=list(map(int,input().split()))
LL = [0] + [i for i in range(N+1)]
RR = [i+1 for i in range(N+1)] + [N+1]
E = [(v, i+1) for i, v in enumerate(p)]
E.sort()
ans=0
for v, i in E:
  r0 = RR[i]
  r1 = RR[r0]
  l0 = LL[i]
  l1 = LL[l0]
  RR[l0] = r0
  LL[r0] = l0

  ans += ((r1 - r0)*(i-l0)+(r0-i)*(l0 - l1))*v

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def second_largest(numbers):
    count = 0
    m1 = m2 = float('-inf')
    for x in numbers:
        count += 1
        if x > m2:
            if x >= m1:
                m1, m2 = x, m1            
            else:
                m2 = x
    return m2 if count >= 2 else None

n = int(input())
p = list(map(int,input().split()))

cnt = 0
for i in range(len(p)):
    for j in range(i+1,len(p)):
        cnt += second_largest(p[i:j+1])
print(cnt)

Fixed submission:
#!/usr/bin/env python3
N=int(input())
p=list(map(int,input().split()))
LL = [0] + [i for i in range(N+1)]
RR = [i+1 for i in range(N+1)] + [N+1]
E = [(v, i+1) for i, v in enumerate(p)]
E.sort()
ans=0
for v, i in E:
  r0 = RR[i]
  r1 = RR[r0]
  l0 = LL[i]
  l1 = LL[l0]
  RR[l0] = r0
  LL[r0] = l0

  ans += ((r1 - r0)*(i-l0)+(r0-i)*(l0 - l1))*v

print(ans)