Language: C++

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


int N;
long long A[310000];

typedef long long VAL;
const VAL INF = 1LL<<60;
const int MAX_R = 310000;

int SIZE_R;
struct RMQ {
    pair<VAL,int> dat[4*MAX_R];
  int AN;
    
    void init(int n = MAX_R, VAL v = INF) {
	  AN = n;
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        for (int i = 0; i < 2*MAX_R-1; ++i) dat[i] = make_pair(v, -1);
    }
    
    inline void init_set(int a, VAL x) {
        int k = a + SIZE_R-1;
        dat[k] = make_pair(x, a);
    }
    
    inline void init_tree(int k = 0, int l = 0, int r = SIZE_R) {
        if (r - l == 1) {}                  // leaf
        else {
            init_tree(k*2+1, l, (l+r)/2);
            init_tree(k*2+2, (l+r)/2, r);
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    
    inline void set(int a, VAL x) {
        int k = a + SIZE_R-1;
        dat[k] = make_pair(x, a);
        while (k > 0) {
            k = (k-1)/2;
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    
    inline pair<VAL,int> get(int a, int b, int k = 0, int l = 0, int r = SIZE_R) {
        if (r <= a || b <= l) return make_pair(INF, -1);
        if (a <= l && r <= b) return dat[k];
        else {
            pair<VAL,int> vl = get(a, b, k*2+1, l, (l+r)/2);
            pair<VAL,int> vr = get(a, b, k*2+2, (l+r)/2, r);
            return min(vl, vr);
        }
    }
    
    inline VAL operator [] (int a) {
	  return get(a, a+1).first;
    }
    
    void print() {
	  for (int i = 0; i < r; ++i) {
		long long val = (*this)[i];
		if (val < INF) cout << val;
		else cout << "INF";
		if (i != r-1) cout << ",";
	  }
	  cout << endl;
    }
};

RMQ rmq;



int main() {
  while (cin >> N) {
	rmq.init(N*3);
	for (int i = 0; i < N*3; ++i) cin >> A[i], rmq.init_set(i, A[i]);
	rmq.init_tree();
	vector<pair<long long,int> > vec;
	for (int i = 0; i < N*2; ++i) vec.push_back(make_pair(A[i+N], i+N));
	sort(vec.begin(), vec.end());

	long long zen = 0, kou = 0;
	for (int i = 0; i < N; ++i) {
	  zen += A[i];
	  kou += vec[i].first;
	  rmq.set(vec[i].second, INF);
	}

	long long res = zen - kou;

	/*
	COUT("first: ");
    rmq.print(N*3);
	COUT(zen); COUT(kou); COUT(res);
	*/
	
	for (int i = N; i < N*2; ++i) {
	  pair<long long,int> Min = rmq.get(0, i + 1);
	  if (Min.first != A[i]) {
		zen += A[i] - Min.first;
		rmq.set(Min.second, INF);
	  }
	  else {
		rmq.set(i, INF);
	  }

	  if (rmq[i] == INF) {
		Min = rmq.get(i, N*3);
		kou += Min.first - A[i];
		rmq.set(Min.second, INF);
	  }

	  /*
	  cout << "-----------------" << endl;
	  COUT(i);
	  rmq.print(N*3);
	  COUT(zen); COUT(kou); COUT(res);
	  */
	  
	  chmax(res, zen - kou);
	}

	cout << res << endl;
  }
}









Compiler output:
a.cc: In member function 'void RMQ::print()':
a.cc:95:31: error: 'r' was not declared in this scope
   95 |           for (int i = 0; i < r; ++i) {
      |                               ^