public void Solve() { Scan(); PriortyQueue <int> pq = new PriortyQueue <int>((a, b) => - a.CompareTo(b)); long[] memoR = new long[N + 1]; long sum = 0; for (int i = 0; i < N; i++) { pq.Add(A[i]); sum += A[i]; } memoR[0] = sum; for (int i = 0; i < N; i++) { sum += A[N + i]; pq.Add(A[N + i]); sum -= pq.Poll(); memoR[i + 1] = sum; } sum = 0; pq = new PriortyQueue <int>((a, b) => a.CompareTo(b)); long[] memoL = new long[N + 1]; for (int i = 0; i < N; i++) { pq.Add(A[3 * N - 1 - i]); sum += A[3 * N - 1 - i]; } memoL[N] = sum; for (int i = 0; i < N; i++) { sum += A[2 * N - 1 - i]; pq.Add(A[2 * N - 1 - i]); sum -= pq.Poll(); memoL[N - 1 - i] = sum; } long ans = long.MinValue; for (int i = 0; i <= N; i++) { ans = Math.Max(ans, memoR[i] - memoL[i]); } Console.WriteLine(ans); }