prithivMLmods/Coder-Stat huggingface dataset:
Problem p03712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define SORTR(v, n) sort(v, v + n, greater<int>());
// #define int long long    // %d=>%lld
#define pb push_back
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef pair<ll, pll> plll;

int h;
int w;

char buf1[100 + 1];
string s[100 + 1];

signed main() {
  scanf("%d", &h);
  scanf("%d", &w);
  for (int i = 0; i < h; i++) {
    scanf("%s", &s[i]);
  }

  for (int i = 0; i < w + 2; i++) {
    cout << '#';
  }
  cout << '\n';
  for (int i = 0; i < h; i++) {
    cout << '#' << s[i] << '#' << '\n';
  }
  for (int i = 0; i < w + 2; i++) {
    cout << '#';
  }
  cout << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define SORTR(v, n) sort(v, v + n, greater<int>());
// #define int long long    // %d=>%lld
#define pb push_back
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef pair<ll, pll> plll;

int h;
int w;

char buf1[100 + 1];
string s[100 + 1];

signed main() {
  scanf("%d", &h);
  scanf("%d", &w);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < w + 2; i++) {
    cout << '#';
  }
  cout << '\n';
  for (int i = 0; i < h; i++) {
    cout << '#' << s[i] << '#' << '\n';
  }
  for (int i = 0; i < w + 2; i++) {
    cout << '#';
  }
  cout << '\n';
  return 0;
}
Problem p03712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

int main() {
  int h, w;
  cin >> h >> w;
  char a[h + 1][w + 1];
  for (int i = 0; i <= h + 1; ++i) {
    for (int j = 0; j <= w + 1; ++j) {
      if (i == 0 || i == h + 1)
        a[i][j] = '#';
      else if (j == 0 || j == w + 1)
        a[i][j] = '#';
      else
        cin >> a[i][j];
    }
  }
  for (int i = 0; i <= h + 1; ++i) {
    for (int j = 0; j <= w + 1; ++j) {
      cout << a[i][j];
    }
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

int main() {
  int h, w;
  cin >> h >> w;
  char a[h + 2][w + 2];
  for (int i = 0; i <= h + 1; ++i) {
    for (int j = 0; j <= w + 1; ++j) {
      if (i == 0 || i == h + 1)
        a[i][j] = '#';
      else if (j == 0 || j == w + 1)
        a[i][j] = '#';
      else
        cin >> a[i][j];
    }
  }
  for (int i = 0; i <= h + 1; ++i) {
    for (int j = 0; j <= w + 1; ++j) {
      cout << a[i][j];
    }
    cout << endl;
  }
}
Problem p03712 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  if (H == 0 || W == 0) {
    cout << "##" << endl;
    cout << "##" << endl;
  } else {
    for (int i = 0; i < W + 2; i++) {
      cout << "#";
    }
    cout << endl;

    for (int i = 0; i < H; i++) {
      vector<char> h('a', W);
      for (int j = 0; j < W; j++) {
        cin >> h.at(j);
      }
      cout << "#";
      for (int j = 0; j < W; j++) {
        cout << h.at(j);
      }
      cout << "#" << endl;
    }

    for (int i = 0; i < W + 2; i++) {
      cout << "#";
    }
    cout << endl;
  }
}


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

int main() {
  int H, W;
  cin >> H >> W;
  if (H == 0 || W == 0) {
    cout << "##" << endl;
    cout << "##" << endl;
  } else {
    for (int i = 0; i < W + 2; i++) {
      cout << "#";
    }
    cout << endl;

    for (int i = 0; i < H; i++) {
      vector<char> h(W);
      for (int j = 0; j < W; j++) {
        cin >> h.at(j);
      }
      cout << "#";
      for (int j = 0; j < W; j++) {
        cout << h.at(j);
      }
      cout << "#" << endl;
    }

    for (int i = 0; i < W + 2; i++) {
      cout << "#";
    }
    cout << endl;
  }
}
Problem p03712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < W + 2; i++) {
    cout << "#";
  }
  cout << endl;
  vector<string> str(W);
  for (int i = 0; i < H; i++) {
    cin >> str.at(i);
  }
  for (int i = 0; i < H; i++) {
    cout << "#" << str.at(i) << "#" << endl;
  }
  for (int i = 0; i < W + 2; i++) {
    cout << "#";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < W + 2; i++) {
    cout << "#";
  }
  cout << endl;
  vector<string> str(H);
  for (int i = 0; i < H; i++) {
    cin >> str.at(i);
  }
  for (int i = 0; i < H; i++) {
    cout << "#" << str.at(i) << "#" << endl;
  }
  for (int i = 0; i < W + 2; i++) {
    cout << "#";
  }
}
Problem p03712 (C++)

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

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define ll long long int
#define ld long double
#define llu long long unsigned
#define vll std::vector<ll>
#define mll std::map<ll, ll>
#define sll std::set<ll>
#define msll std::multiset<ll>
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
#define srt(x) sort(all(x))
#define rsrt(x) sort(allr(x))
#define mp make_pair
#define pb push_back
#define mod 1000000007

/*
 ** author **
 ** Parth Lathiya **
 ** https://www.cse.iitb.ac.in/~parthiitb/ **
 */

int main() {

  ios::sync_with_stdio(false);

#ifndef ONLINE_JUDGE
  freopen("B_in.txt", "r", stdin);
#endif

  //  --------------------------------------------------------------------------------------
  ll h, w;
  cin >> h >> w;
  char a[h][w];

  rep(i, 0, h) rep(j, 0, w) cin >> a[i][j];

  // cout<<h<<w;
  rep(i, 0, h + 2) {
    rep(j, 0, w + 2) {
      if (i == 0 || j == 0 || i == h + 1 || j == w + 1)
        cout << "#";
      else
        cout << a[i - 1][j - 1];
    }
    cout << endl;
  }

  //  --------------------------------------------------------------------------------------

  return 0;
}


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

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define ll long long int
#define ld long double
#define llu long long unsigned
#define vll std::vector<ll>
#define mll std::map<ll, ll>
#define sll std::set<ll>
#define msll std::multiset<ll>
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
#define srt(x) sort(all(x))
#define rsrt(x) sort(allr(x))
#define mp make_pair
#define pb push_back
#define mod 1000000007

/*
 ** author **
 ** Parth Lathiya **
 ** https://www.cse.iitb.ac.in/~parthiitb/ **
 */

int main() {

  ios::sync_with_stdio(false);

  //  --------------------------------------------------------------------------------------
  ll h, w;
  cin >> h >> w;
  char a[h][w];

  rep(i, 0, h) rep(j, 0, w) cin >> a[i][j];

  // cout<<h<<w;
  rep(i, 0, h + 2) {
    rep(j, 0, w + 2) {
      if (i == 0 || j == 0 || i == h + 1 || j == w + 1)
        cout << "#";
      else
        cout << a[i - 1][j - 1];
    }
    cout << endl;
  }

  //  --------------------------------------------------------------------------------------

  return 0;
}
Problem p03712 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;

  vector<string> pixel;
  for (int i = 0; i < H; i++) {
    cin >> pixel.at(i);
  }

  for (int i = 0; i < W + 2; i++) {
    cout << '#';
  }
  cout << endl;

  for (int i = 0; i < H; i++) {
    cout << '#' << pixel.at(i) << '#' << endl;
  }

  for (int i = 0; i < W + 2; i++) {
    cout << '#';
  }
  cout << endl;
}

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

int main() {
  int H, W;
  cin >> H >> W;

  vector<string> pixel(H);
  for (int i = 0; i < H; i++) {
    cin >> pixel.at(i);
  }

  for (int i = 0; i < W + 2; i++) {
    cout << '#';
  }
  cout << endl;

  for (int i = 0; i < H; i++) {
    cout << '#' << pixel.at(i) << '#' << endl;
  }

  for (int i = 0; i < W + 2; i++) {
    cout << '#';
  }
  cout << endl;
}
Problem p03712 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {

  int H, W;
  cin >> H >> W;
  char a[100][100];

  for (int i = 0; i <= H + 1; i++) {
    for (int j = 0; j <= W + 1; j++) {
      if (i == 0 || i == H + 1 || j == 0 || j == W + 1)
        a[i][j] = '#';
      else
        cin >> a[i][j];
    }
  }

  for (int i = 0; i <= H + 1; i++) {
    for (int j = 0; j <= W + 1; j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {

  int H, W;
  cin >> H >> W;
  char a[102][102];

  for (int i = 0; i <= H + 1; i++) {
    for (int j = 0; j <= W + 1; j++) {
      if (i == 0 || i == H + 1 || j == 0 || j == W + 1)
        a[i][j] = '#';
      else
        cin >> a[i][j];
    }
  }

  for (int i = 0; i <= H + 1; i++) {
    for (int j = 0; j <= W + 1; j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
}
Problem p03712 (C++)

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

int main() {

  int H, W;
  cin >> H >> W;

  for (int a = 0; a < W + 1; a++) {
    cout << '#';
  }

  cout << '#' << endl;

  for (int i = 0; i < H; i++) {
    vector<string> vec(W);
    cin >> vec.at(i);
    cout << '#' << vec.at(i) << '#' << endl;
  }

  for (int b = 0; b < W + 2; b++) {
    cout << '#';
  }
  return 0;
}

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

int main() {

  int H, W;
  cin >> H >> W;

  for (int a = 0; a < W + 1; a++) {
    cout << '#';
  }

  cout << '#' << endl;

  for (int i = 0; i < H; i++) {
    vector<string> vec(H);
    cin >> vec.at(i);
    cout << '#' << vec.at(i) << '#' << endl;
  }

  for (int b = 0; b < W + 2; b++) {
    cout << '#';
  }
  return 0;
}
Problem p03712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> pic(W + 2, vector<char>(H + 2, '#'));
  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cin >> pic[i][j];
    }
  }
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      cout << pic[i][j];
    }
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> pic(H + 2, vector<char>(W + 2, '#'));
  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cin >> pic[i][j];
    }
  }
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      cout << pic[i][j];
    }
    cout << endl;
  }
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
const int N = 2e5 + 5;
typedef long long ll;
const ll INF = 1e15;
using namespace std;

int n, a[N];
ll f[N], g[N], ans = -INF;
priority_queue<int, vector<int>, greater<int>> q;

template <typename T> inline T read() {
  T x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * w;
}

ll mabs(ll x) { return x > 0 ? x : -x; }

int main() {
#ifndef ONLINE_JUDGE
  freopen("cpp.in", "r", stdin);
#endif
  n = read<int>();
  for (int i = 1; i <= 3 * n; i++)
    a[i] = read<int>();
  for (int i = 1; i <= 3 * n + 1; i++)
    f[i] = -INF, g[i] = INF;
  f[n] = 0;
  for (int i = 1; i <= n; i++)
    q.push(a[i]), f[n] += a[i];
  for (int i = n + 1; i <= 3 * n; i++) {
    f[i] = f[i - 1];
    if (a[i] > q.top())
      f[i] -= q.top(), f[i] += a[i], q.pop(), q.push(a[i]);
  }
  while (!q.empty())
    q.pop();
  g[2 * n + 1] = 0;
  for (int i = 2 * n + 1; i <= 3 * n; i++)
    q.push(-a[i]), g[2 * n + 1] += a[i];
  for (int i = 2 * n; i >= 1; i--) {
    g[i] = g[i + 1];
    if (a[i] < -q.top())
      g[i] += q.top(), g[i] += a[i], q.pop(), q.push(-a[i]);
  }
  for (int i = 1; i <= 3 * n; i++)
    g[i] = g[i + 1];
  for (int i = 1; i <= 3 * n; i++)
    ans = max(ans, f[i] - g[i]);
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
const int N = 5e5 + 5;
typedef long long ll;
const ll INF = 1e15;
using namespace std;

int n, a[N];
ll f[N], g[N], ans = -INF;
priority_queue<int, vector<int>, greater<int>> q;

template <typename T> inline T read() {
  T x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * w;
}

ll mabs(ll x) { return x > 0 ? x : -x; }

int main() {
#ifndef ONLINE_JUDGE
  freopen("cpp.in", "r", stdin);
#endif
  n = read<int>();
  for (int i = 1; i <= 3 * n; i++)
    a[i] = read<int>();
  for (int i = 1; i <= 3 * n + 1; i++)
    f[i] = -INF, g[i] = INF;
  f[n] = 0;
  for (int i = 1; i <= n; i++)
    q.push(a[i]), f[n] += a[i];
  for (int i = n + 1; i <= 3 * n; i++) {
    f[i] = f[i - 1];
    if (a[i] > q.top())
      f[i] -= q.top(), f[i] += a[i], q.pop(), q.push(a[i]);
  }
  while (!q.empty())
    q.pop();
  g[2 * n + 1] = 0;
  for (int i = 2 * n + 1; i <= 3 * n; i++)
    q.push(-a[i]), g[2 * n + 1] += a[i];
  for (int i = 2 * n; i >= 1; i--) {
    g[i] = g[i + 1];
    if (a[i] < -q.top())
      g[i] += q.top(), g[i] += a[i], q.pop(), q.push(-a[i]);
  }
  for (int i = 1; i <= 3 * n; i++)
    g[i] = g[i + 1];
  for (int i = 1; i <= 3 * n; i++)
    ans = max(ans, f[i] - g[i]);
  printf("%lld\n", ans);
  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  priority_queue<long, vector<long>, greater<long>> que1;
  priority_queue<long> que2;
  long middle[N];
  long tmp;
  vector<long> ans1, ans2;
  ans1.push_back(0);
  ans2.push_back(0);
  for (int i = 0; i < 3 * N; ++i) {
    cin >> tmp;
    if (i < N) {
      que1.push(tmp);
      ans1[0] += tmp;
    } else if (i < 2 * N) {
      middle[i - N] = tmp;
    } else {
      que2.push(tmp);
      ans2[0] += tmp;
    }
  }
  for (int i = 0; i < N; ++i) {
    tmp = middle[i];
    if (que1.top() < tmp) {
      ans1.push_back(ans1[i] - que1.top() + tmp);
      que1.pop();
      que1.push(tmp);
    } else {
      ans1.push_back(ans1[i]);
    }
  }
  for (int i = N - 1; i >= 0; --i) {
    tmp = middle[i];
    if (que2.top() > tmp) {
      ans2.push_back(ans2[N - 1 - i] - que2.top() + tmp);
      que2.pop();
      que2.push(tmp);
    } else {
      ans2.push_back(ans2[i]);
    }
  }
  long ans = ans1[0] - ans2[N];
  for (int i = 0; i <= N; ++i) {
    if (ans < ans1[i] - ans2[N - i])
      ans = ans1[i] - ans2[N - i];
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  priority_queue<long, vector<long>, greater<long>> que1;
  priority_queue<long> que2;
  long middle[N];
  long tmp;
  vector<long> ans1, ans2;
  ans1.push_back(0);
  ans2.push_back(0);
  for (int i = 0; i < 3 * N; ++i) {
    cin >> tmp;
    if (i < N) {
      que1.push(tmp);
      ans1[0] += tmp;
    } else if (i < 2 * N) {
      middle[i - N] = tmp;
    } else {
      que2.push(tmp);
      ans2[0] += tmp;
    }
  }
  for (int i = 0; i < N; ++i) {
    tmp = middle[i];
    if (que1.top() < tmp) {
      ans1.push_back(ans1[i] - que1.top() + tmp);
      que1.pop();
      que1.push(tmp);
    } else {
      ans1.push_back(ans1[i]);
    }
  }
  for (int i = N - 1; i >= 0; --i) {
    tmp = middle[i];
    if (que2.top() > tmp) {
      ans2.push_back(ans2[N - 1 - i] - que2.top() + tmp);
      que2.pop();
      que2.push(tmp);
    } else {
      ans2.push_back(ans2[N - 1 - i]);
    }
  }
  long ans = ans1[0] - ans2[N];
  for (int i = 0; i <= N; ++i) {
    if (ans < ans1[i] - ans2[N - i])
      ans = ans1[i] - ans2[N - i];
  }
  cout << ans << endl;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define m(a, b) memset(a, b, sizeof(a))
#define lowbit(x) (x & (-x))
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5 + 10;
const int inf = 1 << 30;
const int mod = 1e9 + 7;
priority_queue<LL, vector<LL>, less<LL>> q;
priority_queue<LL, vector<LL>, greater<LL>> p;
LL ma[maxn], ans;
LL num[maxn], mi[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%lld", &num[i]);
  LL sum = 0;
  for (int i = 1; i <= 3 * n; i++) {
    p.push(num[i]);
    sum += num[i];
    if (p.size() == n + 1) {
      sum -= p.top();
      p.pop();
    }
    ma[i] = sum;
  }
  sum = 0;
  for (int i = 3 * n; i > 0; i--) {
    q.push(num[i]);
    sum += num[i];
    while (q.size() > n) {
      sum -= q.top();
      q.pop();
    }
    mi[i] = sum;
  }
  ans = -1e16;
  for (int i = n; i <= 2 * n; i++) {
    //	printf("%lld %lld\n",ma[i],mi[i + 1]);
    ans = max(ans, ma[i] - mi[i + 1]);
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define m(a, b) memset(a, b, sizeof(a))
#define lowbit(x) (x & (-x))
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 3e5 + 10;
const int inf = 1 << 30;
const int mod = 1e9 + 7;
priority_queue<LL, vector<LL>, less<LL>> q;
priority_queue<LL, vector<LL>, greater<LL>> p;
LL ma[maxn], ans;
LL num[maxn], mi[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%lld", &num[i]);
  LL sum = 0;
  for (int i = 1; i <= 3 * n; i++) {
    p.push(num[i]);
    sum += num[i];
    if (p.size() == n + 1) {
      sum -= p.top();
      p.pop();
    }
    ma[i] = sum;
  }
  sum = 0;
  for (int i = 3 * n; i > 0; i--) {
    q.push(num[i]);
    sum += num[i];
    while (q.size() > n) {
      sum -= q.top();
      q.pop();
    }
    mi[i] = sum;
  }
  ans = -1e16;
  for (int i = n; i <= 2 * n; i++) {
    //	printf("%lld %lld\n",ma[i],mi[i + 1]);
    ans = max(ans, ma[i] - mi[i + 1]);
  }
  printf("%lld\n", ans);
}
Problem p03714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#define MAX_N 100001
#define INF 2147483647
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef pair<int, int> P;

bool operator<(const P &a, const P &b) { return a.first < b.first; };
int main() {
  bool used[300010];
  int i, N, a[300010];
  long long int lsum = 0, rsum = 0, res = -999999999999999;
  P s, t;
  cin >> N;
  REP(i, 3 * N) scanf("%d", &a[i]);
  priority_queue<P, vector<P>, greater<P>> left, right;
  REP(i, N) {
    lsum += a[i];
    left.push(P(a[i], i));
  }
  for (i = N; i < 3 * N; i++)
    right.push(P(a[i], i));
  for (i = 0; i < N; i++) {
    s = right.top();
    right.pop();
    rsum += s.first;
    used[s.second] = 1;
  }
  res = max(res, lsum - rsum);
  for (i = N; i < 2 * N; i++) {
    s = left.top();
    if (s.first < a[i]) {
      left.pop();
      lsum -= s.first;
      lsum += a[i];
      left.push(P(a[i], i));
    }
    if (used[i] == 1) {
      used[i] = 0;
      rsum -= a[i];
      do {
        s = right.top();
        right.pop();
      } while (s.second <= i);
      used[s.second] = 1;
      rsum += s.first;
    }
    res = max(res, lsum - rsum);
  }

  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#define MAX_N 100001
#define INF 2147483647
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef pair<int, int> P;

bool operator<(const P &a, const P &b) { return a.first < b.first; };
int main() {
  bool used[300010];
  int i, N, a[300010];
  long long int lsum = 0, rsum = 0, res = -999999999999999;
  P s, t;
  cin >> N;
  REP(i, 3 * N) scanf("%d", &a[i]);
  priority_queue<P, vector<P>, greater<P>> left, right;
  REP(i, N) {
    lsum += a[i];
    left.push(P(a[i], i));
  }
  for (i = N; i < 3 * N; i++)
    right.push(P(a[i], i));
  for (i = 0; i < N; i++) {
    s = right.top();
    right.pop();
    rsum += s.first;
    used[s.second] = 1;
  }
  res = max(res, lsum - rsum);
  for (i = N; i < 2 * N; i++) {
    s = left.top();
    if (s.first < a[i]) {
      left.pop();
      lsum -= s.first;
      lsum += a[i];
      left.push(P(a[i], i));
    }
    if (used[i] == 1) {
      used[i] = 0;
      rsum -= a[i];
      do {
        s = right.top();
        right.pop();
      } while (s.second <= i && !right.empty());
      used[s.second] = 1;
      rsum += s.first;
    }
    res = max(res, lsum - rsum);
  }

  cout << res << endl;
  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const long long MAX = -1e18;
const long long MAXN = 1e5 + 5;
long long dp_f[MAXN], dp_e[MAXN], a[MAXN];

priority_queue<long long, vector<long long>, greater<long long>> que;
priority_queue<long long> pque;

void Slove_f(int n) {
  long long sum_f = 0;
  for (int i = 1; i <= 3 * n; i++) {
    que.push(a[i]);
    sum_f += a[i];
    dp_f[i] = sum_f;
    if (que.size() > n) {
      sum_f -= que.top();
      que.pop();
      dp_f[i] = sum_f;
    }
  }
}

void Slove_e(int n) {
  long long sum_e = 0;
  for (int i = 3 * n; i >= 1; i--) {
    pque.push(a[i]);
    sum_e += a[i];
    dp_e[i] = sum_e;
    if (pque.size() > n) {
      sum_e -= pque.top();
      pque.pop();
      dp_e[i] = sum_e;
    }
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%lld", &a[i]);

  Slove_f(n);
  Slove_e(n);

  long long ans = MAX;
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, dp_f[i] - dp_e[i + 1]);
  }
  printf("%lld", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const long long MAX = -1e18;
const long long MAXN = 3e5 + 5;
long long dp_f[MAXN], dp_e[MAXN], a[MAXN];

priority_queue<long long, vector<long long>, greater<long long>> que;
priority_queue<long long> pque;

void Slove_f(int n) {
  long long sum_f = 0;
  for (int i = 1; i <= 3 * n; i++) {
    que.push(a[i]);
    sum_f += a[i];
    dp_f[i] = sum_f;
    if (que.size() > n) {
      sum_f -= que.top();
      que.pop();
      dp_f[i] = sum_f;
    }
  }
}

void Slove_e(int n) {
  long long sum_e = 0;
  for (int i = 3 * n; i >= 1; i--) {
    pque.push(a[i]);
    sum_e += a[i];
    dp_e[i] = sum_e;
    if (pque.size() > n) {
      sum_e -= pque.top();
      pque.pop();
      dp_e[i] = sum_e;
    }
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%lld", &a[i]);

  Slove_f(n);
  Slove_e(n);

  long long ans = MAX;
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, dp_f[i] - dp_e[i + 1]);
  }
  printf("%lld", ans);
}
Problem p03714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
typedef unsigned long long ull;
typedef std::pair<ll, ll> P;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool fl = true;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
std::string abc = "abcdefghijklmnopqrstuvwxyz";
struct edge {
  ll to, cost;
};
double pai = 3.141592653589793;
std::vector<P> v[100010];

int main() {
  ll n, a[300010], ans = -INF2;
  std::cin >> n;
  for (int i = 0; i < 3 * n; i++) {
    std::cin >> a[i];
  }
  for (int k = n; k <= 2 * n; k++) {
    std::priority_queue<ll> p, q;
    ll front = 0, back = 0;
    for (int i = 0; i < k; i++)
      p.push(-a[i]);
    for (int i = k; i < 3 * n; i++)
      q.push(a[i]);
    for (int i = n; i < k; i++)
      p.pop();
    for (int i = 2 * n; i > k; i--)
      q.pop();
    for (int i = 0; i < n; i++) {
      ll b = p.top(), c = q.top();
      p.pop(), q.pop();
      front += -b, back += c;
    }
    ans = std::max(front - back, ans);
  }
  std::cout << ans << std::endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
typedef unsigned long long ull;
typedef std::pair<ll, ll> P;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool fl = true;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
std::string abc = "abcdefghijklmnopqrstuvwxyz";
struct edge {
  ll to, cost;
};
double pai = 3.141592653589793;
std::vector<P> v[100010];

int main() {
  ll n, a[300010], ans = -INF2;
  std::cin >> n;
  for (int i = 0; i < 3 * n; i++) {
    std::cin >> a[i];
  }
  std::priority_queue<ll> p, q;
  ll front[100010] = {0}, back[100010] = {0};
  for (int i = 0; i < n; i++) {
    p.push(-a[i]);
    front[0] += a[i];
  }
  front[1] = front[0];
  int j = 1;
  for (int k = n; k <= 2 * n - 1; k++) {
    p.push(-a[k]);
    front[j] += a[k];
    front[j] += p.top();
    p.pop();
    front[j + 1] = front[j];
    j++;
  }
  for (int i = 3 * n - 1; i >= 2 * n; i--) {
    q.push(a[i]);
    back[n] += a[i];
  }
  back[n - 1] = back[n];
  j = n - 1;
  for (int k = 2 * n - 1; k >= n; k--) {
    q.push(a[k]);
    back[j] += a[k];
    back[j] -= q.top();
    q.pop();
    back[j - 1] = back[j];
    j--;
  }
  for (int i = 0; i <= n; i++) {
    ans = std::max(ans, front[i] - back[i]);
  }
  std::cout << ans << std::endl;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << endl
#define all(x) begin(x), end(x)
#define _ << ' ' <<
using namespace std;
using ll = long long;

int a[300000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  multiset<int> s3, s4;
  priority_queue<int, vector<int>, greater<int>> s1;
  ll lsum = 0, rsum = 0;
  for (int i = 0; i < n; ++i) {
    lsum += a[i];
    s1.push(a[i]);
  }

  for (int i = n; i < 3 * n; ++i) {
    if (s3.size() < n) {
      rsum += a[i];
      s3.insert(a[i]);
    } else if (a[i] < *s3.rbegin()) {
      int x = *s3.rbegin();
      s3.erase(x);
      s4.insert(x);
      s3.insert(a[i]);
      rsum -= x;
      rsum += a[i];
    } else {
      s4.insert(a[i]);
    }
  }
  ll sol = lsum - rsum;
  for (int i = n; i < 2 * n; ++i) {
    if (a[i] > s1.top()) {
      lsum -= s1.top();
      s1.pop();
      s1.push(a[i]);
      lsum += a[i];
    }
    auto it = s3.find(a[i]);
    if (it != s3.end()) {
      s3.erase(it);
      rsum -= a[i];
      rsum += *s4.begin();
      s3.insert(*s4.begin());
      s4.erase(s4.begin());
    } else
      s4.erase(s4.find(a[i]));

    sol = max(sol, lsum - rsum);
  }
  cout << sol;
}


Fixed submission:
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << endl
#define all(x) begin(x), end(x)
#define _ << ' ' <<
using namespace std;
using ll = long long;

int a[300000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  multiset<int> s3, s4;
  priority_queue<int, vector<int>, greater<int>> s1;
  ll lsum = 0, rsum = 0;
  for (int i = 0; i < n; ++i) {
    lsum += a[i];
    s1.push(a[i]);
  }

  for (int i = n; i < 3 * n; ++i) {
    if (s3.size() < n) {
      rsum += a[i];
      s3.insert(a[i]);
    } else if (a[i] < *s3.rbegin()) {
      int x = *s3.rbegin();
      s3.erase(s3.find(x));
      s4.insert(x);
      s3.insert(a[i]);
      rsum -= x;
      rsum += a[i];
    } else {
      s4.insert(a[i]);
    }
  }
  ll sol = lsum - rsum;
  for (int i = n; i < 2 * n; ++i) {
    if (a[i] > s1.top()) {
      lsum -= s1.top();
      s1.pop();
      s1.push(a[i]);
      lsum += a[i];
    }
    auto it = s3.find(a[i]);
    if (it != s3.end()) {
      s3.erase(it);
      rsum -= a[i];
      rsum += *s4.begin();
      s3.insert(*s4.begin());
      s4.erase(s4.begin());
    } else
      s4.erase(s4.find(a[i]));

    sol = max(sol, lsum - rsum);
  }
  cout << sol;
}
Problem p03714 (C++)

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

int main(void) {
  int N;
  cin >> N;
  vector<int> a(3 * N);
  for (int i = 0; i < 3 * N; i++) {
    cin >> a[i];
  }

  vector<long long> m_f(N + 1);
  {
    long long sum = 0;
    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 0; i < N; i++) {
      sum += a[i];
      pq.push(a[i]);
    }
    m_f[0] = sum;
    for (int i = N; i <= 2 * N; i++) {
      int m_in_sum = pq.top();
      if (m_in_sum < a[i]) {
        sum -= m_in_sum;
        sum += a[i];
        pq.pop();
        pq.push(a[i]);
      }
      m_f[i + 1 - N] = sum;
    }
  }

  vector<long long> m_b(N + 1);
  {
    priority_queue<int> pq;
    long long sum = 0;
    for (int i = 2 * N; i < 3 * N; i++) {
      sum += a[i];
      pq.push(a[i]);
    }
    m_b[N] = sum;
    for (int i = 2 * N - 1; i >= N; i--) {
      int m_in_sum = pq.top();
      if (m_in_sum > a[i]) {
        sum -= m_in_sum;
        sum += a[i];
        pq.pop();
        pq.push(a[i]);
      }
      m_b[i - N] = sum;
    }
  }

  long long ret = -1e18;
  for (int i = 0; i <= N; i++) {
    ret = max(ret, m_f[i] - m_b[i]);
  }
  cout << ret << endl;

  return 0;
}

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

int main(void) {
  int N;
  cin >> N;
  vector<int> a(3 * N);
  for (int i = 0; i < 3 * N; i++) {
    cin >> a[i];
  }

  vector<long long> m_f(N + 1);
  {
    long long sum = 0;
    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 0; i < N; i++) {
      sum += a[i];
      pq.push(a[i]);
    }
    m_f[0] = sum;
    for (int i = N; i < 2 * N; i++) {
      int m_in_sum = pq.top();
      if (m_in_sum < a[i]) {
        sum -= m_in_sum;
        sum += a[i];
        pq.pop();
        pq.push(a[i]);
      }
      m_f[i + 1 - N] = sum;
    }
  }

  vector<long long> m_b(N + 1);
  {
    priority_queue<int> pq;
    long long sum = 0;
    for (int i = 2 * N; i < 3 * N; i++) {
      sum += a[i];
      pq.push(a[i]);
    }
    m_b[N] = sum;
    for (int i = 2 * N - 1; i >= N; i--) {
      int m_in_sum = pq.top();
      if (m_in_sum > a[i]) {
        sum -= m_in_sum;
        sum += a[i];
        pq.pop();
        pq.push(a[i]);
      }
      m_b[i - N] = sum;
    }
  }

  long long ret = -1e18;
  for (int i = 0; i <= N; i++) {
    ret = max(ret, m_f[i] - m_b[i]);
  }
  cout << ret << endl;

  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
// review
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;
int n;
ll a[100010];
ll former[40010], latter[40010];

void solve() {
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  priority_queue<ll> pq2;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    pq.push(a[i]);
    sum += a[i];
  }
  former[0] = sum;
  for (int i = n; i < 2 * n; i++) {
    pq.push(a[i]);
    sum -= pq.top();
    pq.pop();
    sum += a[i];
    former[i - n + 1] = sum;
  }
  sum = 0;
  for (int i = 2 * n; i < 3 * n; i++) {
    pq2.push(a[i]);
    sum += a[i];
  }
  latter[n] = sum;
  for (int i = 2 * n - 1; i >= n; i--) {
    pq2.push(a[i]);
    sum -= pq2.top();
    pq2.pop();
    sum += a[i];
    latter[i - n] = sum;
  }
  ll ans = -1e16 - 7;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, former[i] - latter[i]);
  }
  cout << ans << endl;
}

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


Fixed submission:
// review
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;
int n;
ll a[300010];
ll former[100010], latter[100010];

void solve() {
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  priority_queue<ll> pq2;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    pq.push(a[i]);
    sum += a[i];
  }
  former[0] = sum;
  for (int i = n; i < 2 * n; i++) {
    pq.push(a[i]);
    sum -= pq.top();
    pq.pop();
    sum += a[i];
    former[i - n + 1] = sum;
  }
  sum = 0;
  for (int i = 2 * n; i < 3 * n; i++) {
    pq2.push(a[i]);
    sum += a[i];
  }
  latter[n] = sum;
  for (int i = 2 * n - 1; i >= n; i--) {
    pq2.push(a[i]);
    sum -= pq2.top();
    pq2.pop();
    sum += a[i];
    latter[i - n] = sum;
  }
  ll ans = -1e16 - 7;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, former[i] - latter[i]);
  }
  cout << ans << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n * 3; i++) {
    cin >> a[i];
  }
  solve();
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#define N 100005
using namespace std;
typedef long long ll;
int n;
int a[N];
ll ls[N], rs[N];
int main() {
  cin >> n;
  for (int i = 1; i <= 3 * n; ++i) {
    cin >> a[i];
  }
  ll ans = 0LL - 1000000000000000000LL;
  set<pair<ll, int>> ss;
  ll sum = 0LL;
  for (int i = 1; i <= 3 * n; ++i) {
    ls[i] = ans;
    rs[i] = -ans;
  }
  for (int i = 1; i <= 3 * n; ++i) {
    ss.insert(make_pair(a[i], i));
    sum += (ll)a[i];
    while (ss.size() > n) {
      sum -= (*ss.begin()).first;
      ss.erase(*ss.begin());
    }
    if (ss.size() == n)
      ls[i] = sum;
  }
  for (int i = 2; i <= 3 * n; ++i) {
    ls[i] = max(ls[i], ls[i - 1]);
    // cout<<i<<" "<<ls[i]<<endl;
  }
  ss.clear();
  sum = 0LL;
  for (int i = 3 * n; i >= 1; --i) {
    ss.insert(make_pair(a[i], i));
    sum += a[i];
    while (ss.size() > n) {
      sum -= (*ss.rbegin()).first;
      ss.erase(*ss.rbegin());
    }
    if (ss.size() == n)
      rs[i] = sum;
  }
  for (int i = 3 * n - 1; i >= 1; --i) {
    rs[i] = min(rs[i + 1], rs[i]);
    // cout<<i<<" "<<rs[i]<<endl;
  }
  for (int i = 1; i < 3 * n; ++i) {
    if (ls[i] > 0LL - 1000000000000000000LL &&
        rs[i + 1] < 1000000000000000000LL) {
      ans = max(ans, ls[i] - rs[i + 1]);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#define N 300005
using namespace std;
typedef long long ll;
int n;
int a[N];
ll ls[N], rs[N];
int main() {
  cin >> n;
  for (int i = 1; i <= 3 * n; ++i) {
    cin >> a[i];
  }
  ll ans = 0LL - 1000000000000000000LL;
  set<pair<ll, int>> ss;
  ll sum = 0LL;
  for (int i = 1; i <= 3 * n; ++i) {
    ls[i] = ans;
    rs[i] = -ans;
  }
  for (int i = 1; i <= 3 * n; ++i) {
    ss.insert(make_pair(a[i], i));
    sum += (ll)a[i];
    while (ss.size() > n) {
      sum -= (*ss.begin()).first;
      ss.erase(*ss.begin());
    }
    if (ss.size() == n)
      ls[i] = sum;
  }
  for (int i = 2; i <= 3 * n; ++i) {
    ls[i] = max(ls[i], ls[i - 1]);
    // cout<<i<<" "<<ls[i]<<endl;
  }
  ss.clear();
  sum = 0LL;
  for (int i = 3 * n; i >= 1; --i) {
    ss.insert(make_pair(a[i], i));
    sum += a[i];
    while (ss.size() > n) {
      sum -= (*ss.rbegin()).first;
      ss.erase(*ss.rbegin());
    }
    if (ss.size() == n)
      rs[i] = sum;
  }
  for (int i = 3 * n - 1; i >= 1; --i) {
    rs[i] = min(rs[i + 1], rs[i]);
    // cout<<i<<" "<<rs[i]<<endl;
  }
  for (int i = 1; i < 3 * n; ++i) {
    if (ls[i] > 0LL - 1000000000000000000LL &&
        rs[i + 1] < 1000000000000000000LL) {
      ans = max(ans, ls[i] - rs[i + 1]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 10;

class cmp {
public:
  bool operator()(int l, int r) { return l > r; }
};

ll a[maxn];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < 3 * n; i++)
    scanf("%I64d", &a[i]);
  priority_queue<ll, vector<ll>, cmp> q1;
  ll l[maxn], r[maxn];
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  ll s = 0;
  for (int i = 0; i < n; i++) {
    q1.push(a[i]);
    s += a[i];
    l[i] = s;
  }
  for (int j = n; j < 3 * n; j++) {
    if (a[j] > q1.top()) {
      s -= q1.top();
      q1.pop();
      q1.push(a[j]);
      s += a[j];
    }
    l[j] = s;
  }
  priority_queue<ll> q;
  reverse(a, a + 3 * n);
  s = 0;
  for (int i = 0; i < n; i++) {
    q.push(a[i]);
    s += a[i];
    r[i] = s;
  }
  //    for(int i=0;i<3*n;i++)
  //        cout<<l[i]<<endl;
  for (int i = n; i < 3 * n; i++) {
    if (q.top() > a[i]) {
      s -= q.top();
      q.pop();
      q.push(a[i]);
      s += a[i];
    }
    r[i] = s;
  }

  //    for(int i=0;i<3*n;i++)
  //        cout<<r[i]<<endl;
  //
  ll res = LLONG_MIN;
  for (int i = n - 1; i < 2 * n; i++) {
    if (l[i] - r[3 * n - i - 1 - 1] > res)
      res = l[i] - r[3 * n - i - 1 - 1];
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 3e5 + 10;

class cmp {
public:
  bool operator()(int l, int r) { return l > r; }
};

ll a[maxn];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < 3 * n; i++)
    scanf("%I64d", &a[i]);
  priority_queue<ll, vector<ll>, cmp> q1;
  ll l[maxn], r[maxn];
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  ll s = 0;
  for (int i = 0; i < n; i++) {
    q1.push(a[i]);
    s += a[i];
    l[i] = s;
  }
  for (int j = n; j < 3 * n; j++) {
    if (a[j] > q1.top()) {
      s -= q1.top();
      q1.pop();
      q1.push(a[j]);
      s += a[j];
    }
    l[j] = s;
  }
  priority_queue<ll> q;
  reverse(a, a + 3 * n);
  s = 0;
  for (int i = 0; i < n; i++) {
    q.push(a[i]);
    s += a[i];
    r[i] = s;
  }
  //    for(int i=0;i<3*n;i++)
  //        cout<<l[i]<<endl;
  for (int i = n; i < 3 * n; i++) {
    if (q.top() > a[i]) {
      s -= q.top();
      q.pop();
      q.push(a[i]);
      s += a[i];
    }
    r[i] = s;
  }

  //    for(int i=0;i<3*n;i++)
  //        cout<<r[i]<<endl;
  //
  ll res = LLONG_MIN;
  for (int i = n - 1; i < 2 * n; i++) {
    if (l[i] - r[3 * n - i - 1 - 1] > res)
      res = l[i] - r[3 * n - i - 1 - 1];
  }
  cout << res << endl;
  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
priority_queue<int> d2;
priority_queue<int, vector<int>, greater<int>> d1;
const int N = 100010;
ll n, a[N], i, ans, ma[N], s;
inline void up(ll &a, ll b) {
  if (a < b)
    a = b;
}
int main() {
  scanf("%lld", &n);
  for (i = 1; i <= 3 * n; ++i)
    scanf("%lld", a + i);
  for (i = 1; i <= n; ++i) {
    d1.push(a[i]);
    ma[n] += a[i];
  }
  s = ma[n];
  ans = -1ll << 60;
  for (; i <= n << 1; ++i) {
    if (a[i] > d1.top()) {
      s += a[i] - d1.top();
      d1.pop();
      d1.push(a[i]);
    }
    ma[i] = s;
  }
  s = 0;
  for (i = 3 * n; i > n << 1; --i) {
    d2.push(a[i]);
    s += a[i];
  }
  up(ans, ma[n << 1] - s);
  for (; i > n; --i) {
    if (a[i] < d2.top()) {
      s -= d2.top() - a[i];
      d2.pop();
      d2.push(a[i]);
    }
    up(ans, ma[i - 1] - s);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
priority_queue<int> d2;
priority_queue<int, vector<int>, greater<int>> d1;
const int N = 300010;
ll n, a[N], i, ans, ma[N], s;
inline void up(ll &a, ll b) {
  if (a < b)
    a = b;
}
int main() {
  scanf("%lld", &n);
  for (i = 1; i <= 3 * n; ++i)
    scanf("%lld", a + i);
  for (i = 1; i <= n; ++i) {
    d1.push(a[i]);
    ma[n] += a[i];
  }
  s = ma[n];
  ans = -1ll << 60;
  for (; i <= n << 1; ++i) {
    if (a[i] > d1.top()) {
      s += a[i] - d1.top();
      d1.pop();
      d1.push(a[i]);
    }
    ma[i] = s;
  }
  s = 0;
  for (i = 3 * n; i > n << 1; --i) {
    d2.push(a[i]);
    s += a[i];
  }
  up(ans, ma[n << 1] - s);
  for (; i > n; --i) {
    if (a[i] < d2.top()) {
      s -= d2.top() - a[i];
      d2.pop();
      d2.push(a[i]);
    }
    up(ans, ma[i - 1] - s);
  }
  cout << ans << endl;
  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <functional>
#include <numeric>
#include <queue>

using namespace std;

typedef long long ll;

#define INF 1LL << 61

int n;
ll a[100000];
ll s[100001], t[100001];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < 3 * n; i++)
    scanf("%lld", &a[i]);
  fill(s + 1, s + n + 1, -INF);
  ll sum = accumulate(a, a + n, 0LL);
  s[0] = sum;
  priority_queue<ll, vector<ll>, greater<ll>> qs;
  for (int i = 0; i < n; i++)
    qs.push(a[i]);
  for (int i = 0; i < n; i++) {
    sum += a[n + i] - qs.top();
    qs.pop();
    qs.push(a[n + i]);
    s[i + 1] = max(sum, s[i]);
  }
  fill(t + 1, t + n + 1, INF);
  sum = accumulate(a + 2 * n, a + 3 * n, 0LL);
  t[0] = sum;
  priority_queue<ll> qt;
  for (int i = 0; i < n; i++)
    qt.push(a[2 * n + i]);
  for (int i = 0; i < n; i++) {
    sum += a[2 * n - i - 1] - qt.top();
    qt.pop();
    qt.push(a[2 * n - i - 1]);
    t[i + 1] = min(sum, t[i]);
  }
  ll res = -INF;
  for (int i = 0; i < n + 1; i++)
    res = max(res, s[i] - t[n - i]);
  printf("%lld\n", res);
}

Fixed submission:
#include <cstdio>
#include <functional>
#include <numeric>
#include <queue>

using namespace std;

typedef long long ll;

#define INF 1LL << 61

int n;
ll a[300000];
ll s[100001], t[100001];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < 3 * n; i++)
    scanf("%lld", &a[i]);
  fill(s + 1, s + n + 1, -INF);
  ll sum = accumulate(a, a + n, 0LL);
  s[0] = sum;
  priority_queue<ll, vector<ll>, greater<ll>> qs;
  for (int i = 0; i < n; i++)
    qs.push(a[i]);
  for (int i = 0; i < n; i++) {
    sum += a[n + i] - qs.top();
    qs.pop();
    qs.push(a[n + i]);
    s[i + 1] = max(sum, s[i]);
  }
  fill(t + 1, t + n + 1, INF);
  sum = accumulate(a + 2 * n, a + 3 * n, 0LL);
  t[0] = sum;
  priority_queue<ll> qt;
  for (int i = 0; i < n; i++)
    qt.push(a[2 * n + i]);
  for (int i = 0; i < n; i++) {
    sum += a[2 * n - i - 1] - qt.top();
    qt.pop();
    qt.push(a[2 * n - i - 1]);
    t[i + 1] = min(sum, t[i]);
  }
  ll res = -INF;
  for (int i = 0; i < n + 1; i++)
    res = max(res, s[i] - t[n - i]);
  printf("%lld\n", res);
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
int a[300000];
LL Max[30000];
LL Min[30000];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < 3 * N; i++) {
    cin >> a[i];
  }
  priority_queue<int, vector<int>, greater<int>> Q1;
  priority_queue<int> Q2;
  for (int i = 0; i < N; i++) {
    Q1.push(a[i]);
    Max[N - 1] += a[i];
  }
  for (int i = N; i < 2 * N; i++) {
    Q1.push(a[i]);
    Max[i] = Max[i - 1] + a[i] - Q1.top();
    Q1.pop();
  }
  for (int i = 3 * N - 1; i >= 2 * N; i--) {
    Q2.push(a[i]);
    Min[2 * N] += a[i];
  }
  for (int i = 2 * N - 1; i >= N; i--) {
    Q2.push(a[i]);
    Min[i] = Min[i + 1] + a[i] - Q2.top();
    Q2.pop();
  }
  LL ans = -(1LL << 60);
  for (int i = N - 1; i < 2 * N; i++) {
    ans = max(ans, Max[i] - Min[i + 1]);
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
int a[300000];
LL Max[300000];
LL Min[300000];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < 3 * N; i++) {
    cin >> a[i];
  }
  priority_queue<int, vector<int>, greater<int>> Q1;
  priority_queue<int> Q2;
  for (int i = 0; i < N; i++) {
    Q1.push(a[i]);
    Max[N - 1] += a[i];
  }
  for (int i = N; i < 2 * N; i++) {
    Q1.push(a[i]);
    Max[i] = Max[i - 1] + a[i] - Q1.top();
    Q1.pop();
  }
  for (int i = 3 * N - 1; i >= 2 * N; i--) {
    Q2.push(a[i]);
    Min[2 * N] += a[i];
  }
  for (int i = 2 * N - 1; i >= N; i--) {
    Q2.push(a[i]);
    Min[i] = Min[i + 1] + a[i] - Q2.top();
    Q2.pop();
  }
  LL ans = -(1LL << 60);
  for (int i = N - 1; i < 2 * N; i++) {
    ans = max(ans, Max[i] - Min[i + 1]);
  }
  cout << ans << endl;
}