Problem:
Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n)cin >> a[i];
    vector<ll> cnt(1001);
    ll ans = 0;
    rep(i,n){
        cnt[a[i]]++;
    }
    for(ll x : cnt)if(x >= 3)ans+=x*(x-1)*(x-2)/6;
    for(int i = 1; i < cnt.size(); i++){
        if(cnt[i] >= 2){
            for(int j = 1; j < cnt.size(); j++){
                if(i == j)continue;
                if(cnt[j] >= 1){
                    if(j < i + i and i < j + i){
                        ans+=cnt[j]*(cnt[j]-1)/2;
                    }
                }
            }
        }
    }
    auto hoge = cnt;
    for(int i = 0; i < (int)cnt.size() - 1; i++){
        cnt[i+1] += cnt[i];
    }
    for(int a = 1; a < 1000; a++){
        if(hoge[a] == 0)continue;
        for(int b = a + 1; b < 1000; b++){
            //a<b
            if(hoge[b]==0)continue;
            // b < c < a + b
            show(a,b);
            if(cnt[min(a+b-1,1000)]-cnt[b]>=1)show(a, b, cnt[min(a+b-1,1000)] - cnt[b]);
            ans += cnt[min(a+b-1,1000)] - cnt[b];
        }
    }
    cout << ans << endl;
}