n = gets.to_i
a = gets.split.map(&:to_i)
l = 0
r = n - 1
diff = a[l] - a[r]
while l + 1 < r do
  if diff < 0
    l += 1
    diff += a[l]
  else
    r -= 1
    diff -= a[r]
  end
end
if diff < 0
  diff2 = diff + a[l + 1] * 2
elsif diff > 0
  diff2 = diff - a[l] * 2
else
  diff2 = 0
end
puts [diff.abs, diff2.abs].min
