Scanf.scanf "%d" (fun n ->
    let module S = Set.Make (struct type t = int * int let compare = compare end) in
    let a = Array.init (n * 3) (fun _ -> Scanf.scanf " %d" (fun a -> a)) in

    let karr = Array.make (3 * n) 0 in

    let rec init i sum set lim =
        if i = lim then sum, set else
            init (i + 1) (sum + a.(i)) (S.add (a.(i), i) set) lim
    in
    let sum, set = init 0 0 S.empty n in
    let rec loop k sum set =
        if k <= 2 * n then (
            karr.(k) <- sum;
            let set = S.add (a.(k), k) set in
            let sum = sum + a.(k) in
            let (mk, _) as mi = S.min_elt set in
            loop (k + 1) (sum - mk) (S.remove mi set)
        )
    in
    loop n sum set;

    let sum, set = init (2 * n) 0 S.empty (3 * n) in

    let rec loop k sum set =
        if k >= n then (
            karr.(k) <- karr.(k) - sum;
            let k = k - 1 in
            let set = S.add (a.(k), k) set in
            let sum = sum + a.(k) in
            let (mk, _) as mx = S.max_elt set in
            loop k (sum - mk) (S.remove mx set)
        )
    in
    loop (2 * n) sum set;

    let rec loop i acc =
        if i > 2 * n then acc else
            loop (i + 1) (max acc karr.(i))
    in
    loop n min_int |> Printf.printf "%d\n"
)