Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

You are given an integer $K$ and a random permutation ― sequence $A_{1}, A_{2}, \ldots, A_{N}$ of integers $1$ through $N$. You have to perform the following process:
1. Split the permutation $A$ into $K$ non-empty contiguous subsequences $S_{1}, S_{2}, \dots, S_{K}$ such that each element of $A$ appears in exactly one of them.
2. Choose an arbitrary permutation $P$ of the integers $1$ through $K$.
3. Create a new permutation $B_{1}, B_{2}, \ldots, B_{N}$ by concatenating the sequences $S_{P_{1}}, S_{P_{2}}, \ldots, S_{P_{K}}$ in this order.

You are allowed to split the permutation $A$ into $K$ subsequences in any valid way and choose any permutation $P$ you want. Your goal is to make the length of the longest increasing subsequence of the permutation $B$ as large as possible.

------  Input ------
The first line of the input contains two space-separated integers $N$ and $K$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.

------  Output ------
Print a single line containing $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$.

------  Constraints ------
$5 ≤ N ≤ 5 \cdot 10^{5}$
$3 ≤ K ≤ N$
$1 ≤ A_{i} ≤ N$ for each valid $i$
$A_{i} \neq A_{j}$ for each valid $i, j$ ($i \neq j$)

------  Scoring ------
In each test case (and therefore each test file), let $L_{A}$ and $L_{B}$ be the lengths of the longest increasing subsequences of the permutations $A$ and $B$ respectively. If $L_{B} ≤ L_{A}$, your submission will receive the Wrong Answer verdict. Otherwise, your score for this test case is $(L_{B} - L_{A}) \cdot W$, where $W$ is a parameter of the grader. The values of $W$ for all tests are specified in the Test Generation section. The score of a submission is equal to the sum of its scores over all test cases. The goal is to maximise the score of your submission.

There are 18 test files. During the contest, the displayed score will account for exactly five test files (belonging to groups 1, 3, 5, 7, 9 from the test generation section), i.e. your score reflects your submission's performance on roughly 28% (5 / 18) of the test files. However, if your program gets a non-AC verdict on any test file, your submission's verdict will be non-AC. In other words, an AC verdict denotes that your program runs successfully on all the test files. After the end of the contest, your score will be changed to include the sum of your program's scores over the other $13$ test files.

------  Example Input ------

7 3
5 3 7 1 2 6 4

------  Example Output ------

1 2 6 4 5 3 7

------  Explanation ------
The permutation $B = (1, 2, 6, 4, 5, 3, 7)$ could be obtained from $A = (5, 3, 7, 1, 2, 6, 4)$ in the following way:
1. Choose subsequences $S_{1} = (5, 3)$, $S_{2} = (7)$, $S_{3} = (1, 2, 6, 4)$.
2. Choose the permutation $P = (3, 1, 2)$.
3. Concatenate $S_{P_{1}}, S_{P_{2}}, S_{P_{3}} = S_{3}, S_{1}, S_{2} = (1, 2, 6, 4), (5, 3), (7)$.

The longest increasing subsequence of $A$ is $(1, 2, 6)$ and its length is $L_{A} = 3$. The longest increasing subsequence of $B$ is $(1, 2, 4, 5, 7)$ and its length is $L_{B} = 5$. If $W = 1$, the final score of this solution on this test case is $L_{B} - L_{A} = 5 - 3 = 2$.

------  Test generation ------
There are nine test groups. In each test group, there are two test cases; the values of $N$, $K$ and $W$ (the scoring coefficient) are listed in the table below. In each test case, the permutation $A$ is chosen uniformly randomly among all $N!-1$ permutations other than $1, 2, \ldots, N-1, N$.

Group
N
K
W
    
1
5 · 10^{3}
50
8284
    
2
5 · 10^{3}
140
4745
    
3
5 · 10^{3}
400
2124
    
4
5 · 10^{4}
150
2676
    
5
5 · 10^{4}
450
1484
    
6
5 · 10^{4}
1500
580
    
7
5 · 10^{5}
500
828
    
8
5 · 10^{5}
1400
474
    
9
5 · 10^{5}
5000
175

Difficulty: VERY_HARD

Solutions:
import sys

def input():
	return sys.stdin.readline().strip()

def array_lele():
	return list(map(int, sys.stdin.readline().strip().split()))

def integers_lele():
	return map(int, sys.stdin.readline().strip().split())

def CeilIndex(v, l, r, key):
	while r - l > 1:
		m = (l + r) // 2
		if v[m] >= key:
			r = m
		else:
			l = m
	return r

def LIS(v):
	if len(v) == 0:
		return 0
	tail = [0] * len(v)
	length = 1
	tail[0] = v[0]
	for i in range(1, len(v)):
		if v[i] < tail[0]:
			tail[0] = v[i]
		elif v[i] > tail[length - 1]:
			tail[length] = v[i]
			length += 1
		else:
			tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i]
	return length
(N, K) = integers_lele()
Arr = array_lele()
Arr2 = reversed(Arr)
if N == K:
	result = sorted(Arr)
	print(*result)
	exit()
result = []
i = 0
count = 0
while i < N and count < K:
	j = i
	new = []
	new.append(Arr[j])
	j = j + 1
	while j < N and Arr[j] > Arr[j - 1]:
		new.append(Arr[j])
		j = j + 1
	i = j
	count = count + 1
	result.append(new)
while i < N:
	result[-1].append(Arr[i])
	i = i + 1
answer1 = result.copy()
answer1.sort()
normal = []
answer2 = result.copy()
answer2.sort(key=lambda z: z[-1])
ordinary = []
for i in answer1:
	for j in i:
		normal.append(j)
for i in answer2:
	for j in i:
		ordinary.append(j)
lis1 = LIS(normal)
lis2 = LIS(ordinary)
maxi = 0
if lis1 > lis2:
	maxi = lis1
	answer = normal
else:
	maxi = lis2
	answer = ordinary
result = []
i = 0
count = 0
while i < N and count < K:
	j = i
	new = []
	new.append(Arr[j])
	j = j + 1
	while j < N and Arr[j] < Arr[j - 1]:
		new.append(Arr[j])
		j = j + 1
	i = j
	count = count + 1
	result.append(new)
while i < N:
	result[-1].append(Arr[i])
	i = i + 1
answer1 = result.copy()
answer1.sort()
normal = []
answer2 = result.copy()
answer2.sort(key=lambda z: z[-1])
ordinary = []
for i in answer1:
	for j in i:
		normal.append(j)
for i in answer2:
	for j in i:
		ordinary.append(j)
lis1 = LIS(normal)
lis2 = LIS(ordinary)
if lis1 > lis2:
	second = lis1
	answer10 = normal
else:
	second = lis2
	answer10 = ordinary
if maxi > second:
	print(*answer)
else:
	print(*answer10)


from bisect import bisect, insort_left
(n, k) = map(int, input().split())
K = k
data = list(map(int, input().split()))
inlis = [0] * n

def upperbound(data, T, key, l, r):
	while r - l > 1:
		m = l + (r - l) // 2
		if data[T[m]] >= key:
			r = m
		else:
			l = m
	return r

def ubound(tails, data, l, r, s):
	if s >= data[tails[r]]:
		return r + 1
	elif s < data[tails[l]]:
		return 0
	if r - l < 2:
		return r
	mid = (l + r) // 2
	if data[tails[mid]] <= s:
		ans = ubound(tails, data, mid, r, s)
	else:
		ans = ubound(tails, data, l, mid, s)
	return ans

def lislen(data, x=0, y=10 ** 9):
	data = [i for i in data if x <= i <= y]
	tails = [0 for i in range(len(data))]
	index = 1
	for i in range(1, len(data)):
		if data[i] < data[tails[0]]:
			tails[0] = i
		elif data[i] > data[tails[index - 1]]:
			tails[index] = i
			index += 1
		else:
			pos = ubound(tails, data, 0, index - 1, data[i])
			tails[pos] = i
	return index

def lismark(data, inlis):
	tails = [0 for i in range(len(data))]
	prevs = [-1 for i in range(len(data))]
	index = 1
	for i in range(1, len(data)):
		if data[i] < data[tails[0]]:
			tails[0] = i
		elif data[i] >= data[tails[index - 1]]:
			prevs[i] = tails[index - 1]
			tails[index] = i
			index += 1
		else:
			pos = upperbound(data, tails, data[i], -1, index - 1)
			prevs[i] = tails[pos - 1]
			tails[pos] = i
	i = tails[index - 1]
	imax = i
	imin = 0
	while i >= 0:
		if data[i] < data[imin]:
			imin = i
		inlis[i] = 1
		i = prevs[i]
	return (index, imax, imin)
if n == k:
	print(*sorted(data))
else:
	(olis, imax, imin) = lismark(data, inlis)
	ans = []
	if olis != n:
		if k > 3 or imax != n - 1 or imin != 0:
			process = []
			used = [0] * n
			index = 0
			while k > 2 and index < imin:
				process.append(data[index])
				used[index] = 1
				index += 1
				k -= 2
			index = n - 1
			while k > 2 and index > imax:
				process.append(data[index])
				used[index] = 1
				index -= 1
				k -= 2
			index = 0
			while k > 3 and index < imax:
				if not inlis[index] and (not used[index]):
					process.append(data[index])
					used[index] = 1
					k -= 3
				index += 1
			process.sort()
			a = 0
			b = 0
			while a < len(process) and b < len(data):
				if used[b]:
					b += 1
					continue
				while a < len(process) and inlis[b] and (data[b] >= process[a]):
					ans.append(process[a])
					a += 1
				ans.append(data[b])
				b += 1
			while b < len(data):
				if used[b]:
					b += 1
					continue
				ans.append(data[b])
				b += 1
			while a < len(process):
				ans.append(process[a])
				a += 1
		else:
			index = 0
			segs = []
			mic = mac = 0
			for i in range(n):
				if inlis[i]:
					if index != i:
						segs.append((index, i - 1, mic, mac))
					index = i + 1
					mic = mac = 0
				else:
					if data[i] <= data[imin]:
						mic += 1
					if data[i] >= data[imax]:
						mac += 1
			gain = 0
			iseg = 0
			inStart = True
			for i in range(len(segs)):
				if segs[i][2] > gain:
					gain = segs[i][2]
					iseg = i
					inStart = True
				if segs[i][3] > gain:
					gain = segs[i][3]
					iseg = i
					inStart = False
			if inStart:
				ans.extend(data[segs[iseg][0]:segs[iseg][1] + 1])
			ans.extend(data[:segs[iseg][0]])
			ans.extend(data[segs[iseg][1] + 1:])
			if not inStart:
				ans.extend(data[segs[iseg][0]:segs[iseg][1] + 1])
	nlis = lislen(ans)
	ns1lis = K - 1 + lislen(data[K - 1:], max(data[:K - 1]))
	ns2lis = K - 1 + lislen(data[K - 1:], y=min(data[:K - 1]))
	ne1lis = K - 1 + lislen(data[:-K + 1], max(data[-K + 1:]))
	ne2lis = K - 1 + lislen(data[:-K + 1], y=min(data[-K + 1:]))
	if nlis < ns1lis:
		nlis = ns1lis
		ans = sorted(data[:K - 1]) + data[K - 1:]
	if nlis < ns2lis:
		nlis = ns2lis
		ans = data[K - 1:] + sorted(data[:K - 1])
	if nlis < ne1lis:
		nlis = ne1lis
		ans = sorted(data[-K + 1:]) + data[:-K + 1]
	if nlis < ne2lis:
		nlis = ne2lis
		ans = data[:-K + 1] + sorted(data[-K + 1:])
	used = [0] * n
	(i, j) = (0, n - 1)
	add = []
	k = K
	while k > 1 and i < n and (j >= 0):
		if used[i]:
			i += 1
			continue
		if used[j]:
			j -= 1
			continue
		if data[i] <= data[j]:
			add.append(data[i])
			used[i] = 1
			i += 1
		else:
			add.append(data[j])
			used[j] = 1
			j -= 1
		k -= 1
	mi1 = sorted(add) + [data[i] for i in range(n) if not used[i]]
	mi2 = [data[i] for i in range(n) if not used[i]] + sorted(add)
	mi1lis = lislen(mi1)
	mi2lis = lislen(mi2)
	if nlis < mi1lis:
		nlis = mi1lis
		ans = mi1
	if nlis < mi2lis:
		nlis = mi2lis
		ans = mi2
	used = [0] * n
	(i, j) = (0, n - 1)
	add = []
	k = K
	while k > 1 and i < n and (j >= 0):
		if used[i]:
			i += 1
			continue
		if used[j]:
			j -= 1
			continue
		if data[i] >= data[j]:
			add.append(data[i])
			used[i] = 1
			i += 1
		else:
			add.append(data[j])
			used[j] = 1
			j -= 1
		k -= 1
	ma1 = [data[i] for i in range(n) if not used[i]] + sorted(add)
	ma2 = sorted(add) + [data[i] for i in range(n) if not used[i]]
	ma1lis = lislen(ma1)
	ma2lis = lislen(ma2)
	if nlis < ma1lis:
		nlis = ma1lis
		ans = ma1
	if nlis < ma2lis:
		nlis = ma2lis
		ans = ma2
	if n <= 5000:
		sdata = sorted(data)
		k = K
		used = [0] * n
		i = j = 0
		while k > 2:
			for x in range(n):
				if not used[x] and data[x] == sdata[j]:
					used[x] = 1
					j += 1
					i = x + 1
					break
			while i < n and j < n and (data[i] == sdata[j]) and (not used[i]):
				used[i] = 1
				i += 1
				j += 1
			k -= 2
		d1 = sdata[:j] + [data[i] for i in range(n) if not used[i]]
		d1lis = lislen(d1)
		if nlis < d1lis:
			nlis = d1lis
			ans = d1
		k = K
		used = [0] * n
		i = j = n - 1
		while k > 2:
			for x in range(n):
				if not used[x] and data[x] == sdata[j]:
					used[x] = 1
					j -= 1
					i = x - 1
					break
			while i >= 0 and j >= 0 and (data[i] == sdata[j]) and (not used[i]):
				used[i] = 1
				i -= 1
				j -= 1
			k -= 2
		d2 = [data[i] for i in range(n) if not used[i]] + sdata[j + 1:]
		d2lis = lislen(d2)
		if nlis < d2lis:
			nlis = d2lis
			ans = d2
		k = K
		s = K - 1
		for i in range(1, len(data) - s, 100):
			d1 = data[:i]
			d2 = data[i:i + s]
			d3 = data[i + s:]
			sd2 = sorted(d2)
			p1 = sd2 + d1 + d3
			p2 = sd2 + d3 + d1
			p3 = d1 + d3 + sd2
			p4 = d3 + d1 + sd2
			p1lis = lislen(p1)
			p2lis = lislen(p2)
			p3lis = lislen(p3)
			p4lis = lislen(p4)
			if p1lis > nlis:
				ans = p1
			if p2lis > nlis:
				ans = p2
			if p3lis > nlis:
				ans = p3
			if p4lis > nlis:
				ans = p4
	print(*ans)


import collections
from bisect import bisect_left

def BinarySearch(l, x):
	i = bisect_left(l, x)
	if i:
		return i - 1
	else:
		return -1

def CeilIndex(A, l, r, key):
	while r - l > 1:
		m = l + (r - l) // 2
		if A[m] >= key:
			r = m
		else:
			l = m
	return r

def LIS(A, size):
	if size == 0:
		return 1
	tailTable = [0 for i in range(size + 1)]
	len = 0
	tailTable[0] = A[0]
	len = 1
	for i in range(1, size):
		if A[i] < tailTable[0]:
			tailTable[0] = A[i]
		elif A[i] > tailTable[len - 1]:
			tailTable[len] = A[i]
			len += 1
		else:
			tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
	return len
(n, k) = map(int, input().strip().split(' '))
arr = list(map(int, input().strip().split(' ')))
temparr = arr.copy()
marri = {}
for i in range(n):
	marri[arr[i]] = i
marr = sorted(arr)
arrm = sorted(arr, reverse=True)
ede = collections.deque([])
ode = collections.deque([])
anslist1 = []
anslist2 = []
ind2 = {}
d = {0: n - 1}
ind = {}
i = 0
j = 0
loop = 0
mi = -10
while k > len(d) + len(ind2) and i + j < n:
	flag = 1
	if loop % 2:
		prev = mi
		mi = marri[marr[i]]
		if mi - 1 == prev:
			ind2[prev] = mi
			ind[mi] = mi
			ode.append(arr[mi])
			i += 1
			continue
	else:
		prev = mi
		mi = marri[arrm[j]]
		if mi + 1 == prev:
			del ind2[prev]
			ind2[mi] = prev
			ind[mi] = mi
			ede.appendleft(arr[mi])
			j += 1
			continue
	if mi in d:
		ind[mi] = mi
		ind2[mi] = mi
		if mi + 1 == d[mi]:
			ind[mi + 1] = mi + 1
			ind2[mi + 1] = mi + 1
		else:
			d[mi + 1] = d[mi]
		del d[mi]
	elif mi in ind:
		pass
	else:
		l = list(sorted(d))
		x = BinarySearch(l, mi)
		if x != -1:
			y = d[l[x]]
			x = l[x]
		else:
			x = 0
			y = d[l[x]]
		if x < mi and y > mi:
			del d[x]
			if len(d) + len(ind2) < k - 2:
				if x == mi - 1:
					ind[x] = x
					ind2[x] = x
				else:
					d[x] = mi - 1
				if y == mi + 1:
					ind[y] = y
					ind2[y] = y
				else:
					d[mi + 1] = y
				ind[mi] = mi
				ind2[mi] = mi
			else:
				if x == mi - 1:
					ind[x] = x
				d[x] = mi - 1
				d[mi] = y
				flag = 0
		elif x == mi:
			del d[x]
			ind[x] = x
			ind2[x] = x
			if x + 1 == y:
				ind[y] = y
				ind2[y] = y
			else:
				d[x + 1] = y
		elif y == mi:
			ind[y] = y
			ind2[y] = y
			if x == y - 1:
				del d[x]
				ind[x] = x
				ind2[x] = x
			else:
				d[x] = y - 1
	if flag:
		if loop % 2:
			ode.append(arr[mi])
			i += 1
		else:
			ede.appendleft(arr[mi])
			j += 1
	loop += 1
vis = {}
dic = {}
for i in d.keys():
	ans = []
	j = i
	while j <= d[i]:
		ans.append(arr[j])
		j += 1
	dic[min(ans)] = ans
for i in sorted(ode):
	vis[i] = 1
for i in sorted(dic):
	for j in dic[i]:
		vis[j] = 1
for i in sorted(ede):
	vis[i] = 1
for i in range(1, n + 1):
	if i not in vis:
		dic[i] = [i]
for i in sorted(ode):
	anslist2.append(i)
for i in sorted(dic):
	for j in dic[i]:
		anslist2.append(j)
for i in sorted(ede):
	anslist2.append(i)
if LIS(anslist1, len(anslist1)) < LIS(anslist2, len(anslist2)):
	anslist1 = anslist2.copy()
dic = {}
for i in d.keys():
	ans = []
	j = i
	while j <= d[i]:
		ans.append(arr[j])
		j += 1
	dic[max(ans)] = ans
vis = {}
for i in sorted(ode):
	vis[i] = 1
for i in sorted(dic):
	for j in dic[i]:
		vis[j] = 1
for i in sorted(ede):
	vis[i] = 1
for i in range(1, n + 1):
	if i not in vis:
		dic[i] = [i]
anslist2.clear()
for i in sorted(ode):
	anslist2.append(i)
for i in sorted(dic):
	for j in dic[i]:
		anslist2.append(j)
for i in sorted(ede):
	anslist2.append(i)
if LIS(anslist1, len(anslist1)) < LIS(anslist2, len(anslist2)):
	anslist1 = anslist2.copy()
arr = temparr.copy()
marr = sorted(arr)
de = collections.deque([])
l = []
d = {0: n - 1}
ind = {}
ind2 = {}
i = 0
mi = -10
while k > len(d) + len(ind2):
	flag = 1
	prev = mi
	mi = marri[marr[i]]
	if mi - 1 == prev:
		ind2[prev] = mi
		ind[mi] = mi
		de.append(arr[mi])
		i += 1
		continue
	if mi in d:
		ind[mi] = mi
		ind2[mi] = mi
		if mi + 1 == d[mi]:
			ind[mi + 1] = mi + 1
			ind2[mi + 1] = mi + 1
		else:
			d[mi + 1] = d[mi]
		del d[mi]
	elif mi in ind:
		pass
	else:
		l = list(sorted(d))
		x = BinarySearch(l, mi)
		if x != -1:
			y = d[l[x]]
			x = l[x]
		else:
			x = 0
			y = d[l[x]]
		if x < mi and y > mi:
			del d[x]
			if len(d) + len(ind2) < k - 2:
				if x == mi - 1:
					ind[x] = x
					ind2[x] = x
				else:
					d[x] = mi - 1
				if y == mi + 1:
					ind[y] = y
					ind2[y] = y
				else:
					d[mi + 1] = y
				ind[mi] = mi
				ind2[mi] = mi
			else:
				if x == mi - 1:
					ind[x] = x
				d[x] = mi - 1
				d[mi] = y
				flag = 0
		elif x == mi:
			del d[x]
			ind[x] = x
			ind2[x] = x
			if x + 1 == y:
				ind[y] = y
				ind2[y] = y
			else:
				d[x + 1] = y
		elif y == mi:
			ind[y] = y
			ind2[y] = y
			if x == y - 1:
				del d[x]
				ind[x] = x
				ind2[x] = x
			else:
				d[x] = y - 1
	if flag:
		de.append(arr[mi])
		i += 1
dic = {}
for i in d.keys():
	ans = []
	j = i
	while j <= d[i]:
		ans.append(arr[j])
		j += 1
	dic[min(ans)] = ans
vis = {}
for i in de:
	vis[i] = 1
for i in sorted(dic):
	for j in dic[i]:
		vis[j] = 1
for i in range(1, n + 1):
	if i not in vis:
		dic[i] = [i]
anslist2.clear()
for i in de:
	anslist2.append(i)
for i in sorted(dic):
	for j in dic[i]:
		anslist2.append(j)
if LIS(anslist1, len(anslist1)) < LIS(anslist2, len(anslist2)):
	anslist1 = anslist2.copy()
dic = {}
for i in d.keys():
	ans = []
	j = i
	while j <= d[i]:
		ans.append(arr[j])
		j += 1
	dic[max(ans)] = ans
vis = {}
for i in de:
	vis[i] = 1
for i in sorted(dic):
	for j in dic[i]:
		vis[j] = 1
for i in range(1, n + 1):
	if i not in vis:
		dic[i] = [i]
anslist2.clear()
for i in de:
	anslist2.append(i)
for i in sorted(dic):
	for j in dic[i]:
		anslist2.append(j)
if LIS(anslist1, len(anslist1)) < LIS(anslist2, len(anslist2)):
	anslist1 = anslist2.copy()
arr = temparr.copy()
marr = sorted(arr, reverse=True)
de = collections.deque([])
l = []
d = {0: n - 1}
ind = {}
ind2 = {}
i = 0
mi = -10
while k > len(d) + len(ind2):
	flag = 1
	prev = mi
	mi = marri[marr[i]]
	if mi + 1 == prev:
		del ind2[prev]
		ind2[mi] = prev
		ind[mi] = mi
		de.appendleft(arr[mi])
		i += 1
		continue
	if mi in d:
		ind[mi] = mi
		ind2[mi] = mi
		if mi + 1 == d[mi]:
			ind[mi + 1] = mi + 1
			ind2[mi + 1] = mi + 1
		else:
			d[mi + 1] = d[mi]
		del d[mi]
	elif mi in ind:
		pass
	else:
		l = list(sorted(d))
		x = BinarySearch(l, mi)
		if x != -1:
			y = d[l[x]]
			x = l[x]
		else:
			x = 0
			y = d[l[x]]
		if x < mi and y > mi:
			del d[x]
			if len(d) + len(ind2) < k - 2:
				if x == mi - 1:
					ind[x] = x
					ind2[x] = x
				else:
					d[x] = mi - 1
				if y == mi + 1:
					ind[y] = y
					ind2[y] = y
				else:
					d[mi + 1] = y
				ind[mi] = mi
				ind2[mi] = mi
			else:
				if x == mi - 1:
					ind[x] = x
				d[x] = mi - 1
				d[mi] = y
				flag = 0
		elif x == mi:
			del d[x]
			ind[x] = x
			ind2[x] = x
			if x + 1 == y:
				ind[y] = y
				ind2[y] = y
			else:
				d[x + 1] = y
		elif y == mi:
			ind[y] = y
			ind2[y] = y
			if x == y - 1:
				del d[x]
				ind[x] = x
				ind2[x] = x
			else:
				d[x] = y - 1
	if flag:
		de.appendleft(arr[mi])
		i += 1
dic = {}
for i in d.keys():
	ans = []
	j = i
	while j <= d[i]:
		ans.append(arr[j])
		j += 1
	dic[min(ans)] = ans
anslist2.clear()
vis = {}
for i in de:
	vis[i] = 1
for i in sorted(dic):
	for j in dic[i]:
		vis[j] = 1
for i in range(1, n + 1):
	if i not in vis:
		dic[i] = [i]
for i in sorted(dic):
	for j in dic[i]:
		anslist2.append(j)
for i in de:
	anslist2.append(i)
if LIS(anslist1, len(anslist1)) < LIS(anslist2, len(anslist2)):
	anslist1 = anslist2.copy()
dic = {}
for i in d.keys():
	ans = []
	j = i
	while j <= d[i]:
		ans.append(arr[j])
		j += 1
	dic[min(ans)] = ans
vis = {}
for i in de:
	vis[i] = 1
for i in sorted(dic):
	for j in dic[i]:
		vis[j] = 1
for i in range(1, n + 1):
	if i not in vis:
		dic[i] = [i]
anslist2.clear()
for i in sorted(dic):
	for j in dic[i]:
		anslist2.append(j)
for i in de:
	anslist2.append(i)
if LIS(anslist1, len(anslist1)) < LIS(anslist2, len(anslist2)):
	anslist1 = anslist2.copy()
for i in anslist1:
	print(i, end=' ')


import sys

def array_lele():
	return list(map(int, sys.stdin.readline().strip().split()))

def integers_lele():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def lis(nums, cmp=lambda x, y: x < y):
	P = [0] * len(nums)
	M = [0] * (len(nums) + 1)
	L = 0
	for i in range(len(nums)):
		(lo, hi) = (1, L)
		while lo <= hi:
			mid = (lo + hi) // 2
			if cmp(nums[M[mid]], nums[i]):
				lo = mid + 1
			else:
				hi = mid - 1
		newL = lo
		P[i] = M[newL - 1]
		M[newL] = i
		L = max(L, newL)
	S = [0] * L
	k = M[L]
	for i in range(L - 1, -1, -1):
		(S[i], k) = (nums[k], P[k])
	return len(S)
(n, k) = integers_lele()
Arr = array_lele()
result = []
i = 0
count_k = 0
if n == k:
	Arr.sort()
	print(*Arr)
	exit()
while i < n and count_k < k:
	arr = []
	j = i
	arr.append(Arr[j])
	j += 1
	while j < n and Arr[j] >= Arr[j - 1]:
		arr.append(Arr[j])
		j += 1
	count_k += 1
	flag = 0
	i = j
	result.append(arr)
while i < n:
	result[-1].append(Arr[i])
	i += 1
result.sort(key=lambda x: x[-1])
answer = []
for i in result:
	for j in i:
		answer.append(j)
new_lis = lis(answer)
print(*answer)


(n, k) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
index = dict()
for i in range(n):
	index[arr[i]] = i

def lsplit(arr, x, num):
	i = arr.index(x)
	n = len(arr)
	ans = list()
	if num >= 3:
		if i != 0 and i != n - 1:
			ans.append(arr[0:i])
			ans.append([x])
			ans.append(arr[i + 1:n])
		elif i == 0:
			ans.append([x])
			ans.append(arr[i + 1:n])
		elif i == n - 1:
			ans.append(arr[0:n - 1])
			ans.append([x])
	if num == 2:
		if i != 0 and i != n - 1:
			if arr[i - 1] < arr[i + 1]:
				ans.append(arr[0:i + 1])
				ans.append(arr[i + 1:n])
			else:
				ans.append(arr[0:i])
				ans.append(arr[i:n])
		elif i == 0:
			ans.append([x])
			ans.append(arr[i + 1:n])
		elif i == n - 1:
			ans.append(arr[0:n - 1])
			ans.append([x])
	return ans
if k < n - 1:
	mx = n
	mn = 1
	amx = 0
	amn = 0
	for i in range(k - 1):
		amx += abs(index[mx] - (mx - 1))
		mx -= 1
		amn += abs(index[mn] - (mn - 1))
		mn += 1
	if amx > amn:
		arr1 = [x for x in arr]
		splits = []
		mx = n
		num = 0
		while len(splits) < k:
			if arr1 in splits:
				splits.remove(arr1)
			num = k - len(splits)
			for x in lsplit(arr1, mx, num):
				if x != []:
					splits.append(x)
			mx -= 1
			for x in splits:
				if mx in x:
					arr1 = x
		splits.sort()
		ans = []
		for lst in splits:
			for i in lst:
				ans.append(i)
	else:
		arr1 = [x for x in arr]
		splits = []
		mn = 1
		num = 0
		while len(splits) < k:
			if arr1 in splits:
				splits.remove(arr1)
			num = k - len(splits)
			for x in lsplit(arr1, mn, num):
				if x != []:
					splits.append(x)
			mn += 1
			for x in splits:
				if mn in x:
					arr1 = x
		splits.sort()
		ans = []
		for lst in splits:
			for i in lst:
				ans.append(i)
else:
	ans = arr
	ans.sort()
print(' '.join((str(x) for x in ans)))