Submission #16085278
Source Code Expand
N = int(input())
a = list(map(int, input().split()))
left = [0] * (N + 1)
right = [0] * (N + 1)
# 左から
buf = list()
for i in range(N):
while len(buf) > 0 and a[buf[-1]] > a[i]:
buf.pop()
if len(buf) > 0:
left[i] = buf[-1]
else:
left[i] = -1
buf.append(i)
# 右から
buf = list()
for i in reversed(range(N)):
while len(buf) > 0 and a[buf[-1]] > a[i]:
buf.pop()
if len(buf) > 0:
right[i] = buf[-1]
else:
right[i] = N
buf.append(i)
ans = 0
for i in range(N):
ans += a[i] * (i - left[i]) * (right[i] - i)
print(ans)
Submission Info
| Submission Time | |
|---|---|
| Task | B - Minimum Sum |
| User | nebocco |
| Language | Python (3.8.2) |
| Score | 400 |
| Code Size | 638 Byte |
| Status | AC |
| Exec Time | 393 ms |
| Memory | 30856 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0, example1, example2 |
| All | corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2 |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| corner0 | AC | 311 ms | 30856 KiB |
| corner1 | AC | 318 ms | 30744 KiB |
| corner2 | AC | 24 ms | 9036 KiB |
| corner3 | AC | 389 ms | 30788 KiB |
| example0 | AC | 29 ms | 9172 KiB |
| example1 | AC | 25 ms | 9168 KiB |
| example2 | AC | 22 ms | 9136 KiB |
| maxrand0 | AC | 376 ms | 30788 KiB |
| maxrand1 | AC | 393 ms | 30628 KiB |
| maxrand2 | AC | 365 ms | 30632 KiB |
| rand0 | AC | 26 ms | 9164 KiB |
| rand1 | AC | 22 ms | 9140 KiB |
| rand2 | AC | 22 ms | 8912 KiB |