Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#define MYDEBUG
#include <bits/stdc++.h>
using namespace std;

#ifdef MYDEBUG
#define dbp(x) cout << #x << ": " << x << endl
#define dbp2(x, y) cout << #x << "," << #y << ": " << x << "," << y << endl
#define dbp3(x, y, z)                                                          \
  cout << #x << "," << #y << "," << #z << ": " << x << "," << y << "," << z    \
       << endl
#else
#define dbp(x)
#define dbp2(x, y)
#define dbp3(x, y, z)
#endif
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for (int i = from; i < to; ++i)

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last)
      out << ",";
  }
  out << "]";
  return out;
}

ll max3(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); }

const int maxn = 1e5 + 10;
const ll INF = 1e16 * -1;
int n;
ll sum[maxn], rig[maxn];
ll a[maxn];
ll dp[maxn];
ll dp2[maxn];

void solve() {
  cin >> n;
  a[0] = sum[0] = 0;
  for (int i = 1; i <= 3 * n; ++i) {
    cin >> a[i];
    sum[i] = a[i] + sum[i - 1];
  }
  rig[3 * n + 1] = 0;
  for (int i = 3 * n; i >= 1; --i) {
    rig[i] = a[i] + rig[i + 1];
  }

  //  for (int i = 1; i <= 3 * n; ++i) {
  //    printf("%d %d %d\n", a[i], sum[i], rig[i]);
  //  }

  priority_queue<ll, vector<ll>, greater<ll>> q;
  dp[0] = INF;
  for (int i = 1; i <= n; ++i) {
    dp[i] = INF;
    q.push(a[i]);
  }

  dp[n] = sum[n];
  for (int i = n + 1; i <= 2 * n; ++i) {
    int mini = q.top();
    if (mini < a[i]) {
      dp[i] = dp[i - 1] - mini + a[i];
      if (!q.empty()) {
        q.pop();
      }
      q.push(a[i]);
    } else {
      dp[i] = dp[i - 1];
    }
  }

  priority_queue<ll> q2;
  dp2[3 * n] = INF * -1; // できるだけ最小に
  for (int i = 3 * n; i >= 2 * n + 1; --i) {
    dp2[i] = INF * -1;
    q2.push(a[i]);
  }
  dp2[2 * n + 1] = rig[2 * n + 1];
  for (int i = 2 * n; i >= n + 1; --i) {
    int maxi = q2.top();
    if (maxi > a[i]) {
      dp2[i] = dp2[i + 1] - maxi + a[i];
      if (!q2.empty()) {
        q2.pop();
      }
      q2.push(a[i]);
    } else {
      dp2[i] = dp2[i + 1];
    }
    //    printf("dp2[%2d] = %2d\n", i, dp2[i]);
  }
  ll ans = INF;
  for (int i = n; i <= 2 * n; ++i) {
    //    printf("%3d\t%3d\t%3d\n", a[i], dp[i], dp2[i + 1]);
    ans = max(ans, dp[i] - dp2[i + 1]);
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#define MYDEBUG
#include <bits/stdc++.h>
using namespace std;

#ifdef MYDEBUG
#define dbp(x) cout << #x << ": " << x << endl
#define dbp2(x, y) cout << #x << "," << #y << ": " << x << "," << y << endl
#define dbp3(x, y, z)                                                          \
  cout << #x << "," << #y << "," << #z << ": " << x << "," << y << "," << z    \
       << endl
#else
#define dbp(x)
#define dbp2(x, y)
#define dbp3(x, y, z)
#endif
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for (int i = from; i < to; ++i)

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last)
      out << ",";
  }
  out << "]";
  return out;
}

ll max3(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); }

const int maxn = 1e6 + 10;
const ll INF = 1e16 * -1;
int n;
ll sum[maxn], rig[maxn];
ll a[maxn];
ll dp[maxn];
ll dp2[maxn];

void solve() {
  cin >> n;
  a[0] = sum[0] = 0;
  for (int i = 1; i <= 3 * n; ++i) {
    cin >> a[i];
    sum[i] = a[i] + sum[i - 1];
  }
  rig[3 * n + 1] = 0;
  for (int i = 3 * n; i >= 1; --i) {
    rig[i] = a[i] + rig[i + 1];
  }

  //  for (int i = 1; i <= 3 * n; ++i) {
  //    printf("%d %d %d\n", a[i], sum[i], rig[i]);
  //  }

  priority_queue<ll, vector<ll>, greater<ll>> q;
  dp[0] = INF;
  for (int i = 1; i <= n; ++i) {
    dp[i] = INF;
    q.push(a[i]);
  }

  dp[n] = sum[n];
  for (int i = n + 1; i <= 2 * n; ++i) {
    int mini = q.top();
    if (mini < a[i]) {
      dp[i] = dp[i - 1] - mini + a[i];
      if (!q.empty()) {
        q.pop();
      }
      q.push(a[i]);
    } else {
      dp[i] = dp[i - 1];
    }
  }

  priority_queue<ll> q2;
  dp2[3 * n] = INF * -1; // できるだけ最小に
  for (int i = 3 * n; i >= 2 * n + 1; --i) {
    dp2[i] = INF * -1;
    q2.push(a[i]);
  }
  dp2[2 * n + 1] = rig[2 * n + 1];
  for (int i = 2 * n; i >= n + 1; --i) {
    int maxi = q2.top();
    if (maxi > a[i]) {
      dp2[i] = dp2[i + 1] - maxi + a[i];
      if (!q2.empty()) {
        q2.pop();
      }
      q2.push(a[i]);
    } else {
      dp2[i] = dp2[i + 1];
    }
    //    printf("dp2[%2d] = %2d\n", i, dp2[i]);
  }
  ll ans = INF;
  for (int i = n; i <= 2 * n; ++i) {
    //    printf("%3d\t%3d\t%3d\n", a[i], dp[i], dp2[i + 1]);
    ans = max(ans, dp[i] - dp2[i + 1]);
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}