
object Main extends App {

  import scala.io.StdIn._

  val Array(n, k) = readLine.trim.split(' ').map(_.toInt)
  val sushis = Array.tabulate(n){_ ⇒
    val Array(t, d) = readLine.trim.split(' ').map(_.toInt)
    Sushi(t - 1, d)
  }
  val (sumPoints: Array[Long], rest: Array[Long]) = sushis.groupBy(_.neta).mapValues(_.sortBy(_.point).reverse).let{m ⇒ (m.mapValues(_.head).toArray.map(_._2.point).sorted.reverse, 0L +: m.mapValues(_.tail).flatMap(_._2.map(_.point)).toArray.sorted.reverse)}
  for (i ← sumPoints.indices.tail){
    sumPoints(i) += sumPoints(i - 1)
  }
  for (i ← rest.indices.tail){
    rest(i) += rest(i - 1)
  }
  println((1 to math.min(sumPoints.length, k)).map(i ⇒ sumPoints(i - 1) + rest(math.max(math.min(rest.length - 1, k - i), 0)) + i * i).max)

  case class Sushi(neta: Int, point: Long)
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}