import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

// sum[p] = 0..p の中から n 個の要素を選び、最大の和を格納
long[] calcLeft(int[] xs) {
    long[] sum = new long[xs.length];
    long s = 0;
    int d = cast(int)xs.length / 3;
    auto q = new BinaryHeap!(Array!long, "a > b"); // 小さい順から取り出せる
    for (int i = 0; i < d * 2; i++) {
        q.insert(xs[i]);
        s += xs[i];
        if (q.length > d) {
            s -= q.front;
            q.popFront;
        }
        sum[i] = s;
    }
    return sum;
}

// sum[p] = p..$ の中から n 個の要素を選び、最小の和を格納
long[] calcRight(int[] xs) {
    long[] sum = new long[xs.length];
    long s = 0;
    int d = cast(int)xs.length / 3;
    auto q = new BinaryHeap!(Array!long, "a < b"); // 大きい順から取り出す
    for (int i = cast(int)xs.length-1; i >= d; i--) {
        q.insert(xs[i]);
        s += xs[i];
        if (q.length > d) {
            s -= q.front;
            q.popFront;
        }
        sum[i] = s;
    }
    return sum;
}

long calc(int[] xs) {
    auto lefts  = calcLeft(xs);
    auto rights = calcRight(xs);

    auto n = xs.length;
    auto d = cast(int)xs.length / 3;
    long ans = long.min;
    for (int i = d - 1; i < 2 * d; i++) {
        long diff = lefts[i] - rights[i + 1];
        ans = max(ans, diff);
    }
    return ans;
}

void main() {
    readint;
    auto xs = readints;
    auto ans = calc(xs);
    writeln(ans);
}