import java.util.*

fun main(args: Array<String>) {
    val (N, K) = readLine()!!.split(" ").map{it.toInt()}

    val ps = Array(N) {
        val (T, D) = readLine()!!.split(" ").map{it.toLong()}
        Pair(T, D)
    }

    ps.sortWith(Comparator { o1, o2 -> o2.second.compareTo(o1.second) })

    var tmp = 0L
    val st = TreeSet<Long>()
    val Q = PriorityQueue<Long>()

    ps.take(K).forEach {
        tmp += it.second
        if (st.contains(it.first)) {
            Q.add(it.second)
        } else {
            st.add(it.first)
        }
    }

    var res = tmp + st.size * st.size.toLong()

    for (p in ps.takeLast(N - K)) {
        if (st.contains(p.first)) {
            continue
        }

        if (Q.isEmpty())
            continue

        tmp -= Q.poll()

        st.add(p.first)
        tmp += p.second

        res = Math.max(res, tmp + st.size * st.size.toLong())
    }

    println(res)
}
