program bugged
implicit none
integer :: N
integer, allocatable, dimension(:, :) :: score
integer :: i, k
integer :: max_score



read *, N

allocate(score(N, 2))
do i = 1, N
    
    score(i, 1) = i
    read *, score(i, 2)

end do



call heap_sort(score, N)

do i = 1, N

    max_score = score(i, 2)

end do


k = 1
do while (mod(max_score, 10) == 0 .and. max_score > 0)

    max_score = max_score - score(k, 2)
    k = k + 1  


end do


print '(I0)', max_score



end program bugged

! heap sort
subroutine heap_sort( list, n )
implicit none

integer :: n  
integer, dimension( n, 2 ) :: list


integer :: i, k
integer, dimension( 2 ) :: temporary


do k = n/2, 1, -1

    temporary( 1 ) = list( k, 1 )
    temporary( 2 ) = list( k, 2 )

    call make_heap( n, list, k, n, temporary )



end do


do k = n, 2, -1

    
    temporary( 1 ) = list( k, 1 )
    temporary( 2 ) = list( k, 2 )
    
    list( k, 1 ) = list( 1, 1 )
    list( k, 2 ) = list( 1, 2 )  


    call make_heap( n, list, 1, k-1, temporary )



end do



!-----------------------------------------------------------------------------------
contains
    subroutine make_heap( n, list, root, leaf, var )
    implicit none
    
    integer :: n, root, leaf
    integer, dimension( n, 2 ) :: list
    integer, dimension( 2 ) :: var    
    
    
    ! local
    integer :: i, j

    i = root
    j = i * 2
    
    
    ! 条件を満たすまで無限ループ
    do
        if ( j > leaf ) exit
      
        if ( j < leaf ) then

            if ( list( j, 2 ) < list( j + 1, 2 ) ) j = j + 1
       
           
        end if


        if ( list( j, 2 )  >  var( 2 ) ) then

            list( i, 1 ) = list( j, 1 )
            list( i, 2 ) = list( j, 2 )
            
            i = j
            j = i * 2
        
        
        else
        
            j = leaf + 1
        
        
        endif
    
    
    end do
    
    
    list( i, 1 ) = var( 1 )
    list( i, 2 ) = var( 2 )
        
    end subroutine make_heap



!----------------------------------------------------------------------------------

end subroutine heap_sort