N = gets.chomp.to_i
A = gets.chomp.split.map(&:to_i)
# S = gets.chomp.chars

sum = A.inject(&:+)

left_len = 0
i = 0
while (left_len * 2 < sum) 
  left_len += A[i]
  i += 1
end

right_len = sum - left_len
# Now, left_len > right_len. the diff is ans

can1 = left_len - right_len
can2 = (right_len + A[i-1]) - (left_len - A[i-1])
# p "can1: #{can1}"
# p "can2: #{can2}"
ans = [can1, can2].min

p ans

  