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 Ruby (2.3.3) (judge verdict: Accepted)

# https://atcoder.jp/contests/abc116/tasks/abc116_d
class PopHeap
  attr_accessor :nodes, :size
  def initialize
    @nodes = []
    @size = 0
  end

  def push(x)
    current_index = @size
    @size += 1
    while current_index > 0 do
      parent_index = (current_index - 1)>>1
      parent_value = @nodes[parent_index]
      break if parent_value <= x
      @nodes[current_index] = parent_value
      current_index = parent_index
    end
    @nodes[current_index] = x
  end

  def pop
    top = @nodes[0]
    current_node_index = 0
    current_node_value = @nodes[0] = @nodes[@size - 1]
    @nodes[@size - 1] = nil
    @size -= 1

    while
      child_node_index_r = (current_node_index + 1) << 1
      child_node_index_l = child_node_index_r - 1
      return top unless child_node_index_l < @size
      child_node_value_l = @nodes[child_node_index_l]
      child_node_value_r = @nodes[child_node_index_r]
      if child_node_value_r&.<child_node_value_l
        min_child_value = child_node_value_r
        min_child_index = child_node_index_r
      else
        min_child_value = child_node_value_l
        min_child_index = child_node_index_l
      end
      return top unless min_child_value < current_node_value
      change(current_node_index, min_child_index)
      current_node_index = min_child_index
    end
  end

  private

  def change(index_i, index_j)
    @nodes[index_i], @nodes[index_j] = @nodes[index_j], @nodes[index_i]
  end
end

class ReversePopHeap
  attr_accessor :nodes, :size
  def initialize
    @nodes = Array.new
    @size = 0
  end

  def push(x)
    current_index = @size
    @size += 1
    while current_index > 0 do
      parent_index = (current_index - 1)>>1
      parent_value = @nodes[parent_index]
      break if parent_value >= x
      @nodes[current_index] = parent_value
      current_index = parent_index
    end
    @nodes[current_index] = x
  end

  def pop
    top = @nodes[0]
    current_node_index = 0
    current_node_value = @nodes[0] = @nodes[@size - 1]
    @nodes[@size - 1] = nil
    @size -= 1

    while
      child_node_index_r = (current_node_index + 1) << 1
      child_node_index_l = child_node_index_r - 1
      return top unless child_node_index_l < @size
      child_node_value_l = @nodes[child_node_index_l]
      child_node_value_r = @nodes[child_node_index_r]
      if child_node_value_r&.>child_node_value_l
        max_child_value = child_node_value_r
        max_child_index = child_node_index_r
      else
        max_child_value = child_node_value_l
        max_child_index = child_node_index_l
      end
      return top unless max_child_value > current_node_value
      change(current_node_index, max_child_index)
      current_node_index = max_child_index
    end
  end

  private

  def change(index_i, index_j)
    @nodes[index_i], @nodes[index_j] = @nodes[index_j], @nodes[index_i]
  end
end

N, K = gets.split.map(&:to_i)
max_in_type = {}
values = []

N.times do
  temp = gets.split.map(&:to_i)
  max_in_type[temp[0]] = temp[1] if !(max_in_type[temp[0]]&.>(temp[1]))
  values << (temp[1] << 17) + temp[0]
end

ph = PopHeap.new
sum = 0
use_type_count = 0
type_flags = {}

t_mask = (1 << 17) - 1

values.sort.reverse[0...K].each do |t_v|
  t = t_v & t_mask
  v = t_v >> 17
  sum += v
  if !type_flags[t]
    type_flags[t] = true
    use_type_count += 1
  else
    ph.push(v)
  end
end
ans = sum + use_type_count**2

rph = max_in_type.each_with_object(ReversePopHeap.new) do |(k, v), obj|
  if !type_flags[k]
    # p "#{k} #{v}"
    obj.push(v)
  end
end


t = 0
phsize = ph.size
rphsize = rph.size
# p "#{rphsize} #{phsize}"
(phsize > rphsize ? rphsize : phsize).times do
  sum = sum - ph.pop + rph.pop
  t = sum + (use_type_count += 1)**2
  ans = t if t > ans
end

p ans