module subprogs
  implicit none
  contains
  subroutine bubble_sort(n,L)
    integer,intent(in) :: n
    integer,intent(inout) :: L(n)
    integer :: i,j 
    integer :: t
    do i = 1,n-1
       do j = i + 1,n
          if (L(i) .gt.L(j)) then
             t = L(i)
             L(i) = L(j)
             L(j) = t
!             print*,L
          end if
       end do
     end do
  end subroutine bubble_sort
end module subprogs




program Triangles
  use subprogs
  implicit none 
  integer :: n
  integer,allocatable :: L(:)
  integer :: res
  integer ::i,j,k

  read(*,*)n
  allocate(L(n))
  read(*,*) L
  call bubble_sort(n,L)
  res = 0
  do i = 1,n-2
     do j = i+1,n-1
        do k = j+1 ,n
           if (L(k) > L(i)+L(j))then
              exit
           end if
        end do 
        print*,i,j,k
        res = res + k -1  - j
        print*,res
     end do
  end do
           
  write(*,*) res


end program Triangles
