program DDCC2020B
    implicit none
    integer(8)::N,i,diff
    integer(8)::sum_half=0
    integer(8),allocatable,dimension(:)::A

    read(5,*)N
    allocate(A(N))
    read(5,*)(A(i),i=1,N)

    diff=sum(A)

    do i=1,N
        if(2*sum_half-sum(A)>=0)then
            diff=min(2*sum_half-sum(A),sum(A)-2*(sum_half-A(i-1)))
        else
            sum_half=sum_half+A(i)
        end if
    end do
    
    print'(i0)',diff

end program DDCC2020B