Scanf.scanf "%d" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let total = Array.fold_left (+) 0 a in
    let rec loop i acc best =
        if i = n - 1 then best else
            let acc = acc + a.(i) in
            loop (i + 1) acc (min best (abs (total - acc * 2)))
    in
    loop 0 0 max_int |> Printf.printf "%d\n"
)