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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define foreach(c, itr)                                                        \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
using namespace std;
const int mod = 1e9 + 7;
int n, i, j, f[100005], inv[100005], ans;
int main() {
  cin >> n;
  inv[1] = f[1] = 1;
  for (i = 2; i <= n; i++) {
    inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
    f[i] = (f[i - 1] + inv[i]) % mod;
  }
  rep(i, n) {
    int x;
    cin >> x;
    ans = (ans + 1ll * x * (f[x] + f[n - x + 1] - 1)) % mod;
  }
  rep(i, n) { ans = 1ll * ans * i % mod; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define foreach(c, itr)                                                        \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
using namespace std;
const int mod = 1e9 + 7;
int n, i, j, f[100005], inv[100005], ans;
int main() {
  cin >> n;
  inv[1] = f[1] = 1;
  for (i = 2; i <= n; i++) {
    inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
    f[i] = (f[i - 1] + inv[i]) % mod;
  }
  rep(i, n) {
    int x;
    cin >> x;
    ans = (ans + 1ll * x * (f[i] + f[n - i + 1] - 1)) % mod;
  }
  rep(i, n) { ans = 1ll * ans * i % mod; }
  cout << ans << endl;
  return 0;
}
Problem p03232 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
long long read() {
  long long x = 0, w = 1;
  char ch = getchar();
  while (!isdigit(ch))
    w = ch == '-' ? -1 : 1, ch = getchar();
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * w;
}
const int Max_n = 1e5 + 5, mod = 1e9 + 7;
int n, ans;
int a[Max_n], inv[Max_n];
void Mod(int &x) { x %= mod; }
int main() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
  for (int i = 2; i <= n; i++)
    Mod(inv[i] += inv[i - 1]);
  for (int i = 1; i <= n; i++)
    Mod(ans += 1ll * a[i] * (inv[i] + inv[n - i + 1] - 1) % mod);
  for (int i = 2; i <= n; i++)
    ans = 1ll * ans * i % mod;
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
long long read() {
  long long x = 0, w = 1;
  char ch = getchar();
  while (!isdigit(ch))
    w = ch == '-' ? -1 : 1, ch = getchar();
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * w;
}
const int Max_n = 1e5 + 5, mod = 1e9 + 7;
int n, ans;
int a[Max_n], inv[Max_n];
void Mod(int &x) { x %= mod; }
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
  for (int i = 2; i <= n; i++)
    Mod(inv[i] += inv[i - 1]);
  for (int i = 1; i <= n; i++)
    Mod(ans += 1ll * a[i] * (inv[i] + inv[n - i + 1] - 1) % mod);
  for (int i = 2; i <= n; i++)
    ans = 1ll * ans * i % mod;
  cout << ans;
}
Problem p03232 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MN 1000
#define MOD 1000000007
int a[MN + 5], r[MN + 5];
inline int inv(int x) {
  int y = MOD - 2, r = 1;
  for (; y; y >>= 1, x = 1LL * x * x % MOD)
    if (y & 1)
      r = 1LL * r * x % MOD;
  return r;
}
int main() {
  int n, i, ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (i = 1; i <= n; ++i)
    r[i] = (r[i - 1] + inv(i)) % MOD;
  for (i = 1; i <= n; ++i)
    ans = (ans + 1LL * a[i] * (r[i] + r[n - i + 1] - 1)) % MOD;
  for (i = 1; i <= n; ++i)
    ans = 1LL * ans * i % MOD;
  printf("%d", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MN 100000
#define MOD 1000000007
int a[MN + 5], r[MN + 5];
inline int inv(int x) {
  int y = MOD - 2, r = 1;
  for (; y; y >>= 1, x = 1LL * x * x % MOD)
    if (y & 1)
      r = 1LL * r * x % MOD;
  return r;
}
int main() {
  int n, i, ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (i = 1; i <= n; ++i)
    r[i] = (r[i - 1] + inv(i)) % MOD;
  for (i = 1; i <= n; ++i)
    ans = (ans + 1LL * a[i] * (r[i] + r[n - i + 1] - 1)) % MOD;
  for (i = 1; i <= n; ++i)
    ans = 1LL * ans * i % MOD;
  printf("%d", ans);
}
Problem p03232 (C++)

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

const long long mod = 1e9 + 7;
int n;
long long a[10010], ni[100010], s[100010], ans, jie[100010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  ni[1] = 1;
  for (int i = 2; i <= n; ++i)
    ni[i] = (mod - mod / i) * ni[mod % i] % mod;
  for (int i = 1; i <= n; ++i)
    s[i] = (s[i - 1] + ni[i]) % mod;
  for (int i = 1; i <= n; ++i)
    ans = (ans + s[n - i + 1] * a[i] % mod + (s[i] - 1) * a[i] % mod) % mod;
  jie[0] = 1;
  for (int i = 1; i <= n; ++i)
    jie[i] = jie[i - 1] * i % mod;
  ans = (ans * jie[n]) % mod;
  printf("%lld\n", ans);
  return 0;
}

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

const long long mod = 1e9 + 7;
int n;
long long a[100010], ni[100010], s[100010], ans, jie[100010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  ni[1] = 1;
  for (int i = 2; i <= n; ++i)
    ni[i] = (mod - mod / i) * ni[mod % i] % mod;
  for (int i = 1; i <= n; ++i)
    s[i] = (s[i - 1] + ni[i]) % mod;
  for (int i = 1; i <= n; ++i)
    ans = (ans + s[n - i + 1] * a[i] % mod + (s[i] - 1) * a[i] % mod) % mod;
  jie[0] = 1;
  for (int i = 1; i <= n; ++i)
    jie[i] = jie[i - 1] * i % mod;
  ans = (ans * jie[n]) % mod;
  printf("%lld\n", ans);
  return 0;
}
Problem p03232 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = 0;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f ? x : -x;
}

typedef long long LL;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
int inv[maxn], a[maxn];

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif

  int n = read();
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= n; ++i)
    inv[i] = (LL)(mod - mod / i) * inv[mod % i] % mod;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 2; i <= n; ++i)
    (inv[i] += inv[i - 1]) %= mod;
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    (ans += (LL)a[i] * (inv[i] - 1 + inv[n - i + 1]) % mod) %= mod;
  for (int i = 1; i <= n; ++i)
    ans = (LL)ans * i % mod;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = 0;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f ? x : -x;
}

typedef long long LL;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
int inv[maxn], a[maxn];

int main() {

  int n = read();
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= n; ++i)
    inv[i] = (LL)(mod - mod / i) * inv[mod % i] % mod;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 2; i <= n; ++i)
    (inv[i] += inv[i - 1]) %= mod;
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    (ans += (LL)a[i] * (inv[i] - 1 + inv[n - i + 1]) % mod) %= mod;
  for (int i = 1; i <= n; ++i)
    ans = (LL)ans * i % mod;
  cout << ans << endl;

  return 0;
}
Problem p03233 (C++)

Buggy submission (status: Runtime Error):
/****************************************************************
 *	Author: huhao
 *	Email: 826538400@qq.com
 *	Create time: 2020-02-17 15:23:07
 ****************************************************************/
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define fr(i, a, b) for (int i = (a), end_##i = (b); i <= end_##i; i++)
#define fd(i, a, b) for (int i = (a), end_##i = (b); i >= end_##i; i--)
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
#define i64 long long
const int N = 100010;
i64 n, a[N], r[N], ans, vis[N], Ans, flag;
int main() {
  n = read();
  Ans = 1ll << 50;
  fr(i, 0, n - 1) {
    a[i * 2] = read();
    a[i * 2 + 1] = read();
  }
  fr(i, 0, n + n - 1) r[i] = i;
  std::sort(r + 1, r + n + n + 1, [&](int x, int y) { return a[x] < a[y]; });
  fr(i, 1, n) {
    ans += a[r[i]];
    vis[r[i] / 2]++;
    if (vis[r[i] / 2] == 2)
      Ans = 0;
    flag |= 1 << (r[i] % 2);
  }
  if (flag == 1 || flag == 2)
    Ans = 0;
  if (Ans) {
    fr(i, 1, n) Ans = std::min(
        Ans, a[r[i] / 2 == r[n + 1] / 2 ? r[n + 2] : r[n + 1]] - a[r[i]]);
  }
  printf("%lld\n", ans + Ans);
  return 0;
}

Fixed submission:
/****************************************************************
 *	Author: huhao
 *	Email: 826538400@qq.com
 *	Create time: 2020-02-17 15:23:07
 ****************************************************************/
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define fr(i, a, b) for (int i = (a), end_##i = (b); i <= end_##i; i++)
#define fd(i, a, b) for (int i = (a), end_##i = (b); i >= end_##i; i--)
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
#define i64 long long
const int N = 200010;
i64 n, a[N], r[N], ans, vis[N], Ans, flag;
int main() {
  n = read();
  Ans = 1ll << 50;
  fr(i, 0, n - 1) {
    a[i * 2] = read();
    a[i * 2 + 1] = read();
  }
  fr(i, 0, n + n - 1) r[i] = i;
  std::sort(r + 1, r + n + n + 1, [&](int x, int y) { return a[x] < a[y]; });
  fr(i, 1, n) {
    ans += a[r[i]];
    vis[r[i] / 2]++;
    if (vis[r[i] / 2] == 2)
      Ans = 0;
    flag |= 1 << (r[i] % 2);
  }
  if (flag == 1 || flag == 2)
    Ans = 0;
  if (Ans) {
    fr(i, 1, n) Ans = std::min(
        Ans, a[r[i] / 2 == r[n + 1] / 2 ? r[n + 2] : r[n + 1]] - a[r[i]]);
  }
  printf("%lld\n", ans + Ans);
  return 0;
}
Problem p03233 (C++)

Buggy submission (status: Runtime Error):
// In the Name of God

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(a) int(a.size())
#define all(v) v.begin(), v.end()
#define bpc(v) __builtin_popcountll(v)
#define itr iterator
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define ub upper_bound
#define lb lower_bound

const int N = 1e5 + 5;
const ll mod = 8;
const ll inf = 1e18 + 1;
const double eps = 1e-15;
const int pw = 257;

int n;
pair<int, int> p[N];
ll ans = inf, X, Y, res;
set<int> s;

int main() {
#ifdef Madi
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif

  cin >> n;
  rep(i, 1, n) {
    cin >> p[i].fi >> p[n + i].fi;
    X += p[i].fi;
    Y += p[n + i].fi;
    p[i].se = p[i + n].se = i;
  }

  sort(p + 1, p + 1 + n + n);
  rep(i, 1, n) s.insert(p[i].se);
  rep(i, 1, n) res += p[i].fi;

  if (sz(s) == n) {
    rep(i, n + 1, n + n) {
      if (p[n].se == p[i].se)
        ans = min(ans, res - p[n - 1].fi + p[i].fi);
      else
        ans = min(ans, res - p[n].fi + p[i].fi);
    }
  } else
    ans = res;

  cout << min(ans, min(X, Y));

#ifdef Madi
  cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}

Fixed submission:
// In the Name of God

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(a) int(a.size())
#define all(v) v.begin(), v.end()
#define bpc(v) __builtin_popcountll(v)
#define itr iterator
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define ub upper_bound
#define lb lower_bound

const int N = 2e5 + 5;
const ll mod = 8;
const ll inf = 1e18 + 1;
const double eps = 1e-15;
const int pw = 257;

int n;
pair<int, int> p[N];
ll ans = inf, X, Y, res;
set<int> s;

int main() {
#ifdef Madi
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif

  cin >> n;
  rep(i, 1, n) {
    cin >> p[i].fi >> p[n + i].fi;
    X += p[i].fi;
    Y += p[n + i].fi;
    p[i].se = p[i + n].se = i;
  }

  sort(p + 1, p + 1 + n + n);
  rep(i, 1, n) s.insert(p[i].se);
  rep(i, 1, n) res += p[i].fi;

  if (sz(s) == n) {
    rep(i, n + 1, n + n) {
      if (p[n].se == p[i].se)
        ans = min(ans, res - p[n - 1].fi + p[i].fi);
      else
        ans = min(ans, res - p[n].fi + p[i].fi);
    }
  } else
    ans = res;

  cout << min(ans, min(X, Y));

#ifdef Madi
  cerr << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
Problem p03233 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>

#define int long long

const int Maxv = 100010;

int sum[Maxv], val[Maxv], a[Maxv], b[Maxv], suma, sumb, ans;

inline char fgc() {
  static char buf[1 << 15], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 15, stdin), p1 == p2)
             ? EOF
             : *p1++;
}

inline int read() {
  static int x, ch;
  x = 0, ch = fgc();
  while (ch < '0' || ch > '9')
    ch = fgc();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = fgc();
  return x;
}

signed main() {
  int n = read();

  for (int i = 1; i <= n; i++) {
    a[i] = read();
    suma += a[i];
    b[i] = read();
    sumb += b[i];
    val[(i << 1) - 1] = a[i];
    val[i << 1] = b[i];
  }

  ans = std::min(suma, sumb);
  std::sort(val + 1, val + (n << 1) + 1);

  for (int i = 1; i <= (n << 1); i++)
    sum[i] += sum[i - 1] + val[i];

  for (int i = 1; i <= n; i++) {
    int idx = n, tmp = 0;
    if (a[i] <= val[idx - 1])
      idx++;
    else
      tmp += a[i];
    if (b[i] <= val[idx - 1])
      idx++;
    else
      tmp += b[i];

    ans = std::min(ans, tmp + sum[idx - 2]);
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>

#define int long long

const int Maxv = 300010;

int sum[Maxv], val[Maxv], a[Maxv], b[Maxv], suma, sumb, ans;

inline char fgc() {
  static char buf[1 << 15], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 15, stdin), p1 == p2)
             ? EOF
             : *p1++;
}

inline int read() {
  static int x, ch;
  x = 0, ch = fgc();
  while (ch < '0' || ch > '9')
    ch = fgc();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = fgc();
  return x;
}

signed main() {
  int n = read();

  for (int i = 1; i <= n; i++) {
    a[i] = read();
    suma += a[i];
    b[i] = read();
    sumb += b[i];
    val[(i << 1) - 1] = a[i];
    val[i << 1] = b[i];
  }

  ans = std::min(suma, sumb);
  std::sort(val + 1, val + (n << 1) + 1);

  for (int i = 1; i <= (n << 1); i++)
    sum[i] += sum[i - 1] + val[i];

  for (int i = 1; i <= n; i++) {
    int idx = n, tmp = 0;
    if (a[i] <= val[idx - 1])
      idx++;
    else
      tmp += a[i];
    if (b[i] <= val[idx - 1])
      idx++;
    else
      tmp += b[i];

    ans = std::min(ans, tmp + sum[idx - 2]);
  }

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

  return 0;
}
Problem p03233 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;

typedef long long ll;
typedef pair<ll, int> pli;
const int N = 1e5 + 10;
int n, a[N], b[N];
ll tot, suma, sumb, sum, res;
pli all[N];
bool vis[N];

int main() {
  cin >> n;
  rep(i, 1, n) {
    scanf("%d%d", &a[i], &b[i]);
    tot += a[i] + b[i];
    suma += a[i], sumb += b[i];
    all[i << 1] = make_pair(a[i], i);
    all[i << 1 | 1] = make_pair(b[i], i);
  }
  sort(all + 1, all + (n << 1) + 2, greater<pli>());
  bool ff = 0;
  res = max(suma, sumb);
  rep(i, 1, n) {
    if (vis[all[i].second]) {
      ff = 1;
    }
    vis[all[i].second] = 1;
    sum += all[i].first;
  }
  if (ff) {
    res = max(res, sum);
  } else {
    rep(i, n + 1, n << 1) {
      if (all[n].second == all[i].second)
        res = max(res, sum + all[i].first - all[n - 1].first);
      else
        res = max(res, sum + all[i].first - all[n].first);
    }
  }
  printf("%lld\n", tot - res);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;

typedef long long ll;
typedef pair<ll, int> pli;
const int N = 2e5 + 10;
int n, a[N], b[N];
ll tot, suma, sumb, sum, res;
pli all[N];
bool vis[N];

int main() {
  cin >> n;
  rep(i, 1, n) {
    scanf("%d%d", &a[i], &b[i]);
    tot += a[i] + b[i];
    suma += a[i], sumb += b[i];
    all[i << 1] = make_pair(a[i], i);
    all[i << 1 | 1] = make_pair(b[i], i);
  }
  sort(all + 1, all + (n << 1) + 2, greater<pli>());
  bool ff = 0;
  res = max(suma, sumb);
  rep(i, 1, n) {
    if (vis[all[i].second]) {
      ff = 1;
    }
    vis[all[i].second] = 1;
    sum += all[i].first;
  }
  if (ff) {
    res = max(res, sum);
  } else {
    rep(i, n + 1, n << 1) {
      if (all[n].second == all[i].second)
        res = max(res, sum + all[i].first - all[n - 1].first);
      else
        res = max(res, sum + all[i].first - all[n].first);
    }
  }
  printf("%lld\n", tot - res);
  return 0;
}
Problem p03233 (C++)

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

struct T {
  ll value;
  int id;
  bool isB;
};

int main(void) {
  int n;
  cin >> n;
  vector<T> t;

  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    t.push_back({a, i, false});
    t.push_back({b, i, true});
  }

  sort(all(t), [](T x, T y) {
    return x.value != y.value ? x.value < y.value : x.id < y.id;
  });
  vector<vector<bool>> low(n, {0, 0});
  rep(i, n) { low[t[i].id][t[i].isB] = true; }

  int HH = 0, LL = 0, HL = 0, LH = 0;
  ll sum = 0;
  rep(i, n) {
    if (low[i][0] && low[i][1])
      LL++;
    if (!low[i][0] && !low[i][1])
      HH++;
    if (!low[i][0] && low[i][1])
      HL++;
    if (low[i][0] && !low[i][1])
      LH++;
    sum += t[i].value;
  }

  if (LL > 0 || LH == n || HL == n) {
    cout << sum << endl;
  } else if (t[n].id != t[n - 1].id) {
    assert(false);
    cout << sum - t[n - 1].value + t[n].value << endl;
  } else if (sum - t[n - 2].value + t[n].value <
             sum - t[n - 1].value + t[n + 1].value) {
    cout << sum - t[n - 2].value + t[n].value << endl;
  } else {
    cout << sum - t[n - 1].value + t[n + 1].value << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;

struct T {
  ll value;
  int id;
  bool isB;
};

int main(void) {
  int n;
  cin >> n;
  vector<T> t;

  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    t.push_back({a, i, false});
    t.push_back({b, i, true});
  }

  sort(all(t), [](T x, T y) {
    return x.value != y.value ? x.value < y.value : x.id < y.id;
  });
  vector<vector<bool>> low(n, {0, 0});
  rep(i, n) { low[t[i].id][t[i].isB] = true; }

  int HH = 0, LL = 0, HL = 0, LH = 0;
  ll sum = 0;
  rep(i, n) {
    if (low[i][0] && low[i][1])
      LL++;
    if (!low[i][0] && !low[i][1])
      HH++;
    if (!low[i][0] && low[i][1])
      HL++;
    if (low[i][0] && !low[i][1])
      LH++;
    sum += t[i].value;
  }

  if (LL > 0 || LH == n || HL == n) {
    cout << sum << endl;
  } else if (t[n].id != t[n - 1].id) {
    cout << sum - t[n - 1].value + t[n].value << endl;
  } else if (sum - t[n - 2].value + t[n].value <
             sum - t[n - 1].value + t[n + 1].value) {
    cout << sum - t[n - 2].value + t[n].value << endl;
  } else {
    cout << sum - t[n - 1].value + t[n + 1].value << endl;
  }

  return 0;
}
Problem p03233 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PI;
const LL N = 100005;
LL n;
LL a[N], b[N];
LL ans = 0, sum = 0;
PI c[N];
LL tot;
bool cmp(PI x, PI y) { return x.first > y.first; }
bool in[N];
int main() {
  scanf("%lld", &n);
  for (LL u = 1; u <= n; u++) {
    scanf("%lld%lld", &a[u], &b[u]);
    ans = ans + a[u] + b[u];
    sum = sum + a[u];
    c[++tot] = {a[u], u};
    c[++tot] = {b[u], u};
  }
  sort(c + 1, c + 1 + tot, cmp);
  LL mx = 0;
  mx = max(sum, ans - sum);
  sum = 0;
  bool tf = false;
  memset(in, false, sizeof(in));
  for (LL u = 1; u <= n; u++) {
    if (in[c[u].second])
      tf = true;
    sum = sum + c[u].first;
    in[c[u].second] = true;
  }
  if (tf)
    mx = max(mx, sum);
  else {
    for (LL i = n + 1; i <= tot; i++) {
      if (c[i].second != c[n].second)
        mx = max(mx, sum + c[i].first - c[n].first);
      else
        mx = max(mx, sum + c[i].first - c[n - 1].first);
    }
  }
  printf("%lld\n", ans - mx);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PI;
const LL N = 200005;
LL n;
LL a[N], b[N];
LL ans = 0, sum = 0;
PI c[N];
LL tot;
bool cmp(PI x, PI y) { return x.first > y.first; }
bool in[N];
int main() {
  scanf("%lld", &n);
  for (LL u = 1; u <= n; u++) {
    scanf("%lld%lld", &a[u], &b[u]);
    ans = ans + a[u] + b[u];
    sum = sum + a[u];
    c[++tot] = {a[u], u};
    c[++tot] = {b[u], u};
  }
  sort(c + 1, c + 1 + tot, cmp);
  LL mx = 0;
  mx = max(sum, ans - sum);
  sum = 0;
  bool tf = false;
  memset(in, false, sizeof(in));
  for (LL u = 1; u <= n; u++) {
    if (in[c[u].second])
      tf = true;
    sum = sum + c[u].first;
    in[c[u].second] = true;
  }
  if (tf)
    mx = max(mx, sum);
  else {
    for (LL i = n + 1; i <= tot; i++) {
      if (c[i].second != c[n].second)
        mx = max(mx, sum + c[i].first - c[n].first);
      else
        mx = max(mx, sum + c[i].first - c[n - 1].first);
    }
  }
  printf("%lld\n", ans - mx);
  return 0;
}
Problem p03233 (C++)

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

typedef long long LL;
const int N = 100005;

int n;
bool vis[N];
LL A, B, ans;
std::pair<int, int> a[N];

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n;
  for (int i = 0, x, y; i < n; ++i)
    std::cin >> x >> y, a[i] = {x, i}, a[i + n] = {y, i}, A += x, B += y;
  ans = std::min(A, B), std::sort(a, a + n + n);
  for (int i = 0; i < n; ++i)
    vis[a[i].second] = 1;
  LL sum = 0;
  for (int i = 0; i < n; ++i)
    sum += a[i].first;
  for (int i = 0; i < n; ++i)
    if (!vis[i])
      return std::cout << std::min(ans, sum) << '\n', 0;
  if (a[n - 1].second != a[n].second)
    std::cout << std::min(ans, sum - a[n - 1].first + a[n].first) << '\n';
  else
    std::cout << std::min(ans, sum + std::min(a[n].first - a[n - 2].first,
                                              a[n + 1].first - a[n - 1].first))
              << '\n';
  return 0;
}

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

typedef long long LL;
const int N = 200005;

int n;
bool vis[N];
LL A, B, ans;
std::pair<int, int> a[N];

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n;
  for (int i = 0, x, y; i < n; ++i)
    std::cin >> x >> y, a[i] = {x, i}, a[i + n] = {y, i}, A += x, B += y;
  ans = std::min(A, B), std::sort(a, a + n + n);
  for (int i = 0; i < n; ++i)
    vis[a[i].second] = 1;
  LL sum = 0;
  for (int i = 0; i < n; ++i)
    sum += a[i].first;
  for (int i = 0; i < n; ++i)
    if (!vis[i])
      return std::cout << std::min(ans, sum) << '\n', 0;
  if (a[n - 1].second != a[n].second)
    std::cout << std::min(ans, sum - a[n - 1].first + a[n].first) << '\n';
  else
    std::cout << std::min(ans, sum + std::min(a[n].first - a[n - 2].first,
                                              a[n + 1].first - a[n - 1].first))
              << '\n';
  return 0;
}
Problem p03233 (C++)

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

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define pb push_back
#define fi first
#define se second
#define int long long

typedef pair<int, int> ii;
typedef pair<ii, int> pii;
const int N = 4e5 + 10;
int n, suma, sumb;
int a[N], b[N], pre[N], posa[N], posb[N];
vector<pii> tmp;

signed main() {
  freopen("test.inp", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  FOR(i, 1, n) {
    cin >> a[i] >> b[i];
    tmp.pb({{a[i], i}, 0});
    tmp.pb({{b[i], i}, 1});
    suma += a[i];
    sumb += b[i];
  }
  int ans = min(suma, sumb);
  sort(tmp.begin(), tmp.end());
  FOR(i, 0, tmp.size() - 1) {
    if (tmp[i].se == 0)
      posa[tmp[i].fi.se] = i;
    else
      posb[tmp[i].fi.se] = i;
    pre[i] = tmp[i].fi.fi;
    if (i > 0)
      pre[i] += pre[i - 1];
  }
  FOR(i, 1, n) {
    int sum = pre[n - 1];
    int cur = n - 1;
    if (posa[i] > cur) {
      if (tmp[cur].fi.se != i) {
        sum -= tmp[cur].fi.fi;
        sum += a[i];
        cur--;
      } else {
        sum -= tmp[cur - 1].fi.fi;
        sum += a[i];
      }
    }
    if (posb[i] > cur) {
      if (tmp[cur].fi.se != i) {
        sum -= tmp[cur].fi.fi;
        sum += b[i];
      } else {
        sum -= tmp[cur - 1].fi.fi;
        sum += b[i];
      }
    }
    ans = min(ans, sum);
  }
  cout << ans;
}


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

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define pb push_back
#define fi first
#define se second
#define int long long

typedef pair<int, int> ii;
typedef pair<ii, int> pii;
const int N = 4e5 + 10;
int n, suma, sumb;
int a[N], b[N], pre[N], posa[N], posb[N];
vector<pii> tmp;

signed main() {
  // freopen("test.inp", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  FOR(i, 1, n) {
    cin >> a[i] >> b[i];
    tmp.pb({{a[i], i}, 0});
    tmp.pb({{b[i], i}, 1});
    suma += a[i];
    sumb += b[i];
  }
  int ans = min(suma, sumb);
  sort(tmp.begin(), tmp.end());
  FOR(i, 0, tmp.size() - 1) {
    if (tmp[i].se == 0)
      posa[tmp[i].fi.se] = i;
    else
      posb[tmp[i].fi.se] = i;
    pre[i] = tmp[i].fi.fi;
    if (i > 0)
      pre[i] += pre[i - 1];
  }
  FOR(i, 1, n) {
    int sum = pre[n - 1];
    int cur = n - 1;
    if (posa[i] > cur) {
      if (tmp[cur].fi.se != i) {
        sum -= tmp[cur].fi.fi;
        sum += a[i];
        cur--;
      } else {
        sum -= tmp[cur - 1].fi.fi;
        sum += a[i];
      }
    }
    if (posb[i] > cur) {
      if (tmp[cur].fi.se != i) {
        sum -= tmp[cur].fi.fi;
        sum += b[i];
      } else {
        sum -= tmp[cur - 1].fi.fi;
        sum += b[i];
      }
    }
    ans = min(ans, sum);
  }
  cout << ans;
}
Problem p03234 (C++)

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

using ll = long long;

const int N = 305;
const int MOD = 1e9 + 7;

int to[N], sum[N];
ll dp[N][N], g[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    to[u] = v;
    to[v] = u;
    sum[u]++;
    sum[v]++;
  }
  n *= 2;
  g[0] = 1;
  for (int i = 1; i <= n; i++)
    sum[i] += sum[i - 1];
  for (int i = 2; i <= n; i += 2)
    g[i] = g[i - 2] * (i - 1) % MOD;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j += 2) {
      bool flag = 0;
      for (int k = i; k <= j; k++)
        if (to[k] && (to[k] < i || to[k] > j))
          flag = 1;
      if (flag)
        continue;
      dp[i][j] = g[j - i + 1 - sum[j] + sum[i - 1]];
      for (int k = i + 1; k < j; k += 2)
        (dp[i][j] -= dp[i][k] * g[j - k - sum[j] + sum[k]]) %= MOD;
    }
  m *= 2;
  ll res = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j += 2)
      (res += dp[i][j] * g[n - j + i - 1 - m + sum[j] - sum[i - 1]]) %= MOD;
  cout << (res + MOD) % MOD << '\n';
}

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

using ll = long long;

const int N = 605;
const int MOD = 1e9 + 7;

int to[N], sum[N];
ll dp[N][N], g[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    to[u] = v;
    to[v] = u;
    sum[u]++;
    sum[v]++;
  }
  n *= 2;
  g[0] = 1;
  for (int i = 1; i <= n; i++)
    sum[i] += sum[i - 1];
  for (int i = 2; i <= n; i += 2)
    g[i] = g[i - 2] * (i - 1) % MOD;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j += 2) {
      bool flag = 0;
      for (int k = i; k <= j; k++)
        if (to[k] && (to[k] < i || to[k] > j))
          flag = 1;
      if (flag)
        continue;
      dp[i][j] = g[j - i + 1 - sum[j] + sum[i - 1]];
      for (int k = i + 1; k < j; k += 2)
        (dp[i][j] -= dp[i][k] * g[j - k - sum[j] + sum[k]]) %= MOD;
    }
  m *= 2;
  ll res = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j += 2)
      (res += dp[i][j] * g[n - j + i - 1 - m + sum[j] - sum[i - 1]]) %= MOD;
  cout << (res + MOD) % MOD << '\n';
}
Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int f[310], s[310], R[310], lnk[310], F[310][310];
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n = gi() * 2, k = gi(), A, B;
  for (int i = 1; i <= n; ++i)
    s[i] = 1;
  while (k--) {
    A = gi(), B = gi();
    if (A > B)
      std::swap(A, B);
    s[A] = s[B] = 0;
    R[A] = B;
    lnk[A] = B, lnk[B] = A;
  }
  for (int i = 1; i <= n; ++i)
    s[i] += s[i - 1];
  int ans = 0;
  f[0] = 1;
  for (int i = 2; i <= n; i += 2)
    f[i] = 1ll * f[i - 2] * (i - 1) % mod;
  for (int sz = 2; sz <= n; ++sz) {
    for (int l = 1, r = sz; r <= n; ++l, ++r) {
      if (R[l] && R[l] > r)
        continue;
      bool flg = 0;
      for (int i = l; i <= r; ++i)
        if (lnk[i] && (lnk[i] < l || lnk[i] > r)) {
          flg = 1;
          break;
        }
      if (flg)
        continue;
      F[l][r] = f[s[r] - s[l - 1]];
      for (int i = R[l]; i < r; ++i)
        F[l][r] = (F[l][r] - 1ll * F[l][i] * f[s[r] - s[i]] % mod + mod) % mod;
      ans = (ans + 1ll * F[l][r] * f[s[n] - s[r] + s[l - 1]]) % mod;
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int f[610], s[610], R[610], lnk[610], F[610][610];
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n = gi() * 2, k = gi(), A, B;
  for (int i = 1; i <= n; ++i)
    s[i] = 1;
  while (k--) {
    A = gi(), B = gi();
    if (A > B)
      std::swap(A, B);
    s[A] = s[B] = 0;
    R[A] = B;
    lnk[A] = B, lnk[B] = A;
  }
  for (int i = 1; i <= n; ++i)
    s[i] += s[i - 1];
  int ans = 0;
  f[0] = 1;
  for (int i = 2; i <= n; i += 2)
    f[i] = 1ll * f[i - 2] * (i - 1) % mod;
  for (int sz = 2; sz <= n; ++sz) {
    for (int l = 1, r = sz; r <= n; ++l, ++r) {
      if (R[l] && R[l] > r)
        continue;
      bool flg = 0;
      for (int i = l; i <= r; ++i)
        if (lnk[i] && (lnk[i] < l || lnk[i] > r)) {
          flg = 1;
          break;
        }
      if (flg)
        continue;
      F[l][r] = f[s[r] - s[l - 1]];
      for (int i = R[l]; i < r; ++i)
        F[l][r] = (F[l][r] - 1ll * F[l][i] * f[s[r] - s[i]] % mod + mod) % mod;
      ans = (ans + 1ll * F[l][r] * f[s[n] - s[r] + s[l - 1]]) % mod;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e2, mod = 1e9 + 7;
ll n, m, k, f[maxn][maxn], g[maxn] = {1}, match[maxn], vis[maxn], pre[maxn],
                           ans, u, v;
signed main() {
  scanf("%lld%lld", &n, &m), n <<= 1;
  for (ll i = 1; i <= m; ++i) {
    scanf("%lld%lld", &u, &v);
    vis[match[u] = v] = vis[match[v] = u] = 1;
  }
  for (ll i = 1; i <= n; ++i)
    pre[i] = pre[i - 1] + (!vis[i]);
  for (ll i = 2; i <= n; i += 2)
    g[i] = g[i - 2] * (i - 1) % mod;
  for (ll i = 2; i <= n; i += 2)
    for (ll l = 1; l <= n - i + 1; ++l) {
      ll r = l + i - 1;
      for (ll j = l; j <= r; ++j)
        if (match[j] && (match[j] < l || match[j] > r))
          goto end;
      f[l][r] = g[pre[r] - pre[l - 1]];
      for (ll j = l + 1; j < r; j += 2)
        (f[l][r] += (mod - f[l][j] * g[pre[r] - pre[j]] % mod)) %= mod;
      (ans += g[(pre[n] - pre[r] + pre[l - 1])] * f[l][r] % mod) %= mod;
    end:;
    }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3, mod = 1e9 + 7;
ll n, m, k, f[maxn][maxn], g[maxn] = {1}, match[maxn], vis[maxn], pre[maxn],
                           ans, u, v;
signed main() {
  scanf("%lld%lld", &n, &m), n <<= 1;
  for (ll i = 1; i <= m; ++i) {
    scanf("%lld%lld", &u, &v);
    vis[match[u] = v] = vis[match[v] = u] = 1;
  }
  for (ll i = 1; i <= n; ++i)
    pre[i] = pre[i - 1] + (!vis[i]);
  for (ll i = 2; i <= n; i += 2)
    g[i] = g[i - 2] * (i - 1) % mod;
  for (ll i = 2; i <= n; i += 2)
    for (ll l = 1; l <= n - i + 1; ++l) {
      ll r = l + i - 1;
      for (ll j = l; j <= r; ++j)
        if (match[j] && (match[j] < l || match[j] > r))
          goto end;
      f[l][r] = g[pre[r] - pre[l - 1]];
      for (ll j = l + 1; j < r; j += 2)
        (f[l][r] += (mod - f[l][j] * g[pre[r] - pre[j]] % mod)) %= mod;
      (ans += g[(pre[n] - pre[r] + pre[l - 1])] * f[l][r] % mod) %= mod;
    end:;
    }
  printf("%lld\n", ans);
  return 0;
}
Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
const int MaxN = 310, mod = 1000000007;
int N, K, to[MaxN], pre[MaxN], f[MaxN * 2][MaxN * 2], fac[MaxN];
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 0, a, b; i < K; i++)
    scanf("%d%d", &a, &b), to[a] = b, to[b] = a;
  for (int i = 1; i <= N * 2; i++)
    pre[i] = pre[i - 1] + !to[i];
  fac[0] = 1;
  for (int i = 2; i <= N * 2; i++)
    fac[i] = 1ll * (i - 1) * fac[i - 2] % mod;
  f[N * 2 + 1][N * 2] = 1;
  int ans = 0;
  for (int l = N * 2; l; l--) {
    int mn = 1 << 30, mx = -1, c = 0;
    f[l][l - 1] = 1;
    for (int r = l; r <= N * 2; r++) {
      if (!to[r])
        c++;
      else {
        if (to[r] < mn)
          mn = to[r];
        if (to[r] > mx)
          mx = to[r];
      }
      // printf("l=%d r=%d mn=%d mx=%d c=%d\n",l,r,mn,mx,c);
      if ((r - l & 1) && mn >= l && mx <= r && c % 2 == 0) {
        f[l][r] = fac[c];
        for (int m = l, ml = 1 << 30, mr = -1, d = 0; m < r - 1; m++) {
          if (!to[m])
            d++;
          else {
            if (to[m] < ml)
              ml = to[m];
            if (to[m] > mr)
              mr = to[m];
          }
          if ((m - l & 1) && ml >= l && mr <= m && d % 2 == 0)
            f[l][r] = (f[l][r] + 1ll * (mod - f[m + 1][r]) * fac[d]) % mod;
        }
        ans = (ans + 1ll * f[l][r] * fac[pre[N * 2] - c]) % mod;
        // printf("f(%d,%d)=%d\n",l,r,f[l][r]);
      }
    }
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <cstdio>
const int MaxN = 310, mod = 1000000007;
int N, K, to[MaxN * 2], pre[MaxN * 2], f[MaxN * 2][MaxN * 2], fac[MaxN * 2];
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 0, a, b; i < K; i++)
    scanf("%d%d", &a, &b), to[a] = b, to[b] = a;
  for (int i = 1; i <= N * 2; i++)
    pre[i] = pre[i - 1] + !to[i];
  fac[0] = 1;
  for (int i = 2; i <= N * 2; i++)
    fac[i] = 1ll * (i - 1) * fac[i - 2] % mod;
  f[N * 2 + 1][N * 2] = 1;
  int ans = 0;
  for (int l = N * 2; l; l--) {
    int mn = 1 << 30, mx = -1, c = 0;
    f[l][l - 1] = 1;
    for (int r = l; r <= N * 2; r++) {
      if (!to[r])
        c++;
      else {
        if (to[r] < mn)
          mn = to[r];
        if (to[r] > mx)
          mx = to[r];
      }
      // printf("l=%d r=%d mn=%d mx=%d c=%d\n",l,r,mn,mx,c);
      if ((r - l & 1) && mn >= l && mx <= r && c % 2 == 0) {
        f[l][r] = fac[c];
        for (int m = l, ml = 1 << 30, mr = -1, d = 0; m < r - 1; m++) {
          if (!to[m])
            d++;
          else {
            if (to[m] < ml)
              ml = to[m];
            if (to[m] > mr)
              mr = to[m];
          }
          if ((m - l & 1) && ml >= l && mr <= m && d % 2 == 0)
            f[l][r] = (f[l][r] + 1ll * (mod - f[m + 1][r]) * fac[d]) % mod;
        }
        ans = (ans + 1ll * f[l][r] * fac[pre[N * 2] - c]) % mod;
        // printf("f(%d,%d)=%d\n",l,r,f[l][r]);
      }
    }
  }
  printf("%d\n", ans);
}
Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define to edge[i].v
#define mp make_pair
#define rint register int
#define debug(x) cerr << #x << "=" << x << endl
#define fgx cerr << "-------------" << endl
#define N 333
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
int go[N], mx[N][N], mn[N][N], s[N], f[N][N], g[N];
int main() {
  int n, k, x, y, ans = 0;
  cin >> n >> k;
  n *= 2;
  for (rint i = 1; i <= k; i++)
    scanf("%d%d", &x, &y), go[x] = y, go[y] = x;
  for (rint i = 1; i <= n; i++)
    s[i] = s[i - 1] + (!go[i]);
  for (rint i = 1; i <= n; i++) {
    mx[i][i - 1] = 0;
    mn[i][i - 1] = 1e9;
    for (rint j = i; j <= n; j++) {
      mx[i][j] = mx[i][j - 1];
      mn[i][j] = mn[i][j - 1];
      if (go[j])
        mx[i][j] = max(mx[i][j], go[j]), mn[i][j] = min(mn[i][j], go[j]);
    }
  }
  g[0] = 1;
  for (rint i = 2; i <= n; i++)
    g[i] = (ll)g[i - 2] * (i - 1) % mod;
  for (rint i = 2; i <= n; i += 2) {
    for (rint l = 1; l + i - 1 <= n; l++) {
      int r = l + i - 1;
      if (mn[l][r] < l || mx[l][r] > r)
        continue;
      f[l][r] = g[s[r] - s[l - 1]];
      for (rint k = l + 1; k < r; k += 2)
        f[l][r] = (f[l][r] - (ll)f[l][k] * g[s[r] - s[k]] % mod + mod) % mod;
      (ans += (ll)f[l][r] * g[s[l - 1] + s[n] - s[r]] % mod) %= mod;
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define to edge[i].v
#define mp make_pair
#define rint register int
#define debug(x) cerr << #x << "=" << x << endl
#define fgx cerr << "-------------" << endl
#define N 666
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
int go[N], mx[N][N], mn[N][N], s[N], f[N][N], g[N];
int main() {
  int n, k, x, y, ans = 0;
  cin >> n >> k;
  n *= 2;
  for (rint i = 1; i <= k; i++)
    scanf("%d%d", &x, &y), go[x] = y, go[y] = x;
  for (rint i = 1; i <= n; i++)
    s[i] = s[i - 1] + (!go[i]);
  for (rint i = 1; i <= n; i++) {
    mx[i][i - 1] = 0;
    mn[i][i - 1] = 1e9;
    for (rint j = i; j <= n; j++) {
      mx[i][j] = mx[i][j - 1];
      mn[i][j] = mn[i][j - 1];
      if (go[j])
        mx[i][j] = max(mx[i][j], go[j]), mn[i][j] = min(mn[i][j], go[j]);
    }
  }
  g[0] = 1;
  for (rint i = 2; i <= n; i++)
    g[i] = (ll)g[i - 2] * (i - 1) % mod;
  for (rint i = 2; i <= n; i += 2) {
    for (rint l = 1; l + i - 1 <= n; l++) {
      int r = l + i - 1;
      if (mn[l][r] < l || mx[l][r] > r)
        continue;
      f[l][r] = g[s[r] - s[l - 1]];
      for (rint k = l + 1; k < r; k += 2)
        f[l][r] = (f[l][r] - (ll)f[l][k] * g[s[r] - s[k]] % mod + mod) % mod;
      (ans += (ll)f[l][r] * g[s[l - 1] + s[n] - s[r]] % mod) %= mod;
    }
  }
  cout << ans;
  return 0;
}
Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 510
#define ll long long
#define mod 1000000007
using namespace std;
int link[N], tot[N][N];
ll f[N][N], h[N << 1];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  n <<= 1;
  for (int i = 1; i <= k; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    link[u] = v;
    link[v] = u;
  }
  h[0] = 1, h[1] = 0;
  for (int i = 2; i <= n; i += 2)
    h[i] = h[i - 2] * (i - 1) % mod;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++)
      tot[i][j] = tot[i][j - 1] + (!link[j]);
  }
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++) {
      bool have = false;
      for (int l = i; l <= j; l++)
        if (link[l] && (link[l] > j || link[l] < i))
          have = true;
      if (have)
        continue;
      f[i][j] = h[tot[i][j]];
      for (int l = i + 1; l <= j - 1; l++)
        f[i][j] = (f[i][j] - f[i][l] * h[tot[l + 1][j]] % mod + mod) % mod;
    }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++)
      ans = (ans + f[i][j] * h[n - k * 2 - tot[i][j]] % mod) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 610
#define ll long long
#define mod 1000000007
using namespace std;
int link[N], tot[N][N];
ll f[N][N], h[N << 1];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  n <<= 1;
  for (int i = 1; i <= k; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    link[u] = v;
    link[v] = u;
  }
  h[0] = 1, h[1] = 0;
  for (int i = 2; i <= n; i += 2)
    h[i] = h[i - 2] * (i - 1) % mod;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++)
      tot[i][j] = tot[i][j - 1] + (!link[j]);
  }
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++) {
      bool have = false;
      for (int l = i; l <= j; l++)
        if (link[l] && (link[l] > j || link[l] < i))
          have = true;
      if (have)
        continue;
      f[i][j] = h[tot[i][j]];
      for (int l = i + 1; l <= j - 1; l++)
        f[i][j] = (f[i][j] - f[i][l] * h[tot[l + 1][j]] % mod + mod) % mod;
    }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++)
      ans = (ans + f[i][j] * h[n - k * 2 - tot[i][j]] % mod) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03234 (C++)

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

#define M 1000000007
#define maxn 303

using namespace std;
typedef long long ll;

inline ll add(ll a, ll b) { return a + b < M ? a + b : a + b - M; }
inline ll sub(ll a, ll b) { return a - b < 0 ? a - b + M : a - b; }
inline ll mult(ll a, ll b) { return a * b % M; }

int n, k;
int a[maxn], b[maxn];

ll d[maxn][maxn];
int s[maxn][maxn];

ll g[maxn];

int main() {
#define TASK "ABC"
  // freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
  ios_base::sync_with_stdio(0);

  cin >> n >> k;
  n *= 2;

  for (int i = 0; i < k; ++i) {
    int a, b;
    cin >> a >> b;
    ::a[i] = min(a, b);
    ::b[i] = max(a, b);

    s[a][a] = s[b][b] = 1;
  }

  g[0] = 1;
  for (int i = 2; i <= n; i += 2)
    g[i] = mult(g[i - 2], i - 1);

  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      s[i][j] = s[i][j - 1] + s[j][j];

  ll ans = 0;
  for (int l = 1; l < n; l += 2)
    for (int i = 1; i + l <= n; ++i) {
      int j = i + l;

      bool flag = 0;

      for (int ik = 0; ik < k; ++ik)
        if ((i <= a[ik] && a[ik] <= j) ^ (i <= b[ik] && b[ik] <= j))
          flag = 1;

      if (flag)
        continue;

      d[i][j] = g[l + 1 - s[i][j]];

      for (int ik = i + 1; ik < j; ik += 2)
        d[i][j] = sub(d[i][j], mult(d[i][ik], g[j - ik - s[ik + 1][j]]));

      ans = add(ans, mult(d[i][j], g[n - 2 * k + s[i][j] - l - 1]));
    }

  cout << ans;

  return 0;
}

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

#define M 1000000007
#define maxn 603

using namespace std;
typedef long long ll;

inline ll add(ll a, ll b) { return a + b < M ? a + b : a + b - M; }
inline ll sub(ll a, ll b) { return a - b < 0 ? a - b + M : a - b; }
inline ll mult(ll a, ll b) { return a * b % M; }

int n, k;
int a[maxn], b[maxn];

ll d[maxn][maxn];
int s[maxn][maxn];

ll g[maxn];

int main() {
#define TASK "ABC"
  // freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
  ios_base::sync_with_stdio(0);

  cin >> n >> k;
  n *= 2;

  for (int i = 0; i < k; ++i) {
    int a, b;
    cin >> a >> b;
    ::a[i] = min(a, b);
    ::b[i] = max(a, b);

    s[a][a] = s[b][b] = 1;
  }

  g[0] = 1;
  for (int i = 2; i <= n; i += 2)
    g[i] = mult(g[i - 2], i - 1);

  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      s[i][j] = s[i][j - 1] + s[j][j];

  ll ans = 0;
  for (int l = 1; l < n; l += 2)
    for (int i = 1; i + l <= n; ++i) {
      int j = i + l;

      bool flag = 0;

      for (int ik = 0; ik < k; ++ik)
        if ((i <= a[ik] && a[ik] <= j) ^ (i <= b[ik] && b[ik] <= j))
          flag = 1;

      if (flag)
        continue;

      d[i][j] = g[l + 1 - s[i][j]];

      for (int ik = i + 1; ik < j; ik += 2)
        d[i][j] = sub(d[i][j], mult(d[i][ik], g[j - ik - s[ik + 1][j]]));

      ans = add(ans, mult(d[i][j], g[n - 2 * k + s[i][j] - l - 1]));
    }

  cout << ans;

  return 0;
}
Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 305, mod = 1e9 + 7;
int p[Maxn], g[Maxn], f[Maxn][Maxn], cnt[Maxn][Maxn];
int main() {
  int n, k, ans = 0;
  cin >> n >> k;
  g[0] = 1;
  for (int i = 2; i <= 2 * n; i += 2)
    g[i] = 1ll * g[i - 2] * (i - 1) % mod;
  for (int i = 1; i <= k; i++) {
    int x, y;
    cin >> x >> y;
    p[x] = y;
    p[y] = x;
  }
  for (int i = 1; i <= 2 * n; i++)
    for (int j = i; j <= 2 * n; j++)
      cnt[i][j] = cnt[i][j - 1] + (!p[j]);
  for (int len = 1; len <= 2 * n; len++)
    for (int l = 1, r = len; r <= 2 * n; l++, r++) {
      bool succ = true;
      for (int k = l; k <= r; k++)
        if (p[k] && (p[k] < l || p[k] > r))
          succ = false;
      if (!succ)
        continue;
      f[l][r] = g[cnt[l][r]];
      for (int k = l + 1; k < r; k++)
        f[l][r] = (f[l][r] - 1ll * f[l][k] * g[cnt[k + 1][r]]) % mod;
      ans = (ans + 1ll * f[l][r] * g[cnt[1][l - 1] + cnt[r + 1][2 * n]]) % mod;
    }
  cout << (ans + mod) % mod << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 605, mod = 1e9 + 7;
int p[Maxn], g[Maxn], f[Maxn][Maxn], cnt[Maxn][Maxn];
int main() {
  int n, k, ans = 0;
  cin >> n >> k;
  g[0] = 1;
  for (int i = 2; i <= 2 * n; i += 2)
    g[i] = 1ll * g[i - 2] * (i - 1) % mod;
  for (int i = 1; i <= k; i++) {
    int x, y;
    cin >> x >> y;
    p[x] = y;
    p[y] = x;
  }
  for (int i = 1; i <= 2 * n; i++)
    for (int j = i; j <= 2 * n; j++)
      cnt[i][j] = cnt[i][j - 1] + (!p[j]);
  for (int len = 1; len <= 2 * n; len++)
    for (int l = 1, r = len; r <= 2 * n; l++, r++) {
      bool succ = true;
      for (int k = l; k <= r; k++)
        if (p[k] && (p[k] < l || p[k] > r))
          succ = false;
      if (!succ)
        continue;
      f[l][r] = g[cnt[l][r]];
      for (int k = l + 1; k < r; k++)
        f[l][r] = (f[l][r] - 1ll * f[l][k] * g[cnt[k + 1][r]]) % mod;
      ans = (ans + 1ll * f[l][r] * g[cnt[1][l - 1] + cnt[r + 1][2 * n]]) % mod;
    }
  cout << (ans + mod) % mod << "\n";
  return 0;
}