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: Accepted)

#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define lli long long int
#define F first
#define S second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define MAX1 998244353
#define MAX2 1000000007
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#define input(data,n,command) for(lli i=0;i<n;i++){lli tmp;cin>>tmp;data.pb(tmp);command}
#define sort(a) sort(a.begin(),a.end());
#define display(a) for(lli i = 0 ;i<a.size();i++){cout<<a[i]<<" ";} cout<<endl;
#define rep(i,n,p) for(lli i = 0;i<n;i= i +p)
#define endl "\n"

//Written By GHATAK...
/*--------------------------------------------------------------------*/
//vector<pair<lli,lli>>adj[300010];

std::vector<lli> v;

int main()
{
    ios
    lli T=1;
    //cin>>T;
    while(T--)
    {
        //start from here
        lli n;
        cin>>n;
        map<lli,lli>ar;
        // lli ar[10010] = {0};
        rep(i,n,1)
        {
            lli a;
            cin>>a;
            v.pb(a);
            ar[a]++;
        }
        map<lli,lli>dp;
        // lli dp[1000010] = {0};
        dp[0] = 0;
        for(lli i = 1;i<20010;i++)
        {
            dp[i] = dp[i-1] + ar[i-1];
        }
        sort(v);
        lli ans = 0;
        for(lli i = 0;i<n-1;i++)
        {
            for(lli j = i+1;j<n;j++)
            {
                lli sum = v[i] + v[j];
                ans = ans + (dp[sum] - j-1);
                // trace(sum,v[i],v[j],dp[sum]-j-1);
                // if(sum<=1000)
                // {
                    
                // }
                // lli ind = binary(sum,j+1,n);
            }
        }
        cout<<ans;
    }
    return 0;
}
/*-----------------------------------------------------------------------*/
/*
--->  reverse(vec.begin(),vec().end());
*/