Problem p02821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

int main() {
  long long N;
  long long M;
  scanf("%lld", &N);
  vector<long long> A(N - 1 + 1);
  scanf("%lld", &M);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &A[i]);
  }

  SORT(A);
  vector<ll> sum(N);
  repn(i, N) { sum[i] = sum[i - 1] + A[i - 1]; }
  auto calc = [&](ll x) {
    ll res = 0, total = 0;
    rep(i, N) {
      res += N - POSL(A, x - A[i]);
      // if (x == 67)
      //     cout << i << "; " << POSL(A, x - A[i]) << res << " "
      //          << (sum[N] - sum[POSL(A, x - A[i])]) << " " << A[i] *
      //          res
      //          << endl;
      total +=
          (sum[N] - sum[POSL(A, x - A[i])]) + A[i] * (N - POSL(A, x - A[i]));
    }
    return P(res, total);
  };

  long long lb = -1, ub = 10000000000001;
  while (ub - lb > 1) {
    long long mid = (lb + ub) / 2;
    if (calc(mid).first >= M)
      lb = mid;
    else
      ub = mid;
  }
  ll c1 = calc(lb).first, c2 = calc(lb).second;
  // cout << c1 << " " << c2 << " " << lb << endl;
  cout << c2 - (c1 - M) * lb << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

int main() {
  long long N;
  long long M;
  scanf("%lld", &N);
  vector<long long> A(N - 1 + 1);
  scanf("%lld", &M);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &A[i]);
  }

  SORT(A);
  vector<ll> sum(N + 1);
  repn(i, N) { sum[i] = sum[i - 1] + A[i - 1]; }
  auto calc = [&](ll x) {
    ll res = 0, total = 0;
    rep(i, N) {
      res += N - POSL(A, x - A[i]);
      // if (x == 67)
      //     cout << i << "; " << POSL(A, x - A[i]) << res << " "
      //          << (sum[N] - sum[POSL(A, x - A[i])]) << " " << A[i] *
      //          res
      //          << endl;
      total +=
          (sum[N] - sum[POSL(A, x - A[i])]) + A[i] * (N - POSL(A, x - A[i]));
    }
    return P(res, total);
  };

  long long lb = -1, ub = 10000000000001;
  while (ub - lb > 1) {
    long long mid = (lb + ub) / 2;
    if (calc(mid).first >= M)
      lb = mid;
    else
      ub = mid;
  }
  ll c1 = calc(lb).first, c2 = calc(lb).second;
  // cout << c1 << " " << c2 << " " << lb << endl;
  cout << c2 - (c1 - M) * lb << endl;

  return 0;
}