void main()
{
    long n, k;
    rdVals(n, k);
    Pair[] pair = n.rdCol!Pair;

    pair.sort!"a.b > b.b";
    bool[] check = new bool[n+1];
    Pair[] a, b;
    foreach (p; pair)
    {
        if (check[p.a])
        {
            b ~= p;
        }
        else
        {
            a ~= p;
            check[p.a] = true;
        }
    }

    a.sort!"a.b > b.b";
    long[] c = new long[n+1];
    foreach (i, x; a)
    {
        c[i+1] = c[i] + x.b;
    }

    b.sort!"a.b > b.b";
    long[] d = new long[n+1];
    foreach (i, x; b)
    {
        if (k - i - 1 <= 0) break;
        d[k-i-1] = d[k-i] + x.b;
    }

    long result;
    foreach (i; 1 .. k+1)
    {
        if (c[i] == 0) break;
        result = max(result, c[i]+d[i]+i^^2);
    }

    result.writeln;
}

struct Pair
{
    long a, b;
}

enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;

T rdElem(T = long)()
if (!is(T == struct))
{
    return readln.chomp.to!T;
}

alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);

T rdElem(T)()
if (is(T == struct))
{
    T result;
    string[] input = rdRow!string;
    assert(T.tupleof.length == input.length);
    foreach (i, ref x; result.tupleof)
    {
        x = input[i].to!(typeof(x));
    }
    return result;
}

T[] rdRow(T = long)()
{
    return readln.split.to!(T[]);
}

T[] rdCol(T = long)(long col)
{
    return iota(col).map!(x => rdElem!T).array;
}

T[][] rdMat(T = long)(long col)
{
    return iota(col).map!(x => rdRow!T).array;
}

void rdVals(T...)(ref T data)
{
    string[] input = rdRow!string;
    assert(data.length == input.length);
    foreach (i, ref x; data)
    {
        x = input[i].to!(typeof(x));
    }
}

void wrMat(T = long)(T[][] mat)
{
    foreach (row; mat)
    {
        foreach (j, compo; row)
        {
            compo.write;
            if (j == row.length - 1) writeln;
            else " ".write;
        }
    }
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;