994B.md (2214B)
1 Source: https://codeforces.com/problemset/problem/994/B 2 3 B. Knights of a Polygonal Table 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 Now each knight ponders: how many coins he can have if only he kills other knights? 24 25 You should answer this question for each knight. 26 27 Input 28 29 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. 30 31 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. 32 33 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. 34 35 Output 36 37 Print $n$ integers — the maximum number of coins each knight can have it only he kills other knights. 38 39 Examples 40 41 Input 42 43 ``` 44 4 2 45 4 5 9 7 46 1 2 11 33 47 ``` 48 49 Output 50 51 ``` 52 1 3 46 36 53 ``` 54 55 Input 56 57 ``` 58 5 1 59 1 2 3 4 5 60 1 2 3 4 5 61 ``` 62 63 Output 64 65 ``` 66 1 3 5 7 9 67 ``` 68 69 Input 70 71 ``` 72 1 0 73 2 74 3 75 ``` 76 77 Output 78 79 ``` 80 3 81 ``` 82 83 Note 84 85 Consider the first example. 86 87 * The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. 88 * The second knight can kill the first knight and add his coin to his own two. 89 * 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$. 90 * The fourth knight should kill the first and the second knights: $33+1+2 = 36$. 91 92 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. 93 94 In the third example there is only one knight, so he can't kill anyone.