stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#define N 16005

using namespace std;



int x, y, c;

bool d, t[2][N], v[2][N];

string s;

int main()

{

	int i, j;

	cin >> s >> x >> y;

	v[1][8000] = 1;

	for (i = 0; s[i] == 'F'; i++);

	v[0][8000 + i] = 1;

	for (; i <= s.size(); i++) {

		if (s[i] == 'F') c++;

		else {

			for (j = c; j < N - c; j++) {

				t[d][j - c] = max(t[d][j - c], v[d][j]);

				t[d][j + c] = max(t[d][j + c], v[d][j]);

			}

			for (j = 0; j < N; j++) v[d][j] = t[d][j], t[d][j] = 0;

			c = 0;

			d = !d;

		}

	}

    cout << (v[0][x + 8000] & v[1][y + 8000] ? "Yes" : "No") << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3108553199736113x):
#include <iostream>

#include <cstdio>

#include <bitset>

#define M 8005

using namespace std;



int x, y, z, c;

string s;

bitset<M * 2> bs[2];

int main() {

	int i;

	cin >> s >> x >> y;

	for (i = 0; s[i] == 'F'; i++);

	bs[0][i + M] = bs[1][M] = 1;

	for (; i <= s.size(); i++) {

		if (s[i] == 'F') c++;

		else bs[z] = (bs[z] << c) | (bs[z] >> c), z = !z, c = 0;

	}

	cout << (bs[0][x + M] & bs[1][y + M] ? "Yes" : "No");

	return 0;

}
// Slower C++ solution:
#include "bits/stdc++.h"

using namespace std;

typedef long long LL;

typedef pair<int, int> P;

typedef pair<int, P> E;

const LL MOD = 1000000007LL;

bool dpX[2][16001];

bool dpY[2][16001];

int main() {

	string s;

	cin >> s;

	int x, y;

	cin >> x >> y;

	int pos = 0;

	while (pos < s.size() && s[pos] == 'F') x--, pos++;

	vector<int> vx, vy;

	bool dir = 0;

	int d = 0;

	while (pos < s.size()) {

		if (s[pos] == 'T') {

			if (dir) vy.push_back(d);

			else vx.push_back(d);

			d = 0;

			dir ^= 1;

		}

		else {

			d++;

		}

		pos++;

	}

	if (dir) vy.push_back(d);

	else vx.push_back(d);

	dpX[0][8000] = 1;

	for (int i = 0; i < vx.size(); i++) {

		for (int j = 0; j <= 16000; j++) dpX[(i + 1) % 2][j] = 0;

		for (int j = 0; j <= 16000; j++) {

			if (j + vx[i] <= 16000) dpX[(i + 1) % 2][j + vx[i]] |= dpX[i % 2][j];

			if (j - vx[i] >= 0) dpX[(i + 1) % 2][j - vx[i]] |= dpX[i % 2][j];

		}

	}

	dpY[0][8000] = 1;

	for (int i = 0; i < vy.size(); i++) {

		for (int j = 0; j <= 16000; j++) dpY[(i + 1) % 2][j] = 0;

		for (int j = 0; j <= 16000; j++) {

			if (j + vy[i] <= 16000) dpY[(i + 1) % 2][j + vy[i]] |= dpY[i % 2][j];

			if (j - vy[i] >= 0) dpY[(i + 1) % 2][j - vy[i]] |= dpY[i % 2][j];

		}

	}

	if (dpX[vx.size() % 2][x + 8000] && dpY[vy.size() % 2][y + 8000]) {

		cout << "Yes" << endl;

	}

	else {

		cout << "No" << endl;

	}

}

// Faster C++ solution for the same problem (measured speedup: 1.1512579441380462x):
#include <bits/stdc++.h>



using namespace std;

typedef long long ll;

const ll INF = 1LL << 60;

const ll MOD = 1e9 + 7;

bool dp[2][8001];



int main() {

    string s;

    cin >> s;

    int x[2];

    cin >> x[0] >> x[1];

    int n = s.size();

    vector<int> v[2];

    bool dir = 0;

    int len = 0;

    for (int i = 0; i < n; i++) {

        if (s[i] == 'F') {

            len++;

        } else {

            v[dir].push_back(len);

            len = 0;

            dir ^= 1;

        }

    }

    v[dir].push_back(len);

    x[0] -= v[0][0];

    v[0].erase(v[0].begin());

    x[0] = abs(x[0]);

    x[1] = abs(x[1]);

    for (int k = 0; k < 2; k++) {

        int sum = 0;

        for (int i = 0; i < v[k].size(); i++) {

            sum += v[k][i];

        }

        if (sum < x[k] || (sum - x[k]) % 2) {

            cout << "No" << endl;

            return 0;

        }

        memset(dp, 0, sizeof(dp));

        dp[0][0] = 1;

        for (int i = 0; i < v[k].size(); i++) {

            for (int j = 0; j <= 8000; j++) {

                dp[(i + 1) & 1][j] |= dp[i & 1][j];

                if (j + v[k][i] <= 8000) dp[(i + 1) & 1][j + v[k][i]] |= dp[i & 1][j];

            }

            for (int j = 0; j <= 8000; j++) dp[i & 1][j] = 0;

        }

        if (!dp[v[k].size() & 1][(sum - x[k]) / 2]) {

            cout << "No" << endl;

            return 0;

        }

    }

    cout << "Yes" << endl;

}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <string.h>

#include <cmath>



using namespace std;

typedef long long i64;

typedef long double ld;

typedef pair<i64,i64> P;

#define rep(i,s,e) for(int (i) = (s);(i) <= (e);++(i))



string s;

int x;

int y;



bool f = true;



bool ok(vector<int>& d , int A)

{

	vector<bool> dp(20000,false);

	auto acc = [](int i){

		return i + 10000;

	};



	dp[acc(0)] = true;



	for(int i = 0;i < d.size();i++)

	{

		vector<bool> before = move(dp);

		dp.assign(20000,false);

		for(int j = -10000;j < 10000;j++)

		{

			int index = acc(j);

			if(index + d[i] < dp.size() && !f)

				dp[index] = dp[index] || before[index + d[i]];

			if(0 <= index - d[i])

				dp[index] = dp[index] || before[index - d[i]];

		}

      	f =  false;

	}





	return dp[acc(A)];

}

int main()

{

	cin >> s >> x >> y;



	vector<int> d[2];





	int now = 0;

	int cou = 0;



	for(char c : s)

	{

		if(c == 'T')

		{

			d[now].push_back(cou);

			now = 1 - now;

			cou = 0;

		}

		else

		{

			cou++;

		}

	}



	if(cou != 0)

	{

		d[now].push_back(cou);

	}



	if(ok(d[0],x) && ok(d[1] , y))

	{

		cout << "Yes" << endl;

	}

	else

	{

		cout << "No" << endl;

	}



}






// Faster C++ solution for the same problem (measured speedup: 1.2018340114012063x):
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <cmath>

#include <bitset>

using namespace std;

typedef long long i64;

typedef long double ld;

typedef pair<i64,i64> P;

#define rep(i,s,e) for(int i = (s);i <= (e);i++)



string s;

int x;

int y;



bitset<16010> bit[2];





int main(){

	cin >> s >> x >> y;

	x += 8000;

	y += 8000;

	vector<int> vec;

	vec.push_back(0);

	for(char c : s){

		if(c == 'T'){

			vec.push_back(0);

		}

		else{

			vec.back()++;

		}

	}

	bit[0][8000] = 1;

	bit[1][8000] = 1;

	for(int i = 0;i < vec.size();i++){

		if(i == 0){

			bit[i % 2] = (bit[i % 2] << vec[i]);

		}

		else{

			bit[i % 2] = (bit[i % 2] << vec[i]) | (bit[i % 2] >> vec[i]);

		}

	}



	if(bit[0][x] && bit[1][y]){

		cout << "Yes" << endl;

	}

	else{

		cout << "No" << endl;

	}

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main()

{

    int a,b,c,d;

    cin>>a>>b>>c>>d;

    if(a+b>c+d)cout<<"Left\n";

    else if(a+b<c+d)cout<<"Right\n";

    else cout<<"Balanced\n";

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.294681690881032x):
#include<stdio.h>

int main()

{

    int A,B,C,D;

    scanf("%d%d%d%d",&A,&B,&C,&D);

    if((A+B)>(C+D))printf("Left\n");

    else if((A+B)<(C+D))printf("Right\n");

    else

        printf("Balanced\n");





}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

void display()

{

    int a, b, c, d;

    cin>>a>>b>>c>>d;

    puts(a+b == c+d ? "Balanced" : a+b > c+d ? "Left" : "Right");

}

int main(void)

{

    display();

}


// Faster C++ solution for the same problem (measured speedup: 5.27817579287673x):
#include<stdio.h>

int main()

{

    int A,B,C,D;

    scanf("%d%d%d%d",&A,&B,&C,&D);

    if((A+B)>(C+D))printf("Left\n");

    else if((A+B)<(C+D))printf("Right\n");

    else

        printf("Balanced\n");





}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int a,b,c,d;

int main(){

cin>>a>>b>>c>>d;

if (a+b<c+d) puts("Right");

else if (a+b>c+d) puts("Left");

else puts("Balanced");

}

// Faster C++ solution for the same problem (measured speedup: 5.232776611650015x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int a,b,c,d;



int main()

{

    while (scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)

    {

        if (a+b==c+d) puts("Balanced");

        else if (a+b>c+d) puts("Left");

        else puts("Right");

    }

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define Tp template<typename Ty>

#define Ts template<typename Ty,typename... Ar>

#define Reg register

#define RI Reg int

#define Con const

#define CI Con int&

#define I inline

#define W while

#define N 400000

#define int LL

#define LL long long

using namespace std;

int n,b[N+5],S[N+5];LL a[N+5],c[N+5],tmp[N+5];

struct P

{

	LL x,y;I P(Con LL& a=0,Con LL& b=0):x(a),y(b){}

	I P operator - (Con P& o) Con {return P(x-o.x,y-o.y);}

	I LL operator ^ (Con P& o) Con {return x*o.y-y*o.x;}

}p[N+5];

main()

{

	RI i,Mx,t;scanf("%lld",&n);

	for(Mx=0,i=1;i<=n;++i) scanf("%lld",a+i),a[i]>=a[Mx]&&(Mx=i);

	for(t=0,i=Mx;i<=n;++i) tmp[++t]=a[i];for(i=1;i^Mx;++i) tmp[++t]=a[i];

	for(i=1;i<=n;++i) a[i]=tmp[i];

	for(i=1;i<=n;++i) scanf("%lld",b+i);

	for(t=0,i=Mx;i<=n;++i) tmp[++t]=b[i];for(i=1;i^Mx;++i) tmp[++t]=b[i];

	for(i=1;i<=n;++i) b[i]=tmp[i];

	for(a[++n]=a[1],i=2;i<=n;++i) c[i]=2*c[i-1]-c[i-2]+2*b[i-1],a[i]-=c[i];

	RI T=0;for(i=1;i<=n;++i)

	{

		p[i]=P(i,a[i]);W(T>1&&((p[S[T]]-p[S[T-1]])^(p[i]-p[S[T]]))>0) --T;S[++T]=i;

	}

	LL ans=a[S[1]]+a[S[T]];for(i=2;i<=T;++i) ans+=(a[S[i]]+a[S[i-1]])*(S[i]-S[i-1]);

	for(i=1;i<=n;++i) ans+=2*c[i];return printf("%.12lf",0.5*(ans-2*a[1])/(n-1)),0;

}

// Faster C++ solution for the same problem (measured speedup: 1.816316261225832x):
#include<bits/stdc++.h>

#define Tp template<typename Ty>

#define Ts template<typename Ty,typename... Ar>

#define Reg register

#define RI Reg int

#define Con const

#define CI Con int&

#define I inline

#define W while

#define N 200000

#define LL long long

using namespace std;

int n,b[N+5],S[N+5];LL a[N+5],c[N+5],tmp[N+5];

struct P

{

	LL x,y;I P(Con LL& a=0,Con LL& b=0):x(a),y(b){}

	I P operator - (Con P& o) Con {return P(x-o.x,y-o.y);}

	I LL operator ^ (Con P& o) Con {return x*o.y-y*o.x;}

}p[N+5];

int main()

{

	RI i,Mx;scanf("%d",&n);

	for(Mx=0,i=1;i<=n;++i) scanf("%lld",a+i),tmp[i]=a[i],a[i]>=a[Mx]&&(Mx=i);

	for(i=Mx;i<=n;++i) a[i-Mx+1]=tmp[i];for(i=1;i^Mx;++i) a[i+(n-Mx+1)]=tmp[i];

	for(i=1;i<=n;++i) scanf("%d",b+i),tmp[i]=b[i];

	for(i=Mx;i<=n;++i) b[i-Mx+1]=tmp[i];for(i=1;i^Mx;++i) b[i+(n-Mx+1)]=tmp[i];

	for(a[++n]=a[1],i=2;i<=n;++i) c[i]=2*c[i-1]-c[i-2]+2*b[i-1],a[i]-=c[i];

	RI T=0;for(i=1;i<=n;++i)

	{

		p[i]=P(i,a[i]);W(T>1&&((p[S[T]]-p[S[T-1]])^(p[i]-p[S[T]]))>0) --T;S[++T]=i;

	}

	LL ans=a[S[1]]+a[S[T]];for(i=2;i<=T;++i) ans+=(a[S[i]]+a[S[i-1]])*(S[i]-S[i-1]);

	for(i=1;i<=n;++i) ans+=2*c[i];return printf("%.15lf",(ans-2*a[1])/2.0/(n-1)),0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<iostream>

#include<queue>

#include<vector>

#include <bitset>

using namespace std;

#define INF 1000000000000

#define MAX 100001

typedef long long ll;

typedef pair<ll,ll> P;

typedef pair< pair<int,int>, int> p; 

#define bit(n,k) ((n>>k)&1) /*nのk bit目*/





int main(){

    int N;

    ll A[310000],pre[310000],post[310000];

    cin>>N;

    priority_queue<ll> que,que2;

    for(int i=1;i<=3*N;i++) cin>>A[i];

    ll sum=0,sum2=0;

    for(int i=1;i<=N;i++){

        sum+=A[i];

        sum2+=A[3*N+1-i];

        que.push(-A[i]);

        que2.push(A[3*N+1-i]);

    }

    pre[N]=sum; post[2*N+1]=sum2;

    for(int i=N+1;i<=2*N;i++){

        sum+=A[i];

        sum2+=A[3*N+1-i];

        que.push(-A[i]);

        que2.push(A[3*N+1-i]);

        ll x=que.top(),y=que2.top();

        que.pop(); que2.pop();

        sum+=x; sum2-=y;

        pre[i]=sum; post[3*N+1-i]=sum2;

    }

    ll ans=-10000000000000000;

    for(int i=N;i<=2*N;i++){

        ll temp=pre[i]-post[i+1];

        ans=max(ans,temp);

    }

    cout<<ans<<endl;





}

// Faster C++ solution for the same problem (measured speedup: 1.1726092534578865x):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;



#define INF 1LL << 55

#define MAX (1LL << 31) -1

#define MOD 1000000007

typedef long long ll;

//typedef pair<int,int> P;

//typedef pair<pair<int,int>,ll> p;

#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/

#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

struct edge{ll to,cost,val;};

template<class T,class U>bool chmin(T&a,const U&b){if(a<=b)return false;a=b;return true;}

template<class T,class U>bool chmax(T&a,const U&b){if(a>=b)return false;a=b;return true;}

//__builtin_popcount(S);

//C.erase(unique(C.begin(),C.end()),C.end());

//#define int ll

int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

//#define F function<T(T,T)>





signed main(){

    int N;

    cin>>N;

    vector<ll> a(1+N*3);

    vector<ll> pre(1+N*3),post(1+N*3);

    for(int i=1;i<=3*N;i++){

        cin>>a[i];

    }

    priority_queue<ll> que;

    ll sum=0;

    for(int i=1;i<=2*N;i++){

        que.push(-a[i]);

        sum+=a[i];

        if(N<i) sum+=que.top(),que.pop();

        pre[i]=sum;

    }

    priority_queue<ll> que2;

    sum=0;

    for(int i=3*N;i>=N+1;i--){

        que2.push(a[i]);

        sum+=a[i];

        if(2*N>=i) sum-=que2.top(),que2.pop();

        post[i]=sum;

    }

    ll ans=-INF;

    for(int i=N;i<=2*N;i++){

        chmax(ans,pre[i]-post[i+1]);

    }

    cout<<ans<<endl;





}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define int long long

int a[3*114514];

int b[3*114514];

int c[3*114514];

signed main(){

  int n;

  cin>>n;

  for(int i=0;i<n*3;i++) cin>>a[i];

  priority_queue<int,vector<int>,greater<int> > q1;

  priority_queue<int> q2;

  memset(b,0,sizeof(b));

  memset(c,0,sizeof(c));

  int tmp1=0,tmp2=0;

  for(int i=0;i<n;i++){

    q1.push(a[i]);

    tmp1+=a[i];

    b[i]=tmp1;

    q2.push(a[3*n-1-i]);

    tmp2+=a[3*n-1-i];

    c[3*n-1-i]=tmp2;

  }

  for(int i=n;i<n*3;i++){

    q1.push(a[i]);

    tmp1+=a[i];

    int k=q1.top();q1.pop();

    tmp1-=k;

    b[i]=tmp1;

    q2.push(a[3*n-1-i]);

    tmp2+=a[3*n-1-i];

    k=q2.top();q2.pop();

    tmp2-=k;

    c[3*n-1-i]=tmp2;

  }

  int ans=-(1LL<<55LL);

  for(int i=n-1;i<2*n;i++){

    ans=max(ans,b[i]-c[i+1]);

  }

  cout<<ans<<endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 6.523022359575731x):
#include<bits/stdc++.h>

using namespace std;

using Int = long long;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}

template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//BEGIN CUT HERE

// return sum of top K element (default: maximum)

template<typename T, T identity, typename V=vector<T>,

         typename C1=less<T>, typename C2=greater<T> >

struct PrioritySum{

  size_t num;

  T sum;  

  priority_queue<T, V, C1> pq1;

  priority_queue<T, V, C2> pq2;

  PrioritySum():num(0),sum(identity){}

  PrioritySum(size_t num):num(num),sum(identity){}



  void resolve(){

    assert(pq1.size()+pq2.size()>=num);

    while(pq2.size()<num){

      sum+=pq1.top();

      pq2.emplace(pq1.top());

      pq1.pop();

    }

    while(pq2.size()>num){

      sum-=pq2.top();

      pq1.emplace(pq2.top());

      pq2.pop();

    }

    if(pq1.empty()||pq2.empty()) return;

    while(C2()(pq1.top(),pq2.top())){

      T t1=pq1.top();pq1.pop();

      T t2=pq2.top();pq2.pop();

      sum+=t1;

      sum-=t2;

      pq1.emplace(t2);

      pq2.emplace(t1);

    }

  }



  T query(){resolve();return sum;}

  

  void push(const T &x){pq1.emplace(x);}

  void expand(){num++;}

  void shrink(){assert(num);num--;}

};

//END CUT HERE



struct FastIO{

  FastIO(){

    cin.tie(0);

    ios::sync_with_stdio(0);

  }

}fastio_beet;



//INSERT ABOVE HERE

signed ARC074_D(){

  using ll = long long;

  using PS1 = PrioritySum<ll, ll(0), vector<ll>, less<ll>, greater<ll> >;

  using PS2 = PrioritySum<ll, ll(0), vector<ll>, greater<ll>, less<ll> >;

  

  int n;

  cin>>n;

  vector<ll> a(3*n);

  for(int i=0;i<3*n;i++) cin>>a[i];



  PS1 ps1(n);

  vector<ll> dp1(3*n);

  for(int i=0;i<3*n;i++){

    if(i>=n) dp1[i]=ps1.query();

    ps1.push(a[i]);

  }

  

  PS2 ps2(n);

  vector<ll> dp2(3*n);

  for(int i=3*n-1;i>=0;i--){

    ps2.push(a[i]);

    if(i<=2*n) dp2[i]=ps2.query();

  }



  ll ans=dp1[n]-dp2[n];  

  for(int i=n;i<=2*n;i++) chmax(ans,dp1[i]-dp2[i]);

  

  cout<<ans<<endl;

  return 0;

}

/*

  verified on 2019/04/09

  https://atcoder.jp/contests/arc074/tasks/arc074_b 

*/



signed main(){

  ARC074_D(); 

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N = 300003;

const long long infll = 1000000000000000000;

int arr[N];

long long lef[N], righ[N];

int main()

{

    int n;

    scanf("%d",&n);

    for(int i = 0;i<3*n;i++)

        scanf("%d",&arr[i]);

    for(int i = 0;i<N;i++)

        lef[i] = -infll, righ[i] = infll;

    long long sum = 0;

    multiset<int> s;

    for(int i = 0;i<3*n;i++) {

        s.insert(arr[i]);

        sum += arr[i];

        if(s.size() == n) {

            lef[i] = sum;

        } else if(s.size() > n) {

            sum -= *s.begin();

            s.erase(s.begin());

            lef[i] = sum;

        }

    }

    s.clear();

    sum = 0;

    for(int i = 3*n-1;i>=0;i--) {

        sum += arr[i];

        s.insert(arr[i]*-1);

        if(s.size() == n) {

            righ[i] = sum;

        } else if (s.size() > n) {

            sum -= *(s.begin()) * -1;

            s.erase(s.begin());

            righ[i] = sum;

        }

    }

    for(int i = 1;i<N;i++)

        lef[i] = max(lef[i],lef[i-1]);

    for(int i = N-2;i>=0;i--)

        righ[i] = min(righ[i], righ[i+1]);

    long long ans = -infll;

    for(int i = 0;i<3*n;i++)

        ans = max(ans,lef[i] - righ[i+1]);

    printf("%lld",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 10.97815703369947x):
#include <iostream>

#include <bits/stdc++.h>



#define si(x) scanf("%d",&x)



using namespace std;



int main()

{

    int n;

    si(n);

    vector<int> arr(3*n);

    vector<long long> largest(n+1,0) , smallest(n+1,0);

    for(int i=0;i<3*n;i++){

        si(arr[i]);

    }

    priority_queue<int> pq1,pq2;

    for(int i=0;i<n;i++){

        pq1.push(arr[i]*-1);

        largest[0] += arr[i];

    }

    for(int i=n;i<2*n;i++){

        pq1.push(arr[i]*-1);

        largest[i-n+1] = largest[i-n] + pq1.top() + arr[i];

        pq1.pop();

    }



    for(int i=3*n-1;i>=2*n;i--){

        pq2.push(arr[i]);

        smallest[0] += arr[i];

    }

    for(int i=2*n - 1;i>=n;i--){

        pq2.push(arr[i]);

        smallest[3*n -i - n] = smallest[3*n -i - n - 1] - pq2.top() + arr[i];;

        pq2.pop();

    }

    long long maxx = -1e14;

    for(int i=0;i<=n;i++){

        maxx = max(maxx , largest[i] - smallest[n-i]);

    }

    cout << maxx;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N = 300003;

const long long infll = 1000000000000000000;

int arr[N];

long long lef[N], righ[N];

int main()

{

    int n;

    scanf("%d",&n);

    for(int i = 0;i<3*n;i++)

        scanf("%d",&arr[i]);

    for(int i = 0;i<N;i++)

        lef[i] = -infll, righ[i] = infll;

    long long sum = 0;

    multiset<int> s;

    for(int i = 0;i<3*n;i++) {

        s.insert(arr[i]);

        sum += arr[i];

        if(s.size() == n) {

            lef[i] = sum;

        } else if(s.size() > n) {

            sum -= *s.begin();

            s.erase(s.begin());

            lef[i] = sum;

        }

    }

    s.clear();

    sum = 0;

    for(int i = 3*n-1;i>=0;i--) {

        sum += arr[i];

        s.insert(arr[i]*-1);

        if(s.size() == n) {

            righ[i] = sum;

        } else if (s.size() > n) {

            sum -= *(s.begin()) * -1;

            s.erase(s.begin());

            righ[i] = sum;

        }

    }

    for(int i = 1;i<N;i++)

        lef[i] = max(lef[i],lef[i-1]);

    for(int i = N-2;i>=0;i--)

        righ[i] = min(righ[i], righ[i+1]);

    long long ans = -infll;

    for(int i = 0;i<3*n;i++)

        ans = max(ans,lef[i] - righ[i+1]);

    printf("%lld",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 10.963829226768901x):
#include <iostream>

#include <set>

using namespace std;

const long long MAXN = 5e5+5;

multiset<long long> s1;

long long arr[MAXN];

long long dp[MAXN];

long long ans = -1e17;

long long dp2[MAXN];

int main() {

    long long n;

    cin>>n;

    for(long long i=1;i<=3*n;i++){

        cin>>arr[i];

    }

    for(long long i=1;i<=n;i++){

        s1.insert(arr[i]);

        dp[n+1] +=arr[i];

    }

    for(long long i=n+2;i<=3*n+1;i++){

        dp[i] = dp[i-1];

        if(arr[i-1]>*s1.begin()){

            long long hold = *s1.begin();

            s1.erase(s1.begin());

            

            s1.insert(arr[i-1]);

            dp[i]-=hold;

            dp[i]+=arr[i-1];

            

        }

        //cout<<i<<" "<<dp[i]<<endl;

    }

    s1.clear();

    for(long long i=3*n;i>=2*n+1;i--){

        s1.insert(-arr[i]);

        dp2[2*n]+=arr[i];

    }

    for(long long i=2*n-1;i>=0;i--){

        dp2[i] = dp2[i+1];

        if((-1*arr[i+1])>*s1.begin()){

            long long hold = *s1.begin();

            s1.erase(s1.begin());

            

            s1.insert(-1*arr[i+1]);

            dp2[i]+=hold;

            dp2[i]+=arr[i+1];

        }

        //cout<<i<<" "<<dp2[i]<<endl;

    }

    for(long long i=n;i<=2*n;i++){

        ans = max(ans,dp[i+1]-dp2[i]);

    }

    cout<<ans<<endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N = 300003;

const long long infll = 1000000000000000000;

int arr[N];

long long lef[N], righ[N];

int main()

{

    int n;

    scanf("%d",&n);

    for(int i = 0;i<3*n;i++)

        scanf("%d",&arr[i]);

    for(int i = 0;i<N;i++)

        lef[i] = -infll, righ[i] = infll;

    long long sum = 0;

    multiset<int> s;

    for(int i = 0;i<3*n;i++) {

        s.insert(arr[i]);

        sum += arr[i];

        if(s.size() == n) {

            lef[i] = sum;

        } else if(s.size() > n) {

            sum -= *s.begin();

            s.erase(s.begin());

            lef[i] = sum;

        }

    }

    s.clear();

    sum = 0;

    for(int i = 3*n-1;i>=0;i--) {

        sum += arr[i];

        s.insert(arr[i]*-1);

        if(s.size() == n) {

            righ[i] = sum;

        } else if (s.size() > n) {

            sum -= *(s.begin()) * -1;

            s.erase(s.begin());

            righ[i] = sum;

        }

    }

    for(int i = 1;i<N;i++)

        lef[i] = max(lef[i],lef[i-1]);

    for(int i = N-2;i>=0;i--)

        righ[i] = min(righ[i], righ[i+1]);

    long long ans = -infll;

    for(int i = 0;i<3*n;i++)

        ans = max(ans,lef[i] - righ[i+1]);

    printf("%lld",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 10.99718295697348x):
#include <bits/stdc++.h>

using namespace std;



#define MAX_N 300005

#define INF 0x3f3f3f3f3f3f3f3f



int n;

int a[MAX_N];



int main()

{

    scanf("%d",&n);

    for(int i=0; i<3*n; i++)

        scanf("%d",&a[i]);



    vector <long long> dp1(3*n ,-INF) ,dp2(3*n ,INF);

    multiset <int> ms;

    long long sum = 0LL;

    for(int i=0; i<3*n; i++){

        ms.insert(a[i]) ,sum += a[i];

        if(ms.size() > n){

            sum -= *ms.begin();

            ms.erase(ms.begin());

        }

        if(ms.size() == n)

            dp1[i] = sum;

    }

    sum = 0 ,ms.clear();

    for(int i=3*n-1; ~i; i--){

        ms.insert(a[i]) ,sum += a[i];

        if(ms.size() > n){

            sum -= *prev(ms.end());

            ms.erase(prev(ms.end()));

        }

        if(ms.size() == n)

            dp2[i] = sum;

    }



    long long ans = -INF;

    for(int i=0; i<3*n-1; i++)

        ans = max(ans ,dp1[i]-dp2[i+1]);

    printf("%lld\n",ans);

}