Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than $k$ other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.

Now each knight ponders: how many coins he can have if only he kills other knights?

You should answer this question for each knight.


-----Input-----

The first line contains two integers $n$ and $k$ $(1 \le n \le 10^5, 0 \le k \le \min(n-1,10))$ — the number of knights and the number $k$ from the statement.

The second line contains $n$ integers $p_1, p_2 ,\ldots,p_n$ $(1 \le p_i \le 10^9)$ — powers of the knights. All $p_i$ are distinct.

The third line contains $n$ integers $c_1, c_2 ,\ldots,c_n$ $(0 \le c_i \le 10^9)$ — the number of coins each knight has.


-----Output-----

Print $n$ integers — the maximum number of coins each knight can have it only he kills other knights.


-----Examples-----
Input
4 2
4 5 9 7
1 2 11 33

Output
1 3 46 36 
Input
5 1
1 2 3 4 5
1 2 3 4 5

Output
1 3 5 7 9 
Input
1 0
2
3

Output
3 


-----Note-----

Consider the first example.   The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has.  The second knight can kill the first knight and add his coin to his own two.  The third knight is the strongest, but he can't kill more than $k = 2$ other knights. It is optimal to kill the second and the fourth knights: $2+11+33 = 46$.  The fourth knight should kill the first and the second knights: $33+1+2 = 36$. 

In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.

In the third example there is only one knight, so he can't kill anyone.

Source: codeforces | Difficulty: MEDIUM
Tags: Sorting

Implementation

Greedy algorithms


Solutions:
import heapq
(n, k) = map(int, input().split())
powers = list(map(int, input().split()))
coins = list(map(int, input().split()))
pc = [[powers[i], coins[i]] for i in range(n)]
pc.sort()
powerindex = sorted([i for i in range(n)], key=lambda i: powers[i])
currsum = 0
coinscollected = []
ans = [0] * n
curr = 0
if k > 0:
	for i in range(n):
		if curr < k:
			heapq.heappush(coinscollected, pc[i][1])
			curr += 1
			currsum += pc[i][1]
			ans[powerindex[i]] = currsum
		else:
			ans[powerindex[i]] = currsum + pc[i][1]
			least = heapq.heappop(coinscollected)
			if pc[i][1] > least:
				heapq.heappush(coinscollected, pc[i][1])
				currsum -= least
				currsum += pc[i][1]
			else:
				heapq.heappush(coinscollected, least)
	print(*ans)
else:
	print(*coins)


from sys import stdin, stdout
import heapq
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for _ in range(1):
	(n, k) = lst()
	l = sorted(zip(lst(), lst(), range(n)))
	h = []
	sm = 0
	ans = {}
	for i in range(n):
		(pwr, cns, ind) = l[i]
		sm += cns
		if len(h) > k:
			p = 0
			for i in range(len(h)):
				if h[p] > h[i]:
					p = i
			sm -= h.pop(p)
		ans[ind] = sm
		h += [cns]
	for i in range(n):
		stdout.write(str(ans[i]) + ' ')
	print()


R = lambda : map(int, input().split())
(n, k) = R()
p = sorted(zip(R(), R(), range(n)))
c = []
r = [0] * n
s = 0
for i in range(k):
	(_, b, j) = p[i]
	s += b
	r[j] = s
	c.append(b)
(_, b, j) = p[k]
r[j] = s + b
for i in range(k + 1, n):
	(_, b, j) = p[i - 1]
	c.sort()
	if c and b > c[0]:
		s += b - c[0]
		c[0] = b
	(_, b, j) = p[i]
	r[j] = b + s
print(*r)


import heapq as Q
R = lambda : map(int, input().split())
(n, k) = R()
(p, c) = (R(), R())
sum = 0
(ans, heap) = ([None] * n, [])
for (_, coin, idx) in sorted(zip(p, c, range(n))):
	sum += coin
	ans[idx] = sum
	Q.heappush(heap, coin)
	if len(heap) > k:
		sum -= Q.heappop(heap)
print(*ans)


from __future__ import print_function
from queue import Queue
from heapq import *
import sys
import math
import os.path

def log(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

def ni():
	return map(int, input().split())

def nio(offset):
	return map(lambda x: int(x) + offset, input().split())

def nia():
	return list(map(int, input().split()))

def toString(aList, sep=' '):
	return sep.join((str(x) for x in aList))

def toMapInvertIndex(aList):
	return {k: v for (v, k) in enumerate(aList)}
(n, k) = ni()
p = nia()
c = nia()
if True:
	sortedId = sorted(range(n), key=lambda k: p[k])
	cSmall = []
	oldSum = 0
	sortedSum = [0]
	for i in range(1, n):
		heappush(cSmall, c[sortedId[i - 1]])
		oldSum += c[sortedId[i - 1]]
		if len(cSmall) > k:
			x = heappop(cSmall)
			oldSum -= x
		sortedSum += [oldSum]
	s = [c[i] for i in range(n)]
	for i in range(n):
		s[sortedId[i]] += sortedSum[i]
	print(toString(s))
else:
	kill = [[] for _ in range(n)]
	s = [c[i] for i in range(n)]
	for i in range(n):
		for j in range(i):
			if p[i] < p[j]:
				s[j] += c[i]
				heappush(kill[j], c[i])
				if len(kill[j]) > k:
					x = heappop(kill[j])
					s[j] -= x
			else:
				s[i] += c[j]
				heappush(kill[i], c[j])
				if len(kill[i]) > k:
					x = heappop(kill[i])
					s[i] -= x
	print(toString(s))


from collections import defaultdict as dd
from heapq import heapify, heappop, heappush
(n, m) = [int(n) for n in input().split()]
arr = [int(item) for item in input().split()]
brr = [int(item) for item in input().split()]
d = dd(int)
e = dd(int)
for i in range(n):
	e[arr[i]] = i
	d[arr[i]] = brr[i]
drr = [0] * n
crr = []
heapify(crr)
arr.sort()
for i in range(n):
	if len(crr) == 0:
		drr[e[arr[i]]] = d[arr[i]]
		heappush(crr, d[arr[i]])
		continue
	if len(crr) <= m:
		drr[e[arr[i]]] = d[arr[i]] + sum(crr)
		heappush(crr, d[arr[i]])
		continue
	if len(crr) > m:
		heappop(crr)
		drr[e[arr[i]]] = d[arr[i]] + sum(crr)
		heappush(crr, d[arr[i]])
		continue
print(*drr)


from heapq import *
R = lambda : map(int, input().strip().split())
(n, k) = R()
if k == 0:
	R()
	print(*R())
elif k > 0:
	zipped = sorted(zip(R(), R(), range(n)), key=lambda x: x[0])
	heap = []
	ans = [0] * n
	heapSize = 0
	for i in range(len(zipped)):
		ans[zipped[i][2]] += zipped[i][1]
		for j in range(heapSize):
			ans[zipped[i][2]] += heap[j]
		if heapSize < k:
			heappush(heap, zipped[i][1])
			heapSize += 1
		elif zipped[i][1] > heap[0]:
			heapreplace(heap, zipped[i][1])
	print(*ans)


(n, k) = [int(a) for a in input().split()]
power = [int(a) for a in input().split()]
coins = [int(a) for a in input().split()]
dp = [0 for i in range(n)]

def takeSecond(elem):
	return elem[1]

def takeFirst(elem):
	return elem[0]
people = [(power[i], coins[i], i) for i in range(n)]
people.sort(key=takeFirst)
dp[0] = []
for (i, p) in enumerate(people):
	if i == 0:
		continue
	kills = [i for i in dp[i - 1]]
	kills.append(people[i - 1][1])
	x = []
	if len(kills) > k:
		kills.remove(min(kills))
	dp[i] = kills
x = [(people[i][2], str(sum(dp[i]) + people[i][1])) for i in range(n)]
x.sort(key=takeFirst)
print(' '.join([z[1] for z in x]))


import heapq
[n, k] = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
arr = []
for i in range(n):
	arr.append((i, p[i], c[i]))
fin = []
arr.sort(key=lambda x: x[1])
heap = []
for i in range(n):
	fin.append([arr[i][0], arr[i][2]])
	if i <= k:
		heapq.heappush(heap, arr[i][2])
	else:
		heapq.heappop(heap)
		heapq.heappush(heap, arr[i][2])
	fin[i][1] = sum(heap)
fin.sort()
for i in range(n):
	print(fin[i][1], end=' ')


import heapq
(n, k) = [int(x) for x in input().strip().split()]
p = [int(x) for x in input().strip().split()]
c = [int(x) for x in input().strip().split()]
a = sorted(zip(p, c, range(n)))
coins = []
s = 0
ans = []
for ai in a:
	s += ai[1]
	ans.append([ai[2], s])
	heapq.heappush(coins, ai[1])
	if len(coins) > k:
		s -= heapq.heappop(coins)
print(*[c for (i, c) in sorted(ans)])


(n, k) = map(int, input().split())
p = list(sorted(([p, i] for (i, p) in enumerate(map(int, input().split())))))
c = list(map(int, input().split()))
ans = [0] * n
coins = 0
minm = 0
status = []
for i in range(n):
	if i >= 1:
		status.append(c[p[i - 1][1]])
		status.sort()
	if i <= k:
		ans[p[i][1]] = sum(status) + c[p[i][1]]
	else:
		status.pop(0)
		ans[p[i][1]] = sum(status) + c[p[i][1]]
print(*ans)


(n, m) = map(int, input().split())

class Knight:

	def __init__(self, andis, p, c):
		self.p = int(p)
		self.c = int(c)
		self.andis = int(andis)
		self.ans = self.c
p = list(map(int, input().split()))
c = list(map(int, input().split()))
x = []
for i in range(n):
	x.append(Knight(i, p[i], c[i]))
x.sort(key=lambda x: x.p)
coins = []
for i in range(n - 1):
	if len(coins) < m:
		coins.append(x[i].c)
		coins.sort()
	elif len(coins) > 0:
		if coins[0] < x[i].c:
			coins[0] = x[i].c
			coins.sort()
	x[i + 1].ans += sum(coins)
x.sort(key=lambda x: x.andis)
for k in x:
	print(k.ans, end=' ')


import heapq
from bisect import insort
MIS = lambda : map(int, input().split())
(n, k) = MIS()
p = sorted(((x, i) for (i, x) in enumerate(MIS())))
c = list(MIS())
topcoin = []
totcoin = 0
ans = c[:]
for (x, i) in p:
	ans[i] += totcoin
	insort(topcoin, -c[i])
	totcoin += c[i]
	if len(topcoin) > k:
		totcoin += topcoin.pop()
print(*ans)


from math import ceil, log
from heapq import heappop, heappush, heapify
t = 1
for test in range(t):
	(n, k) = list(map(int, input().split()))
	p = list(map(int, input().split()))
	c = list(map(int, input().split()))
	arr = [i for i in sorted(enumerate(p), key=lambda x: x[1])]
	maxcoins = [0 for i in range(k)]
	heapify(maxcoins)
	ans = list(p)
	tmpSum = 0
	tmpSum2 = 0
	prev = arr[0][1]
	for (ind, power) in arr:
		if power > prev:
			ans[ind] = tmpSum + c[ind]
			tmpSum2 = tmpSum
		else:
			ans[ind] = tmpSum2 + c[ind]
		heappush(maxcoins, c[ind])
		tmpSum += c[ind]
		tmpSum -= heappop(maxcoins)
	print(*ans)


import heapq
(n, k) = [int(_) for _ in input().split()]
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = [0] * n
heap = []
count = 0
ana1 = sorted(list(zip(p, c, range(n))))
for (power, coin, index) in ana1:
	count += coin
	ans[index] = count
	heapq.heappush(heap, coin)
	if len(heap) > k:
		count -= heapq.heappop(heap)
print(*ans)


(nk, k) = [int(x) for x in input().split()]
knights = [int(x) for x in input().split()]
coins = [int(x) for x in input().split()]
data = sorted(zip(knights, coins, range(nk)))
have = [0] * nk
selected = []
for (p, c, i) in data:
	ans = c + sum(selected)
	have[i] = ans
	selected.append(c)
	selected = sorted(selected, reverse=True)[:k]
print(*have)


(n, k) = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
z = [x for x in range(n)]
zz = sorted(zip(x, y, z))
pre = []
c = 1
f = [0] * n
vv = 0
ss = 0
for i in range(n):
	if i < k:
		pre.append(zz[i][1])
		gh = int(zz[i][2])
		f[gh] = ss + zz[i][1]
		ss += zz[i][1]
	else:
		pre.sort()
		gh = int(zz[i][2])
		f[gh] = ss + zz[i][1]
		ff = False
		for j in range(0, len(pre)):
			if zz[i][1] > pre[j]:
				ff = True
				ss = ss - pre[j] + zz[i][1]
				pre[j] = zz[i][1]
				break
print(*f)


def input_ints():
	inp = input().split()
	ints = list(map(lambda i: int(i), inp))
	return ints

def transform_with_index(arr):
	transformed = []
	for i in range(len(arr)):
		transformed.append([i, arr[i]])
	return transformed

def find_index_of_the_least(arr):
	n = len(arr)
	if not n:
		return -1
	least_index = 0
	for i in range(1, n):
		if arr[i] < arr[least_index]:
			least_index = i
	return least_index
my_sort = lambda arr, reverse=True: sorted(arr, key=lambda a: a[1], reverse=reverse)
(n, k) = input_ints()
p = input_ints()
transformed_p = transform_with_index(p)
sorted_p = my_sort(transformed_p, False)
c = input_ints()
transformed_c = transform_with_index(c)
sorted_c = my_sort(transformed_c)
pockets = list(map(lambda c: [c], c))
pocket_sums = c
for i in range(1, n):
	tyrant_knight_index = sorted_p[i][0]
	prev_tyrant_knight_index = sorted_p[i - 1][0]
	pockets[tyrant_knight_index] += pockets[prev_tyrant_knight_index]
	pocket_sums[tyrant_knight_index] += pocket_sums[prev_tyrant_knight_index]
	if len(pockets[tyrant_knight_index]) > k + 1:
		least_index = find_index_of_the_least(pockets[prev_tyrant_knight_index]) + 1
		pocket_sums[tyrant_knight_index] -= pockets[tyrant_knight_index][least_index]
		del pockets[tyrant_knight_index][least_index]
output = ''
for i in range(n):
	output += str(pocket_sums[i]) + ' '
print(output)


import heapq
arrInput = []
arrInput.append(input())
arrInput.append(input())
arrInput.append(input())
(N, K) = map(int, arrInput[0].split())
P = dict(zip(range(N), list(map(int, arrInput[1].split()))))
C = dict(zip(range(N), list(map(int, arrInput[2].split()))))
Ps = sorted(P.items(), key=lambda x: x[1])
arrAns = [-1 for _ in range(N)]
selected = []
heapq.heapify(selected)
for n in range(N):
	knightIdx = Ps[n][0]
	heapq.heappush(selected, C[knightIdx])
	eachMaxCoin = sum(selected)
	if K < len(selected):
		heapq.heappop(selected)
	arrAns[knightIdx] = eachMaxCoin
ans = ''
for n in range(N):
	if 0 < n:
		print(' ', end='')
	print(str(arrAns[n]), end='')
print('')


(n, k) = map(int, input().split())
knight = list(map(int, input().split()))
coins = list(map(int, input().split()))
d = {}
ans = [0] * n
for i in range(n):
	knight[i] = [knight[i], i]
for i in coins:
	d[i] = d.get(i, 0) + 1
c = coins[:]
knight = sorted(knight, key=lambda x: x[0])
ans2 = []
ans = coins[:]
if k == 0:
	print(*ans)
else:
	for i in range(n):
		ans1 = 0
		if len(ans2) < k:
			ans1 = sum(ans2)
		else:
			ans2 = sorted(ans2)[-k:]
			ans1 += sum(ans2)
		ans[knight[i][1]] += ans1
		ans2.append(coins[knight[i][1]])
	print(*ans)


import heapq as q
rd = lambda : map(int, input().split())
(n, k) = rd()
a = sorted(zip(rd(), rd(), range(n)))
r = [0] * n
h = []
s = 0
for (x, y, z) in a:
	s += y
	r[z] = s
	q.heappush(h, y)
	if len(h) > k:
		s -= q.heappop(h)
print(' '.join(map(str, r)))


import heapq
(n, k) = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
indexes = sorted(list(range(n)), key=p.__getitem__)
most_vyg_odn_yye = []
res = [1] * n
cur_res = 0
for ind in indexes:
	this_cost = c[ind]
	heapq.heappush(most_vyg_odn_yye, this_cost)
	cur_res += this_cost
	res[ind] = cur_res
	if len(most_vyg_odn_yye) > k:
		cur_res -= heapq.heappop(most_vyg_odn_yye)
print(*res)


import array
(n, k) = list(map(int, input().split()))
p = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
s = []
for i in range(n):
	s.append((i, p[i], c[i]))
s = sorted(s, key=lambda x: x[1])
sum_list = [[s[0][2]]]
sum_res = [s[0][2]]
for i in range(1, n):
	sum_list.append(list(sum_list[i - 1]))
	if i - k - 1 >= 0:
		sum_list[i].remove(min(sum_list[i]))
	sum_list[i].append(s[i][2])
	sum_res.append(sum(sum_list[i]))
ans = [str(x[1]) for x in sorted(enumerate(sum_res), key=lambda x: s[x[0]][0])]
print(' '.join(ans))


from heapq import heappush, heappop
(n, k) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
heap = []
sort_p = sorted([i for i in range(n)], key=lambda x: p[x])
som_k = 0
soms = []
for i in range(n):
	ci = c[sort_p[i]]
	p[sort_p[i]] = som_k + ci
	som_k += ci
	heappush(heap, ci)
	if len(heap) > k:
		som_k -= heappop(heap)
print(*p)


(n, k) = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
m = {}
for i in range(n):
	if p[i] not in m:
		m[p[i]] = list()
	m[p[i]].append(c[i])
a = {}
t = []
for (key, val) in sorted(m.items()):
	a[key] = sum(t)
	t += val
	t.sort()
	t = t[max(0, len(t) - k):len(t)]
print(' '.join([str(a[p[i]] + c[i]) for i in range(n)]))


(n, k) = list(map(int, input().split()))
d = list(map(int, input().split()))
s = list(map(int, input().split()))
if k != 0:
	t = dict()
	for i in range(n):
		if d[i] not in t:
			t[d[i]] = [s[i]]
		else:
			t[d[i]].append(s[i])
	keys = list(t.keys())
	keys.sort()
	c = {keys[0]: 0}
	wealth = [0 for j in range(k)]
	for i in range(len(keys)):
		if i == 0:
			continue
		for w in t[keys[i - 1]]:
			if min(wealth) < w:
				wealth.remove(min(wealth))
				wealth.append(w)
		c[keys[i]] = sum(wealth)
	for i in range(n):
		print(s[i] + c[d[i]], end=' ')
else:
	for i in s:
		print(i, end=' ')


import heapq as q
(n, k) = [int(x) for x in input().split()]
power = list(map(int, input().split()))
coin = list(map(int, input().split()))
heap = []
ans = [0] * n
coun = 0
kni = list(zip(power, coin, range(n)))
kni.sort()
for (p, c, idx) in kni:
	coun += c
	ans[idx] = coun
	q.heappush(heap, c)
	if len(heap) > k:
		coun -= q.heappop(heap)
print(*ans)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def getMat(n):
	return [getInts() for _ in range(n)]

def get_ints():
	return map(int, sys.stdin.readline().split())
(n, k) = map(int, input().split())
knight = list(map(int, input().split()))
coins = list(map(int, input().split()))
d = {}
ans = [0] * n
for i in range(n):
	knight[i] = [knight[i], i]
for i in coins:
	d[i] = d.get(i, 0) + 1
c = coins[:]
knight = sorted(knight, key=lambda x: x[0])
ans2 = []
ans = coins[:]
if k == 0:
	print(*ans)
else:
	for i in range(n):
		ans1 = 0
		if len(ans2) < k:
			ans1 = sum(ans2)
		else:
			ans2 = sorted(ans2)[-k:]
			ans1 += sum(ans2)
		ans[knight[i][1]] += ans1
		ans2.append(coins[knight[i][1]])
	print(*ans)


(n, k) = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
Q = []
for (i, p) in enumerate(P):
	Q.append((p, i))
Q.sort()
q = []
import heapq
heapq.heapify([])
s = 0
ans = [0] * n
if k > 0:
	for (p, i) in Q:
		ans[i] = s + C[i]
		if len(q) == k:
			if q[0] <= C[i]:
				v = heapq.heappop(q)
				heapq.heappush(q, C[i])
				s -= v
				s += C[i]
		else:
			heapq.heappush(q, C[i])
			s += C[i]
	print(*ans)
else:
	for (p, i) in Q:
		ans[i] = C[i]
	print(*ans)


from heapq import *
(n, k) = [int(x) for x in input().split()]
powers = [int(x) for x in input().split()]
coins = [int(x) for x in input().split()]
results = coins[:]
indexes = list(range(n))
pc = sorted(zip(powers, indexes), key=lambda t: t[0])
h = []
z = 0
if k > 0:
	for (p, i) in pc:
		if z > 0:
			c = coins[pc[z - 1][1]]
			if len(h) < k:
				heappush(h, c)
			elif c > h[0]:
				heappushpop(h, c)
		z += 1
		results[i] += sum(h)
print(' '.join([str(i) for i in results]))


(n, k) = map(int, input().split())
lst1 = [int(x) for x in input().split()]
lst2 = [int(x) for x in input().split()]
mass = []
for x in range(n):
	mass.append((lst1[x], lst2[x], x))
mass.sort()
ans = 0
t = []
lst = [0] * n
for x in range(n):
	if k == 0:
		lst[mass[x][2]] = mass[x][1]
	elif x < k:
		ans += mass[x][1]
		t.append(mass[x][1])
		lst[mass[x][2]] = ans
	else:
		lst[mass[x][2]] = ans + mass[x][1]
		if len(t) == 0:
			t.append(mass[x][1])
		else:
			t.sort()
			if mass[x][1] > t[0]:
				ans -= t[0]
				ans += mass[x][1]
				t[0] = mass[x][1]
for x in range(n):
	print(lst[x], end=' ')


__file__ = __file__.startswith('/home/a_farazei')

def input_():
	global __file__
	if not __file__:
		return input()
	if not hasattr(__file__, 'readline'):
		__file__ = open('input.txt', 'r')
	return __file__.readline()
(n, k) = map(int, input_().split())
p_ = list(map(int, input_().split()))
c_ = list(map(int, input_().split()))
d = [0] * n
t = []
pp = -1
tj = 0
q = sorted(zip(range(n), p_, c_), key=lambda x: x[1])
for (i, x) in enumerate(q):
	if x[1] > pp:
		t.extend((q[max(i - j - 1, 0)][2] for j in range(tj)))
		tj = 0
		pp = x[1]
	t = sorted(t, reverse=True)[:k]
	d[x[0]] = sum(t) + x[2]
	tj += 1
print(' '.join(map(str, d)))


from heapq import *
R = lambda : map(int, input().split())
(n, k) = R()
r = [0] * n
l = []
s = 0
for (_, c, i) in sorted(zip(R(), R(), range(n))):
	s += c
	r[i] = s
	heappush(l, c)
	if len(l) > k:
		s -= heappop(l)
print(*r)


import heapq
rd = lambda : map(int, input().split())
(n, k) = rd()
a = sorted(zip(rd(), rd(), range(n)))
minH = []
total = 0
ans = [0] * n
for i in range(len(a)):
	if len(minH) >= k + 1:
		(coins, index) = heapq.heappop(minH)
		total -= coins
	heapq.heappush(minH, (a[i][1], i))
	total += a[i][1]
	ans[a[i][2]] = total
print(*ans)


from heapq import heappop, heappush
from collections import namedtuple

def kill_knights(data, k):
	result = [(data[0][2], data[0][1])]
	suma = 0
	knights_to_kill = []
	for (previous, this) in zip(data, data[1:]):
		if len(knights_to_kill) < k:
			heappush(knights_to_kill, previous[1])
			suma += previous[1]
		elif k != 0 and knights_to_kill[0] < previous[1]:
			suma -= heappop(knights_to_kill)
			heappush(knights_to_kill, previous[1])
			suma += previous[1]
		result.append((this[2], suma + this[1]))
	return list(map(lambda x: x[1], sorted(result)))
(_, k) = map(int, input().split())
powers = list(map(int, input().split()))
coins = list(map(int, input().split()))
Knight = namedtuple('Knight', ['power', 'coins', 'position'])
data = sorted([Knight(powers[i], coins[i], i) for i in range(len(powers))])
print(' '.join(map(str, kill_knights(data, k))))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from heapq import heappop, heappush
for _ in range(int(input()) if not True else 1):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if not k:
		print(*b)
		continue
	vals = []
	for i in range(n):
		vals += [(i, a[i], b[i])]
	vals = sorted(vals, key=lambda k: k[1])
	ans = []
	queue = []
	for i in range(n):
		ans += [[vals[i][0], vals[i][2]]]
		if i <= k:
			heappush(queue, vals[i][2])
			ans[i][1] = sum(queue)
		else:
			heappop(queue)
			heappush(queue, vals[i][2])
			ans[i][1] = sum(queue)
	print(*[k[1] for k in sorted(ans, key=lambda k: k[0])])


import sys
input = sys.stdin.readline
from heapq import *
(n, k) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
if k == 0:
	print(*c)
	exit()
ipc = []
for i in range(n):
	ipc.append((i, p[i], c[i]))
ipc.sort(key=lambda k: k[1])
ans = [-1] * n
pq = []
s = 0
for (i, pi, ci) in ipc:
	ans[i] = s + ci
	if len(pq) >= k:
		if ci > pq[0]:
			s -= pq[0]
			heappop(pq)
			heappush(pq, ci)
			s += ci
	else:
		heappush(pq, ci)
		s += ci
print(*ans)


from collections import namedtuple
from heapq import heappop, heappush

class HighestK:

	def __init__(self, k):
		self._k = k
		self._arr = []
		self._sum = 0

	def sum(self):
		return self._sum

	def add(self, num):
		if self._k == 0:
			return
		if len(self._arr) < self._k:
			heappush(self._arr, num)
			self._sum += num
			return
		if num > self._arr[0]:
			self._sum -= heappop(self._arr)
			heappush(self._arr, num)
			self._sum += num

def kill_knights(data, k):
	result = [(data[0].position, data[0].coins)]
	highestK = HighestK(k)
	for (previous, this) in zip(data, data[1:]):
		highestK.add(previous.coins)
		result.append((this.position, highestK.sum() + this.coins))
	return list(map(lambda x: x[1], sorted(result)))
(_, k) = map(int, input().split())
powers = list(map(int, input().split()))
coins = list(map(int, input().split()))
Knight = namedtuple('Knight', ['power', 'coins', 'position'])
data = sorted([Knight(powers[i], coins[i], i) for i in range(len(powers))])
print(' '.join(map(str, kill_knights(data, k))))


from heapq import heappush, heappop
x = input().split()
n = int(x[0])
k = int(x[1])
a = input().split(' ')
b = input().split(' ')
c = [0] * len(a)
for (i, x) in enumerate(a):
	c[i] = [a[i], b[i], i]
c = sorted(c, key=lambda x: int(x[0]))
p = 0
ks = 0
ls = []
for (i, x) in enumerate(c):
	p = p + int(c[i][1])
	if ks > k:
		x = heappop(ls)
		p = p - x
	heappush(ls, int(c[i][1]))
	c[i][1] = p
	ks = ks + 1
c = sorted(c, key=lambda x: x[2])
for i in c:
	print(i[1])


(n, k) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
if k == 0:
	print(' '.join(map(str, c)))
else:
	for i in range(n):
		p[i] = (p[i], c[i], i)
	p.sort()
	prevk = []
	ans = [0 for i in range(n)]
	ans[p[0][2]] = p[0][1]
	for i in range(1, n):
		prevk.append(p[i - 1][1])
		if i > k:
			prevk.sort(reverse=True)
			prevk.pop()
		ans[p[i][2]] = sum(prevk) + p[i][1]
	print(' '.join(map(str, ans)))


(n, k) = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
arr = [[p[i], i] for i in range(n)]
arr.sort()
z = []
ans = [0 for i in range(n)]
for i in range(n):
	s = 0
	for el in z:
		s += el
	ans[arr[i][1]] = s + c[arr[i][1]]
	z.append(c[arr[i][1]])
	z.sort()
	if len(z) > k:
		z = z[1:]
for el in ans:
	print(el, end=' ')


from heapq import heappop, heappush
[n, k] = map(int, input().strip().split())
pis = list(map(int, input().strip().split()))
cis = list(map(int, input().strip().split()))
res = [0 for _ in range(n)]
nis = list(range(n))
nis.sort(key=lambda i: pis[i])
hc = []
l = 0
sc = 0
for i in nis:
	sc += cis[i]
	res[i] = sc
	heappush(hc, cis[i])
	if l == k:
		sc -= heappop(hc)
	else:
		l += 1
print(' '.join(map(str, res)))


import heapq
(n, k) = map(int, input().split())
powers = list(map(int, input().split()))
coins = list(map(int, input().split()))
sorted_powers = sorted(list(range(n)), key=powers.__getitem__)
res = [0] * n
my_heap = []
current_res = 0
for i in sorted_powers:
	c = coins[i]
	heapq.heappush(my_heap, c)
	current_res += c
	res[i] = current_res
	if len(my_heap) > k:
		current_res -= heapq.heappop(my_heap)
print(*res)


R = lambda : map(int, input().split())
(n, k) = R()
r = [0] * n
l = []
s = 0
for (_, c, i) in sorted(zip(R(), R(), range(n))):
	s += c
	r[i] = s
	l = sorted(l + [c])
	if len(l) > k:
		s -= l[0]
		del l[0]
print(*r)


from collections import defaultdict
import heapq

def find(x):
	return x[0]
(n, m) = map(int, input().split())
ans = defaultdict(int)
heap = []
power = list(map(int, input().split()))
coins = list(map(int, input().split()))
num = list((i for i in range(n + 1)))
knt = list(zip(power, coins, num))
knt.sort(key=find)
Sum = 0
for i in knt:
	ans[i[2]] = i[1] + Sum
	heapq.heappush(heap, i[1])
	Sum += i[1]
	if len(heap) > m:
		Sum -= heapq.heappop(heap)
for i in range(n):
	print(ans[i], end=' ')


(_, k) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = list(sorted(zip(a, b, list(range(len(b))))))
d = [0] * len(b)
if k == 0:
	print(' '.join(map(str, b)))
else:
	best = [0] * k
	for (pwr, cnt, index) in c:
		d[index] = sum(best) + cnt
		if cnt > best[0]:
			for i in range(len(best)):
				if cnt <= best[i]:
					best.insert(i, cnt)
					best = best[1:]
					break
			else:
				best = best[1:] + [cnt]
	print(' '.join(map(str, d)))


maxNumOfKill = list(map(lambda x: int(x), input().split()))[1]
powerArr = list(map(lambda x: int(x), input().split()))
coinsArr = list(map(lambda x: int(x), input().split()))
knightInfo = list(map(lambda x, y: (x, y), powerArr, coinsArr))
knightInfo.sort()
k_largest = []
res = {}
out = []
if maxNumOfKill == 0:
	out = [str(i) for i in coinsArr]
else:
	for knight in knightInfo:
		res[knight[0]] = str(sum(k_largest) + knight[1])
		if len(k_largest) < maxNumOfKill:
			k_largest.append(knight[1])
		else:
			k_largest.sort()
			if k_largest[0] < knight[1]:
				k_largest.remove(k_largest[0])
				k_largest.append(knight[1])
	for i in powerArr:
		out.append(str(res[i]))
print(' '.join(out))


import collections
from functools import cmp_to_key
import sys

def getIntList():
	return list(map(int, input().split()))
import bisect
(N, K) = getIntList()
P = getIntList()
C = getIntList()
ck = [0 for i in range(K + 1)]
z = zip(P, C, [i for i in range(N)])
z = list(z)
z.sort()
score = [0 for i in range(N)]
for i in range(N):
	if i > 0 and z[i][0] == z[i - 1][0]:
		score[z[i][2]] = score[z[i - 1][2]]
	else:
		score[z[i][2]] = sum(ck[:K]) + z[i][1]
	ck[K] = z[i][1]
	ck.sort(reverse=True)
r = ' '.join(map(str, score))
print(r)


(n, k) = input().split()
n = int(n)
k = int(k)
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = list(zip(a, b))
t = list(map(list, f))
for i in range(len(a)):
	t[i].append(i)
if k == 0:
	for i in b:
		print(i, end=' ')
else:
	t.sort()
	p = [0 for x in range(k)]
	t[0].append(t[0][1])
	for i in range(1, len(t)):
		if t[i - 1][1] > min(p):
			p[p.index(min(p))] = t[i - 1][1]
		t[i].append(sum(p) + t[i][1])
	t.sort(key=lambda x: x[2])
	for i in t:
		print(i[3], end=' ')


def solve(input_val):
	(nAndk, powers, coins) = input_val.split('\n')
	(n, k) = nAndk.split(' ')
	n = int(n)
	k = int(k)
	powers_arr = powers.split(' ')
	coins_arr = coins.split(' ')
	tuples = [(int(powers_arr[i]), int(coins_arr[i]), i + 1) for i in range(n)]
	tuples = sorted(tuples, key=lambda x: x[0])
	finals = list()
	top_kills = list()
	top_kills_sum = 0
	for i in range(k + 1):
		curr_tuple = tuples[i]
		curr_start = curr_tuple[1]
		curr_placement = curr_tuple[2]
		curr_final = (curr_start + top_kills_sum, curr_placement)
		top_kills.append(curr_start)
		top_kills_sum += curr_start
		finals.append(curr_final)
	top_kills = sorted(top_kills)
	for i in range(k + 1, n):
		curr_tuple = tuples[i]
		curr_start = curr_tuple[1]
		curr_placement = curr_tuple[2]
		min_coins = top_kills[0]
		final_val = curr_start + top_kills_sum - min_coins
		if curr_start > min_coins:
			top_kills_sum = top_kills_sum + curr_start - min_coins
			top_kills[0] = curr_start
			top_kills = sorted(top_kills)
		finals.append((final_val, curr_placement))
	sorted_finals = sorted(finals, key=lambda x: x[1])
	output = ' '.join((str(x[0]) for x in sorted_finals))
	return output
inputs = input() + '\n' + input() + '\n' + input()
print(solve(inputs))


from heapq import heappushpop
(n, k) = map(int, input().split())
ps = list(map(int, input().split()))
cs = list(map(int, input().split()))
coins = [0] * k
indices = sorted(range(n), key=lambda i: ps[i], reverse=True)
ans = list(cs)
s = 0
for i in reversed(indices):
	while ps[indices[-1]] < ps[i]:
		c = cs[indices.pop()]
		s += c - heappushpop(coins, c)
	ans[i] += s
print(*ans, sep=' ')


from collections import defaultdict
(n, k) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
pairs = {p[i]: c[i] for i in range(n)}
s_p = sorted(pairs.items(), key=lambda x: x[0])
p_ = [k for (k, v) in s_p]
c_ = [v for (k, v) in s_p]
for_ans = {el: 0 for el in p}
money = 0
set_ = set()
dict_ = defaultdict(int)
for i in range(min(k + 1, n)):
	money += c_[i]
	for_ans[p_[i]] = money
	set_.add(c_[i])
	dict_[c_[i]] += 1
for i in range(k + 1, n):
	min_ = min(set_)
	set_.add(c_[i])
	dict_[c_[i]] += 1
	dict_[min_] -= 1
	if dict_[min_] == 0:
		set_.remove(min_)
	money += c_[i] - min_
	for_ans[p_[i]] = money
ans = [for_ans[el] for el in p]
print(' '.join(map(str, ans)))


(n, k) = map(int, input().split())
power = list(map(int, input().split()))
coins = list(map(int, input().split()))
ans = [0 for i in range(n)]
coins_on_sale = []
power_index = [[power[i], i] for i in range(n)]
power_index.sort()
for i in range(n):
	coin_wallet = 0
	for coin in coins_on_sale:
		coin_wallet += coin
	ans[power_index[i][1]] = coin_wallet + coins[power_index[i][1]]
	coins_on_sale.append(coins[power_index[i][1]])
	coins_on_sale.sort()
	if len(coins_on_sale) > k:
		coins_on_sale = coins_on_sale[1:]
for i in ans:
	print(i, end=' ')


import sys
import bisect

def main():
	(n, k) = map(int, sys.stdin.readline().split())
	p = list(map(int, sys.stdin.readline().split()))
	c = list(map(int, sys.stdin.readline().split()))
	pc = sorted(zip(p, c, range(n)))
	richest = [0 for _ in range(k)]
	res = [0 for _ in range(n)]
	if k == 0:
		print(' '.join(map(str, c)))
		return
	for i in range(n):
		(_, coins, index) = pc[i]
		res[index] = sum(richest) + coins
		bisect.insort(richest, coins)
		richest = richest[-k:]
	print(' '.join(map(str, res)))
main()


from heapq import heappush, heappop
(n, k) = map(int, input().split())
powers = list(map(int, input().split()))
coins = list(map(int, input().split()))
A = []
ans = [0] * n
for i in range(n):
	A.append((powers[i], coins[i], i))
A.sort()
h = []
total = 0
for i in range(n):
	(_, c, idx) = A[i]
	ans[idx] = total + c
	if len(h) < k:
		heappush(h, c)
		total += c
	elif h and h[0] < c:
		total -= heappop(h)
		heappush(h, c)
		total += c
for x in ans:
	print(x, end=' ')
print()


R = lambda : map(int, input().split())
(n, k) = R()
v = []
t = [0] * n
for (p, c, i) in sorted(zip(R(), R(), range(n))):
	t[i] = sum(v) + c
	v += [c]
	v = sorted(v)[::-1]
	if len(v) > k:
		v.pop()
print(' '.join(map(str, t)))


import heapq as q
(n, k) = [int(x) for x in input().split()]
p = list(map(int, input().split()))
c = list(map(int, input().split()))
m = []
for i in range(n):
	m.append([p[i], c[i], i])
m.sort(key=lambda x: x[0])
heap = []
ans = [0] * n
c = 0
for i in range(n):
	c += m[i][1]
	ans[m[i][2]] = c
	q.heappush(heap, m[i][1])
	if len(heap) > k:
		c -= q.heappop(heap)
print(*ans)


import bisect
import heapq
read = lambda : map(int, input().split())
(n, k) = read()
p = list(read())
c = list(read())
t = []
for i in range(n):
	t.append((i, p[i], c[i]))
t.sort(key=lambda x: x[1])
(index, power, coin) = zip(*t)
ans = [0] * n
s = [0] * n
h = []
for i in range(n):
	idx = bisect.bisect_left(power, t[i][1])
	ans[t[i][0]] = t[i][2]
	if idx > 0:
		ans[t[i][0]] += s[idx - 1]
	heapq.heappush(h, t[i][2])
	if len(h) > k:
		heapq.heappop(h)
	s[i] = sum(h)
print(*ans)


R = lambda : map(int, input().split())
(n, k) = R()
l = []
ans = [0] * n
s = 0
a = sorted(zip(R(), R(), range(n)))
for (p, c, i) in a:
	s += c
	ans[i] = s
	l = sorted(l + [c])
	if len(l) > k:
		s -= l[0]
		del l[0]
for i in ans:
	print(i, end=' ')


from typing import NamedTuple

class Knight(NamedTuple):
	idx: int
	wealth: int
	strength: int
(knights_nr, max_kills_nr) = (int(x) for x in input().split())
idx___str = tuple((int(x) for x in input().split()))
idx___wlt = tuple((int(x) for x in input().split()))
knights = [Knight(idx, wlt, str) for (idx, (str, wlt)) in enumerate(zip(idx___str, idx___wlt))]
knights = sorted(knights, key=lambda x: x.strength)
top_wealths = []
idx___max_wealth = [-1] * knights_nr
for knight in knights:
	idx___max_wealth[knight.idx] = sum(top_wealths) + knight.wealth
	top_wealths.append(knight.wealth)
	top_wealths = sorted(top_wealths, reverse=True)[:max_kills_nr]
print(*idx___max_wealth)


import heapq
import time

class Knignt:

	def __init__(self, index, power, coin):
		self.index = index
		self.power = power
		self.coin = coin
arrInput = []
arrInput.append(input())
arrInput.append(input())
arrInput.append(input())
(N, K) = map(int, arrInput[0].split())
arrKnights = [Knignt(n, 0, 0) for n in range(N)]
P = list(map(int, arrInput[1].split()))
C = list(map(int, arrInput[2].split()))
for n in range(N):
	arrKnights[n].power = P[n]
	arrKnights[n].coin = C[n]
arrKnightsSortByPower = sorted(arrKnights, key=lambda x: x.power)
selected = []
heapq.heapify(selected)
for n in range(N):
	knightIdx = arrKnightsSortByPower[n].index
	heapq.heappush(selected, arrKnightsSortByPower[n].coin)
	eachMaxCoin = sum(selected)
	if K < len(selected):
		heapq.heappop(selected)
	arrKnights[knightIdx].coin = eachMaxCoin
ans = ''
for n in range(N):
	if 0 < n:
		ans += ' '
	ans += str(arrKnights[n].coin)
print(ans)


from heapq import heappush, heappushpop
(n, k) = map(int, input().split())
pp = list(map(int, input().split()))
id = sorted(range(n), key=pp.__getitem__)
cc = list(map(int, input().split()))
(rr, hh, s, t) = (cc[:], [], 0, 0)
for i in id:
	p = pp[i]
	while pp[id[t]] < p:
		c = cc[id[t]]
		if len(hh) < k:
			s += c
			heappush(hh, c)
		else:
			s += c - heappushpop(hh, c)
		t += 1
	rr[i] += s
print(' '.join(map(str, rr)))


f = lambda : map(int, input().split())
(n, k) = f()
a = []
ans = [0] * n
sum = 0
for (x, c, i) in sorted(zip(f(), f(), range(n))):
	sum += c
	ans[i] = sum
	a = sorted(a + [c])
	if len(a) > k:
		sum -= a[0]
		del a[0]
print(*ans)


(n, k) = tuple(map(lambda x: int(x), input().split(' ')))
p = list(map(lambda x: int(x), input().split(' ')))
c = list(map(lambda x: int(x), input().split(' ')))
out = [i for i in c]
t = [(p[i], i) for i in range(n)]
import heapq
kbest = []
t.sort()
prev = 0
i = 0
while i < len(t):
	j = i
	while j < len(t) and t[j][0] == t[i][0]:
		out[t[j][1]] += sum(kbest)
		j += 1
	while i < j:
		heapq.heappush(kbest, c[t[i][1]])
		if len(kbest) > k:
			heapq.heappop(kbest)
		i += 1
print(' '.join(map(lambda x: str(x), out)))


(n, k) = [int(i) for i in input().split()]
power = [int(i) for i in input().split()]
money = [int(i) for i in input().split()]
r = []
for i in range(n):
	r.append((power[i], money[i], i))
r.sort()
cur = 0
ans = [None for i in range(n)]
for i in range(k + 1):
	cur += r[i][1]
	ans[i] = cur
import heapq
pq = []
for i in range(k + 1):
	pq.append(r[i][1])
heapq.heapify(pq)
for i in range(k + 1, n):
	a = heapq.heappop(pq)
	ans[i] = ans[i - 1] - a + r[i][1]
	heapq.heappush(pq, r[i][1])
end = [None for i in range(n)]
for i in range(n):
	end[r[i][2]] = ans[i]
for i in range(n):
	end[i] = str(end[i])
print(' '.join(end))


[n, k] = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
pc = []
for i in range(0, n):
	pc.append([i, p[i], c[i]])
pc = sorted(pc, key=lambda x: x[1])
total = 0
sk = []
ok = []
if k != 0:
	for i in range(0, n):
		if i < k:
			sk.append(pc[i][2])
			total = sum(sk)
			ok.append([pc[i][0], total])
		else:
			sk = sorted(sk)
			if sk[0] < pc[i][2]:
				total = sum(sk) + pc[i][2]
				sk.pop(0)
				sk.append(pc[i][2])
				ok.append([pc[i][0], total])
			else:
				total = sum(sk) + pc[i][2]
				ok.append([pc[i][0], total])
	ok = sorted(ok, key=lambda x: x[0])
	for e in ok:
		print(e[1], end=' ')
else:
	for e in c:
		print(e, end=' ')


import operator

class Knight:

	def __init__(self, power, coins, index):
		self.power = power
		self.coins = coins
		self.index = index

class MaxKnight:

	def __init__(self, coins, index):
		self.coins = coins
		self.index = index
nk = input().split(' ')
n = int(nk[0])
k = int(nk[1])
p = input().split(' ')
c = input().split(' ')
for i in range(n):
	p[i] = int(p[i])
	c[i] = int(c[i])
knights = []
for i in range(n):
	kn = Knight(p[i], c[i], i)
	knights.append(kn)
knights.sort(key=operator.attrgetter('power'))
max_c = []
max_knights = []
for i in range(n):
	max_knights.append(sum(max_c) + knights[i].coins)
	if len(max_c) < k:
		max_c.append(knights[i].coins)
	else:
		coins = knights[i].coins
		try:
			if coins > min(max_c):
				max_c.remove(min(max_c))
				max_c.append(coins)
		except:
			pass
for i in range(n):
	max_knights[i] = MaxKnight(max_knights[i], knights[i].index)
max_knights.sort(key=operator.attrgetter('index'))
print(' '.join((str(knight.coins) for knight in max_knights)))


(n, k) = map(int, input().split(' '))
ps = list(map(int, input().split(' ')))
cs = list(map(int, input().split(' ')))
res = [0] * n
kns = sorted(((p, c, i) for (i, (p, c)) in enumerate(zip(ps, cs))))
kmax = []
pcount = [kns[0][1]]
res[kns[0][2]] = cs[kns[0][2]]
for (j, (p, c, i)) in list(enumerate(kns))[1:]:
	if p > kns[j - 1][0]:
		kmax = sorted(kmax + pcount, reverse=True)[:k]
		res[i] = sum(kmax) + c
		pcount = []
	pcount += [c]
print(*res)


(N, K) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
rec = []
for i in range(N):
	rec.append((p[i], c[i], i))
rec = sorted(rec, key=lambda x: x[0])
tmp = []
num = []
for i in range(N):
	if len(tmp) < K:
		tmp.append(rec[i][1])
	else:
		tmp = sorted(tmp, reverse=True)
		tmp = tmp[:K] + [rec[i][1]]
	num.append((sum(tmp), rec[i][2]))
num = sorted(num, key=lambda x: x[1])
print(' '.join(map(lambda x: str(x[0]), num)))


import sys
input = sys.stdin.readline
from heapq import heapify, heappush, heappop
(n, k) = map(int, input().split())
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))
if k == 0:
	print(*clst)
	sys.exit()
pc = sorted(((p, c, i) for (i, (p, c)) in enumerate(zip(plst, clst))), key=lambda t: (t[0], t[2]))
res = [0] * n
pq = []
pq_sum = 0
pq_size = 0
for (p, c, i) in pc:
	if i > 0 and plst[i] == plst[i - 1]:
		res[i] = res[i - 1]
	else:
		res[i] = pq_sum + c
	if pq_size < k:
		heappush(pq, c)
		pq_sum += c
		pq_size += 1
	else:
		alt = heappop(pq)
		if alt < c:
			heappush(pq, c)
			pq_sum += c - alt
		else:
			heappush(pq, alt)
print(*res)


from collections import OrderedDict as od
(n, k) = list(map(int, input().split()))
pointsArray = [0] * k
di = {}
indexOrder = list(map(int, input().split()))
index = 0
for i in map(int, input().split()):
	di[indexOrder[index]] = i
	index += 1
di = od(sorted(di.items()))
if k == 0:
	for i in indexOrder:
		print(di[i], end=' ')
else:
	result = {}
	for (key, value) in di.items():
		result[key] = value + sum(pointsArray)
		pointsArray[0] = max(pointsArray[0], value)
		pointsArray.sort()
	for i in indexOrder:
		print(result[i], end=' ')


(n, k) = map(int, input().split())
pws = list(map(int, input().split()))
cns = list(map(int, input().split()))
num = [i for i in range(n)]
comb = list(zip(pws, num, cns))
comb.sort()
less = [0] * k
for i in range(n):
	if i > 0:
		less.append(comb[i - 1][2])
	less.sort(reverse=True)
	less = less[0:k]
	num[comb[i][1]] = sum(less) + comb[i][2]
print(*num)


def __starting_point():
	(n, k) = map(int, input().split())
	knight = [[i, 0, 0, 0] for i in range(n)]
	power = list(map(int, input().split()))
	money = list(map(int, input().split()))
	for i in range(n):
		knight[i][1] = power[i]
		knight[i][2] = money[i]
	knight.sort(key=lambda x: x[1])
	yong = []
	yong_sum = 0
	for i in range(n):
		knight[i][3] = knight[i][2] + yong_sum
		if len(yong) < k:
			yong.append(knight[i][2])
			yong.sort()
			yong_sum = sum(yong)
		elif k == 0:
			continue
		elif yong[0] < knight[i][2]:
			yong[0] = knight[i][2]
			yong.sort()
			yong_sum = sum(yong)
	knight.sort(key=lambda x: x[0])
	for i in range(n - 1):
		print(knight[i][3], end='')
		print(' ', end='')
	print(knight[-1][3])
__starting_point()


(n, k) = [int(s) for s in input().split()]
p = [int(s) for s in input().split()]
p = [(p[i], i) for i in range(n)]
c = [int(s) for s in input().split()]
c = [[c[i], i] for i in range(n)]
p.sort(key=lambda x: x[0], reverse=True)
c_dic = {}
for x in c:
	c_dic[x[1]] = x[0]
c.sort(key=lambda x: x[0], reverse=True)
ans_set = {}
used = set()
i = 0
cur_ans = 0
cur_k = 0
for j in range(n):
	x = p[j]
	if x[1] not in used:
		used.add(x[1])
	else:
		cur_k -= 1
		cur_ans -= c_dic[x[1]]
	while i < n:
		if cur_k == k:
			break
		if c[i][1] in used:
			i += 1
			continue
		cur_ans += c[i][0]
		cur_k += 1
		used.add(c[i][1])
		i += 1
	ans_set[x[1]] = cur_ans
for i in range(n):
	print(ans_set[i] + c_dic[i], end=' ')


(n, k) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
z = [[pi, ci, i] for (i, (pi, ci)) in enumerate(zip(p, c))]
z.sort()
from bisect import bisect_left
a = [0] * n
m = [0] * k
for i in range(n):
	(pi, ci, ii) = z[i]
	a[ii] = ci + sum(m)
	insert_at = bisect_left(m, ci)
	if insert_at > 0:
		m.insert(insert_at, ci)
		del m[0]
print(*a)


import heapq

def main():
	(n, k) = map(int, input().split())
	power = list(map(int, input().split()))
	coins = list(map(int, input().split()))
	power_coins = sorted(zip(power, coins))
	result = {power[i]: coins[i] for i in range(n)}
	kills = 0
	coins = 0
	min_heap = []
	for (p, c) in power_coins:
		result[p] += coins
		if kills < k:
			kills += 1
			heapq.heappush(min_heap, c)
			coins += c
		elif k > 0:
			if c > min_heap[0]:
				coins -= min_heap[0]
				coins += c
				heapq.heapreplace(min_heap, c)
	print(' '.join((str(result[p]) for p in power)))
main()


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
sys.setrecursionlimit(10000)

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, K, P, C):
	P_ = sorted([(p, i) for (i, p) in enumerate(P)])
	conins = []
	ans = []
	for (p, i) in P_:
		ans.append((i, C[i] + sum(conins)))
		heapq.heappush(conins, C[i])
		if len(conins) > K:
			heapq.heappop(conins)
	return ' '.join(map(lambda x: str(x[1]), sorted(ans)))

def main():
	(N, K) = read_int_n()
	P = read_int_n()
	C = read_int_n()
	print(slv(N, K, P, C))
main()


R = lambda : map(int, input().split())
(n, k) = R()
p = [[i] for i in R()]
for (i, j) in enumerate(R()):
	p[i].append(j)
L = sorted(p)
A = []
d = {}
d[L[0][0]] = L[0][1]
for i in range(1, n):
	if len(A) < k:
		A.append(L[i - 1][1])
	elif len(A) > 0 and L[i - 1][1] > min(A):
		A.remove(min(A))
		A.append(L[i - 1][1])
	d[L[i][0]] = sum(A) + L[i][1]
for i in range(n):
	print(d[p[i][0]], end=' ')


import heapq
(n, k) = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
X = []
for i in range(n):
	X.append([P[i], C[i], i])
X.sort(key=lambda x: x[0])
coins = []
heapq.heapify(coins)
curr = 0
res = [0 for i in range(n)]
for i in range(k):
	heapq.heappush(coins, X[i][1])
	curr += X[i][1]
	res[X[i][2]] = curr
for j in range(k, n):
	res[X[j][2]] = X[j][1] + sum(coins)
	if len(coins) > 0:
		x = heapq.heappop(coins)
		if x < X[j][1]:
			heapq.heappush(coins, X[j][1])
		else:
			heapq.heappush(coins, x)
print(*res)


def main():
	(n, k) = list(map(int, input().split()))
	(knights, ans) = ([i for i in range(n)], [0] * n)
	(powers, coins) = (list(map(int, input().split())), list(map(int, input().split())))
	lordOfTheLists = list(zip(knights, powers, coins))
	lordOfTheLists.sort(key=lambda a: a[1])
	for i in range(k):
		ans[lordOfTheLists[i][0]] = lordOfTheLists[i][2] + sum([knight[2] for knight in lordOfTheLists[:i]])
	temp = lordOfTheLists[:k]
	for i in range(k, n):
		ans[lordOfTheLists[i][0]] = lordOfTheLists[i][2] + sum([knight[2] for knight in temp])
		temp += lordOfTheLists[i:i + 1]
		temp.sort(key=lambda a: -a[2])
		temp = temp[:-1]
	ans = ' '.join([str(i) for i in ans])
	print(ans)
	return 0
main()


(n, k) = map(int, input().split())
p = list(map(int, input().split()))
pcop = p.copy()
c = list(map(int, input().split()))
dic = dict(zip(p, c))
p.sort()
li = []
dic1 = {}
for i in range(min(k, n)):
	li.append(dic[p[i]])
	dic1[p[i]] = sum(li)
if n > k and len(li) != 0:
	for i in p[k:]:
		li.sort()
		dic1[i] = sum(li) + dic[i]
		if li[0] < dic[i]:
			del li[0]
			li.append(dic[i])
else:
	for i in p[k:]:
		dic1[i] = dic[i]
for m in range(n):
	print(dic1[pcop[m]], end=' ')


inf = 1000000005
I = input().split()
n = int(I[0])
k = int(I[1])
A = [(0, 0, 0)] * n
res = [0] * n
naj = [0] * 20
suma = 0
P = input().split()
C = input().split()
for i in range(0, n):
	A[i] = (int(P[i]), int(C[i]), i)
A.sort()
for i in range(0, n):
	res[A[i][2]] = suma + A[i][1]
	ind = 0
	mini = inf
	for j in range(0, k):
		if naj[j] < mini:
			mini = naj[j]
			ind = j
	if A[i][1] > mini:
		suma -= naj[ind]
		naj[ind] = A[i][1]
		suma += naj[ind]
for i in range(0, n):
	print(res[i], end=' ')


(n, k) = input().split(' ')
n = int(n)
k = int(k)
power2 = input().split(' ')
gold = input().split(' ')
prof = [0] * k
sums = dict()
s = 0
d = dict()
for i in range(n):
	power2[i] = int(power2[i])
	gold[i] = int(gold[i])
	d[power2[i]] = gold[i]
power = sorted(power2)
for i in range(n):
	ind = power[i]
	coin = d[ind]
	for j in range(k):
		if prof[j] >= coin:
			sums[ind] = s + coin
			prof.insert(j, coin)
			s = s - prof[0] + coin
			del prof[0]
			break
	else:
		sums[ind] = s + coin
		prof.append(coin)
		s = s - prof[0] + coin
		del prof[0]
st = ''
for elem in power2:
	st += str(sums[elem]) + ' '
print(st)


import configparser
import math
import sys
input = sys.stdin.readline

def main():
	(n, k) = [int(x) for x in input().split(' ')]
	powers = [int(x) for x in input().split(' ')]
	coins = [int(x) for x in input().split(' ')]
	pnts = []
	for i in range(n):
		pnts.append((powers[i], coins[i], i))
	pnts = sorted(pnts, key=lambda x: x[0])
	answers = [0 for i in range(n)]
	cur_best = []
	for i in range(n):
		if k != 0:
			answers[pnts[i][2]] = sum(cur_best) + pnts[i][1]
			cur_best.append(pnts[i][1])
			cur_best = sorted(cur_best)
			cur_best = cur_best[-k:]
		else:
			answers[pnts[i][2]] = pnts[i][1]
	for i in answers:
		print(i, end=' ')
main()


r_int = lambda : map(int, input().split())
(n, k) = r_int()
knight = list(zip(range(n), r_int(), r_int()))
result = [0] * n
knight.sort(key=lambda x: x[1])
yong = []
yong_sum = 0
for i in range(n):
	result[knight[i][0]] = knight[i][2] + yong_sum
	if k == 0:
		continue
	elif len(yong) < k:
		yong.append(knight[i][2])
		yong_sum += knight[i][2]
		yong.sort()
	elif yong[0] < knight[i][2]:
		yong_sum = yong_sum + knight[i][2] - yong[0]
		yong[0] = knight[i][2]
		yong.sort()
print(' '.join((str(result[i]) for i in range(n))))


import bisect
(n, k) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
if k == 0:
	print(' '.join(map(str, c)))
	exit(0)
index_map = dict(zip(p, list(range(n))))
power_coin_map = dict(zip(p, c))
sorted_power = sorted(power_coin_map)
ans = [power_coin_map[sorted_power[0]]]
curr_l = [ans[0]]
for i in range(1, n):
	coin = power_coin_map[sorted_power[i]]
	ans.append(coin + sum(curr_l))
	if len(curr_l) < k:
		bisect.insort(curr_l, coin)
	elif coin > curr_l[0]:
		curr_l = curr_l[1:]
		bisect.insort(curr_l, coin)
real_ans = [0] * n
for i in range(n):
	power = sorted_power[i]
	real_ans[index_map[power]] = ans[i]
print(' '.join(map(str, real_ans)))


import heapq
(n, k) = list(map(int, input().strip().split()))
p = list(map(int, input().strip().split()))
c = list(map(int, input().strip().split()))
p = sorted([(x, i) for (i, x) in enumerate(p)], key=lambda x: x[0])
ans = []
top_k = []
cur_gold = 0
for (i, t) in enumerate(p):
	if k == 0:
		ans.append((c[t[1]], t[1]))
	elif i < k:
		cur_gold += c[t[1]]
		ans.append((cur_gold, t[1]))
		heapq.heappush(top_k, c[t[1]])
	else:
		smallest = heapq.nsmallest(1, top_k)[0]
		if smallest < c[t[1]]:
			cur_gold += c[t[1]]
			ans.append((cur_gold, t[1]))
			heapq.heappop(top_k)
			heapq.heappush(top_k, c[t[1]])
			cur_gold -= smallest
		else:
			ans.append((cur_gold + c[t[1]], t[1]))
ans = sorted(ans, key=lambda x: x[1])
print(' '.join(map(lambda x: str(x[0]), ans)))


(n, k) = map(int, input().split())
p = [*map(int, input().split())]
c = {i: int(x) for (i, x) in enumerate(input().split())}
d = {x: i for (i, x) in enumerate(p)}
p.sort()
(lst, res) = ([], list(c.values()))
for (i, x) in enumerate(p):
	res[d[x]] += sum(lst)
	lst.append(c[d[x]])
	lst.sort()
	if i >= k:
		lst.pop(0)
print(*res)


(n, k) = [int(x) for x in input().split()]
p = [(int(x), i) for (i, x) in enumerate(input().split())]
c = [int(x) for x in input().split()]
p.sort()
h = []
import heapq
total = 0
ans = [0 for _ in c]
for (pw, ind) in p:
	ans[ind] = total + c[ind]
	if k > 0 and (len(h) < k or h[0] < c[ind]):
		heapq.heappush(h, c[ind])
		total += c[ind]
		if len(h) > k:
			total -= heapq.heappop(h)
print(' '.join(map(str, ans)))


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = list(map(int, input().split()))
for q in range(n):
	a[q] = [a[q], s[q], q]
a.sort()
(b, d, z) = ([0] * k, 0, [[] for q in range(n)])
for q in range(n):
	z[a[q][2]] = d + a[q][1]
	(l, j) = (0, 0)
	for q1 in range(k):
		if l == 1:
			(b[q1], j) = (j, b[q1])
		elif a[q][1] > b[q1]:
			(l, j) = (1, b[q1])
			d += a[q][1] - b[-1]
			b[q1] = a[q][1]
print(*z)


(n, k) = map(int, input().split())
p = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a = []
count_list = []
count2 = []
for i in range(n):
	a.append((p[i], c[i], i))
a.sort()
count = 0
for i in range(n):
	if i <= k and i != 0 and (k != 0):
		count += a[i - 1][1]
		count2.append(a[i - 1][1])
	elif i > k and k != 0:
		count2.sort()
		if a[i - 1][1] > count2[0]:
			count += a[i - 1][1] - count2[0]
			count2.pop(0)
			count2.append(a[i - 1][1])
	count += a[i][1]
	count_list.append((count, a[i][2]))
	count -= a[i][1]
count_list.sort(key=lambda x: x[1])
for i in range(n):
	print(count_list[i][0], end=' ')


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
l = []
for i in range(n):
	x = a[i]
	y = b[i]
	l.append((x, y))
l.sort()
ans = []
j = 0
if k == 0:
	print(*b)
else:
	for i in range(n):
		if i < k:
			ans.append(l[i][1])
			ans.sort()
			d[l[i][0]] = sum(ans)
		else:
			d[l[i][0]] = sum(ans) + l[i][1]
			if min(ans) < l[i][1]:
				ind = ans.index(min(ans))
				ans[ind] = l[i][1]
	for i in range(n):
		print(d[a[i]], end=' ')


import heapq as q
(n, k) = map(int, input().split())
knights = sorted(zip(map(int, input().split()), map(int, input().split()), range(n)))
heap = []
value = 0
result = [0] * n
for (power, coin, index) in knights:
	value += coin
	result[index] = value
	q.heappush(heap, coin)
	if len(heap) > k:
		value -= q.heappop(heap)
print(*result)


a = input()
b = input()
c = input()
info = a.split()
info = list(map(int, info))
y = b.split()
powers = list(map(int, y))
z = c.split()
coins = list(map(int, z))
l = sorted(zip(powers, coins, range(info[0])))
final = [0] * info[0]
s = 0
w = []
for (_, c, i) in l:
	s += c
	final[i] = s
	w = sorted(w + [c])
	if len(w) > info[1]:
		s -= w[0]
		del w[0]
print(*final)


from heapq import heappop, heappush
(knight_count, k) = map(int, input().split())
powers = list(map(int, input().split()))
coins = list(map(int, input().split()))
data = []
for i in range(len(powers)):
	data.append((powers[i], coins[i], i))
data = sorted(data)
result = [(data[0][2], data[0][1])]
suma = 0
knights_to_kill = []
for i in range(1, len(data)):
	if len(knights_to_kill) < k:
		heappush(knights_to_kill, data[i - 1][1])
		suma += data[i - 1][1]
	elif k != 0 and knights_to_kill[0] < data[i - 1][1]:
		suma -= heappop(knights_to_kill)
		heappush(knights_to_kill, data[i - 1][1])
		suma += data[i - 1][1]
	result.append((data[i][2], suma + data[i][1]))
print(' '.join(map(lambda x: str(x[1]), sorted(result))))


from heapq import heapify, heappush, heappushpop
(n, k) = list(map(int, input().split()))
power = list(map(int, input().split()))
coins = list(map(int, input().split()))
knights = [i for i in range(n)]
knights.sort(key=lambda x: power[x])
cursum = 0
curcoin = []
heapify(curcoin)
res = [0] * n
for i in range(min(k, n)):
	res[knights[i]] = cursum + coins[knights[i]]
	heappush(curcoin, coins[knights[i]])
	cursum += coins[knights[i]]
for i in range(min(k, n), n):
	res[knights[i]] = cursum + coins[knights[i]]
	cursum += coins[knights[i]] - heappushpop(curcoin, coins[knights[i]])
print(*res)


(n, k) = map(int, input().split())
(p, c) = (list(map(int, input().split())), list(map(int, input().split())))
knights = list(zip(p, range(n), c))
knights.sort()
ans = [0] * n
less = [0] * k
for i in range(n):
	if i > 0:
		less.append(knights[i - 1][2])
	less.sort(reverse=True)
	less = less[:k]
	ans[knights[i][1]] = sum(less) + knights[i][2]
print(*ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 998244353
dd = [(0, -1), (1, 0), (0, 1), (-1, 0)]
ddn = [(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, -1), (-1, 0), (-1, 1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	(n, m) = LI()
	a = LI()
	b = LI()
	c = []
	if m == 0:
		return ' '.join(map(str, b))
	for i in range(n):
		c.append((a[i], b[i], i))
	c.sort()
	t = []
	rr = {}
	for (d, e, i) in c:
		tr = e
		tr += sum(t)
		rr[i] = tr
		if len(t) < m:
			t.append(e)
		else:
			tm = min(t)
			if tm < e:
				tmi = t.index(tm)
				t[tmi] = e
	r = []
	for i in range(n):
		r.append(rr[i])
	return ' '.join(map(str, r))
print(main())


(n, tk) = map(int, input().split())
p = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
pc = [[i, p[i], c[i]] for i in range(n)]
pc.sort(key=lambda x: x[-2])
victim = []
x = 0
l = 0
end = []
for i in range(n):
	if l <= tk:
		victim.append(pc[i][-1])
		x += pc[i][-1]
		l += 1
		end.append([pc[i][0], x])
	else:
		victim.sort()
		x += pc[i][-1]
		x -= victim[0]
		del victim[0]
		victim.append(pc[i][-1])
		end.append([pc[i][0], x])
end.sort()
print(' '.join([str(r[-1]) for r in end]))


(n, k) = map(int, input().split())
p = [int(elem) for elem in input().split()]
c = [int(elem) for elem in input().split()]
pair = []
chisles = []
for i in range(n):
	pair.append((p[i], c[i], i))
pair.sort()
anses = [0] * n
for i in range(n):
	if i <= k:
		summ = 0
		chisles = []
		for j in range(i):
			summ = summ + pair[j][1]
			chisles.append(pair[j][1])
		summ = summ + pair[i][1]
		chisles.append(pair[i][1])
		anses[pair[i][2]] = summ
	else:
		chisles.sort(reverse=True)
		chisles = chisles[:k]
		summ = sum(chisles[:k]) + pair[i][1]
		anses[pair[i][2]] = summ
		chisles.append(pair[i][1])
for i in range(n):
	print(anses[i], end=' ')


from heapq import *
(n, k) = map(int, input().split())
p = [int(X) for X in input().split()]
c = [int(x) for x in input().split()]
p = [[p[i], c[i], i] for i in range(n)]
p.sort()
j = 0
an = [0] * n
an[p[0][2]] = p[0][1]
z = []
heapify(z)
for i in range(1, n):
	s = p[i][1]
	if p[i][0] != p[i - 1][0]:
		while j < i:
			heappush(z, -1 * p[j][1])
			j += 1
	tt = []
	for __ in range(k):
		tt.append(heappop(z))
		if not z:
			break
	for v in tt:
		s += abs(v)
		heappush(z, v)
	an[p[i][2]] = s
print(*an)


(n, k) = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
s = []
for i in range(n):
	s.append([i, p[i], c[i]])
s.sort(key=lambda x: x[1])
tk = 0
m = []
for i in range(n):
	coin = s[i][2] + sum(m)
	s[i].append(coin)
	if tk < k:
		m.append(s[i][2])
		tk += 1
	elif len(m) > 0:
		if min(m) < s[i][2]:
			m.remove(min(m))
			m.append(s[i][2])
s.sort()
for a in s:
	print(a[3], end=' ')


(n, k) = map(int, input().split())
l = input()
l = [int(i) for i in l.split()]
x = input()
x = [int(i) for i in x.split()]
d = []
for i in range(n):
	d.append((l[i], x[i], i))
if k == 0:
	for i in x:
		print(i, end=' ')
else:
	d.sort()
	minn = []
	ans = 0
	temp = [0] * n
	for i in range(n):
		if i <= k:
			if i < k:
				ans += d[i][1]
				minn.append(d[i][1])
				temp[d[i][2]] = ans
			else:
				temp[d[i][2]] = ans + d[i][1]
		else:
			if min(minn) < d[i - 1][1]:
				ans = ans - min(minn) + d[i - 1][1]
				minn.remove(min(minn))
				minn.append(d[i - 1][1])
			temp[d[i][2]] = ans + d[i][1]
	print(*temp)


(n, k) = map(int, input().split())
pov = list(map(int, input().split()))
coin = list(map(int, input().split()))
vse = []
for i in range(n):
	vse.append((pov[i], coin[i], i))
if k == 0:
	print(' '.join((str(n) for n in coin)))
else:
	vse.sort()
	ans = []
	k_set = []
	for i in range(n):
		ans.append((vse[i][2], sum(k_set) + vse[i][1]))
		if len(k_set) < k:
			k_set.append(vse[i][1])
		else:
			minn = min(k_set)
			if minn >= vse[i][1]:
				pass
			else:
				k_set.remove(minn)
				k_set.append(vse[i][1])
	ans.sort()
	print(' '.join((str(n[1]) for n in ans)))


import sys
(n, m) = list(map(int, input().split()))
s = list(map(int, input().split()))
s1 = list(map(int, input().split()))
c = []
if m == 0:
	for i in s1:
		print(i, end=' ')
	sys.exit()
for i in range(n):
	c.append([s[i]] + [s1[i]] + [i])
c.sort()
ans = [-1 for i in range(n)]
dp = [c[i][1] for i in range(n)]
dp[0] = c[0][1]
ans[c[0][2]] = dp[0]
mx = []
dpp = 0
for i in range(n):
	dp[i] += dpp
	if len(mx) < m:
		mx.append(c[i][1])
		dpp += c[i][1]
	else:
		mx.sort()
		if mx[0] < c[i][1]:
			dpp += c[i][1] - mx[0]
			mx = mx[1:] + [c[i][1]]
	ans[c[i][2]] = dp[i]
	if m == 0:
		ans[c[i][2]] = c[i][1]
for i in ans:
	print(i, end=' ')


(n, k) = map(int, input().split())
power = list(map(int, input().split()))
coins = list(map(int, input().split()))
arr = []
for i in range(n):
	arr.append([power[i], coins[i], i])
arr.sort()
d = []
summ = 0
ans = []
if k == 0:
	for i in coins:
		print(i, end=' ')
else:
	for i in range(n):
		ans.append([arr[i][2], arr[i][1] + summ])
		if len(d) < k:
			d.insert(0, arr[i][1])
			d.sort(reverse=True)
			summ += arr[i][1]
		else:
			if d[len(d) - 1] >= arr[i][1]:
				continue
			if d[0] <= arr[i][1]:
				d.insert(0, arr[i][1])
				summ += d[0] - d[len(d) - 1]
				del d[len(d) - 1]
			else:
				d.insert(0, arr[i][1])
				summ += d[0] - d[len(d) - 1]
				del d[len(d) - 1]
				d.sort(reverse=True)
	ans.sort()
	for i in ans:
		print(i[1], end=' ')