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[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<long long>());

  for (int i=0; i<N; i++){
    souwa[i+1] = souwa[i] + A[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 = 200001;
  int mid;
  while(right-left>1){
    mid = (left+right)/2;
    //printf("get_possible_count(%d)=%lld\n", mid, get_possible_count(mid));
    long long ans_temp = 0;
    for (int i=0; i<N; i++){
      ans_temp += souwa[kumisuu[std::max(left-A[i],(long long)0)]];
      ans_temp += A[i]*kumisuu[std::max(left-A[i],(long long)0)];
    }
    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[kumisuu[std::max(target-A[i],(long long)0)]];
    //ans += souwa[std::max(target-A[i],(long long)0)];
    ans += A[i]*kumisuu[std::max(target-A[i],(long long)0)];
    //printf("souwa adding = %lld + %lld\n", souwa[kumisuu[std::max(target-A[i],(long long)0)]], A[i]*kumisuu[std::max(target-A[i],(long long)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 -= (long long)target*(target_count-M);
  printf("%lld\n", ans);
  */

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


Compiler output:
a.cc: In function 'int main()':
a.cc:104:18: error: 'ans_temp' was not declared in this scope
  104 |     std::cout << ans_temp-abs(M-get_possible_count(left))*left << std::endl;
      |                  ^~~~~~~~
a.cc:111:18: error: 'ans_temp' was not declared in this scope
  111 |     std::cout << ans_temp+abs(M-get_possible_count(right))*left << std::endl;
      |                  ^~~~~~~~