forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
34 lines (25 loc) · 609 Bytes
/
Copy pathMain.py
File metadata and controls
34 lines (25 loc) · 609 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
n, m = list(map(int, input().split(" ")))
h = [0] + list(map(int, input().split(" ")))
size = n
def down(u):
t = u
if u * 2 <= size and h[u * 2] < h[t]:
t = u * 2
if u * 2 + 1 <= size and h[u * 2 + 1] < h[t]:
t = u * 2 + 1
if t != u:
h[t], h[u] = h[u], h[t]
down(t)
def up(u):
while u // 2 > 0 and h[u // 2] > h[u]:
h[u // 2], h[u] = h[u], h[u // 2]
u //= 2
for i in range(n // 2, 0, -1):
down(i)
res = []
for i in range(m):
res.append(h[1])
h[1] = h[size]
size -= 1
down(1)
print(' '.join(list(map(str, res))))