n = gets.to_i
a = gets.split.map(&:to_i)
sum = 0;
(n).times do |i|
    sum += a[i]
end
half = 0
rec = 0
(n).times do |i|
    half += a[i]
    rec = i
    break if half >= sum/2
end
if half >= sum - half
    ans = [half-(sum-half), (sum-half)+a[rec]-(half-a[rec])].min
else
    ans = [(sum-half)-half, half+a[rec+1]-(sum-half-a[rec+1])].min
end
puts ans