Language: C++

bool isOK(ll k, vector<ll>& A, ll M) {
	int cnt = 0;
	for (int i = A.size()-1; i >= 0; i--) {
		auto itr = lower_bound(A.begin(), A.end(), k - A[i]);
		cnt += (A.end() - itr);
	}
	return cnt >= M;
}
//最小の幸福度をもたらす握手のスコア
ll calcMinScore(vector<ll> A, ll M, ll N) {
	ll ng = A.back()+A.back()+1; //「index = 0」が条件を満たすこともあるので、初期値は -1
	ll ok = A[0]+A[0]-1; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()
 
	/* ok と ng のどちらが大きいかわからないことを考慮 */
	while (abs(ok - ng) > 1) {
		ll mid = (ok + ng) / 2;
 
		if (isOK(mid, A, M)) ok = mid;
		else ng = mid;
	}
	return ok;
}
int main() {
	ll N, M;
	cin >> N >> M;
	vector<ll> A(N), S(N+1,0);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	sort(A.begin(), A.end());
	for (int i = 0; i < N; i++) {
		S[i + 1] = S[i] + A[i];
	}
 
	ll ms = 0;
	ll ans = 0;
	ll tmp = 0;
	for (int i = A.size() - 1; i >= 0; i--) {
		int cnt;
		auto itr = lower_bound(A.begin(), A.end(), ms - A[i]);
		cnt = (A.end() - itr);
		tmp += cnt;
		if (tmp > M)cnt -= tmp-M;
		if (cnt <= 0)continue;
		ans += A[i] * cnt + S[N]-S[N-cnt];
	}
	cout << ans << endl;
}

Compiler output:
a.cc:1:11: error: 'll' was not declared in this scope
    1 | bool isOK(ll k, vector<ll>& A, ll M) {
      |           ^~
a.cc:1:17: error: 'vector' was not declared in this scope
    1 | bool isOK(ll k, vector<ll>& A, ll M) {
      |                 ^~~~~~
a.cc:1:24: error: 'll' was not declared in this scope
    1 | bool isOK(ll k, vector<ll>& A, ll M) {
      |                        ^~
a.cc:1:29: error: 'A' was not declared in this scope
    1 | bool isOK(ll k, vector<ll>& A, ll M) {
      |                             ^
a.cc:1:32: error: 'll' was not declared in this scope
    1 | bool isOK(ll k, vector<ll>& A, ll M) {
      |                                ^~
a.cc:1:36: error: expression list treated as compound expression in initializer [-fpermissive]
    1 | bool isOK(ll k, vector<ll>& A, ll M) {
      |                                    ^
a.cc:10:1: error: 'll' does not name a type
   10 | ll calcMinScore(vector<ll> A, ll M, ll N) {
      | ^~
a.cc: In function 'int main()':
a.cc:24:9: error: 'll' was not declared in this scope
   24 |         ll N, M;
      |         ^~
a.cc:25:9: error: 'cin' was not declared in this scope
   25 |         cin >> N >> M;
      |         ^~~
a.cc:25:16: error: 'N' was not declared in this scope
   25 |         cin >> N >> M;
      |                ^
a.cc:25:21: error: 'M' was not declared in this scope
   25 |         cin >> N >> M;
      |                     ^
a.cc:26:9: error: 'vector' was not declared in this scope
   26 |         vector<ll> A(N), S(N+1,0);
      |         ^~~~~~
a.cc:26:20: error: 'A' was not declared in this scope
   26 |         vector<ll> A(N), S(N+1,0);
      |                    ^
a.cc:26:26: error: 'S' was not declared in this scope
   26 |         vector<ll> A(N), S(N+1,0);
      |                          ^
a.cc:30:9: error: 'sort' was not declared in this scope; did you mean 'short'?
   30 |         sort(A.begin(), A.end());
      |         ^~~~
      |         short
a.cc:35:11: error: expected ';' before 'ms'
   35 |         ll ms = 0;
      |           ^~~
      |           ;
a.cc:36:11: error: expected ';' before 'ans'
   36 |         ll ans = 0;
      |           ^~~~
      |           ;
a.cc:37:11: error: expected ';' before 'tmp'
   37 |         ll tmp = 0;
      |           ^~~~
      |           ;
a.cc:40:60: error: 'ms' was not declared in this scope
   40 |                 auto itr = lower_bound(A.begin(), A.end(), ms - A[i]);
      |                                                            ^~
a.cc:40:28: error: 'lower_bound' was not declared in this scope
   40 |                 auto itr = lower_bound(A.begin(), A.end(), ms - A[i]);
      |                            ^~~~~~~~~~~
a.cc:42:17: error: 'tmp' was not declared in this scope
   42 |                 tmp += cnt;
      |                 ^~~
a.cc:45:17: error: 'ans' was not declared in this scope
   45 |                 ans += A[i] * cnt + S[N]-S[N-cnt];
      |                 ^~~
a.cc:47:9: error: 'cout' was not declared in this scope
   47 |         cout << ans << endl;
      |         ^~~~
a.cc:47:17: error: 'ans' was not declared in this scope
   47 |         cout << ans << endl;
      |                 ^~~
a.cc:47:24: error: 'endl' was not declared in this scope
   47 |         cout << ans << endl;
      |                        ^~~~