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

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

#define F first
#define S second
#define pb push_back
#define ll long long
#define int long long
#define ld double

using namespace std;

const int MAXN = 101 * 1001;

int a[MAXN];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  n *= 3;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ans = -1e18;
  multiset<int> s1, s2, s3;
  int m = n / 3;
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++)
    s2.insert(a[i]), sum2 += a[i];
  while (s2.size() > m) {
    sum2 -= *s2.rbegin();
    s3.insert(*s2.rbegin());
    s2.erase(s2.find(*s2.rbegin()));
  }
  for (int i = 0; i < 2 * m; i++) {
    s1.insert(a[i]);
    sum1 += a[i];
    //	cout <<'*'<< sum1 << ' ' << i << endl ;
    if (s3.find(a[i]) != s3.end())
      s3.erase(s3.find(a[i]));
    else
      s2.erase(s2.find(a[i])), sum2 -= a[i];
    while (s1.size() > m) {
      sum1 -= *s1.begin();
      s1.erase(s1.find(*s1.begin()));
    }
    //	cout << sum1<< endl ;
    while (s2.size() < m) {
      sum2 += *s3.begin();
      s2.insert(*s3.begin());
      s3.erase(s3.find(*s3.begin()));
    }
    if (s1.size() >= m) {
      //		cout << sum2 << ' ' << sum1 << endl ;
      ans = max(ans, sum1 - sum2);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long
#define int long long
#define ld double

using namespace std;

const int MAXN = 1001 * 1001;

int a[MAXN];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  n *= 3;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ans = -1e18;
  multiset<int> s1, s2, s3;
  int m = n / 3;
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++)
    s2.insert(a[i]), sum2 += a[i];
  while (s2.size() > m) {
    sum2 -= *s2.rbegin();
    s3.insert(*s2.rbegin());
    s2.erase(s2.find(*s2.rbegin()));
  }
  for (int i = 0; i < 2 * m; i++) {
    s1.insert(a[i]);
    sum1 += a[i];
    //	cout <<'*'<< sum1 << ' ' << i << endl ;
    if (s3.find(a[i]) != s3.end())
      s3.erase(s3.find(a[i]));
    else
      s2.erase(s2.find(a[i])), sum2 -= a[i];
    while (s1.size() > m) {
      sum1 -= *s1.begin();
      s1.erase(s1.find(*s1.begin()));
    }
    //	cout << sum1<< endl ;
    while (s2.size() < m) {
      sum2 += *s3.begin();
      s2.insert(*s3.begin());
      s3.erase(s3.find(*s3.begin()));
    }
    if (s1.size() >= m) {
      //		cout << sum2 << ' ' << sum1 << endl ;
      ans = max(ans, sum1 - sum2);
    }
  }
  cout << ans << endl;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fo(i, a, b) for (int i = (a); i < (b); i++)
#define MP make_pair
#define PB push_back
typedef long long ll;

int N;
ll a[100100], l[100100], r[100100], pqsum, ans = -1e18;
priority_queue<ll> pq;

int main() {
  scanf("%d", &N);
  fo(i, 0, 3 * N) scanf("%lld", &a[i]);

  fo(i, 0, N) pqsum += a[i], pq.push(-a[i]);
  l[N - 1] = pqsum;
  fo(i, N, 3 * N) {
    pqsum += a[i], pq.push(-a[i]);
    pqsum += pq.top(), pq.pop();
    l[i] = pqsum;
  }
  while (pq.size())
    pq.pop();
  pqsum = 0ll;

  for (int i = 3 * N - 1; i >= 2 * N; i--)
    pqsum += a[i], pq.push(a[i]);
  r[2 * N] = pqsum;
  for (int i = 2 * N; i--;) {
    pqsum += a[i], pq.push(a[i]);
    pqsum -= pq.top(), pq.pop();
    r[i] = pqsum;
  }

  fo(i, N - 1, 2 * N) ans = max(ans, l[i] - r[i + 1]);

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fo(i, a, b) for (int i = (a); i < (b); i++)
#define MP make_pair
#define PB push_back
typedef long long ll;

int N;
ll a[300300], l[300300], r[300300], pqsum, ans = -1e18;
priority_queue<ll> pq;

int main() {
  scanf("%d", &N);
  fo(i, 0, 3 * N) scanf("%lld", &a[i]);

  fo(i, 0, N) pqsum += a[i], pq.push(-a[i]);
  l[N - 1] = pqsum;
  fo(i, N, 3 * N) {
    pqsum += a[i], pq.push(-a[i]);
    pqsum += pq.top(), pq.pop();
    l[i] = pqsum;
  }
  while (pq.size())
    pq.pop();
  pqsum = 0ll;

  for (int i = 3 * N - 1; i >= 2 * N; i--)
    pqsum += a[i], pq.push(a[i]);
  r[2 * N] = pqsum;
  for (int i = 2 * N; i--;) {
    pqsum += a[i], pq.push(a[i]);
    pqsum -= pq.top(), pq.pop();
    r[i] = pqsum;
  }

  fo(i, N - 1, 2 * N) ans = max(ans, l[i] - r[i + 1]);

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

  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout);
using namespace std;
#include <queue>
typedef long long ll;
const int MAXN = 100005;
const ll INF = 1e18;
int n;
int s[MAXN];
ll f[MAXN], g[MAXN];

priority_queue<ll, vector<ll>, greater<ll>> a;
priority_queue<ll> b;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%d", s + i);
  for (int i = 1; i <= n; i++)
    a.push(s[i]), f[n] += s[i];
  for (int i = n + 1; i <= 3 * n; i++)
    if (s[i] > a.top()) {
      f[i] = f[i - 1] + s[i] - a.top();
      a.pop(), a.push(s[i]);
    } else
      f[i] = f[i - 1];
  for (int i = 3 * n; i > n << 1; i--)
    b.push(s[i]), g[n << 1 | 1] += s[i];
  for (int i = n << 1; i; i--)
    if (s[i] < b.top()) {
      g[i] = g[i + 1] + s[i] - b.top();
      b.pop(), b.push(s[i]);
    } else
      g[i] = g[i + 1];
  ll ans = -INF;
  for (int i = n; i <= n << 1; i++)
    ans = max(ans, f[i] - g[i + 1]);
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout);
using namespace std;
#include <queue>
typedef long long ll;
const int MAXN = 3 * 100005;
const ll INF = 1e18;
int n;
int s[MAXN];
ll f[MAXN], g[MAXN];

priority_queue<ll, vector<ll>, greater<ll>> a;
priority_queue<ll> b;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%d", s + i);
  for (int i = 1; i <= n; i++)
    a.push(s[i]), f[n] += s[i];
  for (int i = n + 1; i <= 3 * n; i++)
    if (s[i] > a.top()) {
      f[i] = f[i - 1] + s[i] - a.top();
      a.pop(), a.push(s[i]);
    } else
      f[i] = f[i - 1];
  for (int i = 3 * n; i > n << 1; i--)
    b.push(s[i]), g[n << 1 | 1] += s[i];
  for (int i = n << 1; i; i--)
    if (s[i] < b.top()) {
      g[i] = g[i + 1] + s[i] - b.top();
      b.pop(), b.push(s[i]);
    } else
      g[i] = g[i + 1];
  ll ans = -INF;
  for (int i = n; i <= n << 1; i++)
    ans = max(ans, f[i] - g[i + 1]);
  printf("%lld", ans);
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Maxn = 1e5 + 10;
int N;
int A[Maxn];
int Pre[Maxn], Suf[Maxn];
multiset<int> M, V;
int Ans = -1e18;
signed main() {
  scanf("%d", &N);
  int X = 3 * N;
  for (int i = 1; i <= X; i++)
    scanf("%d", A + i);
  for (int i = 1; i <= N; i++)
    M.insert(A[i]), Pre[N] += A[i];
  for (int i = N + 1; i <= X; i++)
    if (A[i] > *M.begin()) {
      int tmp = A[i] - *M.begin();
      M.erase(M.begin());
      M.insert(A[i]);
      Pre[i] = Pre[i - 1] + tmp;
    } else
      Pre[i] = Pre[i - 1];
  int X2 = 2 * N + 1;
  for (int i = X2; i <= X; i++)
    V.insert(A[i]), Suf[X2] += A[i];
  for (int i = X2 - 1; i >= 1; i--) {
    if (A[i] < *V.rbegin()) {
      int tmp = *V.rbegin() - A[i];
      multiset<int>::iterator it = V.end();
      --it;
      V.erase(it);
      V.insert(A[i]);
      Suf[i] = Suf[i + 1] - tmp;
    } else
      Suf[i] = Suf[i + 1];
  }
  int X3 = 2 * N;
  for (int i = N; i <= X3; i++)
    Ans = max(Ans, Pre[i] - Suf[i + 1]);
  cout << Ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Maxn = 3e5 + 10;
int N;
int A[Maxn];
int Pre[Maxn], Suf[Maxn];
multiset<int> M, V;
int Ans = -1e18;
signed main() {
  scanf("%d", &N);
  int X = 3 * N;
  for (int i = 1; i <= X; i++)
    scanf("%d", A + i);
  for (int i = 1; i <= N; i++)
    M.insert(A[i]), Pre[N] += A[i];
  for (int i = N + 1; i <= X; i++)
    if (A[i] > *M.begin()) {
      int tmp = A[i] - *M.begin();
      M.erase(M.begin());
      M.insert(A[i]);
      Pre[i] = Pre[i - 1] + tmp;
    } else
      Pre[i] = Pre[i - 1];
  int X2 = 2 * N + 1;
  for (int i = X2; i <= X; i++)
    V.insert(A[i]), Suf[X2] += A[i];
  for (int i = X2 - 1; i >= 1; i--) {
    if (A[i] < *V.rbegin()) {
      int tmp = *V.rbegin() - A[i];
      multiset<int>::iterator it = V.end();
      --it;
      V.erase(it);
      V.insert(A[i]);
      Suf[i] = Suf[i + 1] - tmp;
    } else
      Suf[i] = Suf[i + 1];
  }
  int X3 = 2 * N;
  for (int i = N; i <= X3; i++)
    Ans = max(Ans, Pre[i] - Suf[i + 1]);
  cout << Ans << endl;
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 2e5 + 20;
ll dp[maxn][2], a[maxn], sum, res = -1e18;
multiset<ll> st;
void f(int n, ll x) {
  sum = 0;
  st.clear();
  if (x) {
    reverse(a, a + 3 * n);
    for (int i = 0; i < 3 * n; i++)
      a[i] *= -1;
  }
  for (int i = 0; i < n; i++) {
    st.insert(a[i]);
    sum += a[i];
  }
  dp[n - 1][x] = sum;
  for (int i = n; i < 3 * n; i++) {
    if (a[i] > *st.begin()) {
      sum -= *st.begin() - a[i];
      st.erase(st.begin());
      st.insert(a[i]);
    }
    dp[i][x] = sum;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < 3 * n; i++)
    cin >> a[i];
  for (int i = 0; i < maxn; i++)
    dp[i][0] = dp[i][1] = res;
  f(n, 0);
  f(n, 1);
  for (int i = n - 1; i < 2 * n; i++) {
    res = max(res, dp[i][0] + dp[3 * n - i - 2][1]);
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 3e5 + 20;
ll dp[maxn][2], a[maxn], sum, res = -1e18;
multiset<ll> st;
void f(int n, ll x) {
  sum = 0;
  st.clear();
  if (x) {
    reverse(a, a + 3 * n);
    for (int i = 0; i < 3 * n; i++)
      a[i] *= -1;
  }
  for (int i = 0; i < n; i++) {
    st.insert(a[i]);
    sum += a[i];
  }
  dp[n - 1][x] = sum;
  for (int i = n; i < 3 * n; i++) {
    if (a[i] > *st.begin()) {
      sum -= *st.begin() - a[i];
      st.erase(st.begin());
      st.insert(a[i]);
    }
    dp[i][x] = sum;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < 3 * n; i++)
    cin >> a[i];
  for (int i = 0; i < maxn; i++)
    dp[i][0] = dp[i][1] = res;
  f(n, 0);
  f(n, 1);
  for (int i = n - 1; i < 2 * n; i++) {
    res = max(res, dp[i][0] + dp[3 * n - i - 2][1]);
  }
  cout << res;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <memory.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef double D;

const int N = 1e5;

LL arr[N], r[N], l[N];
priority_queue<LL> pq;

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n * 3; ++i)
    cin >> arr[i];

  LL sum = 0;

  for (int i = n * 3 - 1; i >= n * 2; --i) {
    sum += arr[i];
    pq.push(arr[i]);
  }

  r[n * 2] = sum;

  for (int i = n * 2 - 1; i >= n; --i) {
    sum += arr[i];
    pq.push(arr[i]);
    sum -= pq.top();
    pq.pop();
    r[i] = sum;
  }

  sum = 0;

  while (!pq.empty())
    pq.pop();

  for (int i = 0; i <= n - 1; ++i) {
    sum -= arr[i];
    pq.push(-arr[i]);
  }

  l[n - 1] = -sum;

  for (int i = n; i <= n * 2 - 1; ++i) {
    sum -= arr[i];
    pq.push(-arr[i]);
    sum -= pq.top();
    pq.pop();
    l[i] = -sum;
  }

  LL res = -1e18;

  for (int i = n; i <= 2 * n; ++i)
    res = max(res, l[i - 1] - r[i]);

  cout << res;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <memory.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef double D;

const int N = 3e5 + 1;

LL arr[N], r[N], l[N];
priority_queue<LL> pq;

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n * 3; ++i)
    cin >> arr[i];

  LL sum = 0;

  for (int i = n * 3 - 1; i >= n * 2; --i) {
    sum += arr[i];
    pq.push(arr[i]);
  }

  r[n * 2] = sum;

  for (int i = n * 2 - 1; i >= n; --i) {
    sum += arr[i];
    pq.push(arr[i]);
    sum -= pq.top();
    pq.pop();
    r[i] = sum;
  }

  sum = 0;

  while (!pq.empty())
    pq.pop();

  for (int i = 0; i <= n - 1; ++i) {
    sum -= arr[i];
    pq.push(-arr[i]);
  }

  l[n - 1] = -sum;

  for (int i = n; i <= n * 2 - 1; ++i) {
    sum -= arr[i];
    pq.push(-arr[i]);
    sum -= pq.top();
    pq.pop();
    l[i] = -sum;
  }

  LL res = -1e18;

  for (int i = n; i <= 2 * n; ++i)
    res = max(res, l[i - 1] - r[i]);

  cout << res;
}
Problem p03716 (C++)

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

typedef long long ll;

multiset<ll> hv;
ll l[100005], r[100005], v[100005], s, ans(-(1ll << 60));
int n;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < 3 * n; i++)
    scanf("%lld", v + i);
  for (int i = 0; i < 3 * n; i++) {
    hv.insert(v[i]);
    s += v[i];
    if (hv.size() > n) {
      s -= *hv.begin();
      hv.erase(hv.begin());
    }
    if (hv.size() == n)
      l[i] = s;
    else
      l[i] = -(1ll << 60);
  }
  s = 0;
  hv.clear();
  for (int i = 3 * n - 1; i >= 0; i--) {
    hv.insert(v[i]);
    s += v[i];
    if (hv.size() > n) {
      s -= *hv.rbegin();
      hv.erase(--hv.end());
    }
    if (hv.size() == n)
      r[i] = s;
    else
      r[i] = 1ll << 60;
  }
  for (int i = 1; i < 3 * n; i++)
    ans = max(ans, l[i - 1] - r[i]);
  printf("%lld\n", ans);
  return 0;
}


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

typedef long long ll;

multiset<ll> hv;
ll l[300005], r[300005], v[300005], s, ans(-(1ll << 60));
int n;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < 3 * n; i++)
    scanf("%lld", v + i);
  for (int i = 0; i < 3 * n; i++) {
    hv.insert(v[i]);
    s += v[i];
    if (hv.size() > n) {
      s -= *hv.begin();
      hv.erase(hv.begin());
    }
    if (hv.size() == n)
      l[i] = s;
    else
      l[i] = -(1ll << 60);
  }
  s = 0;
  hv.clear();
  for (int i = 3 * n - 1; i >= 0; i--) {
    hv.insert(v[i]);
    s += v[i];
    if (hv.size() > n) {
      s -= *hv.rbegin();
      hv.erase(--hv.end());
    }
    if (hv.size() == n)
      r[i] = s;
    else
      r[i] = 1ll << 60;
  }
  for (int i = 1; i < 3 * n; i++)
    ans = max(ans, l[i - 1] - r[i]);
  printf("%lld\n", ans);
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define f(i, a, b) for (long long int i = a; i < b; i++)
#define ll long long int
#define pb push_back

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("ou.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll a[3 * n];
  priority_queue<ll> q2;
  priority_queue<int, vector<int>, greater<int>> q1;
  ll sum = 0;
  ll s = 0;
  ll pos[n + 2];
  ll neg[n + 2];
  ll max = -10000000000000000;
  f(i, 0, 3 * n) { cin >> a[i]; }
  f(i, 0, 2 * n) {
    q1.push(a[i]);
    sum += a[i];

    if (i > n - 1) {
      ll o = q1.top();
      sum -= o;

      q1.pop();
    }
    if (i >= n - 1)
      pos[i] = sum;
  }
  f(i, 0, 2 * n) {
    q2.push(a[3 * n - i - 1]);
    s += a[3 * n - i - 1];

    if (i > n - 1) {
      ll j = q2.top();
      s -= j;

      q2.pop();
    }
    if (i >= n - 1)
      neg[3 * n - i - 1] = s;
  }

  f(i, n - 1, 2 * n) {
    ll u = pos[i] - neg[i + 1];
    if (max < u)
      max = u;
  }
  cout << max;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define f(i, a, b) for (long long int i = a; i < b; i++)
#define ll long long int
#define pb push_back

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  ll a[3 * n];
  priority_queue<ll> q2;
  priority_queue<int, vector<int>, greater<int>> q1;
  ll sum = 0;
  ll s = 0;
  ll pos[n + 2];
  ll neg[n + 2];
  ll max = -10000000000000000;
  f(i, 0, 3 * n) { cin >> a[i]; }
  f(i, 0, 2 * n) {
    q1.push(a[i]);
    sum += a[i];

    if (i > n - 1) {
      ll o = q1.top();
      sum -= o;

      q1.pop();
    }
    if (i >= n - 1)
      pos[i] = sum;
  }
  f(i, 0, 2 * n) {
    q2.push(a[3 * n - i - 1]);
    s += a[3 * n - i - 1];

    if (i > n - 1) {
      ll j = q2.top();
      s -= j;

      q2.pop();
    }
    if (i >= n - 1)
      neg[3 * n - i - 1] = s;
  }

  f(i, n - 1, 2 * n) {
    ll u = pos[i] - neg[i + 1];
    if (max < u)
      max = u;
  }
  cout << max;

  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(), (x).end()
template <typename T>
using ord_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 123;
int n, a[N], k;
ll p[N], s[N];
priority_queue<int, vector<int>, greater<int>> P;
priority_queue<int> S;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  cin >> n;
  n *= 3;
  k = n / 3;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n - k; i++) {
    p[i] = p[i - 1];
    P.push(a[i]), p[i] += a[i];
    if ((int)P.size() > k)
      p[i] -= P.top(), P.pop();
  }
  ll res = -1e18;
  for (int i = n; i > k; i--) {
    s[i] = s[i + 1];
    S.push(a[i]), s[i] += a[i];
    if ((int)S.size() > k)
      s[i] -= S.top(), S.pop();
    if (i <= n - k + 1) {
      res = max(res, p[i - 1] - s[i]);
    }
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(), (x).end()
template <typename T>
using ord_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 3e5 + 123;
int n, a[N], k;
ll p[N], s[N];
priority_queue<int, vector<int>, greater<int>> P;
priority_queue<int> S;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  cin >> n;
  n *= 3;
  k = n / 3;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n - k; i++) {
    p[i] = p[i - 1];
    P.push(a[i]), p[i] += a[i];
    if ((int)P.size() > k)
      p[i] -= P.top(), P.pop();
  }
  ll res = -1e18;
  for (int i = n; i > k; i--) {
    s[i] = s[i + 1];
    S.push(a[i]), s[i] += a[i];
    if ((int)S.size() > k)
      s[i] -= S.top(), S.pop();
    if (i <= n - k + 1) {
      res = max(res, p[i - 1] - s[i]);
    }
  }
  cout << res;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

priority_queue<int> QD;
priority_queue<int, vector<int>, greater<int>> QU;

typedef long long ll;
int n;
int a[110000];
ll pmx[110000], qmn[110000], smx, smn, ans = -(1ll << 60);

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    smx += a[i];
    QU.push(a[i]);
  }
  pmx[n] = smx;
  for (int i = n + 1; i <= 3 * n; i++) {
    if (a[i] > QU.top()) {
      smx -= QU.top();
      QU.pop();
      smx += a[i];
      QU.push(a[i]);
    }
    pmx[i] = smx;
  }
  for (int i = 3 * n; i > 2 * n; i--) {
    smn += a[i];
    QD.push(a[i]);
  }
  qmn[2 * n + 1] = smn;
  for (int i = 2 * n; i; i--) {
    if (a[i] < QD.top()) {
      smn -= QD.top();
      QD.pop();
      smn += a[i];
      QD.push(a[i]);
    }
    qmn[i] = smn;
  }
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, pmx[i] - qmn[i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

priority_queue<int> QD;
priority_queue<int, vector<int>, greater<int>> QU;

typedef long long ll;
int n;
int a[310000];
ll pmx[310000], qmn[310000], smx, smn, ans = -(1ll << 60);

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    smx += a[i];
    QU.push(a[i]);
  }
  pmx[n] = smx;
  for (int i = n + 1; i <= 3 * n; i++) {
    if (a[i] > QU.top()) {
      smx -= QU.top();
      QU.pop();
      smx += a[i];
      QU.push(a[i]);
    }
    pmx[i] = smx;
  }
  for (int i = 3 * n; i > 2 * n; i--) {
    smn += a[i];
    QD.push(a[i]);
  }
  qmn[2 * n + 1] = smn;
  for (int i = 2 * n; i; i--) {
    if (a[i] < QD.top()) {
      smn -= QD.top();
      QD.pop();
      smn += a[i];
      QD.push(a[i]);
    }
    qmn[i] = smn;
  }
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, pmx[i] - qmn[i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03716 (C++)

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

using namespace std;

#define int long long

int n;
int a[100001];
int pre_mx[100001], suf_mn[100001];

priority_queue<int> suf;
priority_queue<int, vector<int>, greater<int>> pre;

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

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

  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    pre.push(a[i]);
  }
  pre_mx[n] = sum;
  for (int i = n + 1; i <= 2 * n; i++) {
    sum += a[i];
    pre.push(a[i]);
    sum -= pre.top();
    pre.pop();
    pre_mx[i] = sum;
  }

  sum = 0;
  for (int i = 3 * n; i >= 2 * n + 1; i--) {
    sum += a[i];
    suf.push(a[i]);
  }
  suf_mn[2 * n + 1] = sum;
  for (int i = 2 * n; i >= n + 1; i--) {
    sum += a[i];
    suf.push(a[i]);
    sum -= suf.top();
    suf.pop();
    suf_mn[i] = sum;
  }
  int ans = -1e18;
  for (int i = n; i <= 2 * n; i++)
    ans = max(ans, pre_mx[i] - suf_mn[i + 1]);
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long

int n;
int a[300001];
int pre_mx[300001], suf_mn[300001];

priority_queue<int> suf;
priority_queue<int, vector<int>, greater<int>> pre;

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

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

  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    pre.push(a[i]);
  }
  pre_mx[n] = sum;
  for (int i = n + 1; i <= 2 * n; i++) {
    sum += a[i];
    pre.push(a[i]);
    sum -= pre.top();
    pre.pop();
    pre_mx[i] = sum;
  }

  sum = 0;
  for (int i = 3 * n; i >= 2 * n + 1; i--) {
    sum += a[i];
    suf.push(a[i]);
  }
  suf_mn[2 * n + 1] = sum;
  for (int i = 2 * n; i >= n + 1; i--) {
    sum += a[i];
    suf.push(a[i]);
    sum -= suf.top();
    suf.pop();
    suf_mn[i] = sum;
  }
  int ans = -1e18;
  for (int i = n; i <= 2 * n; i++)
    ans = max(ans, pre_mx[i] - suf_mn[i + 1]);
  cout << ans;
}
Problem p03716 (C++)

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

typedef long long ll;

const int maxn = 1e5 + 6;

ll a[maxn];
ll sm[maxn];
ll sma[maxn];

int n;
int m;

void smc() {
  multiset<int> S = *new multiset<int>();
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    S.insert(a[i]);
    sum += a[i];
    if (S.size() > m) {
      sum -= *S.begin();
      S.erase(S.begin());
    }
    sm[i] = sum;
  }
}

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

  cin >> n;
  m = n;
  n = n * 3;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  smc();
  for (int i = 1; i <= n; i++) {
    sma[i] = sm[i];
    a[i] = -a[i];
  }

  reverse(a + 1, a + n + 1);
  smc();

  ll ans = -1e15;

  for (int i = m; i <= n - m; i++) {
    ans = max(ans, sma[i] + sm[n - i]);
  }

  cout << ans << endl;
}


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

typedef long long ll;

const int maxn = 3e5 + 6;

ll a[maxn];
ll sm[maxn];
ll sma[maxn];

int n;
int m;

void smc() {
  multiset<int> S = *new multiset<int>();
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    S.insert(a[i]);
    sum += a[i];
    if (S.size() > m) {
      sum -= *S.begin();
      S.erase(S.begin());
    }
    sm[i] = sum;
  }
}

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

  cin >> n;
  m = n;
  n = n * 3;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  smc();
  for (int i = 1; i <= n; i++) {
    sma[i] = sm[i];
    a[i] = -a[i];
  }

  reverse(a + 1, a + n + 1);
  smc();

  ll ans = -1e15;

  for (int i = m; i <= n - m; i++) {
    ans = max(ans, sma[i] + sm[n - i]);
  }

  cout << ans << endl;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

int main() {
  priority_queue<ll> que2;
  priority_queue<ll, vector<ll>, greater<ll>> que;
  ll n, a[112345], fst[112345], ltr[112345], sum1, sum2;
  cin >> n;

  sum1 = 0;
  sum2 = 0;
  REP(i, n * 3) {
    cin >> a[i];

    if (i < n) {
      que.push(a[i]);

      sum1 += a[i];
    }

    if (2 * n <= i) {
      que2.push(a[i]);

      sum2 += a[i];
    }
  }

  for (int i = n - 1; i < 3 * n; i++) {
    fst[i] = sum1;

    if (a[i + 1] > que.top()) {
      sum1 += a[i + 1] - que.top();
      que.pop();
      que.push(a[i + 1]);
    }
  }

  for (int i = 2 * n; i > 0; i--) {
    ltr[i] = sum2;

    if (a[i - 1] < que2.top()) {
      sum2 += a[i - 1] - que2.top();
      que2.pop();
      que2.push(a[i - 1]);
    }
  }

  ll ans = -10000000000000000;
  for (int i = n - 1; i < n * 2; i++) {
    ans = max(ans, fst[i] - ltr[i + 1]);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

int main() {
  priority_queue<ll> que2;
  priority_queue<ll, vector<ll>, greater<ll>> que;
  ll n, a[312345], fst[312345], ltr[312345], sum1, sum2;
  cin >> n;

  sum1 = 0;
  sum2 = 0;
  REP(i, n * 3) {
    cin >> a[i];

    if (i < n) {
      que.push(a[i]);

      sum1 += a[i];
    }

    if (2 * n <= i) {
      que2.push(a[i]);

      sum2 += a[i];
    }
  }

  for (int i = n - 1; i < 3 * n; i++) {
    fst[i] = sum1;

    if (a[i + 1] > que.top()) {
      sum1 += a[i + 1] - que.top();
      que.pop();
      que.push(a[i + 1]);
    }
  }

  for (int i = 2 * n; i > 0; i--) {
    ltr[i] = sum2;

    if (a[i - 1] < que2.top()) {
      sum2 += a[i - 1] - que2.top();
      que2.pop();
      que2.push(a[i - 1]);
    }
  }

  ll ans = -10000000000000000;
  for (int i = n - 1; i < n * 2; i++) {
    ans = max(ans, fst[i] - ltr[i + 1]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define MAXN 100001
ll a[3 * MAXN];
multiset<ll> big;
multiset<ll> small;
ll M[MAXN];
ll m[MAXN];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= 3 * n; i++) {
    cin >> a[i];
  }
  ll now1 = 0;
  for (int i = 1; i <= n; i++) {
    now1 += a[i];
    big.insert(a[i]);
  }
  M[n] = now1;
  for (int i = n + 1; i <= 2 * n; i++) {
    big.insert(a[i]);
    now1 = now1 + a[i] - *big.begin();
    big.erase(big.begin());
    M[i] = now1;
  }
  ll now2 = 0;
  for (int i = 3 * n; i >= 2 * n + 1; i--) {
    now2 += a[i];
    small.insert(a[i]);
  }
  m[2 * n + 1] = now2;
  for (int i = 2 * n; i >= n + 1; i--) {
    small.insert(a[i]);
    now2 = now2 + a[i] - *small.rbegin();
    small.erase(small.find(*small.rbegin()));
    m[i] = now2;
  }
  ll ret = M[n] - m[n + 1];
  for (int i = n + 1; i <= 2 * n; i++) {
    if (ret < M[i] - m[i + 1])
      ret = M[i] - m[i + 1];
  }
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define MAXN 100001
ll a[3 * MAXN];
multiset<ll> big;
multiset<ll> small;
ll M[3 * MAXN];
ll m[3 * MAXN];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= 3 * n; i++) {
    cin >> a[i];
  }
  ll now1 = 0;
  for (int i = 1; i <= n; i++) {
    now1 += a[i];
    big.insert(a[i]);
  }
  M[n] = now1;
  for (int i = n + 1; i <= 2 * n; i++) {
    big.insert(a[i]);
    now1 = now1 + a[i] - *big.begin();
    big.erase(big.begin());
    M[i] = now1;
  }
  ll now2 = 0;
  for (int i = 3 * n; i >= 2 * n + 1; i--) {
    now2 += a[i];
    small.insert(a[i]);
  }
  m[2 * n + 1] = now2;
  for (int i = 2 * n; i >= n + 1; i--) {
    small.insert(a[i]);
    now2 = now2 + a[i] - *small.rbegin();
    small.erase(small.find(*small.rbegin()));
    m[i] = now2;
  }
  ll ret = M[n] - m[n + 1];
  for (int i = n + 1; i <= 2 * n; i++) {
    if (ret < M[i] - m[i + 1])
      ret = M[i] - m[i + 1];
  }
  cout << ret << endl;
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>

#define ll long long

using namespace std;

int n;
ll a[300000];
multiset<ll> l, r, rn;
ll suml, sumr;
ll res = -1e18;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    l.insert(a[i]);
    suml += a[i];
  }
  for (int i = n; i < 3 * n; ++i) {
    cin >> a[i];
    rn.insert(a[i]);
  }
  for (int i = 0; i < n; ++i) {
    ll c = *rn.begin();
    r.insert(c);
    rn.erase(rn.find(c));
    sumr += c;
  }
  for (int i = 0; i < n; ++i) {
    res = max(res, suml - sumr);
    ll c = a[n + i];
    if (c > *l.begin()) {
      suml -= *l.begin();
      l.erase(l.begin());
      l.insert(c);
      suml += c;
    }
    if (rn.count(c)) {
      rn.erase(c);
    } else {
      sumr -= c;
      r.erase(r.find(c));
      ll cc = *rn.begin();
      r.insert(cc);
      rn.erase(rn.find(cc));
      sumr += cc;
    }
  }
  res = max(res, suml - sumr);
  cout << res << "\n";
}


Fixed submission:
#include <iostream>
#include <set>

#define ll long long

using namespace std;

int n;
ll a[300000];
multiset<ll> l, r, rn;
ll suml, sumr;
ll res = -1e18;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    l.insert(a[i]);
    suml += a[i];
  }
  for (int i = n; i < 3 * n; ++i) {
    cin >> a[i];
    rn.insert(a[i]);
  }
  for (int i = 0; i < n; ++i) {
    ll c = *rn.begin();
    r.insert(c);
    rn.erase(rn.find(c));
    sumr += c;
  }
  for (int i = 0; i < n; ++i) {
    res = max(res, suml - sumr);
    ll c = a[n + i];
    if (c > *l.begin()) {
      suml -= *l.begin();
      l.erase(l.begin());
      l.insert(c);
      suml += c;
    }
    if (rn.count(c)) {
      rn.erase(rn.find(c));
    } else {
      sumr -= c;
      r.erase(r.find(c));
      ll cc = *rn.begin();
      r.insert(cc);
      rn.erase(rn.find(cc));
      sumr += cc;
    }
  }
  res = max(res, suml - sumr);
  cout << res << "\n";
}
Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define mod 1000000007
#define ll long long
#define N 110
using namespace std;
ll f[N][N][N];
int head[N], nxt[N << 1], x[N << 1], to[N << 1], cnt;
void add(int r, int l, int lim) {
  nxt[++cnt] = head[r];
  to[cnt] = l;
  x[cnt] = lim;
  head[r] = cnt;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    add(r, l, x);
  }
  f[1][0][0] = 1;
  for (int r = 1; r <= n; r++) {
    for (int o = head[r]; o; o = nxt[o]) {
      int l = to[o];
      for (int i = 0; i < r; i++)
        for (int j = 0; j <= max(0, i - 1); j++) {
          if (x[o] == 1 && l <= i)
            f[r][i][j] = 0;
          if (x[o] == 2 && (i < l || l <= j))
            f[r][i][j] = 0;
          if (x[o] == 3 && j < l)
            f[r][i][j] = 0;
        }
    }
    for (int l = 0; l < r; l++)
      for (int i = 0; i <= max(0, l - 1); i++)
        if (f[r][l][i]) {
          f[r + 1][l][i] = (f[r + 1][l][i] + f[r][l][i]) % mod;
          f[r + 1][r][i] = (f[r + 1][r][i] + f[r][l][i]) % mod;
          f[r + 1][r][l] = (f[r + 1][r][l] + f[r][l][i]) % mod;
        }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= max(0, i - 1); j++)
      ans = (ans + f[n][i][j]) % mod;
  printf("%lld\n", ans * 3 % mod);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define mod 1000000007
#define ll long long
#define N 310
using namespace std;
ll f[N][N][N];
int head[N], nxt[N << 1], x[N << 1], to[N << 1], cnt;
void add(int r, int l, int lim) {
  nxt[++cnt] = head[r];
  to[cnt] = l;
  x[cnt] = lim;
  head[r] = cnt;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    add(r, l, x);
  }
  f[1][0][0] = 1;
  for (int r = 1; r <= n; r++) {
    for (int o = head[r]; o; o = nxt[o]) {
      int l = to[o];
      for (int i = 0; i < r; i++)
        for (int j = 0; j <= max(0, i - 1); j++) {
          if (x[o] == 1 && l <= i)
            f[r][i][j] = 0;
          if (x[o] == 2 && (i < l || l <= j))
            f[r][i][j] = 0;
          if (x[o] == 3 && j < l)
            f[r][i][j] = 0;
        }
    }
    for (int l = 0; l < r; l++)
      for (int i = 0; i <= max(0, l - 1); i++)
        if (f[r][l][i]) {
          f[r + 1][l][i] = (f[r + 1][l][i] + f[r][l][i]) % mod;
          f[r + 1][r][i] = (f[r + 1][r][i] + f[r][l][i]) % mod;
          f[r + 1][r][l] = (f[r + 1][r][l] + f[r][l][i]) % mod;
        }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= max(0, i - 1); j++)
      ans = (ans + f[n][i][j]) % mod;
  printf("%lld\n", ans * 3 % mod);
  return 0;
}
Problem p03717 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
const int MAXN = 330;
vector<pair<int, int>> vc[MAXN];
ll dp[MAXN][MAXN][MAXN];
bool check(int r, int g, int b) {
  int k = max(r, max(g, b));
  int len = vc[k].size();
  for (int i = 0; i < len; i++) {
    int l = vc[k][i].first, x = vc[k][i].second;
    int cnt = 0;
    if (r >= l)
      cnt++;
    if (g >= l)
      cnt++;
    if (b >= l)
      cnt++;
    if (cnt != x)
      return false;
  }
  return true;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    vc[r].push_back(make_pair(l, x));
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  ll ans = 0;
  for (int r = 0; r <= n; r++) {
    for (int g = 0; g <= n; g++) {
      for (int b = 0; b <= n; b++) {
        if (!dp[r][g][b])
          continue;
        if (!check(r, g, b)) {
          dp[r][g][b] = 0;
          continue;
        }
        int k = max(r, max(g, b));
        if (k == n)
          ans = (ans + dp[r][g][b]) % mod;
        dp[k + 1][g][b] += dp[r][g][b] % mod;
        dp[r][k + 1][b] += dp[r][g][b] % mod;
        dp[r][g][k + 1] += dp[r][g][b] % mod;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
const int MAXN = 305;
vector<pair<int, int>> vc[MAXN];
ll dp[MAXN][MAXN][MAXN];
bool check(int r, int g, int b) {
  int k = max(r, max(g, b));
  int len = vc[k].size();
  for (int i = 0; i < len; i++) {
    int l = vc[k][i].first, x = vc[k][i].second;
    int cnt = 0;
    if (r >= l)
      cnt++;
    if (g >= l)
      cnt++;
    if (b >= l)
      cnt++;
    if (cnt != x)
      return false;
  }
  return true;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    vc[r].push_back(make_pair(l, x));
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  ll ans = 0;
  for (int r = 0; r <= n; r++) {
    for (int g = 0; g <= n; g++) {
      for (int b = 0; b <= n; b++) {
        if (!dp[r][g][b])
          continue;
        if (!check(r, g, b)) {
          dp[r][g][b] = 0;
          continue;
        }
        int k = max(r, max(g, b));
        if (k == n)
          ans = (ans + dp[r][g][b]) % mod;
        dp[k + 1][g][b] += dp[r][g][b] % mod;
        dp[r][k + 1][b] += dp[r][g][b] % mod;
        dp[r][g][k + 1] += dp[r][g][b] % mod;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define sz(c) c.size()
#define all(c) c.begin(), c.end()

constexpr int MOD = 1e9 + 7;

int dp[301][301][301];

signed main() {
  int N, M;
  cin >> N >> M;
  unordered_map<int, vector<tuple<int, int, int>>> query;
  rep(i, 0, M) {
    int l, r, x;
    cin >> l >> r >> x;
    query[r].push_back(make_tuple(l, r, x));
  }

  dp[0][0][0] = 1;
  rep(r, 0, N + 1) {
    rep(g, 0, N + 1) {
      rep(b, 0, N + 1) {
        //                fprintf(stderr, "r:%lld, g:%lld, b:%lld\n", r, g, b);
        int mx = max({r, g, b});
        int nxt = mx + 1;
        for (auto &tpl : query[mx]) {
          int L, R, X;
          tie(L, R, X) = tpl;
          int cnt = 0;
          if (L <= r)
            cnt++;
          if (L <= g)
            cnt++;
          if (L <= b)
            cnt++;
          if (cnt != X)
            dp[r][g][b] = 0;
        }
        (dp[nxt][g][b] += dp[r][g][b]) %= MOD;
        (dp[r][nxt][b] += dp[r][g][b]) %= MOD;
        (dp[r][g][nxt] += dp[r][g][b]) %= MOD;
      }
    }
  }
  int ans = 0;
  rep(i, 0, N + 1) {
    rep(j, 0, N + 1) {
      (ans += dp[N][i][j]) %= MOD;
      (ans += dp[i][N][j]) %= MOD;
      (ans += dp[i][j][N]) %= MOD;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define sz(c) c.size()
#define all(c) c.begin(), c.end()

constexpr int MOD = 1e9 + 7;

int dp[310][310][310];

signed main() {
  int N, M;
  cin >> N >> M;
  unordered_map<int, vector<tuple<int, int, int>>> query;
  rep(i, 0, M) {
    int l, r, x;
    cin >> l >> r >> x;
    query[r].push_back(make_tuple(l, r, x));
  }

  dp[0][0][0] = 1;
  rep(r, 0, N + 1) {
    rep(g, 0, N + 1) {
      rep(b, 0, N + 1) {
        //                fprintf(stderr, "r:%lld, g:%lld, b:%lld\n", r, g, b);
        int mx = max({r, g, b});
        int nxt = mx + 1;
        for (auto &tpl : query[mx]) {
          int L, R, X;
          tie(L, R, X) = tpl;
          int cnt = 0;
          if (L <= r)
            cnt++;
          if (L <= g)
            cnt++;
          if (L <= b)
            cnt++;
          if (cnt != X)
            dp[r][g][b] = 0;
        }
        (dp[nxt][g][b] += dp[r][g][b]) %= MOD;
        (dp[r][nxt][b] += dp[r][g][b]) %= MOD;
        (dp[r][g][nxt] += dp[r][g][b]) %= MOD;
      }
    }
  }
  int ans = 0;
  rep(i, 0, N + 1) {
    rep(j, 0, N + 1) {
      (ans += dp[N][i][j]) %= MOD;
      (ans += dp[i][N][j]) %= MOD;
      (ans += dp[i][j][N]) %= MOD;
    }
  }
  cout << ans << endl;
}
Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second

using namespace std;

const int mod = 1e9 + 7;

int dp[2][35][35] = {};
vector<pair<int, int>> seg[35];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;

  while (m--) {
    int l, r, x;
    cin >> l >> r >> x;
    seg[r].emplace_back(l, x);
  }

  dp[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    int cur = i & 1, nxt = (i + 1) & 1;
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= j; k++) {

        for (auto s : seg[i]) {

          int cx = 1 + (j >= s.fi) + (k >= s.fi);

          // cout << dp[cur][j][k] << " " << j << " " << k << " " << cx << endl;

          dp[cur][j][k] *= (cx == s.se);
        }

        dp[nxt][j][k] = (dp[nxt][j][k] + dp[cur][j][k]) % mod;
        dp[nxt][i][k] = (dp[nxt][i][k] + dp[cur][j][k]) % mod;
        dp[nxt][i][j] = (dp[nxt][i][j] + dp[cur][j][k]) % mod;
      }
    }

    if (i < n)
      memset(dp[cur], 0, sizeof(dp[cur]));
  }

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      ans = (ans + dp[n & 1][i][j]) % mod;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second

using namespace std;

const int mod = 1e9 + 7;

int dp[2][305][305] = {};
vector<pair<int, int>> seg[305];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;

  while (m--) {
    int l, r, x;
    cin >> l >> r >> x;
    seg[r].emplace_back(l, x);
  }

  dp[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    int cur = i & 1, nxt = (i + 1) & 1;
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= j; k++) {

        for (auto s : seg[i]) {

          int cx = 1 + (j >= s.fi) + (k >= s.fi);

          // cout << dp[cur][j][k] << " " << j << " " << k << " " << cx << endl;

          dp[cur][j][k] *= (cx == s.se);
        }

        dp[nxt][j][k] = (dp[nxt][j][k] + dp[cur][j][k]) % mod;
        dp[nxt][i][k] = (dp[nxt][i][k] + dp[cur][j][k]) % mod;
        dp[nxt][i][j] = (dp[nxt][i][j] + dp[cur][j][k]) % mod;
      }
    }

    if (i < n)
      memset(dp[cur], 0, sizeof(dp[cur]));
  }

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      ans = (ans + dp[n & 1][i][j]) % mod;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 305, mod = 1e9 + 7;
inline int add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int dp[N][N][N], n, m, ans;
vector<int> l[N], x[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    l[b].push_back(a);
    x[b].push_back(c);
  }
  dp[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        int r = max(max(i, j), k);
        int &f = dp[i][j][k];
        for (int z = 0; z < l[r].size(); z++) {
          int s = (i >= l[r][z]) + (j >= l[r][z]) + (k >= l[r][z]);
          if (s != x[r][z])
            f = 0;
        }
        add(dp[r + 1][j][k], f);
        add(dp[i][r + 1][k], f);
        add(dp[i][j][r + 1], f);
        if (r == n)
          add(ans, f);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 305, mod = 1e9 + 7;
inline void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int dp[N][N][N], n, m, ans;
vector<int> l[N], x[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    l[b].push_back(a);
    x[b].push_back(c);
  }
  dp[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        int r = max(max(i, j), k);
        int &f = dp[i][j][k];
        for (int z = 0; z < l[r].size(); z++) {
          int s = (i >= l[r][z]) + (j >= l[r][z]) + (k >= l[r][z]);
          if (s != x[r][z])
            f = 0;
        }
        add(dp[r + 1][j][k], f);
        add(dp[i][r + 1][k], f);
        add(dp[i][j][r + 1], f);
        if (r == n)
          add(ans, f);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 301;

int n, m, dp[N][N][N], mod = 1e9 + 7;
vector<pair<int, int>> v[N];

void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
bool invalid(int i, int j, int k) {
  if (j < i)
    swap(i, j);
  if (k < i)
    swap(i, k);
  if (k < j)
    swap(j, k);
  for (auto &s : v[k]) {
    int cnt = 1;
    cnt += s.first <= i;
    cnt += s.first <= j;
    if (cnt != s.second)
      return 1;
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    v[r].push_back(make_pair(l, x));
  }
  dp[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        if (invalid(i, j, k))
          dp[i][j][k] = 0;
        int f = max(i, max(j, k)) + 1;
        add(dp[f][j][k], dp[i][j][k]);
        add(dp[i][f][k], dp[i][j][k]);
        add(dp[i][j][f], dp[i][j][k]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        if (i == n || j == n || k == n) {
          add(ans, dp[i][j][k]);
        }
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 305;

int n, m, dp[N][N][N], mod = 1e9 + 7;
vector<pair<int, int>> v[N];

void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
bool invalid(int i, int j, int k) {
  if (j < i)
    swap(i, j);
  if (k < i)
    swap(i, k);
  if (k < j)
    swap(j, k);
  for (auto &s : v[k]) {
    int cnt = 1;
    cnt += s.first <= i;
    cnt += s.first <= j;
    if (cnt != s.second)
      return 1;
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    v[r].push_back(make_pair(l, x));
  }
  dp[0][0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        if (invalid(i, j, k))
          dp[i][j][k] = 0;
        int f = max(i, max(j, k)) + 1;
        add(dp[f][j][k], dp[i][j][k]);
        add(dp[i][f][k], dp[i][j][k]);
        add(dp[i][j][f], dp[i][j][k]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        if (i == n || j == n || k == n) {
          add(ans, dp[i][j][k]);
        }
      }
    }
  }
  cout << ans << endl;
}