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 &th, VI &a, LL &n, LL &m) {
    LL cnt = 0;
    RREP(i,0,n) {
        auto it = lower_bound(all(a), th - a[i]);
        cnt += max(0LL, (i - distance(a.begin(), it)) * 2 + 1);
    }
    return cnt >= m;
}

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

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

    return ok;
}

void solve() {
    LL n,m;
    cin >> n >> m;
    VI a(n);
    REP(i,0,n) cin >> a[i];
    sort(all(a));
    int th = meguru_bin(a,n,m);
    LL cnt = 0;
    LL sum = 0;
    VLL ru(n+1);
    REP(i,0,n) ru[i+1] = ru[i] + a[i];

    RREP(i,0,n) {
        auto it = lower_bound(all(a), th - a[i]);
        int d = distance(a.begin(), it);
        cnt += max(0, (i - d) * 2 + 1);
        sum += max(0LL, (ru[i] - ru[d]) * 2 + ((i - d + 1) * 2) * a[i]);
    }
    sum -= max(0LL, cnt - m) * th;
    cout << sum << endl;
}

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

Compiler output:
a.cc: In function 'bool solve(LL&, VI&, LL&, LL&)':
a.cc:58:19: error: no matching function for call to 'max(long long int, std::__iterator_traits<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, void>::difference_type)'
   58 |         cnt += 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:58:19: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'std::__iterator_traits<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, void>::difference_type' {aka 'long int'})
   58 |         cnt += 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:58:19: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   58 |         cnt += max(0LL, (i - distance(a.begin(), it)) * 2 + 1);
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~