program sample
	implicit none
    integer(8) ::i,n,count,sum1,count2
    integer(8),allocatable :: a(:)
    
    read(*,*) n
    
    allocate(a(n))
	
    read(*,*) a
    
    count=0
    sum1=sum(a)
    i=1
    !write(*,*) a
    !write(*,*) sum(a)
    
    do while(.true.)
    	!write(*,*) sum1,count,i
        if (count>=(sum1+1)/2) then
        	exit
        end if
        count =count+a(i)
        i=i+1
    end do
    
    !write(*,*) sum1,count,i
    
    count2=sum1-count
    if (count2==0) then
    	count2 = count2+a(i-1)
        count=count-a(i-1)
    	write(*,*) abs(count2-count)
    else
    	write(*,*) abs(count2-count)
    end if	
    stop
end program sample