Problem:
Score : 1200 points

Problem Statement

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.

Sample Input 1

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

Sample Output 1

-1
9
6
14
15

We cannot choose exactly one jewel.

When choosing exactly two jewels, the total value is maximized when Jewel 4 and 5 are chosen.

When choosing exactly three jewels, the total value is maximized when Jewel 1, 2 and 3 are chosen.

When choosing exactly four jewels, the total value is maximized when Jewel 2, 3, 4 and 5 are chosen.

When choosing exactly five jewels, the total value is maximized when Jewel 1, 2, 3, 4 and 5 are chosen.

Sample Input 2

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

Sample Output 2

-1
9
12
12
15

Sample Input 3

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

Sample Output 3

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

Sample Input 4

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

Sample Output 4

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

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////

ll N,K;
vector<ll> jv[202020];
ll jvi[202020];
priority_queue<pll> jqa2, jqa1,jqb2,jqb3;
priority_queue<pair<ll,pll>> jqb1;
ll i, j, ans[202020];

int main(){
    scanf("%lld%lld", &N,&K);
    fornum(i,0,N){
        ll c, v;
        scanf("%lld%lld", &c, &v);
        jv[c].push_back(v);
    }
    fornum(i,1,K+1){
        sort(jv[i].begin(), jv[i].end(), greater<ll>());
        jqb2.push(mp(jv[i][0] + jv[i][1], i));
        if(jv[i].size()>=3){
            jqb3.push(mp(jv[i][0] + jv[i][1] + jv[i][2], i));
        }

    }
    fornum(i,1,N+1){
       // printf("%lld %d ", i,jqb1.size());
        ll cc = -1;
        ll vv = 0;
        if(jqa2.size()>0&&jqb3.size()>0){
            vv = jqa2.top().first+jqb3.top().first;
            cc = 1;
        }
        if(jqa1.size()>0&&jqb2.size()>0){
            ll vvv = jqa1.top().first + jqb2.top().first;

            if(vv<=vvv){
                vv = vvv;
                cc = 2;
            }
        }
        if(jqb1.size()>0){
            ll vvv = jqb1.top().first;
            if(vv<=vvv){
                vv = vvv;
                cc = 3;
            }
        }
        //printf("%lld ", cc);
        ans[i] = ans[i-1]+vv;
        switch(cc){
            case 1:
                cc = jqb3.top().second;
                jqb3.pop();
                jqa1.push(mp(-jv[cc][2], cc));
                if(jv[cc].size()>3){
                    jqb1.push(mp(jv[cc][3], mp(3,cc)));
                }
                jvi[cc] = 3;
                cc = jqa2.top().second;
                jqa2.pop();
                jqb2.push(mp(jv[cc][0] + jv[cc][1],cc));
                if(jv[cc].size()>=3){
                    jqb3.push(mp(jv[cc][0] + jv[cc][1] + jv[cc][2], cc));
                }
                jvi[cc] = 0;
                break;
            case 2:
                //printf("a");
                cc = jqa1.top().second;
                jqa1.pop();
                if(cc!=0){
                    --jvi[cc];
                    jqb1.push(mp(jv[cc][jvi[cc]], mp(jvi[cc], cc)));
                    if(jvi[cc]==2){
                        jqa2.push(mp(-jv[cc][0] - jv[cc][1], cc));
                    }else{
                        jqa1.push(mp(-jv[cc][jvi[cc]-1], cc));
                    }
                }
                cc = jqb2.top().second;
                jqb2.pop();
                jqa2.push(mp(-jv[cc][0] - jv[cc][1], cc));
                if(jv[cc].size()>2){
                    jqb1.push(mp(jv[cc][2], mp(2,cc)));
                }
                jvi[cc] = 2;
                break;
            case 3:
                cc = jqb1.top().second.second;
                jqb1.pop();
                jqa1.push(mp(-jv[cc][jvi[cc]],cc));
                jvi[cc]++;
                if(jv[cc].size()>jvi[cc]){
                    jqb1.push(mp(jv[cc][jvi[cc]],mp( jvi[cc],cc)));
                }
                break;
            case -1:
                jqa1.push(mp(0, 0));
                break;
        }
        while(jqb1.size()&&jvi[jqb1.top().second.second]!=jqb1.top().second.first){
            jqb1.pop();
        }
        while(jqb2.size()&&jvi[jqb2.top().second]!=0){
            jqb2.pop();
        }
        while(jqb3.size()&&jvi[jqb3.top().second]!=0){
            jqb3.pop();
        }
        printf("%lld\n", cc!=-1?ans[i]:-1ll);
    }
    
    return 0;
}