ary = $stdin.readlines.map {| s | s.split(" ").map {| x | x.to_i } }
_, k = ary.shift

puts ary.combination(k).map {| comb |
  variety       = []
  deliciousness = 0
  comb.map {| t, d |
    variety << t
    deliciousness += d
  }
  variety.uniq!
  x = variety.size
  deliciousness + x * x
}.max
