Language: C++

#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  long N,M; cin>>N>>M;
  vector<int> A(N);
  for (auto& a : A) cin>>a;
  sort(A.begin(), A.end(), [](auto l, auto r) {return l > r;});

  vector<int> cnt(2e5+1, 0);
  for (auto a : A) cnt[a]++;
  for (int i = cnt.size()-2; i >= 0; i--) {
    cnt[i] += cnt[i+1];
  }
  auto count_pairs = [&](int hold) {
    int res = 0;
    for (int i = 0; i < N; i++) {
      if (hold - A[i] < 0)
        res += N;
      else
        res += cnt[hold - A[i]];
    }
    return res;
  };

  int l = 0, r = A[0]*2;
  int maxx = l;
  while (l <= r) {
    auto c = (l+r)/2;
    if (count_pairs(c) >= M) {
      maxx = c;
      l = c+1;
    } else {
      r = c-1;
    }
  }

  vector<long> CA(N+1);
  for (int i = 1; i <= N; i++) {
    CA[i] = CA[i-1]+A[i-1];
  }

  long ans = 0;
  long min_n = A[0]*2;
  for (int i = 0; i < N; i++) {
    auto num = maxx-A[i] >= 0 ? cnt[maxx-A[i]] : N;
    ans += long(A[i])*num + CA[num];
    if (num > 0)
      min_n = min(min_n, A[i]+A[num-1]);
  }

  auto members = count_pairs(maxx);
  if (members > M)
    ans -= min_n*(members-M);
  cout << ans << endl;

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:52:18: error: no matching function for call to 'min(long int&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type)'
   52 |       min_n = min(min_n, A[i]+A[num-1]);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~
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:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:52:18: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
   52 |       min_n = min(min_n, A[i]+A[num-1]);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281: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:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:52:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   52 |       min_n = min(min_n, A[i]+A[num-1]);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~