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# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;


class Qsort
{
    public void Sort(int[] data, int start, int end)
    {
        if (end - start <= 1)  //要素数が1個以下。end=data.Lengthよりdata[end-1]が最後の要素であることに注意する。
        {
            return;
        }

        int p = data[start];                   //データの1番目をピボットする
        int i = start - 1;
        int j = end;

        while (true)
        {
            //データのstart(1番目、左端)から一つずつ調べて、p以上ならbreakしてiの値を保存。次に調べるのはi+1番目から
            for (i = i + 1; i < data.Length; i++)
            {
                if (p <= data[i])
                    break;
            }
            //データのend-1(最後、右端)から一つずつ調べて、p以下ならbreakしてjの値を保存。次に調べるのはj-1番目から
            for (j = j - 1; j > 0; j--)
            {
                if (p >= data[j])
                    break;
            }

            if (j < i)　　　　　//iとjの値が逆転したなら、whileから抜け出す。
                break;
            else        //そうでないなら、i番目とj番目を交代
            {
                int tmp = data[i];
                data[i] = data[j];
                data[j] = tmp;
            }
        }
        //i-1番目とi番目の間を境界にして分けて再帰。data[end-1]がデータの最後であるので前半(start～i-1)、後半(i～end-1)
        Sort(data, start, i);
        Sort(data, i, end);
    }
}

class Program
{
    public static long Read()
    {
        return long.Parse(Console.ReadLine());
    }

    public static int[] Reads()
    {
        return Console.ReadLine().Split().Select(int.Parse).ToArray();
    }


    public static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);

        var n =(int) Read();
        var input = Reads();
        var l = new int[n];
        for(int i = 0; i < n; i++)
        {
            l[i] = input[i];
        }
        long count = 0;

        Array.Sort(l);
        for (int i = 0; i < n - 2; i++)
        {
            for (int j = i + 1; j < n - 1; j++)
            {
                int start = j;
                int end = n - 1;
                while (start <= end)
                {
                    int mid= start+(start - end) / 2;
                    if (l[mid] >= l[i] + l[j])
                        end = mid - 1;
                    else if (l[mid] < l[i] + l[j])
                        start = mid+1;
                }
                count += start - (j + 1);
            }
        }
        Console.WriteLine(count);


        Console.Out.Flush();
    }
}