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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n := scanInt()
	k := scanInt()
	a := make(map[int][]int, n)
	for i := 0; i < n; i++ {
		t, d := scanInt(), scanInt()
		a[t] = append(a[t], d)
	}

	ans := 0
	b := piis{}
	c := piis{}
	for i, e := range a {
		sort.Ints(e)
		b = append(b, pii{i, e[len(e)-1]})
		for j := 0; j < len(e)-1; j++ {
			c = append(c, pii{i, e[j]})
		}
	}

	used := piis{}
	cnt := map[int]int{}
	sort.Sort(b)
	sort.Sort(c)

	for i := 0; i < len(b); i++ {
		ans += b[i].d
		used = append(used, b[i])
		cnt[b[i].t]++
		k--
		if k == 0 {
			break
		}
	}
	if k != 0 {
		for k > 0 {
			ans += c[0].d
			used = append(used, c[0])
			cnt[c[0].t]++
			c = c[1:]
			k--
		}
	}

	sort.Sort(used)
	num := len(cnt)
	for i := 0; i < len(c); i++ {
		u := used[len(used)-1]
		if cnt[c[0].t] > 1 {
			break
		}
		if ans+num*num < ans-u.d+c[0].d+(num-1)*(num-1) {
			ans += c[0].d - u.d
			used = used[:len(used)-1]
			num--
		} else {
			break
		}
	}

	fmt.Fprintln(wr, ans+num*num)

}

type pii struct{ t, d int }

type piis []pii

func (a piis) Len() int           { return len(a) }
func (a piis) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a piis) Less(i, j int) bool { return a[i].d > a[j].d }