Problem p02821 (C++)

Buggy submission (status: Runtime Error):
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Handshake
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_A = 100000;

/* typedef */

typedef long long ll;
typedef pair<ll, ll> pll;

/* global variables */

int as[MAX_N];
ll ass[MAX_N + 1];

/* subroutines */

ll check(int n, int b) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    int k = lower_bound(as, as + n, b - as[i]) - as;
    cnt += (n - k);
  }
  return cnt;
}

pll cntsum(int n, int b) {
  ll cnt = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    int k = lower_bound(as, as + n, b - as[i]) - as;
    cnt += n - k;
    sum += ass[n] - ass[k] + (ll)(n - k) * as[i];
  }
  return pll(cnt, sum);
}

/* main */

int main() {
  int n;
  ll m;
  scanf("%d%lld", &n, &m);

  for (int i = 0; i < n; i++)
    scanf("%d", as + i);
  sort(as, as + n);
  // for (int i = 0; i < n; i++) printf("%d ", as[i]); putchar('\n');

  for (int i = 0; i < n; i++)
    ass[i + 1] = ass[i] + as[i];

  int b0 = 0, b1 = MAX_A * 2;
  while (b0 + 1 < b1) {
    int b = (b0 + b1) / 2;
    if (check(n, b) >= m)
      b0 = b;
    else
      b1 = b;
  }

  pll p0 = cntsum(n, b0);
  pll p1 = cntsum(n, b1);
  ll c0 = p0.first, s0 = p0.second;
  ll c1 = p1.first, s1 = p1.second;
  // printf("b0=%d, cnt=%lld, sum=%lld\n", b0, c0, s0);
  // printf("b1=%d, cnt=%lld, sum=%lld\n", b1, c1, s1);

  ll s = (s0 - s1) / (c0 - c1) * (m - c1) + s1;
  printf("%lld\n", s);
  return 0;
}


Fixed submission:
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Handshake
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_A = 100000;

/* typedef */

typedef long long ll;
typedef pair<ll, ll> pll;

/* global variables */

int as[MAX_N];
ll ass[MAX_N + 1];

/* subroutines */

ll check(int n, int b) {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    int k = lower_bound(as, as + n, b - as[i]) - as;
    cnt += (n - k);
  }
  return cnt;
}

pll cntsum(int n, int b) {
  ll cnt = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    int k = lower_bound(as, as + n, b - as[i]) - as;
    cnt += n - k;
    sum += ass[n] - ass[k] + (ll)(n - k) * as[i];
  }
  return pll(cnt, sum);
}

/* main */

int main() {
  int n;
  ll m;
  scanf("%d%lld", &n, &m);

  for (int i = 0; i < n; i++)
    scanf("%d", as + i);
  sort(as, as + n);
  // for (int i = 0; i < n; i++) printf("%d ", as[i]); putchar('\n');

  for (int i = 0; i < n; i++)
    ass[i + 1] = ass[i] + as[i];

  int b0 = 0, b1 = MAX_A * 2 + 1;
  while (b0 + 1 < b1) {
    int b = (b0 + b1) / 2;
    if (check(n, b) >= m)
      b0 = b;
    else
      b1 = b;
  }

  pll p0 = cntsum(n, b0);
  pll p1 = cntsum(n, b1);
  ll c0 = p0.first, s0 = p0.second;
  ll c1 = p1.first, s1 = p1.second;
  // printf("b0=%d, cnt=%lld, sum=%lld\n", b0, c0, s0);
  // printf("b1=%d, cnt=%lld, sum=%lld\n", b1, c1, s1);

  ll s = (s0 - s1) / (c0 - c1) * (m - c1) + s1;
  printf("%lld\n", s);
  return 0;
}