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
    dif = half - (sum - half)
    (rec).downto(0) do |i|
        if a[i] < dif
            dif -= a[i]
        else
            break
       end
    end
else
    dif = sum-half-half
    (rec+1).upto(n-1) do |i|
        if a[i] < dif
            dif -= a[i]
        else
            break
       end
    end
end
puts dif