program name
    implicit none
    integer(4):: n
    integer(4):: i,j
    integer(4):: ai, aj, fst, lst, mdl
    integer(8):: cnt
    integer(4),allocatable:: l(:)

    read*, n
    allocate(l(n))
    read*, l(:)

    call merge_sort(l,1,n)
    cnt = 0
    do i=1,n-2
        ai = l(i)
        do j=i+1,n-1
            aj = l(j)
            if (l(j+1) >= ai+aj) cycle
            if (l(n) < ai+aj) then
                cnt=cnt+(n-j)
                cycle
            end if

            fst = j+1
            lst = n
            do while(lst-fst > 1)
                mdl = (fst+lst)/2
                if (l(mdl) < ai+aj) then
                    fst = mdl
                else
                    lst = mdl
                end if
            end do
            cnt=cnt+fst-j
        end do
    end do
    print*, cnt
    
contains
subroutine swap(x,y)
    integer(4),intent(inout):: x,y
    integer(4):: tmp
    tmp=x
    x=y
    y=tmp
end subroutine


recursive subroutine merge_sort(ar, fst, lst)
    integer(4),intent(inout):: ar(:)
    integer(4), intent(in):: fst,lst
    integer(4):: mdl

    if (lst - fst <= 1) then
        if (ar(fst) > ar(lst)) call swap(ar(fst), ar(lst))
        return
    end if

    mdl = (fst+lst)/2
    call merge_sort(ar,fst,mdl)
    call merge_sort(ar,mdl+1,lst)
    call merge_(ar,fst,mdl,lst)
end subroutine


subroutine merge_(ar,fst,mdl,lst)
    integer(4),intent(inout):: ar(:)
    integer(4),intent(in):: fst, mdl, lst
    integer(4),allocatable:: tmp(:)
    integer(4):: li, ri, ti

    allocate(tmp(lst-fst+1))
    li=fst
    ri=mdl+1
    ti=1

    do while(li <= mdl .and. ri <= lst)
        if (ar(li) <= ar(ri)) then
            tmp(ti) = ar(li)
            li=li+1
        else
            tmp(ti) = ar(ri)
            ri=ri+1
        end if
        ti=ti+1
    end do
    
    if (li <= mdl) then
        tmp(ti:) = ar(li:mdl)
    else
        tmp(ti:) = ar(ri:lst)
    end if

    ar(fst:lst) = tmp(:)
    deallocate(tmp)
end subroutine
end program name