Problem:
Score : 400 points

Problem Statement

There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i.
You are choosing K among these N pieces to eat.
Your "satisfaction" here will be calculated as follows:

The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".

The base total deliciousness is the sum of the deliciousness of the pieces you eat.

The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.

You want to have as much satisfaction as possible.
Find this maximum satisfaction.

Constraints

1 \leq K \leq N \leq 10^5

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

5 3
1 9
1 7
2 6
2 5
3 1

Sample Output 1

26

If you eat Sushi 1,2 and 3:

The base total deliciousness is 9+7+6=22.

The variety bonus is 2*2=4.

Thus, your satisfaction will be 26, which is optimal.

Sample Input 2

7 4
1 1
2 1
3 1
4 6
4 5
4 5
4 5

Sample Output 2

25

It is optimal to eat Sushi 1,2,3 and 4.

Sample Input 3

6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3

4900000016

Note that the output may not fit into a 32-bit integer type.

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.Set;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Comparator;
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;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            List<TaskD.Sushi> sushis = new ArrayList<>();

            for (int i = 0; i < n; i++) {
                TaskD.Sushi cs = new TaskD.Sushi(in.nextLong(), in.nextLong(), i);
                sushis.add(cs);
            }

            long ansD = 0;
            sushis.sort(Comparator.comparing(TaskD.Sushi::getD).reversed());
            Set<Long> checked = new HashSet<>();
            long tn = 0;
            for (int i = 0; i < k; i++) {
                ansD += sushis.get(i).getD();
                if (!checked.contains(sushis.get(i).getT())) {
                    checked.add(sushis.get(i).getT());
                    tn++;
                }
            }
            ansD += tn * tn;

            long ansT = 0;
            sushis.sort(Comparator.comparing(TaskD.Sushi::getD).reversed());
            checked = new HashSet<>();
            Set<TaskD.Sushi> checkedSushi = new HashSet<>();
            tn = 0;
            for (int i = 0; i < n || tn < k; i++) {
                TaskD.Sushi cs = sushis.get(i);
                if (checked.contains(cs.getT()))
                    continue;
                ansT += sushis.get(i).getD();
                checked.add(cs.getT());
                checkedSushi.add(cs);
                tn++;
            }
            ansT += tn * tn;
            if (tn < k) {
                for (int i = 0; i < n || tn < k; i++) {
                    TaskD.Sushi cs = sushis.get(i);
                    if (checkedSushi.contains(cs))
                        continue;
                    ansT += sushis.get(i).getD();
                }
            }
            out.println(Math.max(ansD, ansT));
        }

        private static class Sushi {
            long d;
            long t;
            long n;

            Sushi(long t, long d, long n) {
                this.t = t;
                this.d = d;
                this.n = n;
            }

            public long getT() {
                return t;
            }

            public long getD() {
                return d;
            }

        }

    }

    static class FastScanner {
        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int bufPointer;
        private int bufLength;

        public FastScanner(InputStream in) {
            this.in = in;
        }

        private int readByte() {
            if (bufPointer >= bufLength) {
                if (bufLength == -1)
                    throw new InputMismatchException();

                bufPointer = 0;
                try {
                    bufLength = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (bufLength <= 0)
                    return -1;
            }
            return buffer[bufPointer++];
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public long nextLong() {
            long n = 0;

            int b = readByte();
            while (isSpaceChar(b))
                b = readByte();

            boolean minus = (b == '-');
            if (minus)
                b = readByte();

            while (b >= '0' && b <= '9') {
                n *= 10;
                n += b - '0';
                b = readByte();
            }

            if (!isSpaceChar(b))
                throw new NumberFormatException();

            return minus ? -n : n;
        }

        public int nextInt() {
            long n = nextLong();

            if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE)
                throw new NumberFormatException();

            return (int) n;
        }

    }
}