Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
#define maxn 1010
using namespace std;
using namespace __gnu_pbds;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int l[maxn];
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  int n;
  while (cin >> n) {
    memset(l, 0, sizeof(l));
    for (int i = 0; i < n; i++)
      scanf("%d", &l[i]);
    sort(l, l + n);
    // for(int i=0;i<n;i++) printf("%d ",l[i]);
    // cout<<endl;
    int ans = 0;
    for (int i = 0; i <= n - 3; i++) {
      for (int j = i + 1; j <= n - 2; j++) {
        int sum = l[i] + l[j];
        int pos = lower_bound(l + j + 1, l + n, sum) - l;
        // printf("sum=%d,pos=%d\n",sum,pos);
        if (pos == 0)
          ;
        else
          ans += (pos - j - 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
#define maxn 3010
using namespace std;
using namespace __gnu_pbds;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int l[maxn];
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  int n;
  while (cin >> n) {
    memset(l, 0, sizeof(l));
    for (int i = 0; i < n; i++)
      scanf("%d", &l[i]);
    sort(l, l + n);
    // for(int i=0;i<n;i++) printf("%d ",l[i]);
    // cout<<endl;
    int ans = 0;
    for (int i = 0; i <= n - 3; i++) {
      for (int j = i + 1; j <= n - 2; j++) {
        int sum = l[i] + l[j];
        int pos = lower_bound(l + j + 1, l + n, sum) - l;
        // printf("sum=%d,pos=%d\n",sum,pos);
        if (pos == 0)
          ;
        else
          ans += (pos - j - 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}