Language: C++

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>

int N;
long long M;
std::vector<long long > A(100000);
//std::vector<int > A(100000);
std::vector<long long > kumisuu(200000, 0); // A_i>=x を満たすiの数
std::vector<long long > souwa(200000, 0); // A_i>=x を満たすiについてA_iの総和

long long get_possible_count(int value){ // 値がvalue以上となる組み合わせ総数を求める
  long long count = 0;
  for (int i=0; i<N; i++){
    //printf("value-A[%d]=%d-%d=%d\n",i,value,A[i],value-A[i]);
    count += kumisuu[(int)std::max(value-A[i], (long long)0)];
    //printf("adding kumisuu[%d] = %d\n",std::max(value-A[i], 0), kumisuu[std::max(value-A[i], 0)]);
  }
  return count;
}

int main(){
  scanf("%d",&N);
  scanf("%lld",&M);

  for (int i=0; i<N; i++){
    //printf("i=%d\n",i);
    scanf("%lld",&A[i]);
    kumisuu[A[i]]++;
    souwa[A[i]] += A[i];
  }
  //printf("read end\n");
  sort(A.begin(), A.end(), std::greater<int>());

  /*for (int i=100; i<120; i++){
    printf("kumisuu[%d]=%d\n",i,kumisuu[i]);
    }*/

  /*  累積和を求める */
  for (int i=200000; i>0; i--){
    kumisuu[i-1] += kumisuu[i];
    souwa[i-1] += souwa[i];
  }

  /*for (int i=0; i<120; i++){
    printf("souwa[%d]=%d\n",i,souwa[i]);
    }*/

  int left = 0;
  int right = 200000;
  int mid;
  while(right-left>1){
    mid = (left+right)/2;
    //printf("get_possible_count(%d)=%lld\n", mid, get_possible_count(mid));
    if (get_possible_count(mid)>=M){ // 
      left = mid;
    } else {
      right = mid;
    }
  }
  //printf("left=%d, right=%d\n", left, right);

  int target;
  long long target_count;
  if (get_possible_count(right)>=M){
    target = right;
    target_count= get_possible_count(right);
  } else {
    target = left;
    target_count= get_possible_count(left);
  }
  //printf("target=%d\n", target);
  long long ans;
  for (int i=0; i<N; i++){
    ans += souwa[std::max(target-A[i],0)];
    ans += A[i]*kumisuu[std::max(target-A[i],0)];
    //printf("souwa adding + A[i] adding = %lld + %d = %lld\n", souwa[std::max(target-A[i],0)], A[i]*kumisuu[std::max(target-A[i],0)], souwa[std::max(target-A[i],0)]+A[i]*kumisuu[std::max(target-A[i],0)]);
    //printf("adding souwa[%d-%d] = souwa[%d] = %lld\n",target,A[i],std::max(target-A[i],0),souwa[std::max(target-A[i],0)]);
  }
  ans -= target*(target_count-M);
  /*if(left==mid) {
    std::cout << ans-abs(M-get_possible_count(left))*left << std::endl;
  } else if(right==mid) {
    std::cout << ans+abs(M-get_possible_count(right))*left << std::endl;
    }*/
  //printf("%lld\n", ans);
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:81:26: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type, int)'
   81 |     ans += souwa[std::max(target-A[i],0)];
      |                  ~~~~~~~~^~~~~~~~~~~~~~~
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:81:26: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   81 |     ans += souwa[std::max(target-A[i],0)];
      |                  ~~~~~~~~^~~~~~~~~~~~~~~
/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:8:
/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:81:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   81 |     ans += souwa[std::max(target-A[i],0)];
      |                  ~~~~~~~~^~~~~~~~~~~~~~~
a.cc:82:33: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type, int)'
   82 |     ans += A[i]*kumisuu[std::max(target-A[i],0)];
      |                         ~~~~~~~~^~~~~~~~~~~~~~~
/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:82:33: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   82 |     ans += A[i]*kumisuu[std::max(target-A[i],0)];
      |                         ~~~~~~~~^~~~~~~~~~~~~~~
/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
/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:82:33: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   82 |     ans += A[i]*kumisuu[std::max(target-A[i],0)];
      |                         ~~~~~~~~^~~~~~~~~~~~~~~