Problem p02821 (C++)

Buggy submission (status: Runtime Error):

// Problem : E - Handshake
// Contest : AtCoder Beginner Contest 149
// URL : https://atcoder.jp/contests/abc149/tasks/abc149_e
// Memory Limit : 1024.000000 MB
// Time Limit : 2000.000000 milisec
// Powered by CP Editor (https://github.com/coder3101/cp-editor)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
#define all(x) (x).begin(), (x).end()
#define io()                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
vector<int> a;
int n;
ll m;
bool check(int mid) {
  int j = n;
  ll ret = 0;
  for (int i = 1; i <= n; i++) {
    while (j >= 1 && a[i] + a[j] >= mid)
      j--;
    ret += n - j;
  }
  return ret >= m;
}
int main() {
  scanf("%d %lld", &n, &m);
  a.resize(n + 1);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a.begin() + 1, a.end());
  int low = a[0] << 1, high = a[n] << 1 | 1, mid, ans = -1;
  while (low <= high) {
    mid = (low + high) >> 1;
    if (check(mid))
      low = mid + 1, ans = mid;
    else
      high = mid - 1;
  }
  assert(ans != -1);
  vector<ll> psa(n + 1);
  for (int i = 1; i <= n; i++)
    psa[i] = psa[i - 1] + a[i];
  ll ret = 0;
  int j = n;
  ll cnt = 0;
  int worst = INT_MAX;
  for (int i = 1; i <= n; i++) {
    while (j >= 1 && a[i] + a[j] >= ans)
      j--;
    if (j < n)
      worst = min(worst, a[i] + a[j + 1]);
    ret += psa[n] - psa[j] + a[i] * ll(n - j);
    cnt += n - j;
  }
  assert(cnt - m <= 1);
  printf("%lld\n", cnt > m ? ret - worst : ret);
  return 0;
}

Fixed submission:

// Problem : E - Handshake
// Contest : AtCoder Beginner Contest 149
// URL : https://atcoder.jp/contests/abc149/tasks/abc149_e
// Memory Limit : 1024.000000 MB
// Time Limit : 2000.000000 milisec
// Powered by CP Editor (https://github.com/coder3101/cp-editor)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
#define all(x) (x).begin(), (x).end()
#define io()                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
vector<int> a;
int n;
ll m;
bool check(int mid) {
  int j = n;
  ll ret = 0;
  for (int i = 1; i <= n; i++) {
    while (j >= 1 && a[i] + a[j] >= mid)
      j--;
    ret += n - j;
  }
  return ret >= m;
}
int main() {
  scanf("%d %lld", &n, &m);
  a.resize(n + 1);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a.begin() + 1, a.end());
  int low = a[0] << 1, high = a[n] << 1 | 1, mid, ans = -1;
  while (low <= high) {
    mid = (low + high) >> 1;
    if (check(mid))
      low = mid + 1, ans = mid;
    else
      high = mid - 1;
  }
  assert(ans != -1);
  vector<ll> psa(n + 1);
  for (int i = 1; i <= n; i++)
    psa[i] = psa[i - 1] + a[i];
  ll ret = 0;
  int j = n;
  ll cnt = 0;
  int worst = INT_MAX;
  for (int i = 1; i <= n; i++) {
    while (j >= 1 && a[i] + a[j] >= ans)
      j--;
    if (j < n)
      worst = min(worst, a[i] + a[j + 1]);
    ret += psa[n] - psa[j] + a[i] * ll(n - j);
    cnt += n - j;
  }
  assert(worst == ans);
  printf("%lld\n", ret - ans * (cnt - m));
  return 0;
}