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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>

void merge(int *p, int n)
{
    int *left = NULL;
    int *right = NULL;
    int i = 0;
    if (n == 1)
    {
        return;
    }
    else
    {
        i = n / 2;
        left = (int *)calloc(i, sizeof(int));
        right = (int *)calloc(n-i, sizeof(int));
        int k1 = 0, k2 = 0;
        for (k1 = 0; k1 < i; k1++)
        {
            left[k1] = p[k1];
        }
        for (k2 = i; k2 < n; k2++)
        {
            right[k2-i] = p[k2];
        }
        merge(left, i);
        merge(right, n-i);
        k1 = 0;
        k2 = 0;
        int count = 0;
        while (k1 < i && k2 < n-i)
        {
            if (left[k1] > right[k2])
            {
                p[count] = left[k1];
                k1++;
            }
            else
            {
                p[count] = right[k2];
                k2++;
            }
            count++;
        }
        if (k1 == i)
        {
            while (k2 < n-i)
            {
                p[count] = right[k2];
                count++;
                k2++;
            }
        }
        if (k2 == n-i)
        {
            while (k1 < i)
            {
                p[count] = left[k1];
                count++;
                k1++;
            }
        }
        free(left);
        free(right);
    }
    return;
}
int search(int *p, int n)
{
    int i = 0, j = 0, k = 0, count = 0;
    if (n < 3) return 0;
    if (p[0] > p[1] + p[2]) return search(p+1, n-1);
    if (p[n-1] + p[n-2] < p[n-3]) return search(p, n-1);
    for (i = 0; i < n-2; i++)
    {
        for (j = i+1; j < n-1; j++)
        {
            for (k = n-1; k > j; k--)
            {
                if (p[j] + p[k] > p[i])
                {
                    count += k-j;
                    break;
                }
            }
        }
    }
    return count;
}


int main()
{
    int n = 0, sum = 0, i = 0;
    int *p = NULL;
    scanf("%d", &n);
    p = (int *)calloc(n, sizeof(int));
    for(i = 0; i < n; i++)
    {
        scanf("%d", &p[i]);
    }
    merge(p, n);
    sum = search(p, n);
    free(p);
    printf("%d", sum);
    return 0;
}