Language: C++

#include<bits/stdc++.h>

int main(){
    using ll = long long;
    constexpr int MAX_A=100001;
    int N;
    ll M;
    std::cin>>N>>M;
    std::vector<ll> A(N);
    for(int i=0;i<N;++i) std::cin>>A[i];
    std::vector<ll> cnt(MAX_A);
    for(int i=0;i<N;++i) cnt[A[i]]++;
    std::vector<ll> S(MAX_A+1);
    for(int i=0;i<MAX_A;++i) S[i+1]=S[i]+cnt[i];
    int l=-1,r=3*MAX_A+1;
    while(l+1<r){
        int X=(l+r)>>1;
        ll res=0;
        for(int i=0;i<MAX_A;++i){
            res+=cnt[i]*(S[MAX_A]-S[std::max(X-i,0)]);
        }
        if(res>=M) l=X;
        else r=X;
    }
    std::vector<ll> SS(MAX_A+1);
    for(ll i=0;i<MAX_A;++i) SS[i+1]=SS[i]+i*cnt[i];
    ll ans=0,res=0;
    for(ll i=0;i<MAX_A;++i){
        ans+=cnt[i]*((S[MAX_A]-S[std::max(l-i,0)])*i+SS[MAX_A]-SS[std::max(l-i,0)]);
        res+=cnt[i]*(S[MAX_A]-S[std::max(l-i,0)]);
    }
    ans-=(res-M)*l;
    std::cout<<ans<<std::endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:29:42: error: no matching function for call to 'max(ll, int)'
   29 |         ans+=cnt[i]*((S[MAX_A]-S[std::max(l-i,0)])*i+SS[MAX_A]-SS[std::max(l-i,0)]);
      |                                  ~~~~~~~~^~~~~~~
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: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:29:42: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   29 |         ans+=cnt[i]*((S[MAX_A]-S[std::max(l-i,0)])*i+SS[MAX_A]-SS[std::max(l-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:
/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:29:42: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   29 |         ans+=cnt[i]*((S[MAX_A]-S[std::max(l-i,0)])*i+SS[MAX_A]-SS[std::max(l-i,0)]);
      |                                  ~~~~~~~~^~~~~~~
a.cc:29:75: error: no matching function for call to 'max(ll, int)'
   29 |         ans+=cnt[i]*((S[MAX_A]-S[std::max(l-i,0)])*i+SS[MAX_A]-SS[std::max(l-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:29:75: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   29 |         ans+=cnt[i]*((S[MAX_A]-S[std::max(l-i,0)])*i+SS[MAX_A]-SS[std::max(l-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:29:75: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   29 |         ans+=cnt[i]*((S[MAX_A]-S[std::max(l-i,0)])*i+SS[MAX_A]-SS[std::max(l-i,0)]);
      |                                                                   ~~~~~~~~^~~~~~~
a.cc:30:41: error: no matching function for call to 'max(ll, int)'
   30 |         res+=cnt[i]*(S[MAX_A]-S[std::max(l-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:30:41: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   30 |         res+=cnt[i]*(S[MAX_A]-S[std::max(l-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:30:41: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   30 |         res+=cnt[i]*(S[MAX_A]-S[std::max(l-i,0)]);
      |                                 ~~~~~~~~^~~~~~~