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<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[std::max(value-A[i], 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("%d",&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;
  while(right-left>1){
    int 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(l==mid) {
    cout << ans-abs(M-target_count(left))*left << endl;
  } else if(r==mid) {
    cout << ans+abs(M-target_count(right))*left << endl;
  }
  printf("%lld\n", ans);
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:85:6: error: 'l' was not declared in this scope
   85 |   if(l==mid) {
      |      ^
a.cc:85:9: error: 'mid' was not declared in this scope
   85 |   if(l==mid) {
      |         ^~~
a.cc:86:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   86 |     cout << ans-abs(M-target_count(left))*left << endl;
      |     ^~~~
      |     std::cout
In file included from a.cc:1:
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:86:35: error: 'target_count' cannot be used as a function
   86 |     cout << ans-abs(M-target_count(left))*left << endl;
      |                       ~~~~~~~~~~~~^~~~~~
a.cc:86:51: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   86 |     cout << ans-abs(M-target_count(left))*left << endl;
      |                                                   ^~~~
      |                                                   std::endl
In file included from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~
a.cc:87:13: error: 'r' was not declared in this scope
   87 |   } else if(r==mid) {
      |             ^
a.cc:88:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   88 |     cout << ans+abs(M-target_count(right))*left << endl;
      |     ^~~~
      |     std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:88:35: error: 'target_count' cannot be used as a function
   88 |     cout << ans+abs(M-target_count(right))*left << endl;
      |                       ~~~~~~~~~~~~^~~~~~~
a.cc:88:52: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   88 |     cout << ans+abs(M-target_count(right))*left << endl;
      |                                                    ^~~~
      |                                                    std::endl
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~