import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    long N, M;
    scan(N, M);
    auto A = aryread();
    A.sort!"b<a"();
    PQueue!(Tuple!(long, long, long)) Q;
    Q.insert(Tuple!(long, long, long)(A[0] + A[0], 0, 0));
    long ans;
    long i;
    // writeln(A);
    auto visited = new RedBlackTree!(long);
    while ((!Q.empty) && i < M)
    {
        auto t = Q.front;
        // writeln(t);
        Q.removeFront();
        if (t[1] + t[2] * 10 ^^ 5 in visited)
            continue;
        visited.insert(t[1] + t[2] * 10 ^^ 5);
        ans += t[0];
        i++;
        if (t[1] + 1 < N)
            Q.insert(Tuple!(long, long, long)(A[t[1] + 1] + A[t[2]], t[1] + 1, t[2]));
        if (t[2] + 1 < N)
            Q.insert(Tuple!(long, long, long)(A[t[1]] + A[t[2] + 1], t[1], t[2] + 1));
    }
    writeln(ans);
}
