Contest Duration: - (local time) (100 minutes) Back to Home

Submission #7407118

Source Code Expand

Copy
```N = int(input())
Ps = list(map(int, input().split()))

iPs = [0] * (N+1)
for iP, A in enumerate(Ps):
iPs[A] = iP

ans = 0
iLs = list(range(N+1))
iRs = list(range(N+1))
for P in range(1, N+1):
i = iPs[P]
iL, iR = iLs[i], iRs[i]
if iL <= 1:
iL2 = 0
else:
iL2 = iLs[iL-1]
if iR >= N-2:
iR2 = N-1
else:
iR2 = iRs[iR+1]
num = 0
if iL != 0:
num += (iL-iL2)*(iR-i+1)
if iR != N-1:
num += (i-iL+1)*(iR2-iR)
ans += P * num
iLs[iR+1] = iL
iRs[iL-1] = iR

print(ans)
```

#### Submission Info

Submission Time 2019-09-07 23:31:30+0900 E - Second Sum ZollingerPython3 Python (3.4.3) 500 587 Byte AC 266 ms 19192 KB

#### Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
 AC × 3
 AC × 22
Set Name Test Cases
Sample 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt
All 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt, 01-small-01.txt, 01-small-02.txt, 01-small-03.txt, 01-small-04.txt, 01-small-05.txt, 02-large-01.txt, 02-large-02.txt, 02-large-03.txt, 02-large-04.txt, 02-large-05.txt, 03-max-01.txt, 03-max-02.txt, 04-min-01.txt, 05-sorted-01.txt, 05-sorted-02.txt, 06-almost-sorted-01.txt, 06-almost-sorted-02.txt, 06-almost-sorted-03.txt, 06-almost-sorted-04.txt
Case Name Status Exec Time Memory
00-sample-01.txt AC 18 ms 3064 KB
00-sample-02.txt AC 17 ms 3064 KB
00-sample-03.txt AC 17 ms 3064 KB
01-small-01.txt AC 17 ms 3064 KB
01-small-02.txt AC 18 ms 3064 KB
01-small-03.txt AC 18 ms 3064 KB
01-small-04.txt AC 18 ms 3064 KB
01-small-05.txt AC 18 ms 3064 KB
02-large-01.txt AC 232 ms 17004 KB
02-large-02.txt AC 146 ms 11792 KB
02-large-03.txt AC 234 ms 17752 KB
02-large-04.txt AC 239 ms 17816 KB
02-large-05.txt AC 155 ms 12632 KB
03-max-01.txt AC 266 ms 18940 KB
03-max-02.txt AC 261 ms 18944 KB
04-min-01.txt AC 18 ms 3064 KB
05-sorted-01.txt AC 174 ms 18252 KB
05-sorted-02.txt AC 188 ms 18948 KB
06-almost-sorted-01.txt AC 167 ms 17756 KB
06-almost-sorted-02.txt AC 179 ms 17440 KB
06-almost-sorted-03.txt AC 172 ms 19192 KB
06-almost-sorted-04.txt AC 176 ms 17704 KB