Language: C++

#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

bool solve(LL &t, LL &n, LL m, VLL &a) {
    m -= (n - t) * 2 - 1;
    LL th = a[t] + a[n-1];
    RREP(i,0,n-1) {
        if (m < 0) return false;
        auto it = lower_bound(all(a), th - a[i]);
        m -= max(0LL, (i - distance(a.begin(), it)) * 2 - 1);
    }
    return m >= 0;
}

long long meguru_bin(LL &n, LL &m, VLL &a) {
    long long ok = n;
    long long ng = -1;

    while (abs(ok - ng) > 1) {
        long long mid = ng + (ok - ng) / 2;
        if (solve(mid,n,m,a)) ok = mid;
        else ng = mid;
    }

    return ok;
}

void solve() {
    LL n,m;
    cin >> n >> m;
    VLL a(n);
    REP(i,0,n) cin >> a[i];
    sort(all(a));
    LL t = meguru_bin(n,m,a);
    //dump(t);
    VLL ru(n+1);
    REP(i,0,n) ru[i+1] = ru[i] + a[i];

    LL ans = 0;
    m -= (n - t) * 2 - 1;
    ans += ((ru[n] - ru[t]) + (n - t - 1) * a[n-1]) * 2;
    //dump(ans);
    LL th = a[t] + a[n-1];
    VI ps(n);
    ps[n-1] = t - 1;
    RREP(i,0,n-1) {
        auto it = lower_bound(all(a), th - a[i]);
        int t = distance(a.begin(), it);
        ps[i] = min(i, t - 1);
        LL diff = (i + 1 - distance(a.begin(), it)) * 2 - 1;
        if (diff <= 0) continue;
        m -= diff;
        ans += ((ru[i+1] - ru[t]) + (i - t) * a[i]) * 2;
        //dump(m);
        //dump(ans);
    }
    //dump(m);
    //dump(ans);
    priority_queue<PII> q;
    RREP(i,0,n) {
        if (ps[i] < 0) continue;
        if (ps[i] == i) {
            q.emplace(a[i] * 2, i);
        } else {
            q.emplace(a[ps[i]] + a[i], i);
            q.emplace(a[ps[i]] + a[i], i);
        }
        --ps[i];
    }
    while (m > 0 && !q.empty()) {
        auto p = q.top();
        q.pop();
        ans += p.first;
        //dump(p.first);
        //dump(p.second);
        //dump(m);
        //dump(q.size());
        int i = p.second;
        if (ps[i] >= 0) {
            q.emplace(a[ps[i]] + a[i], i);
            q.emplace(a[ps[i]] + a[i], i);
            --ps[i];
        }
        --m;
    }
    cout << ans << endl;
}

signed main() {
    solve();
    
    return 0;
}

Compiler output:
a.cc: In function 'bool solve(LL&, LL&, LL, VLL&)':
a.cc:60:17: error: no matching function for call to 'max(long long int, std::__iterator_traits<__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >, void>::difference_type)'
   60 |         m -= max(0LL, (i - distance(a.begin(), it)) * 2 - 1);
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:60:17: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'std::__iterator_traits<__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >, void>::difference_type' {aka 'long int'})
   60 |         m -= max(0LL, (i - distance(a.begin(), it)) * 2 - 1);
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:60:17: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   60 |         m -= max(0LL, (i - distance(a.begin(), it)) * 2 - 1);
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~