import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;


void main() {
    int n, k;
    scan(n, k);
    auto t = new int[](n);
    auto d = new long[](n);
    iota(n).each!(i => scan(t[i], d[i]));
    t[] -= 1;

    auto c = new long[][](n, 0);
    foreach (i ; 0 .. n) {
        c[t[i]] ~= d[i];
    }

    long[] top;
    long[] nkr;

    foreach (i ; 0 .. n) {
        c[i].sort!"a > b"();
        if (!c[i].empty) {
            top ~= c[i].front;
            nkr ~= c[i][1 .. $];
        }
    }

    auto m = top.length.to!int;

    top.sort!"a > b"();
    nkr.sort!"a > b"();

    debug {
        writeln(top);
        writeln(nkr);
    }

    auto ts = new long[](m + 1);
    foreach (i ; 1 .. m + 1) {
        ts[i] = top[i-1] + ts[i-1];
    }
    auto ns = new long[](nkr.length + 1);
    foreach (i ; 1 .. nkr.length + 1) {
        ns[i] = ns[i-1] + nkr[i-1];
    }

    long ans;

    foreach (i ; 1 .. n + 1) {
        if (i > k || i > m) {
            break;
        }
        if (k - i > nkr.length) {
            continue;
        }
        long tmp = ts[i] + ns[k - i] + 1L * i * i;
        ans = max(ans, tmp);
    }

    writeln(ans);
}





void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
