#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;




int main(){
    ll n,k;
    cin>>n>>k;
    vector<vector<ll>> a(k);
    for(int i=0;i<n;i++){
        ll c,v;
        cin>>c>>v;
        c--; v*=2;
        a[c].push_back(v);
    }
    vector<pair<ll,pll>> A;
    priority_queue<pll> Tri;
    for(int i=0;i<k;i++){
        sort(a[i].begin(),a[i].end(),greater<ll>());
        a[i][0]+=a[i][1];
        a[i][0]/=2;
        a[i][1]=a[i][0];
        if(a[i].size()>=3){
            Tri.push({a[i][0]+a[i][1]+a[i][2],i});
        }
        for(int t=0;t<a[i].size();t++){
            A.push_back({a[i][t],{i,n-t}});
        }
    }
    priority_queue<pair<ll,pll>> Sgl;
    priority_queue<pair<ll,pll>,vector<pair<ll,pll>>,greater<pair<ll,pll>>> D_Sgl;
    priority_queue<pll,vector<pll>,greater<pll>> D_Dbl;
    sort(A.begin(),A.end(),greater<pair<ll,pll>>());
    ll sum=0;
    vector<ll> cnt(k,0);
    for(int i=0;i<n;i++){
        cnt[A[i].S.F]++;
        if(A[i].S.S!=n-0){sum+=A[i].F;}
        if(A[i].S.S==n-1){sum+=A[i].F;}
        ll ans=-E;
        if(A[i].S.S!=n-0){ans=sum;}
        else{
            while(!Sgl.empty() && Sgl.top()>=A[i]){Sgl.pop();}
            while(!Tri.empty() && A[i]<pair<ll,pll>(a[Tri.top().S][0],pll(Tri.top().S,n-0))){Tri.pop();}
            while(!D_Dbl.empty() && cnt[D_Dbl.top().S]==3){D_Dbl.pop();}
            if(!Sgl.empty()){
                ans=max(ans,sum+Sgl.top().F);
            }
            if(!D_Sgl.empty()){
                ans=max(ans,sum-D_Sgl.top().F+A[i].F*2);
            }
            if(!D_Dbl.empty() && !Tri.empty()){
                ans=max(ans,sum-D_Dbl.top().F+Tri.top().F);
            }
        }
        cout<<(ans==-E?-1:ans/2)<<endl;
        if(A[i].S.S<=n-2){
            D_Sgl.push(A[i]);
        }
        if(A[i].S.S==n-1 || A[i].S.S<=n-3){
            D_Dbl.push({a[A[i].S.F][n-A[i].S.S]+a[A[i].S.F][n-A[i].S.S-1],A[i].S.F});
        }
        if(A[i].S.S==n-1){
            for(int t=2;t<a[A[i].S.F].size();t++){
                Sgl.push({a[A[i].S.F][t],{A[i].S.F,n-t}});
            }
        }
    }
    
    
    
    return 0;
}

### Prompt

Your task is to create a CPP solution 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<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;




int main(){
    ll n,k;
    cin>>n>>k;
    vector<vector<ll>> a(k);
    for(int i=0;i<n;i++){
        ll c,v;
        cin>>c>>v;
        c--; v*=2;
        a[c].push_back(v);
    }
    vector<pair<ll,pll>> A;
    priority_queue<pll> Tri;
    for(int i=0;i<k;i++){
        sort(a[i].begin(),a[i].end(),greater<ll>());
        a[i][0]+=a[i][1];
        a[i][0]/=2;
        a[i][1]=a[i][0];
        if(a[i].size()>=3){
            Tri.push({a[i][0]+a[i][1]+a[i][2],i});
        }
        for(int t=0;t<a[i].size();t++){
            A.push_back({a[i][t],{i,n-t}});
        }
    }
    priority_queue<pair<ll,pll>> Sgl;
    priority_queue<pair<ll,pll>,vector<pair<ll,pll>>,greater<pair<ll,pll>>> D_Sgl;
    priority_queue<pll,vector<pll>,greater<pll>> D_Dbl;
    sort(A.begin(),A.end(),greater<pair<ll,pll>>());
    ll sum=0;
    vector<ll> cnt(k,0);
    for(int i=0;i<n;i++){
        cnt[A[i].S.F]++;
        if(A[i].S.S!=n-0){sum+=A[i].F;}
        if(A[i].S.S==n-1){sum+=A[i].F;}
        ll ans=-E;
        if(A[i].S.S!=n-0){ans=sum;}
        else{
            while(!Sgl.empty() && Sgl.top()>=A[i]){Sgl.pop();}
            while(!Tri.empty() && A[i]<pair<ll,pll>(a[Tri.top().S][0],pll(Tri.top().S,n-0))){Tri.pop();}
            while(!D_Dbl.empty() && cnt[D_Dbl.top().S]==3){D_Dbl.pop();}
            if(!Sgl.empty()){
                ans=max(ans,sum+Sgl.top().F);
            }
            if(!D_Sgl.empty()){
                ans=max(ans,sum-D_Sgl.top().F+A[i].F*2);
            }
            if(!D_Dbl.empty() && !Tri.empty()){
                ans=max(ans,sum-D_Dbl.top().F+Tri.top().F);
            }
        }
        cout<<(ans==-E?-1:ans/2)<<endl;
        if(A[i].S.S<=n-2){
            D_Sgl.push(A[i]);
        }
        if(A[i].S.S==n-1 || A[i].S.S<=n-3){
            D_Dbl.push({a[A[i].S.F][n-A[i].S.S]+a[A[i].S.F][n-A[i].S.S-1],A[i].S.F});
        }
        if(A[i].S.S==n-1){
            for(int t=2;t<a[A[i].S.F].size();t++){
                Sgl.push({a[A[i].S.F][t],{A[i].S.F,n-t}});
            }
        }
    }
    
    
    
    return 0;
}

```