"use strict";
const Main = (input) =>{
   let xs = input.trim().split('\n');
   let nums = xs[1].split(' ').map(v=>parseInt(v,10));
   let ans = 0;
   for(let i = 0; i < xs[0]; i++){
   ans += nums[i];
   }
   let A = ans/2;
   let answer1 = 0;
   let answer2 = 0;
   for(let l = 0; l < xs[0]; l++){
     if(answer1 < A){
     answer1 = nums[l] + answer1;
     }else{
     answer2 = nums[l] + answer2;
     }
   }
   let X =(Math.abs(answer1 - answer2));
   
   let answer3 = 0;
   let answer4 = 0;
   for(let m = xs[0]-1; m >= 0; m--){
     if(answer3 < A){
     answer3 = nums[m] + answer3;
     }else{
     answer4 = nums[m] + answer4;
     }
   }  
   let Y =(Math.abs(answer4-answer3));
   
   if(X >= Y){
   console.log(Y);
   }else{
   console.log(X);
   }
}
Main(require('fs').readFileSync('/dev/stdin','utf8'));