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: Wrong Answer)

#include<stdio.h>
#include<stdlib.h>

void swap (int *x, int *y) {
  int temp; 
  temp = *x;
  *x = *y;
  *y = temp;
}

int partition (int array[], int left, int right) {
  int i, j, pivot;
  i = left;
  j = right + 1;
  pivot = left;

  do {
    do { i++; } while (array[i] < array[pivot]);
    do { j--; } while (array[pivot] < array[j]);
    if (i < j) { swap(&array[i], &array[j]); }
  } while (i < j);

  swap(&array[pivot], &array[j]);

  return j;
}

void quick_sort (int array[], int left, int right) {
  int pivot;

  if (left < right) {
    pivot = partition(array, left, right);
    quick_sort(array, left, pivot-1); 
    quick_sort(array, pivot+1, right);
  }
}

int main(void){
    int N,i,j,j2,k,l,a,b,cou1,cou2,cou3,S=0,s,t,r,u;
    int *L;
    scanf("%d",&N);
    L = malloc(sizeof(int)*N);
    for(i=0;i<N;i++) scanf("%d",&L[i]);
    quick_sort(L,0,N-1);
    i=0;
    while(1){
        a = L[i];
        b = a;
        cou1 = 1;
        j = i;
        j++;
        if(j < N) while(a == L[j]) j++,cou1++;
        s=0,t=0,r=0,u=0;
            while(s == 0 && t == 0 && r == 0){
                cou2 = 1,cou3 = 0;
                if(j >= N) s = 1;
                b = L[j];
                k = j+1;
                if(s == 1 || k >= N) t = 1;
                else while(k < N && b == L[k]) k++,cou2++;
                l = k;
                if(l == N) r = 1;
                else while(l < N && a+b > L[l]) l++,cou3++;
                if(s == 0 && t == 0 && r == 0){
                    S += cou1*cou2*cou3;
                    S += cou1*cou2*(cou2-1);
                    if(a*2 > b) S += cou1*(cou1-1)*cou2;
                }
                if(u == 0) j2 = j,u++;
                j = k;
            }
        i = j2;
        if(i == N) break;
    }
    printf("%d",S);
    return 0;
}