#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
 
#define mod 1000000007
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//------------------------------------------------------- 
pair<long,int> Je[200000];
vector<long> G[100000];
vector<long> G2[100000];
pair<long,int> O[100000];
int MaxThree[100000];
long ans[200001];
void Calc(){
	int N = rei();
	int K = rei();
	for(int i=0;i<N;i++){
		int c = rei()-1;
		long v = rei();
		Je[i] = {v,c};
	}
	sort(Je,Je+N);
	for(int i=N-1;i>=0;i--){
		G2[Je[i].second].push_back(Je[i].first);	
	}
	for(int i=0;i<K;i++){
		O[i] = {-G2[i][0]-G2[i][1],i};
	}
	sort(O,O+K);
	for(int i=0;i<K;i++){
		G[i] = G2[O[i].second];
	}
	MaxThree[K-1] = K;
	MaxThree[K] = K;
	for(int i=K-2;i>=0;i--){
		MaxThree[i] = MaxThree[i+1];
		if(G[i+1].size() >= 3 && (MaxThree[i] == K || G[i+1][0]+G[i+1][1]+G[i+1][2] >= G[MaxThree[i]][0]+G[MaxThree[i]][1]+G[MaxThree[i]][2])){
			MaxThree[i] = i+1;
		}
	}
	fill(ans,ans+N,-1);
	priority_queue<long> X;
	if(MaxThree[0] != K){
		ans[3] = G[MaxThree[0]][0] + G[MaxThree[0]][1] + G[MaxThree[0]][2];
	}
	long sum = G[0][0]+G[0][1];
	long min = 10000000000000000;
	for(int i=2;i<G[0].size();i++){
		X.push(G[0][i]);
	}
	ans[2] = sum;
	if(MaxThree[0] != K && N >= 5){
		ans[5] = sum + G[MaxThree[0]][0] + G[MaxThree[0]][1] + G[MaxThree[0]][2];
	}
	int p = 1;
	bool wait = false;
	for(int c=3;c<=N;c++){
		if(X.empty() && min == 10000000000000000){
			if(wait){
				wait = false;
				sum += G[p][0] + G[p][1];
				for(int i=2;i<G[p].size();i++){
					X.push(G[p][i]);
				}
				p++;
			}
			else{
				wait = true;
				continue;
			}
		}
		else{
			if(p < K && (X.empty() || min + X.top() < G[p][0] + G[p][1])){
				sum += G[p][0] + G[p][1] - min;
				X.push(min);
				min = 10000000000000000;
				for(int i=2;i<G[p].size();i++){
					X.push(G[p][i]);
				}
				p++;
			}
			else{
				sum += X.top();
				min = X.top();
				X.pop();
			}
		}
		ans[c] = max(ans[c],sum);
		if(c+3 <= N && MaxThree[p] != K){
			ans[c+3] = max(ans[c+3],sum + G[MaxThree[p]][0] + G[MaxThree[p]][1] + G[MaxThree[p]][2]);
		}
	}
	for(int i=1;i<=N;i++){
		cout << ans[i] << "\n";
	}
}
int main(int argc,char** argv){
	ios::sync_with_stdio(false), cin.tie(0);
	cout.tie(0); Calc(); return 0;
}
### Prompt

Please create a solution in CPP to the following problem:

There are N jewels, numbered 1 to N. The color of these jewels are represented by integers between 1 and K (inclusive), and the color of Jewel i is C_i. Also, these jewels have specified values, and the value of Jewel i is V_i.

Snuke would like to choose some of these jewels to exhibit. Here, the set of the chosen jewels must satisfy the following condition:

* For each chosen jewel, there is at least one more jewel of the same color that is chosen.



For each integer x such that 1 \leq x \leq N, determine if it is possible to choose exactly x jewels, and if it is possible, find the maximum possible sum of the values of chosen jewels in that case.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq K \leq \lfloor N/2 \rfloor
* 1 \leq C_i \leq K
* 1 \leq V_i \leq 10^9
* For each of the colors, there are at least two jewels of that color.
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N K
C_1 V_1
C_2 V_2
:
C_N V_N


Output

Print N lines. In the i-th line, if it is possible to choose exactly i jewels, print the maximum possible sum of the values of chosen jewels in that case, and print -1 otherwise.

Examples

Input

5 2
1 1
1 2
1 3
2 4
2 5


Output

-1
9
6
14
15


Input

5 2
1 1
1 2
2 3
2 4
2 5


Output

-1
9
12
12
15


Input

8 4
3 2
2 3
4 5
1 7
3 11
4 13
1 17
2 19


Output

-1
24
-1
46
-1
64
-1
77


Input

15 5
3 87
1 25
1 27
3 58
2 85
5 19
5 39
1 58
3 12
4 13
5 54
4 100
2 33
5 13
2 55


Output

-1
145
173
285
318
398
431
491
524
576
609
634
653
666
678

### Response

```cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
 
#define mod 1000000007
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//------------------------------------------------------- 
pair<long,int> Je[200000];
vector<long> G[100000];
vector<long> G2[100000];
pair<long,int> O[100000];
int MaxThree[100000];
long ans[200001];
void Calc(){
	int N = rei();
	int K = rei();
	for(int i=0;i<N;i++){
		int c = rei()-1;
		long v = rei();
		Je[i] = {v,c};
	}
	sort(Je,Je+N);
	for(int i=N-1;i>=0;i--){
		G2[Je[i].second].push_back(Je[i].first);	
	}
	for(int i=0;i<K;i++){
		O[i] = {-G2[i][0]-G2[i][1],i};
	}
	sort(O,O+K);
	for(int i=0;i<K;i++){
		G[i] = G2[O[i].second];
	}
	MaxThree[K-1] = K;
	MaxThree[K] = K;
	for(int i=K-2;i>=0;i--){
		MaxThree[i] = MaxThree[i+1];
		if(G[i+1].size() >= 3 && (MaxThree[i] == K || G[i+1][0]+G[i+1][1]+G[i+1][2] >= G[MaxThree[i]][0]+G[MaxThree[i]][1]+G[MaxThree[i]][2])){
			MaxThree[i] = i+1;
		}
	}
	fill(ans,ans+N,-1);
	priority_queue<long> X;
	if(MaxThree[0] != K){
		ans[3] = G[MaxThree[0]][0] + G[MaxThree[0]][1] + G[MaxThree[0]][2];
	}
	long sum = G[0][0]+G[0][1];
	long min = 10000000000000000;
	for(int i=2;i<G[0].size();i++){
		X.push(G[0][i]);
	}
	ans[2] = sum;
	if(MaxThree[0] != K && N >= 5){
		ans[5] = sum + G[MaxThree[0]][0] + G[MaxThree[0]][1] + G[MaxThree[0]][2];
	}
	int p = 1;
	bool wait = false;
	for(int c=3;c<=N;c++){
		if(X.empty() && min == 10000000000000000){
			if(wait){
				wait = false;
				sum += G[p][0] + G[p][1];
				for(int i=2;i<G[p].size();i++){
					X.push(G[p][i]);
				}
				p++;
			}
			else{
				wait = true;
				continue;
			}
		}
		else{
			if(p < K && (X.empty() || min + X.top() < G[p][0] + G[p][1])){
				sum += G[p][0] + G[p][1] - min;
				X.push(min);
				min = 10000000000000000;
				for(int i=2;i<G[p].size();i++){
					X.push(G[p][i]);
				}
				p++;
			}
			else{
				sum += X.top();
				min = X.top();
				X.pop();
			}
		}
		ans[c] = max(ans[c],sum);
		if(c+3 <= N && MaxThree[p] != K){
			ans[c+3] = max(ans[c+3],sum + G[MaxThree[p]][0] + G[MaxThree[p]][1] + G[MaxThree[p]][2]);
		}
	}
	for(int i=1;i<=N;i++){
		cout << ans[i] << "\n";
	}
}
int main(int argc,char** argv){
	ios::sync_with_stdio(false), cin.tie(0);
	cout.tie(0); Calc(); return 0;
}
```