#require 'pp'

n, k = gets.chomp!.split(" ").map(&:to_i)
all_sushi = []
group_max = {}

n.times do |i|
    t, d = gets.chomp!.split(" ").map(&:to_i)
    all_sushi[i] = [d, t]
    group_max[t] ||= 0
    group_max[t] = group_max[t] < d ? d : group_max[t]
end
all_sushi.sort!.reverse!
sorted_group_max = group_max.sort {|a, b| b[1] <=> a[1]}.to_h

#pp all_sushi
#pp sorted_group_max

# 大きいほうからK個取り出す
max_point = 0
max_comb = {}
others = []
k.times do |i|
    max_point += all_sushi[i][0]
    if max_comb[all_sushi[i][1]] == nil
        max_comb[all_sushi[i][1]] = all_sushi[i][0]
    else
        others << all_sushi[i][0]
    end
end

max_comb.each do |x, _|
    sorted_group_max.delete(x)
end

#pp max_point
#pp max_comb
#pp others
#pp sorted_group_max

tmp_max = max_point 
tmp_kind = max_comb.size
tmp_point = tmp_max + tmp_kind ** 2
ans = tmp_point

others.each do |o|
    k, v = sorted_group_max.first
    break if k == nil
    sorted_group_max.delete(k)
    tmp_kind += 1
    tmp_max = tmp_max - o + v
    tmp_point = tmp_max + tmp_kind ** 2
    if tmp_point > ans
        ans = tmp_point
    end
end

puts ans
