Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int n;
ll k;
int a[maxn]; 
ll sum[maxn];
ll calc(int x)
{
	ll ret = 0;
	for(int i = 1; i <= n; ++i)
		ret += upper_bound(a + 1, a + n + 1, x - a[i], greater<int>()) - 1 - a;
	return ret;
}
void solve(int x)
{
	ll ans = 0;
	for(int i = 1; i <= n; ++i)
	{
		int r = min(upper_bound(a + 1, a + n + 1, x - a[i], greater<int>()) - 1 - a, k);
		ans += sum[r] + 1ll * a[i] * r;
		k -= r;
		if(k == 0) break;
	}
	printf("%lld\n", ans);
}
int main()
{
	scanf("%d%lld", &n, &k);
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
	sort(a + 1, a + n + 1, greater<int>());
	for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
	int l = 2, r = 2e5;
	while(l <= r)
	{
		int mid = l + r >> 1;
		if(calc(mid) >= k)
			l = mid + 1;
		else
			r = mid - 1;
	}
	solve(r);
	return 0;
}
		

Compiler output:
a.cc: In function 'void solve(int)':
a.cc:21:28: error: no matching function for call to 'min(long int, ll&)'
   21 |                 int r = min(upper_bound(a + 1, a + n + 1, x - a[i], greater<int>()) - 1 - a, k);
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:21:28: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'll' {aka 'long long int'})
   21 |                 int r = min(upper_bound(a + 1, a + n + 1, x - a[i], greater<int>()) - 1 - a, k);
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:21:28: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   21 |                 int r = min(upper_bound(a + 1, a + n + 1, x - a[i], greater<int>()) - 1 - a, k);
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~