object Main {
  import java.util.Scanner

  def readStrings(n: Int)(implicit sc: Scanner): Array[String] = Array.fill(n) { sc.next() }
  def readInts(n: Int)(implicit sc: Scanner): Array[Int] = Array.fill(n) { sc.nextInt() }
  def readLongs(n: Int)(implicit sc: Scanner): Array[Long] = Array.fill(n) { sc.nextLong() }
  def readLineStrings(implicit sc: Scanner): Array[String] = sc.nextLine().split(' ')
  def readLineInts(implicit sc: Scanner): Array[Int] = readLineStrings.map(_.toInt)
  def readLineLongs(implicit sc: Scanner): Array[Long] = readLineStrings.map(_.toLong)

  def main(args: Array[String]): Unit = solve(new Scanner(System.in))

  def solve(implicit sc: Scanner): Unit = {
    case class Sushi(neta: Int, score: Long)
    val Array(n, k) = readLineInts
    val sushi = Array.fill(n) { val Array(a, b) = readLineInts; Sushi(a, b) }

    val scoreSortedSushi = sushi.sortBy(_.score).reverse

    val topKSushis = scoreSortedSushi.take(k).reverse

    val (bests, stocks) = {
      val groups = topKSushis.groupBy(_.neta)
      val partition = groups.mapValues(sushis => {
        val best = sushis.maxBy(_.score)
        val firstBestIndex = sushis.indexWhere(_ == best)
        val nonBests = sushis.zipWithIndex.filter { case (_, i) => i != firstBestIndex }.map(_._1)
        (best, nonBests)
      })
      val stocks = partition.mapValues(_._2).values.toArray.flatten.sortBy(_.score)
      val bests = partition.mapValues(_._1)
      (bests, stocks)
    }

    val restBests = {
      val groups = scoreSortedSushi.drop(k).groupBy(_.neta)
      groups.mapValues(_.maxBy(_.score)).values.toArray.sortBy(_.score).reverse
    }

    var totalBaseScore = topKSushis.map(_.score).sum
    var totalNetaNumber = topKSushis.map(_.neta).length
    def score = totalBaseScore + (totalNetaNumber * totalNetaNumber)

    var maxTotal = score

    var stockIndex = 0
    for { restBest <- restBests } {
      if (stockIndex < stocks.length) {
        totalBaseScore += restBest.score - stocks(stockIndex).score
        totalNetaNumber += 1

        val newScore = score
        if (newScore > maxTotal) maxTotal = newScore

        stockIndex += 1
      } else {
        return println(maxTotal)
      }
    }

    println(maxTotal)
  }
}