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 Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

program main
  implicit none
  integer :: n, i, j, res
  integer, allocatable :: l(:)
  read (*, *) n
  allocate (l(n))
  read (*, *) l(:)
  res = 0
  call quicksort(l)
  do i = 1, n - 2
    do j = i + 2, n
      res = res + count_larger(l(i + 1:j - 1), l(j) - l(i))
    end do
  end do
  write (*, "(i0)") res
  contains

  integer function count_larger(ary, thres)
    integer, intent(in) :: ary(:), thres
    integer :: n, ma, mi, md, i
    n = size(ary)
    ma = n + 1  ! always larger than thres
    mi = 0  ! no more than thres
    do i = 1, 100
      md = mi + (ma - mi) / 2
      if (ary(md) > thres) then
        mi = md
      else
        ma = md
      end if
      if (ma - mi <= 1) exit
    end do
    count_larger = mi
  end function count_larger

  recursive subroutine quicksort(a)
    integer, intent(inout) :: a(:)
    integer :: pivot, i, j, k, tmp, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main