PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000058. Tổng thành tích cao nhất (Best Performances)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng thành tích cao nhất (Best Performances)

Best Performances

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence A1,…,ANA_1,\ldots,A_N of nonnegative integers, initially all zero, and a fixed integer KK. After every assignment, print the sum of exactly the KK largest values currently in the entire sequence. Equal values count separately as many times as they appear.

Each update Xi,YiX_i,Y_i means assign AXi=YiA_{X_i}=Y_i rather than add YiY_i. Print the new top-KK sum immediately after applying each update.

Input

  • Line 1: integers N,K,QN,K,Q for array length, number of largest values, and update count.
  • Next QQ lines: update ii contains integers Xi,YiX_i,Y_i assigning the new value at position XiX_i.

Output

Print QQ lines; line ii gives the sum of the KK largest array entries after update ii.

Subtasks

Subtask 1 (20 points):

  • 1≤K≤N≤201\le K\le N\le 20; 1≤Q≤201\le Q\le 20; 1≤Xi≤N1\le X_i\le N, 0≤Yi≤1090\le Y_i\le10^9 for every update.

Subtask 2 (30 points):

  • 1≤K≤N≤20001\le K\le N\le 2000; 1≤Q≤20001\le Q\le 2000; 1≤Xi≤N1\le X_i\le N, 0≤Yi≤1090\le Y_i\le10^9 for every update.

Subtask 3 (50 points):

  • 1≤K≤N≤5000001\le K\le N\le 500000; 1≤Q≤5000001\le Q\le 500000; 1≤Xi≤N1\le X_i\le N, 0≤Yi≤1090\le Y_i\le10^9 for every update.

Examples

Examples 1

Input

4 2 10
1 5
2 1
3 3
4 2
2 10
1 0
4 0
3 1
2 0
3 0

Output

5
6
8
8
15
13
13
11
1
0

Explanation

Initially [0,0,0,0][0,0,0,0]. After updates 1–4, the top-two sums are respectively 5, 6, 8, 8. Update 5 makes the array [5,10,3,2][5,10,3,2] with top-two sum 15. Applying the remaining assignments yields sums 13,13,11,1,0 in sequence.

Examples 2

Input

3 3 4
2 5
1 4
3 7
2 0

Output

5
9
16
11

Explanation

Since K=N=3K=N=3, the top-three sum is the sum of the entire array. The states are [0,5,0][0,5,0], [4,5,0][4,5,0], [4,5,7][4,5,7], and [4,0,7][4,0,7], yielding 5,9,16,11.

The updates set A2=5A_2=5, A1=4A_1=4, A3=7A_3=7, and finally A2=0A_2=0. Because all three elements are included in every top-KK sum, sorting them in descending order does not change the sum. Adding the entries of each resulting state yields the four displayed outputs.