import java.math.*

fun main(args: Array<String>) {
    val (n, k) = readInputLine().split(" ").map{it.toInt()}
    
    val sushi = mutableListOf<Pair<Long, Long>>()
    
    for (i in 1..n) {
        val (t, d) = readInputLine().split(" ").map{it.toLong()}
        sushi.add(Pair(t, d))
    }
    
    sushi.sortByDescending{it.second}
    
    val f = LongArray(n + 1, {Long.MIN_VALUE})
    
    var score = 0L
    val kinds = mutableSetOf<Long>()
    val removable = mutableListOf<Long>()
    
    for (i in 1..k) {
        val (t, d) = sushi.first()
        sushi.removeAt(0)
        score += d
        if (kinds.contains(t)) {
            removable.add(0, d)
        } else {
            kinds.add(t)
        }
    }

    f[kinds.size] = score
    
    while (removable.isNotEmpty() && sushi.size != 0) {
        val (t, d) = sushi.first()
        sushi.removeAt(0)
        if (kinds.contains(t)) {
            continue
        }
        kinds.add(t)
        f[kinds.size] = f[kinds.size - 1] - removable.first() + d
        removable.removeAt(0)
    }

    println(f.foldIndexed(Long.MIN_VALUE, {index, acc, elem -> Math.max(acc, elem + index.toLong() * index.toLong())}))
}

fun readInputLine(): String {
    return readLine()!!
}

