Skip to content

Commit fa90bcb

Browse files
Merge pull request #727 from gmlrude/main
[박희경] 115차 라이브 코테 제출
2 parents 16f4225 + 3c7f138 commit fa90bcb

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
3+
input = sys.stdin.readline
4+
5+
n = int(input())
6+
7+
cnt = 0
8+
stack = [int(input())]
9+
max_num = stack[-1]
10+
for _ in range(n - 1):
11+
num = int(input())
12+
if stack[-1] < num:
13+
cnt += num - stack[-1]
14+
max_num = max(max_num, num)
15+
stack.pop()
16+
stack.append(num)
17+
cnt += max_num * len(stack) - sum(stack)
18+
print(cnt)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
4+
input = sys.stdin.readline
5+
6+
n = int(input())
7+
a = list(map(int, input().split()))
8+
9+
stack = []
10+
res = [-1] * n
11+
for i in range(n - 1, -1, -1): # 역순으로
12+
while stack and stack[-1] <= a[i]:
13+
stack.pop()
14+
15+
if stack:
16+
res[i] = stack[-1]
17+
18+
stack.append(a[i])
19+
20+
print(*res)
21+
22+
23+
"""
24+
4
25+
3 5 2 7
26+
"""

0 commit comments

Comments
 (0)