#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) ((x)*(x))

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

const int mn = 200005;
const int mod = 1e9+7;
#define P mod

int n,k;
vector<ll> v[mn];
set<pll> nus1,nus2,us2,us1,nus3;
int poi[mn];//pointer
ll ans=0;

void ins(int color){
    //printf("ins:%d\n",color);
    int &p=poi[color];
    int u=v[color][p];
    p--;
    int size=sz(v[color]);
    { //nus1
        if (size-1-p>=3){
            auto g=nus1.find({u,color});
            nus1.erase(g);
        }
        if (p!=-1&&size-1-p>=2) nus1.insert({v[color][p],color});
    }
    if (p==size-1-1){
        //printf("erase :color%d %lld\n",color,u+v[color][p]);
        nus2.erase({u+v[color][p],color}); //nus2
        //for(auto &i:nus2) printf(":::%lld %lld\n",i.first,i.second);
    }
    if (p>=1){//nus3
        ll g=1ll*u+v[color][p]+v[color][p-1];
        nus3.erase({g,color});
        if (p>=2) nus3.insert({v[color][p]+v[color][p-1]+v[color][p-2],color});
    }
    if (size-1-p>=3) {
        if (size-1-p>3) us1.erase({v[color][p+2],color});
        us1.insert({u,color}); // us1
    }
    if (size-1-p==3){//us2
        auto g=us2.find({v[color][p+2]+v[color][p+3],color});
        us2.erase(g);
    }
    if (size-1-p==2){
        us2.insert({v[color][p+1]+v[color][p+2],color});
    }
    ans+=u;
}

void del(int color){
    int &p=poi[color];
    p++;
    int u=v[color][p];
    int size=sz(v[color]);
    {//nus1
        if (p!=0&&size-1-p>=1){
            auto g=nus1.find({v[color][p-1],color});
            nus1.erase(g);
        }
        if (size-1-p>=2)nus1.insert({u,color});
    }
    if (p==size-1){
        //printf("insert :color%d\n",color);
        nus2.insert({u+v[color][p-1],color}); //nus2
    }
    if (p+1>=4) nus3.erase({v[color][p-1]+v[color][p-2]+v[color][p-3],color});
    if (p+1>=3){//nus3
        nus3.insert({u+v[color][p-1]+v[color][p-2],color});
    }
    if (size-1-p>=2){//us1
        auto g=us1.find({u,color});
        us1.erase(g);
        if (size-1-p>2) us1.insert({v[color][p+1],color});
    }
    if (size-1-p==2){
        us2.insert({v[color][p+1]+v[color][p+2],color});
    }
    if (size-1-p==1){
        auto g=us2.find({v[color][p]+v[color][p+1],color});
        us2.erase(g);
    }
    ans-=u;
}

ll getA(){
    if (nus1.empty()) return -1e18;
    return nus1.rbegin()->first;
}
void doA(){
    auto u=nus1.end();
    u--;
    ins(u->second);
}

ll getB(){
    if (us1.empty()||nus2.empty()) return -1e18;
    auto u=us1.begin();
    auto v=nus2.rbegin();
    return v->first-u->first;
}
void doB(){
    auto u=*us1.begin();
    auto v=*nus2.rbegin();
    del(u.second);
    ins(v.second);
    ins(v.second);
}

ll getC(){
    //printf("getC: %d %d\n",sz(us2),sz(nus3));
    if (us2.empty()||nus3.empty()) return -1e18;
    auto u=us2.begin();
    auto v=nus3.rbegin();
    return v->first-u->first;
}
void doC(){
    auto u=*us2.begin();
    auto v=*nus3.rbegin();
    del(u.second);
    del(u.second);
    ins(v.second);
    ins(v.second);
    ins(v.second);
}

ll getD(){
    if (us1.empty()||sz(nus2)<2) return -1e18;
    ll ans=0;
    auto u=us1.begin();
    int color=u->second;
    //printf("%d %d\n",poi[color],sz(v[color]));
    for(int i=1;i<=3;i++) ans-=v[color][poi[color]+i];
    auto v=nus2.rbegin();
    ans+=v->first;
    v++;
    ans+=v->first;
    return ans;
}
void doD(){
    if (us2.empty()||nus2.empty()) return;
    auto u=*us2.begin();
    auto v=*nus2.rbegin();
    del(u.second);
    del(u.second);
    ins(v.second);
    ins(v.second);
}

int main() {
#ifdef trote
    freopen("../1.txt", "r", stdin);
#endif
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        int u,w;
        scanf("%d%d",&u,&w);
        v[u].pb(w);
    }
    for(int i=1;i<=k;i++) {
        sort(v[i].begin(),v[i].end()),poi[i]=sz(v[i])-1;
        nus2.insert({v[i][poi[i]]+v[i][poi[i]-1],i});
        if (sz(v[i])>=3) nus3.insert({v[i].back()+v[i][poi[i]-1]+v[i][poi[i]-2],i});
    }
    for(int i=1;i<=n;i++){
        ll nxtans=-1e17,choose=0;
        if (getA()>nxtans) nxtans=getA(),choose=1;
        if (getB()>nxtans) nxtans=getB(),choose=2;
        if (getC()>nxtans) nxtans=getC(),choose=3;
        if (getD()>nxtans) nxtans=getD(),choose=4;
        //printf("%lld %lld %lld %lld\n",getA(),getB(),getC(),getD());
        //printf("choose:%d\n",choose);
        if (choose==0) {
            puts("-1");
            auto u=*nus2.rbegin();
            ins(u.second);
            ins(u.second);
            i++;
        }
        else if (choose==1) doA();
        else if (choose==2) doB();
        else if (choose==3) doC();
        else doB(),doD();
        printf("%lld\n",ans);
        //printf("--------------------------%d\n",i);
        //for(int i=1;i<=k;i++) printf("%d %d\n",i,sz(v[i])-1-poi[i]);
    }
    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 <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) ((x)*(x))

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

const int mn = 200005;
const int mod = 1e9+7;
#define P mod

int n,k;
vector<ll> v[mn];
set<pll> nus1,nus2,us2,us1,nus3;
int poi[mn];//pointer
ll ans=0;

void ins(int color){
    //printf("ins:%d\n",color);
    int &p=poi[color];
    int u=v[color][p];
    p--;
    int size=sz(v[color]);
    { //nus1
        if (size-1-p>=3){
            auto g=nus1.find({u,color});
            nus1.erase(g);
        }
        if (p!=-1&&size-1-p>=2) nus1.insert({v[color][p],color});
    }
    if (p==size-1-1){
        //printf("erase :color%d %lld\n",color,u+v[color][p]);
        nus2.erase({u+v[color][p],color}); //nus2
        //for(auto &i:nus2) printf(":::%lld %lld\n",i.first,i.second);
    }
    if (p>=1){//nus3
        ll g=1ll*u+v[color][p]+v[color][p-1];
        nus3.erase({g,color});
        if (p>=2) nus3.insert({v[color][p]+v[color][p-1]+v[color][p-2],color});
    }
    if (size-1-p>=3) {
        if (size-1-p>3) us1.erase({v[color][p+2],color});
        us1.insert({u,color}); // us1
    }
    if (size-1-p==3){//us2
        auto g=us2.find({v[color][p+2]+v[color][p+3],color});
        us2.erase(g);
    }
    if (size-1-p==2){
        us2.insert({v[color][p+1]+v[color][p+2],color});
    }
    ans+=u;
}

void del(int color){
    int &p=poi[color];
    p++;
    int u=v[color][p];
    int size=sz(v[color]);
    {//nus1
        if (p!=0&&size-1-p>=1){
            auto g=nus1.find({v[color][p-1],color});
            nus1.erase(g);
        }
        if (size-1-p>=2)nus1.insert({u,color});
    }
    if (p==size-1){
        //printf("insert :color%d\n",color);
        nus2.insert({u+v[color][p-1],color}); //nus2
    }
    if (p+1>=4) nus3.erase({v[color][p-1]+v[color][p-2]+v[color][p-3],color});
    if (p+1>=3){//nus3
        nus3.insert({u+v[color][p-1]+v[color][p-2],color});
    }
    if (size-1-p>=2){//us1
        auto g=us1.find({u,color});
        us1.erase(g);
        if (size-1-p>2) us1.insert({v[color][p+1],color});
    }
    if (size-1-p==2){
        us2.insert({v[color][p+1]+v[color][p+2],color});
    }
    if (size-1-p==1){
        auto g=us2.find({v[color][p]+v[color][p+1],color});
        us2.erase(g);
    }
    ans-=u;
}

ll getA(){
    if (nus1.empty()) return -1e18;
    return nus1.rbegin()->first;
}
void doA(){
    auto u=nus1.end();
    u--;
    ins(u->second);
}

ll getB(){
    if (us1.empty()||nus2.empty()) return -1e18;
    auto u=us1.begin();
    auto v=nus2.rbegin();
    return v->first-u->first;
}
void doB(){
    auto u=*us1.begin();
    auto v=*nus2.rbegin();
    del(u.second);
    ins(v.second);
    ins(v.second);
}

ll getC(){
    //printf("getC: %d %d\n",sz(us2),sz(nus3));
    if (us2.empty()||nus3.empty()) return -1e18;
    auto u=us2.begin();
    auto v=nus3.rbegin();
    return v->first-u->first;
}
void doC(){
    auto u=*us2.begin();
    auto v=*nus3.rbegin();
    del(u.second);
    del(u.second);
    ins(v.second);
    ins(v.second);
    ins(v.second);
}

ll getD(){
    if (us1.empty()||sz(nus2)<2) return -1e18;
    ll ans=0;
    auto u=us1.begin();
    int color=u->second;
    //printf("%d %d\n",poi[color],sz(v[color]));
    for(int i=1;i<=3;i++) ans-=v[color][poi[color]+i];
    auto v=nus2.rbegin();
    ans+=v->first;
    v++;
    ans+=v->first;
    return ans;
}
void doD(){
    if (us2.empty()||nus2.empty()) return;
    auto u=*us2.begin();
    auto v=*nus2.rbegin();
    del(u.second);
    del(u.second);
    ins(v.second);
    ins(v.second);
}

int main() {
#ifdef trote
    freopen("../1.txt", "r", stdin);
#endif
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        int u,w;
        scanf("%d%d",&u,&w);
        v[u].pb(w);
    }
    for(int i=1;i<=k;i++) {
        sort(v[i].begin(),v[i].end()),poi[i]=sz(v[i])-1;
        nus2.insert({v[i][poi[i]]+v[i][poi[i]-1],i});
        if (sz(v[i])>=3) nus3.insert({v[i].back()+v[i][poi[i]-1]+v[i][poi[i]-2],i});
    }
    for(int i=1;i<=n;i++){
        ll nxtans=-1e17,choose=0;
        if (getA()>nxtans) nxtans=getA(),choose=1;
        if (getB()>nxtans) nxtans=getB(),choose=2;
        if (getC()>nxtans) nxtans=getC(),choose=3;
        if (getD()>nxtans) nxtans=getD(),choose=4;
        //printf("%lld %lld %lld %lld\n",getA(),getB(),getC(),getD());
        //printf("choose:%d\n",choose);
        if (choose==0) {
            puts("-1");
            auto u=*nus2.rbegin();
            ins(u.second);
            ins(u.second);
            i++;
        }
        else if (choose==1) doA();
        else if (choose==2) doB();
        else if (choose==3) doC();
        else doB(),doD();
        printf("%lld\n",ans);
        //printf("--------------------------%d\n",i);
        //for(int i=1;i<=k;i++) printf("%d %d\n",i,sz(v[i])-1-poi[i]);
    }
    return 0;
}
```