Problem:
Score : 1200 points

Problem Statement

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.

Sample Input 1

5 2
1 1
1 2
1 3
2 4
2 5

Sample Output 1

-1
9
6
14
15

We cannot choose exactly one jewel.

When choosing exactly two jewels, the total value is maximized when Jewel 4 and 5 are chosen.

When choosing exactly three jewels, the total value is maximized when Jewel 1, 2 and 3 are chosen.

When choosing exactly four jewels, the total value is maximized when Jewel 2, 3, 4 and 5 are chosen.

When choosing exactly five jewels, the total value is maximized when Jewel 1, 2, 3, 4 and 5 are chosen.

Sample Input 2

5 2
1 1
1 2
2 3
2 4
2 5

Sample Output 2

-1
9
12
12
15

Sample Input 3

8 4
3 2
2 3
4 5
1 7
3 11
4 13
1 17
2 19

Sample Output 3

-1
24
-1
46
-1
64
-1
77

Sample Input 4

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

Sample Output 4

-1
145
173
285
318
398
431
491
524
576
609
634
653
666
678

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.AbstractQueue;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.util.AbstractCollection;
import java.io.BufferedReader;
import java.util.Comparator;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        MyInput in = new MyInput(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        FJewels solver = new FJewels();
        solver.solve(1, in, out);
        out.close();
    }

    static class FJewels {
        public void solve(int testNumber, MyInput in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();

            List<Long>[] val = new List[k];
            for (int i = 0; i < k; i++) {
                val[i] = new ArrayList<>();
            }
            for (int i = 0; i < n; i++) {
                int c = in.nextInt() - 1;
                long v = in.nextInt();
                val[c].add(v);
            }

            S s = new S();
            for (List<Long> x : val) {
                Collections.sort(x, Comparator.comparingLong(y -> -y));
                s.unused2.add(x);
                if (x.size() > 2) {
                    s.unused3.add(x);
                }
            }

            long ans = 0;
            final long inf = 1L << 60;
            Set<List<Long>> used = new TreeSet<>((x, y) -> {
                if (x.size() != y.size()) return x.size() - y.size();
                for (int i = 0; i < x.size(); i++) {
                    if (x.get(i) != y.get(i)) return Long.compare(x.get(i), y.get(i));
                }
                return 0;
            });
            long minPair = inf;
            long minSingle = inf;

            out.println(-1);
            for (int i = 2; i <= n; i += 2) {
                {
                    while (!s.unused2.isEmpty() && used.contains(s.unused2.peek())) {
                        s.unused2.poll();
                    }
                    long x = s.unused2.isEmpty() ? -inf : s.unused2.peek().get(0) + s.unused2.peek().get(1);
                    long z1 = s.used.isEmpty() ? -inf : s.used.poll();
                    long z2 = s.used.isEmpty() ? -inf : s.used.poll();
                    long z = z1 + z2;
                    if (x >= z) {
                        ans += x;
                        List<Long> xs = s.unused2.poll();
                        s.used.addAll(xs.subList(2, xs.size()));
                        if (z1 != -inf) s.used.add(z1);
                        if (z2 != -inf) s.used.add(z2);
                        used.add(xs);
                        minPair = Math.min(minPair, x);
                    } else {
                        ans += z;
                        minPair = Math.min(minPair, z);
                        minSingle = Math.min(minSingle, z2);
                    }
                }
                out.println(ans);

                if (i + 1 <= n) {
                    while (!s.unused3.isEmpty() && used.contains(s.unused3.peek())) {
                        s.unused3.poll();
                    }

                    long x = s.unused2.isEmpty() ? -inf : s.unused2.peek().get(0) + s.unused2.peek().get(1);
                    long y = s.unused3.isEmpty() ? -inf : s.unused3.peek().get(0) + s.unused3.peek().get(1) + s.unused3.peek().get(2);
                    long z1 = s.used.isEmpty() ? -inf : s.used.peek();
                    long z = z1;
                    long v = -1;
                    v = Math.max(v, ans + y - minPair);
                    v = Math.max(v, ans + z1);
                    v = Math.max(v, ans + x - minSingle);
                    out.println(v);
                }
            }
        }

        class S {
            PriorityQueue<Long> used = new PriorityQueue<>(Comparator.comparingLong(x -> -x));
            PriorityQueue<List<Long>> unused2 = new PriorityQueue<>((x, y) -> -Long.compare(x.get(0) + x.get(1), y.get(0) + y.get(1)));
            PriorityQueue<List<Long>> unused3 = new PriorityQueue<>((x, y) -> -Long.compare(x.get(0) + x.get(1) + x.get(2), y.get(0) + y.get(1) + y.get(2)));

        }

    }

    static class MyInput {
        private final BufferedReader in;
        private static int pos;
        private static int readLen;
        private static final char[] buffer = new char[1024 * 8];
        private static char[] str = new char[500 * 8 * 2];
        private static boolean[] isDigit = new boolean[256];
        private static boolean[] isSpace = new boolean[256];
        private static boolean[] isLineSep = new boolean[256];

        static {
            for (int i = 0; i < 10; i++) {
                isDigit['0' + i] = true;
            }
            isDigit['-'] = true;
            isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
            isLineSep['\r'] = isLineSep['\n'] = true;
        }

        public MyInput(InputStream is) {
            in = new BufferedReader(new InputStreamReader(is));
        }

        public int read() {
            if (pos >= readLen) {
                pos = 0;
                try {
                    readLen = in.read(buffer);
                } catch (IOException e) {
                    throw new RuntimeException();
                }
                if (readLen <= 0) {
                    throw new MyInput.EndOfFileRuntimeException();
                }
            }
            return buffer[pos++];
        }

        public int nextInt() {
            int len = 0;
            str[len++] = nextChar();
            len = reads(len, isSpace);
            int i = 0;
            int ret = 0;
            if (str[0] == '-') {
                i = 1;
            }
            for (; i < len; i++) ret = ret * 10 + str[i] - '0';
            if (str[0] == '-') {
                ret = -ret;
            }
            return ret;
        }

        public char nextChar() {
            while (true) {
                final int c = read();
                if (!isSpace[c]) {
                    return (char) c;
                }
            }
        }

        int reads(int len, boolean[] accept) {
            try {
                while (true) {
                    final int c = read();
                    if (accept[c]) {
                        break;
                    }
                    if (str.length == len) {
                        char[] rep = new char[str.length * 3 / 2];
                        System.arraycopy(str, 0, rep, 0, str.length);
                        str = rep;
                    }
                    str[len++] = (char) c;
                }
            } catch (MyInput.EndOfFileRuntimeException e) {
            }
            return len;
        }

        static class EndOfFileRuntimeException extends RuntimeException {
        }

    }
}