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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
*/
import java.util.*;
import java.io.*;

   public class Main
   {
      public static void main (String[] args) throws Exception
        { 
            BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
             StringTokenizer st = new StringTokenizer(infile.readLine());
             int N = Integer.parseInt(st.nextToken());
            int arr[] = new int[N];
            st = new StringTokenizer(infile.readLine());
             for(int i=0; i < N; i++)
                arr[i] = Integer.parseInt(st.nextToken());
            System.out.println(findNumberOfTriangles(arr)); 
        } 
       public static int findNumberOfTriangles(int arr[]) 
        { 
            int n = arr.length; 
            // Sort the array elements in non-decreasing order 
            Arrays.sort(arr); 
      
            // Initialize count of triangles 
            int count = 0; 
      
            // Fix the first element. We need to run till n-3 as 
            // the other two elements are selected from arr[i+1...n-1] 
            for (int i = 0; i < n-2; ++i) 
            { 
                // Initialize index of the rightmost third element 
                int k = i + 2; 
      
                // Fix the second element 
                for (int j = i+1; j < n; ++j) 
                { 
                    /* Find the rightmost element which is smaller 
                    than the sum of two fixed elements 
                    The important thing to note here is, we use 
                    the previous value of k. If value of arr[i] + 
                    arr[j-1] was greater than arr[k], then arr[i] + 
                    arr[j] must be greater than k, because the 
                    array is sorted. */
                    while (k < n && arr[i] + arr[j] > arr[k]) 
                        ++k; 
      
                /* Total number of possible triangles that can be 
                    formed with the two fixed elements is k - j - 1. 
                    The two fixed elements are arr[i] and arr[j]. All 
                    elements between arr[j+1] to arr[k-1] can form a 
                    triangle with arr[i] and arr[j]. One is subtracted 
                    from k because k is incremented one extra in above 
                    while loop. k will always be greater than j. If j 
                    becomes equal to k, then above loop will increment 
                    k, because arr[k] + arr[i] is always/ greater than 
                    arr[k] */
                    if(k>j) 
                    count += k - j - 1; 
                } 
            } 
            return count; 
        }
   }