Problem p02821 (C++)

Buggy submission (status: Runtime Error):
// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (size_t i = 0; i < size_t(n); i++)
#define forn1(i,n) for (size_t i = 1; i <= size_t(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

const int MAX_A = 100000;
const int MAX_N = 100000;
ll n, m;
int a[MAX_N];
ll cnt[MAX_A + 1];
ll sum[MAX_A + 1];

bool check(int x) {
  ll c = 0;
  forn(i, n) { c += cnt[max(0, x - a[i])]; }
  return c >= m;
}

int main() {
  fastio();

  cin >> n >> m;
  forn(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  sum[MAX_A] = cnt[MAX_A] * MAX_A;
  for (int i = MAX_A - 1; i >= 0; i--) {
    sum[i] += sum[i + 1] + cnt[i] * i;
    cnt[i] += cnt[i + 1];
  }

  int l = 1, r = 200001;
  while (r - l > 1) {
    int x = (l + r) / 2;
    if (check(x))
      l = x;
    else
      r = x;
  }

  // cout << l << endl;

  ll c = 0;
  ll ans = 0;
  forn(i, n) {
    int j = max(0, l + 1 - a[i]);
    if (j > MAX_A)
      continue;
    // cout << j << " " << cnt[j] << " " << sum[j] << endl;
    c += cnt[j];
    ans += sum[j] + 1LL * cnt[j] * a[i];
  }

  ans += (m - c) * l;

  cout << ans << endl;

  return 0;
}

Fixed submission:
// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (size_t i = 0; i < size_t(n); i++)
#define forn1(i,n) for (size_t i = 1; i <= size_t(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

const int MAX_A = 100000;
const int MAX_N = 100000;
ll n, m;
int a[MAX_N];
ll cnt[MAX_A + 1];
ll sum[MAX_A + 1];

bool check(int x) {
  ll c = 0;
  forn(i, n) {
    int j = max(0, x - a[i]);
    if (j > MAX_A)
      continue;
    c += cnt[j];
  }
  return c >= m;
}

int main() {
  fastio();

  cin >> n >> m;
  forn(i, n) {
    cin >> a[i];
    cnt[a[i]]++;
  }

  sum[MAX_A] = cnt[MAX_A] * MAX_A;
  for (int i = MAX_A - 1; i >= 0; i--) {
    sum[i] += sum[i + 1] + cnt[i] * i;
    cnt[i] += cnt[i + 1];
  }

  int l = 1, r = 200001;
  while (r - l > 1) {
    int x = (l + r) / 2;
    if (check(x))
      l = x;
    else
      r = x;
  }

  // cout << l << endl;

  ll c = 0;
  ll ans = 0;
  forn(i, n) {
    int j = max(0, l + 1 - a[i]);
    if (j > MAX_A)
      continue;
    // cout << j << " " << cnt[j] << " " << sum[j] << endl;
    c += cnt[j];
    ans += sum[j] + 1LL * cnt[j] * a[i];
  }

  ans += (m - c) * l;

  cout << ans << endl;

  return 0;
}