Language: C++

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
  int n;
  long long m;
  cin >> n >> m;
  int a[n];
  for (int i=0; i<n; i++) {
    cin >> a[i];
  }
  sort(a, a+n);
  reverse(a, a+n);
  
  // 片手の握手でX以上のスコアを取れる相手は何人？
  int dp[200002];
  for (int i=0; i<200002; i++) dp[i] = 0;
  int tmp = 0;
  for (int x=200001; x>=0; x--) {
    while (tmp < n && x <= a[tmp]) {
      tmp += 1;
    }
    dp[x] = tmp;
  }
  //cout << dp[0] << " " << dp[6000] << " " << dp[50000] << endl;
  // 高い順にi個の和
  int cumsum[200002];
  cumsum[0] = 0;
  for (int i=1; i<=n; i++) cumsum[i] = cumsum[i-1] + a[i-1];
  
  // 両手でX以上のスコアを取れる握手は何パターン？
  // Xに対して広義単調減少になる
  // → Mをぎりぎり割るXを二分探索
  long long left = 0;
  long long right = 200001;
  while (left != right) {
    // X = middle以上のスコアを取れる人を数える
    long long middle = (left + right) / 2;
    long long sum = 0;
    for (int i=0; i<n; i++) { // 左手を先に決める
      if (middle <= a[i]) sum += n; // 左手だけで間に合うなら右手はn通り
      else sum += dp[middle - a[i]]; // 左手で間に合わないなら右手でx-a[i]を作る
    }
    if (sum >= m) left = middle + 1;
    else right = middle;
  }
  // left - 1がぎりぎりM通り以上の実現方法があるX
  //cout << left - 1 << endl;
  
  // 各左手に対して、和がleft以上になる出し方をする
  long long result = 0;
  long long used = 0;
  for (int i=0; i<n; i++) {
  	long long usable = dp[max(0, left - a[i])];
    result += cumsum[usable] + a[i] * usable;
    used += usable;
  }
  // 残りはleft-1
  result += (left - 1) * (m - used);
  
  cout << result << endl;
  
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:56:34: error: no matching function for call to 'max(int, long long int)'
   56 |         long long usable = dp[max(0, left - a[i])];
      |                               ~~~^~~~~~~~~~~~~~~~
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:56:34: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   56 |         long long usable = dp[max(0, left - a[i])];
      |                               ~~~^~~~~~~~~~~~~~~~
/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:2:
/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:56:34: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   56 |         long long usable = dp[max(0, left - a[i])];
      |                               ~~~^~~~~~~~~~~~~~~~