#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
	res = 0;T f = 1;char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		res = res * 10 + c - '0';
		c = getchar();
	}
	res *= f;
}
template<class T>
void out(T x) {
	if(x < 0) {x = -x;putchar('-');}
	if(x >= 10) {
		out(x / 10);
	}
	putchar('0' + x % 10);
}
struct node {
	int pos;int64 v;
	friend bool operator < (const node &a,const node &b) {
		return a.v > b.v || (a.v == b.v && a.pos < b.pos);
	}
};
set<node> S,D,T,CD,CS;
vector<int64> ver[MAXN];
int p[MAXN],N,K;
int64 ans,rec[MAXN];
void Choose_one() {
	node t = *S.begin();S.erase(S.begin());CS.insert(t);
	ans += t.v;
	++p[t.pos];
	if(p[t.pos] <= ver[t.pos].size() - 1) {
		S.insert((node){t.pos,ver[t.pos][p[t.pos]]});
	}
}
void Delete_one() {
	node t = *(--CS.end());CS.erase(--CS.end());
	ans -= t.v;
	if(p[t.pos] <= ver[t.pos].size() - 1) {
		S.erase((node){t.pos,ver[t.pos][p[t.pos]]});
	}
	S.insert(t);--p[t.pos];
}
void Choose_two() {
	node t = *D.begin();D.erase(D.begin());CD.insert(t);
	ans += t.v;
	p[t.pos] = 2;
	if(p[t.pos] <= ver[t.pos].size() - 1) {
		S.insert((node){t.pos,ver[t.pos][p[t.pos]]});
	}
	if(ver[t.pos].size() >= 3) {
		T.erase((node){t.pos,ver[t.pos][0] + ver[t.pos][1] + ver[t.pos][2]});
	}
}
void Delete_two() {
	node t = *(--CD.end());CD.erase(--CD.end());
	S.erase((node){t.pos,ver[t.pos][p[t.pos]]});
	D.insert((node){t.pos,ver[t.pos][0] + ver[t.pos][1]});
	if(ver[t.pos].size() >= 3) {
		T.insert((node){t.pos,ver[t.pos][0] + ver[t.pos][1] + ver[t.pos][2]});
	}
	ans -= t.v;
}
void Choose_three() {
	node t = *T.begin();T.erase(T.begin());
	ans += t.v;
	p[t.pos] = 3;
	D.erase((node){t.pos,ver[t.pos][0] + ver[t.pos][1]});
	CD.insert((node){t.pos,ver[t.pos][0] + ver[t.pos][1]});
	CS.insert((node){t.pos,ver[t.pos][2]});
	if(p[t.pos] <= ver[t.pos].size() - 1) {
		S.insert((node){t.pos,ver[t.pos][p[t.pos]]});
	}
}
void Solve() {
	read(N);read(K);
	int c;int64 v;
	for(int i = 1 ; i <= N ; ++i) {
		read(c);read(v);ver[c].pb(v);
	}
	for(int i = 1 ; i <= K ; ++i) {
		sort(ver[i].begin(),ver[i].end(),[](int64 a,int64 b){return a > b;});
	}
	for(int i = 1 ; i <= K ; ++i) {
		if(ver[i].size()) {
			D.insert((node){i,ver[i][0] + ver[i][1]});
			if(ver[i].size() >= 3) T.insert((node){i,ver[i][0] + ver[i][1] + ver[i][2]});
		}
	}
	rec[1] = -1;ans = 0;
	puts("-1");
	for(int i = 2 ; i <= N ; ++i) {
		if(rec[i - 1] == -1) {
			Choose_two();
			rec[i] = ans;
		}
		else {
			int64 w[3] = {-1,-1,-1};
			if(S.size()) {
				w[0] = ans + (*S.begin()).v;
			}
			if(CS.size() && D.size()) {
				w[1] = ans - (*(--CS.end())).v + (*D.begin()).v;
			}
			if(CD.size() && T.size()) {
				w[2] = ans - (*(--CD.end())).v + (*T.begin()).v;
			}
			if(w[0] < 0 && w[1] < 0 && w[2] < 0) {rec[i] = -1;}
			else {
				if(w[0] >= max(w[1],w[2])) {Choose_one();}
				else if(w[1] >= max(w[0],w[2])) {Delete_one();Choose_two();}
				else {Delete_two();Choose_three();}
				rec[i] = ans;
			}
			while(1) {
				if(!S.size()) break;
				if(!CS.size()) break;
				node t = *(--CS.end());
				node s = *S.begin();
				if(s.v > t.v) {
					Delete_one();Choose_one();
				}
				else break;
				rec[i] = ans;
			}
			while(1) {
				if(!D.size()) break;
				if(!CD.size()) break;
				node t = *(--CD.end());
				node s = *D.begin();
				if(p[t.pos] == 2) {
					if(s.v > t.v) {
						Delete_two();Choose_two();
					}
					else break;
				}
				else break;
				rec[i] = ans;
			}
		}
		out(rec[i]);enter;
	}
}
int main() {
#ifdef ivorysi
	freopen("f1.in","r",stdin);
#endif
	Solve();
}


Measured memory_bytes: 24704