Language: C++

#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int N = 1e5 + 5;
ll n, m, ans, a[N], acc[N];
ll sum(int l, int r){
    if(l > r) return 0;
    if(!l) return acc[r];
    return acc[r] - acc[l - 1];
}
int between(int s, int e){
    auto l = lower_bound(a, a + n, s) - a;
    auto r = upper_bound(a, a + n, e) - a;
    if(a[0] > e || a[n - 1] < s) return 0;
    return max(r - l, 0ll);
}
bool ok(int mid){
    ll res = 0;
    for(int i = 0; i < n; i++){
        if(a[i] >= mid) res += n;
        else res += between(mid - a[i], 2 * N);
    }
    return res >= m;
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    sort(a, a + n);
    for(int i = 0; i < n; i++)
        acc[i] = a[i] + (i? acc[i - 1] : 0);
    ll s = 0, e = 2e5, mid, res = 2;
    while(s <= e){
        mid = s + e >> 1;
        if(ok(mid))
            s = mid + 1, res = mid;
        else
            e = mid - 1;
    }
    res++;
    for(int i = 0; i < n; i++){
        if(a[i] >= res)
            ans += n * a[i] + acc[n - 1], m -= n;
        else{
            auto l = lower_bound(a, a + n, res - a[i]) - a;
            auto r = upper_bound(a, a + n, 2 * N) - a;
            r--;
            if(l > r) continue;
            ans += a[i] * (r - l + 1) + sum(l, r);
            m -= r - l + 1;
        }
    }
    res--;
    cout << ans + m * res;
    return 0;
}


Compiler output:
a.cc: In function 'int between(int, int)':
a.cc:17:15: error: no matching function for call to 'max(long int, long long int)'
   17 |     return max(r - l, 0ll);
      |            ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/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:17:15: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'long long int')
   17 |     return max(r - l, 0ll);
      |            ~~~^~~~~~~~~~~~
/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:
/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:17:15: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   17 |     return max(r - l, 0ll);
      |            ~~~^~~~~~~~~~~~