Problem p02821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18)
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P std::pair<int, int>
#define PiP std::pair<int, std::pair<int, int>>
#define all(v) v.begin(), v.end()
#define mkp std::make_pair
#define prique(T) std::priority_queue<T, vector<T>, greater<T>>
using namespace std;
template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return x != 1;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m = mod) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
  std::random_device rnd;
  std::mt19937 mt32(rnd());
  std::uniform_int_distribution<int> engine(MIN, MAX);
  return engine(mt32);
}
/*--------Library Zone!--------*/

int n, m, a[100005];
int sum[100005];
signed main() {
  cin >> n >> m;
  REP(i, n + 1) { cin >> a[i]; }
  sort(a + 1, a + n + 1);
  REP(i, n + 1) {
    sum[i] += a[i];
    sum[i + 1] += sum[i];
  }
  int ok = inf, ng = 0;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    int sum = 0;
    REP(i, n + 1) {
      int ko = lower_bound(a + 1, a + n + 1, mid - a[i]) - a;
      sum += n - ko + 1;
    }
    if (sum <= m)
      ok = mid;
    else
      ng = mid;
  }
  vector<int> v;
  int all = 0;
  int ans = 0;
  REP(i, n + 1) {
    int ko = lower_bound(a + 1, a + n + 1, ok - a[i]) - a;
    all += n - ko + 1;
    ans += sum[n] - sum[ko - 1];
    ans += a[i] * (n - ko + 1);
    if (ko != 1) {
      v.push_back(a[i] + a[ko - 1]);
    }
  }
  sort(all(v), greater<>());
  rep(i, m - all) ans += v[i];
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18)
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P std::pair<int, int>
#define PiP std::pair<int, std::pair<int, int>>
#define all(v) v.begin(), v.end()
#define mkp std::make_pair
#define prique(T) std::priority_queue<T, vector<T>, greater<T>>
using namespace std;
template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return x != 1;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m = mod) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
  std::random_device rnd;
  std::mt19937 mt32(rnd());
  std::uniform_int_distribution<int> engine(MIN, MAX);
  return engine(mt32);
}
/*--------Library Zone!--------*/

int n, m, a[100005];
int sum[100005];
signed main() {
  cin >> n >> m;
  REP(i, n + 1) { cin >> a[i]; }
  sort(a + 1, a + n + 1);
  REP(i, n + 1) {
    sum[i] += a[i];
    sum[i + 1] += sum[i];
  }
  int ok = inf, ng = 0;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    int sum = 0;
    REP(i, n + 1) {
      int ko = lower_bound(a + 1, a + n + 1, mid - a[i]) - a;
      sum += n - ko + 1;
    }
    if (sum <= m)
      ok = mid;
    else
      ng = mid;
  }
  vector<int> v;
  int all = 0;
  int ans = 0;
  REP(i, n + 1) {
    int ko = lower_bound(a + 1, a + n + 1, ok - a[i]) - a;
    all += n - ko + 1;
    ans += sum[n] - sum[ko - 1];
    ans += a[i] * (n - ko + 1);
    if (ko != 1) {
      v.push_back(a[i] + a[ko - 1]);
    }
  }
  sort(all(v), greater<>());
  ans += (m - all) * (ok - 1);
  cout << ans << endl;
}