import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional;
import std.container;

void main() {
    auto tmp = readln.split.to!(long[]);
    auto N = tmp[0], K = tmp[1];

    long[][long] sushiList;
    foreach (i; 0..N) {
        tmp = readln.split.to!(long[]);
        auto t = tmp[0], d = tmp[1];
        if (t !in sushiList) sushiList[t] = [];
        sushiList[t] ~= d;
    }
    foreach (key; sushiList.keys) {
        sushiList[key] = sushiList[key].sort!"a>b".array;
    }

    long[] topList;
    long[] leftList;
    foreach (key; sushiList.keys) {
        topList ~= sushiList[key].front;
        leftList ~= sushiList[key][1..$];
    }
    topList = topList.sort!"a>b".array;
    leftList = leftList.sort!"a>b".array;

    long[] topSum = [0];
    foreach (i; 0..topList.length) topSum ~= topSum[$-1] + topList[i];
    long[] leftSum = [0];
    foreach (i; 0..leftList.length) leftSum ~= leftSum[$-1] + leftList[i];

    long ans = 0;
    foreach (i; 1..N+1) {
        if (i >= topSum.length) continue;
        if (K-i >= leftSum.length) continue;
        if (K-i < 0) continue;
        ans = max(ans, topSum[i] + leftSum[K-i] + i^^2);
    }
    writeln(ans);
}

