Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(3 * N);
  ll i, j, k;
  rep(i, 3 * N) { cin >> a[i]; }
  /*
  //O(n^2)の解法
  if(N>1000) return -1;
  ans = -(ll)INF*INF;
  vector<ll> pre,aft;
  for(i=N;i<=2*N;i++){
    pre.clear();aft.clear();
    ll tmp = 0;
    rep(j,i){
      pre.pb(a[j]);
    }
    for(j=i;j<3*N;j++){
      aft.pb(a[j]);
    }
    SORT(pre);
    SORT(aft);
    ll x = pre.size();
    ll y = aft.size();
    for(j=x-1;j>=x-N;j--){
      tmp += pre[j];
    }
    rep(j,N){
      tmp -= aft[j];
    }
    // cout<<tmp<<endl;
    ans = max(ans,tmp);
  }
  cout<<ans<<endl;
  */
  // O(nlogn)の解法
  ll ans = -(ll)INF * (ll)INF;
  priority_queue<ll, vector<ll>, greater<ll>> que1;
  priority_queue<ll> que2;
  vector<ll> b(N), c(N);
  rep(i, N) {
    b[0] += a[i];
    que1.pu(a[i]);
  }
  for (i = 2 * N; i < 3 * N; i++) {
    c[0] += a[i];
    que2.pu(a[i]);
  }
  // priority_queueを利用してb,cを更新していく
  for (i = N; i < 2 * N; i++) {
    que1.pu(a[i]);
    ll tmp1 = que1.top();
    que1.pop();
    b[i - N + 1] = max((ll)0, a[i] - tmp1) + b[i - N];
  }
  for (i = 2 * N - 1; i >= N; i--) {
    que2.pu(a[i]);
    ll tmp2 = que2.top();
    que2.pop();
    c[2 * N - i] = min((ll)INF, a[i] - tmp2) + c[2 * N - i - 1];
  }
  rep(i, N + 1) {
    // cout<<i<<" "<<b[i]<<" "<<c[N-i]<<endl;
    // cout<<ans<<endl;
    ans = max(ans, b[i] - c[N - i]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(3 * N);
  ll i, j, k;
  rep(i, 3 * N) { cin >> a[i]; }
  /*
  //O(n^2)の解法
  if(N>1000) return -1;
  ans = -(ll)INF*INF;
  vector<ll> pre,aft;
  for(i=N;i<=2*N;i++){
    pre.clear();aft.clear();
    ll tmp = 0;
    rep(j,i){
      pre.pb(a[j]);
    }
    for(j=i;j<3*N;j++){
      aft.pb(a[j]);
    }
    SORT(pre);
    SORT(aft);
    ll x = pre.size();
    ll y = aft.size();
    for(j=x-1;j>=x-N;j--){
      tmp += pre[j];
    }
    rep(j,N){
      tmp -= aft[j];
    }
    // cout<<tmp<<endl;
    ans = max(ans,tmp);
  }
  cout<<ans<<endl;
  */
  // O(nlogn)の解法
  ll ans = -(ll)INF * (ll)INF;
  priority_queue<ll, vector<ll>, greater<ll>> que1;
  priority_queue<ll> que2;
  vector<ll> b(N + 1), c(N + 1);
  rep(i, N) {
    b[0] += a[i];
    que1.pu(a[i]);
  }
  for (i = 2 * N; i < 3 * N; i++) {
    c[0] += a[i];
    que2.pu(a[i]);
  }
  // priority_queueを利用してb,cを更新していく
  for (i = N; i < 2 * N; i++) {
    que1.pu(a[i]);
    ll tmp1 = que1.top();
    que1.pop();
    b[i - N + 1] = max((ll)0, a[i] - tmp1) + b[i - N];
  }
  for (i = 2 * N - 1; i >= N; i--) {
    que2.pu(a[i]);
    ll tmp2 = que2.top();
    que2.pop();
    c[2 * N - i] = min((ll)INF, a[i] - tmp2) + c[2 * N - i - 1];
  }
  rep(i, N + 1) {
    // cout<<i<<" "<<b[i]<<" "<<c[N-i]<<endl;
    // cout<<ans<<endl;
    ans = max(ans, b[i] - c[N - i]);
  }
  cout << ans << endl;
}