Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (ll i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define tii tuple<int, int, int>
#define pq priority_queue<ll>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define edge(v, a, b)                                                          \
  v[a].pb(b);                                                                  \
  v[b].pb(a);
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecbl vector<bool>
#define endl "\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}

int main() {
  int n = in();
  vec l(n);
  rep(i, n) l[i] = in();
  ll ans = 0;
  sort(ALL(l));
  vec cnt(2010);
  cnt[0] = 0;
  int tmp = 0;
  for (int i = 1; i <= 2010; i++) {
    cnt[i] = cnt[i - 1];
    while (l[tmp] <= i) {
      if (tmp < n - 1) {
        tmp++;
        cnt[i]++;
      } else
        break;
    }
    if (i >= l[n - 1])
      cnt[i] = n;
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += cnt[l[i] + l[j] - 1] - j - 1;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (ll i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define tii tuple<int, int, int>
#define pq priority_queue<ll>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define edge(v, a, b)                                                          \
  v[a].pb(b);                                                                  \
  v[b].pb(a);
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecbl vector<bool>
#define endl "\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}

int main() {
  int n = in();
  vec l(n);
  rep(i, n) l[i] = in();
  ll ans = 0;
  sort(ALL(l));
  vec cnt(2010);
  cnt[0] = 0;
  int tmp = 0;
  for (int i = 1; i < 2010; i++) {
    cnt[i] = cnt[i - 1];
    while (l[tmp] <= i) {
      if (tmp < n - 1) {
        tmp++;
        cnt[i]++;
      } else
        break;
    }
    if (i >= l[n - 1])
      cnt[i] = n;
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += cnt[l[i] + l[j] - 1] - j - 1;
    }
  }
  cout << ans << endl;
}