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 Python (3.4.3) (judge verdict: Wrong Answer)

import itertools

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

import time
start = time.time()

def binary_search_nearest(list, item):#もっとも近い値のindexをbsで探して返す関数
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return low-1

def iftriangle(first,second,third):
    if third < len(Lmat):
        if Lmat[first]>=Lmat[second]+Lmat[third]:
            return 0
        elif Lmat[second]>=Lmat[first]+Lmat[third]:
            return 0
        elif Lmat[third]>=Lmat[first]+Lmat[second]:
            return 0
        else:
            #print([first,second,third])
            return 1
    else:
        return 0

N2patern = list(itertools.combinations(range(N), 2)) #大小関係はこの時点で決まってしまう(小、大)のペアが出てくる

sum = 0
for patern in N2patern:
    sum+=binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]])-patern[1]
    """
    #print("hoge")
    min_index = patern[1]
    max_index = binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]])#これだとjustの値しか取得できない=>bs自体を実装する必要がある
    #print(binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]]))#これだとjustの値しか取得できない=>bs自体を実装する必要がある)
    for Lk_index in range(min_index+1,max_index+1):
        sum += iftriangle(patern[0],patern[1],Lk_index)#ここは1に置き換え不可。計算が必要
    """

print(sum)

elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")