//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
//~ #pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));


using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (1e6)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

VLL wek[maxn];
set<PLL> P, Q, R, S, T;
PLL elo[maxn];
int ile[maxn];

int main()
{
	_upgrade
  int n, k;
  cin>>n>>k;
  for(int i = 1;i <= n;i++)
  {
    int a, b;
    cin>>a>>b;
    wek[a].pb(b * 2);
  }
  VPLL V;
  for(int i = 1;i <= k;i++)
  {
    sort(all(wek[i]));
    reverse(all(wek[i]));
    wek[i][0] = wek[i][1] = (wek[i][0] + wek[i][1]) / 2;
    for(auto s : wek[i])
      V.pb({s, i});
    S.insert({wek[i][0] * 2, i});
    if(SZ(wek[i]) > 2)
    {
      elo[i] = {wek[i][0] + wek[i][1] + (LL)wek[i][2], i};
      T.insert(elo[i]);
    }
  }
  sort(all(V));
  reverse(all(V));
  LL suma = 0LL;
  for(int i = 0;i < SZ(V);i++)
  {
    int kolor = V[i].se;
    if(ile[kolor] > 0)
    {
      Q.erase(V[i]);
      if(ile[kolor] > 1)
        P.insert(V[i]);
      if(ile[kolor] == 1)
      {
        for(int j = 2;j < SZ(wek[kolor]);j++)
          Q.insert({wek[kolor][j], kolor});
        R.insert({V[i].fi * 2, V[i].se});
        S.erase({V[i].fi * 2, V[i].se});
        T.erase(elo[kolor]);
      }
      suma += V[i].fi;
      cout<<suma / 2LL<<"\n";
      ile[kolor]++;
    }
    else
    {
      LL res = -2LL;
      if(SZ(Q))
      {
        auto it = Q.end();
        it--;
        res = max(res, suma + it->fi);
      }
      if(SZ(P) && SZ(S))
      {
        auto it = S.end();
        it--;
        res = max(res, suma - P.begin()->fi + it->fi);
      }
      dbg(SZ(R), SZ(T));
      if(SZ(R) && SZ(T))
      {
        auto it = T.end();
        it--;
        res = max(res, suma - R.begin()->fi + it->fi);
      }
      dbg("r");
      // for(auto s : R)
        // dbg(s.fi, s.se);
      dbg("t");
      // for(auto s : T)
        // dbg(s.fi, s.se);
      Q.erase(V[i]);
      cout<<res / 2LL<<"\n";
      suma += V[i].fi;
      ile[V[i].se]++;
    }
  }


	return 0;
}


Measured memory_bytes: 49392