import scala.collection.mutable.Map
import scala.collection.mutable.PriorityQueue


object Main {
    def main(args : Array[String]) = {
        val sc = new java.util.Scanner(System.in)
        val n, k = sc.nextInt
        val sushis = List.fill(n)(sc.nextInt, sc.nextInt).sortWith(_._2 > _._2)
        var kind, ans : Long = 0
        val used = new Array[Boolean](n+1)
        var selected = PriorityQueue[Long]()(Ordering.Long.reverse)
        for ((t, d) <- sushis take k) {
            ans += d
            if (used(t)) selected += d
            else {
                used(t) = true
                kind += 1
            }
        }
        val toeat_ : Map[Int, List[Long]] = Map()
        for ((t, d) <- sushis) 
            if (toeat_ contains t) toeat_(t) = d :: toeat_(t)
            else toeat_(t) = List(d)
        var toeat : List[List[Long]] = toeat_.filter{case (k, _) => !used(k)}.values.toList
        toeat = toeat.map(_.sortWith(_ > _)).sortWith(_.head > _.head)
        var candidate : Long = kind * kind + ans
        for (xs <- toeat if selected.size > 0) {
            kind += 1
            ans += (xs.head - selected.dequeue)
            candidate = candidate max (kind * kind + ans)
        }
        println(candidate)
    }
}
