
#include<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;


#ifdef DEB
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
#define dumpR(x) cerr<<"\x1b[31m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpY(x) cerr<<"\x1b[33m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpG(x) cerr<<"\x1b[32m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define dumpR(x) ;
#define dumpY(x) ;
#define dumpG(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

template<class T> bool chmin(T& a,const T& b) {
  if(a>b) {
    a=b;
    return true;
  }
  return false;
}
template<class T> bool chmax(T& a,const T& b) {
  if(a<b){
    a=b;
    return true;
  }
  return false;
}

typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fr<<','<<a.sc<<')';
    return out;
  }
}

const lint INF=1e18;

int n,K;
vector<lint> ar[200005];

pi br[200005];
int main(){
  cin>>n>>K;
  REP(i,n){
    int c,v;cin>>c>>v;
    v*=2;
    --c;
    ar[c].pb(v);
  }
  int m=0;
  REP(i,K){
    sort(ALL(ar[i]),greater<int>());
    lint avg=(ar[i][0]+ar[i][1])/2;
    ar[i][0]=ar[i][1]=avg;
    REP(j,ar[i].size()){
      br[m++]={i,j};
    }
  }
  sort(br,br+m,[&](pi a,pi b){
      return mp(ar[a.fr][a.sc],mp(a.fr,-a.sc))>mp(ar[b.fr][b.sc],mp(b.fr,-b.sc));
      });

  vector<lint> ans={-1};
  lint lastSingle=INF;
  set<pair<lint,int> >lastPair;
  set<pair<lint,int> >nextSingle;
  set<pair<lint,int> >triple;

  REP(i,K){
    if(ar[i].size()>=3 && br[0].fr!=i){
      triple.insert({ar[i][0]+ar[i][1]+ar[i][2],i});
    }
  }
  {
    int col=br[0].fr,idx=br[0].sc;
    if(ar[col].size()>=3){
      nextSingle.insert({ar[col][2],col});
    }
    lastPair.insert({ar[col][idx],col});
  }

  lint tot=ar[br[0].fr][br[0].sc]+ar[br[1].fr][br[1].sc];

  assert(m==n);
  debug(br,br+n);
  for(int i=2;i<n;++i){
    dump(i);
    int col=br[i].fr,idx=br[i].sc;
    lint cur=ar[br[i].fr][br[i].sc];
    if(br[i].sc==1){
      dumpR(i);
      debug(ALL(triple));
      assert(br[i-1].fr==br[i].fr);

      auto getbest=[&](int ng){
        if(nextSingle.empty()) return -INF;
        pair<lint,int> p=*(--nextSingle.end());
        if(p.sc!=ng) return p.fr;
        if(nextSingle.size()==1) return -INF;
        pair<lint,int> q=*(--(--nextSingle.end()));
        assert(q.sc!=ng);
        return q.fr;
      };
      lint tmp=-1;

      {
        {
          chmax(tmp,tot-cur+getbest(-1));
        }
        if(!triple.empty() && !lastPair.empty()){
          chmax(tmp,tot-cur+((--triple.end())->fr)-(lastPair.begin()->fr*2));
        }
      }

      {
        if(ar[col].size()>=3){
          nextSingle.insert({ar[col][2],col});
        }
      }
      debug(ALL(nextSingle));
      debug(ALL(lastPair));
      {
        if(lastSingle<INF){
          chmax(tmp,tot+cur-lastSingle);
        }
        pair<lint,int> p1,p2;

        if(!lastPair.empty()){
          p1=*lastPair.begin();
          chmax(tmp,tot+cur-p1.fr*2+getbest(p1.sc));
        }
        if(lastPair.size()>=2){
          p2=*(++lastPair.begin());
          chmax(tmp,tot+cur-p2.fr*2+getbest(p2.sc));
        }
      }
      ans.pb(tmp);
    }else{
      ans.pb(tot);
    }
    tot+=cur;
    dump(col);dump(idx);
    if(idx==0 && ar[col].size()>=3){
      assert(triple.erase(mp(ar[col][0]+ar[col][1]+ar[col][2],col))==1);
    }
    if(idx==1){
      lastPair.insert({cur,col});
    }

    if(idx>1){
      chmin(lastSingle,cur);
      assert(nextSingle.erase(mp(cur,col)));
      if(idx+1<ar[col].size()){
        nextSingle.insert(mp(ar[col][idx+1],col));
      }
    }
  }
  ans.pb(tot);
  for(auto e:ans){
    lint tmp=(e==-1?e:e/2);
    assert(e%2==0 || e==-1);
    printf("%lld\n",tmp);
  }
  return 0;
}




### Prompt

Your challenge is to write 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<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;


#ifdef DEB
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
#define dumpR(x) cerr<<"\x1b[31m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpY(x) cerr<<"\x1b[33m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpG(x) cerr<<"\x1b[32m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define dumpR(x) ;
#define dumpY(x) ;
#define dumpG(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

template<class T> bool chmin(T& a,const T& b) {
  if(a>b) {
    a=b;
    return true;
  }
  return false;
}
template<class T> bool chmax(T& a,const T& b) {
  if(a<b){
    a=b;
    return true;
  }
  return false;
}

typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fr<<','<<a.sc<<')';
    return out;
  }
}

const lint INF=1e18;

int n,K;
vector<lint> ar[200005];

pi br[200005];
int main(){
  cin>>n>>K;
  REP(i,n){
    int c,v;cin>>c>>v;
    v*=2;
    --c;
    ar[c].pb(v);
  }
  int m=0;
  REP(i,K){
    sort(ALL(ar[i]),greater<int>());
    lint avg=(ar[i][0]+ar[i][1])/2;
    ar[i][0]=ar[i][1]=avg;
    REP(j,ar[i].size()){
      br[m++]={i,j};
    }
  }
  sort(br,br+m,[&](pi a,pi b){
      return mp(ar[a.fr][a.sc],mp(a.fr,-a.sc))>mp(ar[b.fr][b.sc],mp(b.fr,-b.sc));
      });

  vector<lint> ans={-1};
  lint lastSingle=INF;
  set<pair<lint,int> >lastPair;
  set<pair<lint,int> >nextSingle;
  set<pair<lint,int> >triple;

  REP(i,K){
    if(ar[i].size()>=3 && br[0].fr!=i){
      triple.insert({ar[i][0]+ar[i][1]+ar[i][2],i});
    }
  }
  {
    int col=br[0].fr,idx=br[0].sc;
    if(ar[col].size()>=3){
      nextSingle.insert({ar[col][2],col});
    }
    lastPair.insert({ar[col][idx],col});
  }

  lint tot=ar[br[0].fr][br[0].sc]+ar[br[1].fr][br[1].sc];

  assert(m==n);
  debug(br,br+n);
  for(int i=2;i<n;++i){
    dump(i);
    int col=br[i].fr,idx=br[i].sc;
    lint cur=ar[br[i].fr][br[i].sc];
    if(br[i].sc==1){
      dumpR(i);
      debug(ALL(triple));
      assert(br[i-1].fr==br[i].fr);

      auto getbest=[&](int ng){
        if(nextSingle.empty()) return -INF;
        pair<lint,int> p=*(--nextSingle.end());
        if(p.sc!=ng) return p.fr;
        if(nextSingle.size()==1) return -INF;
        pair<lint,int> q=*(--(--nextSingle.end()));
        assert(q.sc!=ng);
        return q.fr;
      };
      lint tmp=-1;

      {
        {
          chmax(tmp,tot-cur+getbest(-1));
        }
        if(!triple.empty() && !lastPair.empty()){
          chmax(tmp,tot-cur+((--triple.end())->fr)-(lastPair.begin()->fr*2));
        }
      }

      {
        if(ar[col].size()>=3){
          nextSingle.insert({ar[col][2],col});
        }
      }
      debug(ALL(nextSingle));
      debug(ALL(lastPair));
      {
        if(lastSingle<INF){
          chmax(tmp,tot+cur-lastSingle);
        }
        pair<lint,int> p1,p2;

        if(!lastPair.empty()){
          p1=*lastPair.begin();
          chmax(tmp,tot+cur-p1.fr*2+getbest(p1.sc));
        }
        if(lastPair.size()>=2){
          p2=*(++lastPair.begin());
          chmax(tmp,tot+cur-p2.fr*2+getbest(p2.sc));
        }
      }
      ans.pb(tmp);
    }else{
      ans.pb(tot);
    }
    tot+=cur;
    dump(col);dump(idx);
    if(idx==0 && ar[col].size()>=3){
      assert(triple.erase(mp(ar[col][0]+ar[col][1]+ar[col][2],col))==1);
    }
    if(idx==1){
      lastPair.insert({cur,col});
    }

    if(idx>1){
      chmin(lastSingle,cur);
      assert(nextSingle.erase(mp(cur,col)));
      if(idx+1<ar[col].size()){
        nextSingle.insert(mp(ar[col][idx+1],col));
      }
    }
  }
  ans.pb(tot);
  for(auto e:ans){
    lint tmp=(e==-1?e:e/2);
    assert(e%2==0 || e==-1);
    printf("%lld\n",tmp);
  }
  return 0;
}




```