program prob4
    implicit none
    integer(8) :: N
    integer(8), allocatable :: A(:)
    integer(8) :: L
    integer(8) :: i, index, S, ans

    read(*,*) N
    allocate(A(N))
    read(*,*) A

    L = sum(A)

    S = 0
    do i = 1, N
        S = S + A(i)
        if(S > L/2) then
            index = i
            exit
        end if
    end do

    ans = min(2*S - L, L-2*S+2*A(index))
    write(*,*) ans
    deallocate(A)
    stop
contains
end program prob4