let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map int_of_string (List.rev (loop 0 "" false []))
    in
    let rec proc left right acc = function
        | [_] -> acc
        | [] -> acc
        | hd :: tl ->  
                let left = left + hd in
                let right = right - hd in
                let acc = min acc (max left right - min left right) in
                proc left right acc tl
    in

    let main () =
        let _ = int_of_string (read_line ()) in
        let a = split (read_line ()) in
        let s = List.fold_left (+) 0 a in
        let r = match a with
        | hd :: tl -> proc hd (s - hd) (max hd (s - hd) - min hd (s - hd)) tl
        | _ -> 0
        in
        Printf.printf "%d\n" r
    in
    main ()