#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;
}





Measured memory_bytes: 20976