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

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

const int N = 1e5 + 10;
pair<int, int> a[N];

bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.first - a.second > b.first - b.second;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].first;
  for (int i = 0; i < n; i++)
    cin >> a[i].second;
  long long cnt1 = 0, cnt2 = 0;
  for (int i = 0; i < n; i++) {
    cnt1 += a[i].first;
    cnt2 += a[i].second;
  }
  if (cnt1 < cnt2)
    cout << -1 << endl;
  else {
    sort(a, a + n, cmp);
    int st = 0, en = n - 1, ans = 0;
    bool flag = true, flag2 = true;
    while (st < en) {
      if (a[en].first < a[en].second) {
        if (flag2) {
          ans++;
          flag2 = false;
        }
        if (flag) {
          ans++;
          flag = false;
        }
        while (a[en].first < a[en].second && a[st].first > a[st].second) {
          a[en].first++;
          a[st].first--;
        }
        if (a[st].first == a[st].second) {
          st++;
          flag = true;
        }
        if (a[en].first == a[en].second) {
          en--;
          flag2 = true;
        }
      } else
        break;
    }
    cout << ans << endl;
  }
}

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

const int N = 1e5 + 10;
pair<int, int> a[N];

bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.first - a.second > b.first - b.second;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].first;
  for (int i = 0; i < n; i++)
    cin >> a[i].second;
  long long cnt1 = 0, cnt2 = 0;
  for (int i = 0; i < n; i++) {
    cnt1 += a[i].first;
    cnt2 += a[i].second;
  }
  if (cnt1 < cnt2)
    cout << -1 << endl;
  else {
    sort(a, a + n, cmp);
    int st = 0, en = n - 1, ans = 0;
    bool flag = true, flag2 = true;
    while (st < en) {
      if (a[en].first < a[en].second) {
        if (flag2) {
          ans++;
          flag2 = false;
        }
        if (flag) {
          ans++;
          flag = false;
        }
        if (a[en].second - a[en].first < a[st].first - a[st].second) {
          a[st].first -= a[en].second - a[en].first;
          a[en].first = a[en].second;
        } else {
          a[en].first += a[st].first - a[st].second;
          a[st].first = a[st].second;
        }
        if (a[st].first == a[st].second) {
          st++;
          flag = true;
        }
        if (a[en].first == a[en].second) {
          en--;
          flag2 = true;
        }
      } else
        break;
    }
    cout << ans << endl;
  }
}
Problem p03151 (C++)

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

ll n;
string s, t;
ll a[10005], b[100005], c[100005];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  ll ans = 0, sum = 0;
  rep(i, n) {
    c[i] = a[i] - b[i];
    if (c[i] < 0)
      ans++, sum += c[i];
  }
  sort(c, c + n, greater<ll>());
  rep(i, n) if (c[i] > 0 && sum < 0) sum += c[i], ans++;
  cout << (sum >= 0 ? ans : -1) << endl;
}

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

ll n;
string s, t;
ll a[100005], b[100005], c[100005];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  ll ans = 0, sum = 0;
  rep(i, n) {
    c[i] = a[i] - b[i];
    if (c[i] < 0)
      ans++, sum += c[i];
  }
  sort(c, c + n, greater<ll>());
  rep(i, n) if (c[i] > 0 && sum < 0) sum += c[i], ans++;
  cout << (sum >= 0 ? ans : -1) << endl;
}
Problem p03151 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define llint long long

using namespace std;

llint n;
llint a[10005], b[10005];
vector<llint> vec;

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];

  llint sum = 0, ans = 0, need = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] - b[i];
    if (b[i] > a[i]) {
      ans++;
      need += b[i] - a[i];
    }
    if (b[i] < a[i]) {
      vec.push_back(a[i] - b[i]);
    }
  }
  if (sum < 0) {
    cout << -1 << endl;
    return 0;
  }
  sort(vec.rbegin(), vec.rend());

  for (int i = 0; i < vec.size(); i++) {
    if (need <= 0)
      break;
    ans++;
    need -= vec[i];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define llint long long

using namespace std;

llint n;
llint a[100005], b[100005];
vector<llint> vec;

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];

  llint sum = 0, ans = 0, need = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] - b[i];
    if (b[i] > a[i]) {
      ans++;
      need += b[i] - a[i];
    }
    if (b[i] < a[i]) {
      vec.push_back(a[i] - b[i]);
    }
  }
  if (sum < 0) {
    cout << -1 << endl;
    return 0;
  }
  sort(vec.rbegin(), vec.rend());

  for (int i = 0; i < vec.size(); i++) {
    if (need <= 0)
      break;
    ans++;
    need -= vec[i];
  }
  cout << ans << endl;

  return 0;
}
Problem p03151 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000;
int a[N], b[N], c[N];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}
bool cmp(ll p, ll q) { return p > q; }

int main() {
  int n = read(), cnt = 0, ans = 0;
  ll sum = 0;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    b[i] = read();
  for (int i = 1; i <= n; i++) {
    if (a[i] > b[i])
      c[++cnt] = a[i] - b[i];
    else if (a[i] < b[i]) {
      ans++;
      sum += (b[i] - a[i]);
    }
  }
  if (sum == 0) {
    puts("0");
    return 0;
  }
  sort(c + 1, c + 1 + cnt, cmp);
  for (int i = 1; i <= cnt; i++) {
    if (sum >= c[i])
      sum -= c[i];
    else
      sum = 0;
    ans++;
    if (sum == 0)
      break;
  }
  if (sum)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}


Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
int a[N], b[N], c[N];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}
bool cmp(ll p, ll q) { return p > q; }

int main() {
  int n = read(), cnt = 0, ans = 0;
  ll sum = 0;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    b[i] = read();
  for (int i = 1; i <= n; i++) {
    if (a[i] > b[i])
      c[++cnt] = a[i] - b[i];
    else if (a[i] < b[i]) {
      ans++;
      sum += (b[i] - a[i]);
    }
  }
  if (sum == 0) {
    puts("0");
    return 0;
  }
  sort(c + 1, c + 1 + cnt, cmp);
  for (int i = 1; i <= cnt; i++) {
    if (sum >= c[i])
      sum -= c[i];
    else
      sum = 0;
    ans++;
    if (sum == 0)
      break;
  }
  if (sum)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
Problem p03151 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
int main() {
  long long N, A[100], B[100], SumA = 0, SumB = 0, C[100], rest = 0, result = 0;
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    SumA += A[i];
  }
  for (int i = 0; i < N; i++) {
    std::cin >> B[i];
    SumB += B[i];
    C[i] = A[i] - B[i];
  }
  if (SumA < SumB) {
    std::cout << -1;
    return 0;
  }
  std::sort(C, C + N);
  int i = 0, j = N - 1;
  while (C[i] < 0) {
    rest += C[j];
    result++;
    while (rest > 0 && C[i] < 0) {
      rest += C[i];
      result++;
      i++;
    }
    j--;
  }
  if (rest < 0)
    std::cout << result + 1;
  else
    std::cout << result;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
int main() {
  long long N, A[100000], B[100000], SumA = 0, SumB = 0, C[100000], rest = 0,
                                     result = 0;
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    SumA += A[i];
  }
  for (int i = 0; i < N; i++) {
    std::cin >> B[i];
    SumB += B[i];
    C[i] = A[i] - B[i];
  }
  if (SumA < SumB) {
    std::cout << -1;
    return 0;
  }
  std::sort(C, C + N);
  int i = 0, j = N - 1;
  while (C[i] < 0) {
    rest += C[j];
    result++;
    while (rest > 0 && C[i] < 0) {
      rest += C[i];
      result++;
      i++;
    }
    j--;
  }
  if (rest < 0)
    std::cout << result + 1;
  else
    std::cout << result;
  return 0;
}
Problem p03151 (C++)

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

long A[20000];
long B[20000];
long d[20000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int n = 0; n < N; n++)
    cin >> A[n];
  for (int n = 0; n < N; n++)
    cin >> B[n];
  for (int n = 0; n < N; n++)
    d[n] = A[n] - B[n];
  int num = 0, m = N, sp = 0;
  sort(d, d + N);
  for (int n = 0; n < N; n++) {
    if (d[n] >= 0) {
      num = n + (N - m);
      break;
    }
    sp += d[n];
    while (sp < 0) {
      m--;
      if (d[m] < 0 || m <= n) {
        num = -1;
        break;
      }
      sp += d[m];
    }
    if (num == -1)
      break;
  }
  cout << num << "\n";
  return 0;
}

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

long A[200000];
long B[200000];
long d[200000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int n = 0; n < N; n++)
    cin >> A[n];
  for (int n = 0; n < N; n++)
    cin >> B[n];
  for (int n = 0; n < N; n++)
    d[n] = A[n] - B[n];
  int num = 0, m = N, sp = 0;
  sort(d, d + N);
  for (int n = 0; n < N; n++) {
    if (d[n] >= 0) {
      num = n + (N - m);
      break;
    }
    sp += d[n];
    while (sp < 0) {
      m--;
      if (d[m] < 0 || m <= n) {
        num = -1;
        break;
      }
      sp += d[m];
    }
    if (num == -1)
      break;
  }
  cout << num << "\n";
  return 0;
}
Problem p03151 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using vin = vector<int>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll INF = 1e18;

int main() {
  int n;
  cin >> n;
  vin a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  ////////総和　少ない数をカウント
  ll aSum = 0, bSum = 0;
  bool ok = true;
  rep(i, n) {
    aSum += a[i];
    bSum += b[i];
  }
  if (aSum < bSum)
    ok = false;
  ll cnt = 0;
  ll husoku = 0;
  priority_queue<int, vector<int>, less<int>> q;
  rep(i, n) {
    if (a[i] < b[i]) {
      cnt++;
      husoku += (b[i] - a[i]);
    } else {
      q.push(a[i] - b[i]);
    }
  }
  while (husoku > 0) {
    cnt++;
    husoku -= q.top();
    q.pop();
  }

  if (ok)
    cout << cnt << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using vin = vector<int>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll INF = 1e18;

int main() {
  int n;
  cin >> n;
  vin a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  ////////総和　少ない数をカウント
  ll aSum = 0, bSum = 0;
  bool ok = true;
  rep(i, n) {
    aSum += a[i];
    bSum += b[i];
  }
  if (aSum < bSum)
    ok = false;
  ll cnt = 0;
  ll husoku = 0;
  priority_queue<int, vector<int>, less<int>> q;
  rep(i, n) {
    if (a[i] < b[i]) {
      cnt++;
      husoku += (b[i] - a[i]);
    } else {
      q.push(a[i] - b[i]);
    }
  }
  if (ok) {
    while (husoku > 0) {
      cnt++;
      husoku -= q.top();
      q.pop();
    }
  }

  if (ok)
    cout << cnt << endl;
  else
    cout << -1 << endl;
}
Problem p03151 (C++)

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

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

bool compare(P a, P b) { return a.first - a.second < b.first - b.first; }

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

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];

  vector<P> x(n);
  rep(i, n) x[i] = {a[i], b[i]};
  sort(ALL(x), compare);

  int sum = 0;
  int s = -1;
  rep(i, n) {
    if (x[i].first >= x[i].second) {
      break;
    }
    sum += x[i].second - x[i].first;
    s = i;
  }

  int t = -1;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i].first <= x[i].second) {
      break;
    }
    if (sum <= 0) {
      break;
    }
    sum -= x[i].first - x[i].second;
    t = i;
  }

  if (sum > 0) {
    cout << -1 << endl;
    return 0;
  }

  if (s == -1 || t == -1) {
    cout << 0 << endl;
  } else {
    cout << s + 1 + n - t << endl;
  }

  return 0;
}


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

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

bool compare(P a, P b) { return a.first - a.second < b.first - b.second; }

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

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];

  vector<P> x(n);
  rep(i, n) x[i] = {a[i], b[i]};
  sort(ALL(x), compare);

  int sum = 0;
  int s = -1;
  rep(i, n) {
    if (x[i].first >= x[i].second) {
      break;
    }
    sum += x[i].second - x[i].first;
    s = i;
  }

  int t = -1;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i].first <= x[i].second) {
      break;
    }
    if (sum <= 0) {
      break;
    }
    sum -= x[i].first - x[i].second;
    t = i;
  }

  if (sum > 0) {
    cout << -1 << endl;
    return 0;
  }

  if (s == -1 || t == -1) {
    cout << 0 << endl;
  } else {
    cout << s + 1 + n - t << endl;
  }

  return 0;
}
Problem p03151 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 100000;
int a[N + 1], b[N];

int main() {
  int n;
  scanf("%d", &n);
  f(i, 0, n) scanf("%d", a + i);
  f(i, 0, n) scanf("%d", b + i);
  ll c = 0;
  f(i, 0, n) c += b[i] - a[i];
  if (c > 1) {
    printf("-1\n");
    return 0;
  }
  f(i, 0, n) a[i] -= b[i];
  sort(a, a + n);
  int j = n, an = 0;
  f(i, 0, n) if (a[i] < 0) {
    ++an;
    while (a[i] < 0) {
      if (a[j] == 0)
        --j;
      int z = min(-a[i], a[j]);
      a[i] += z;
      a[j] -= z;
    }
  }
  printf("%d\n", an + n - j);
}

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

int const N = 100000;
int a[N + 1], b[N];

int main() {
  int n;
  scanf("%d", &n);
  f(i, 0, n) scanf("%d", a + i);
  f(i, 0, n) scanf("%d", b + i);
  ll c = 0;
  f(i, 0, n) c += b[i] - a[i];
  if (c > 0) {
    printf("-1\n");
    return 0;
  }
  f(i, 0, n) a[i] -= b[i];
  sort(a, a + n);
  int j = n, an = 0;
  f(i, 0, n) if (a[i] < 0) {
    ++an;
    while (a[i] < 0) {
      if (a[j] == 0)
        --j;
      int z = min(-a[i], a[j]);
      a[i] += z;
      a[j] -= z;
    }
  }
  printf("%d\n", an + n - j);
}
Problem p03151 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<int64_t> plus;
  int64_t sumOfMinus = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int64_t diff = a[i] - b[i];
    if (diff < (int64_t)0) {
      sumOfMinus -= diff;
      ans++;
    } else if (diff > (int64_t)0) {
      plus.push_back(diff);
    }
  }
  sort(plus.rbegin(), plus.rend());
  for (int i = 0; i < n && sumOfMinus > (int64_t)0; i++) {
    sumOfMinus -= plus[i];
    ans++;
  }
  if (sumOfMinus > (int64_t)0)
    ans = -1;
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<int64_t> plus;
  int64_t sumOfMinus = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int64_t diff = a[i] - b[i];
    if (diff < (int64_t)0) {
      sumOfMinus -= diff;
      ans++;
    } else if (diff > (int64_t)0) {
      plus.push_back(diff);
    }
  }
  sort(plus.rbegin(), plus.rend());
  for (int i = 0; i < plus.size() && sumOfMinus > (int64_t)0; i++) {
    sumOfMinus -= plus[i];
    ans++;
  }
  if (sumOfMinus > (int64_t)0)
    ans = -1;
  cout << ans << endl;
}
Problem p03151 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define EPS 1e-6
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
#define pi acos(-1)
#define index Index
#define Line pll
typedef long long LL;
int main() {
  int n;
  scanf("%d", &n);
  int a[105];
  int b[105];
  LL sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    a[i] -= b[i];
    sum -= b[i];
  }
  if (sum < 0) {
    printf("-1\n");
    return 0;
  }
  sort(a, a + n);
  int ori = a[n - 1];
  int r = n - 1;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0) {
      if (a[r] == ori)
        printf("%d\n", n - (r - i + 1));
      else
        printf("%d\n", n - (r - i));
      break;
    }
    while (a[i] < 0) {
      a[i] += a[r];
      if (a[i] < 0) {
        r--;
        ori = a[r];
      } else {
        a[r] = a[i];
      }
    }
  }
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define EPS 1e-6
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
#define pi acos(-1)
#define index Index
#define Line pll
typedef long long LL;
int main() {
  int n;
  scanf("%d", &n);
  int a[100005];
  int b[100005];
  LL sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    a[i] -= b[i];
    sum -= b[i];
  }
  if (sum < 0) {
    printf("-1\n");
    return 0;
  }
  sort(a, a + n);
  int ori = a[n - 1];
  int r = n - 1;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0) {
      if (a[r] == ori)
        printf("%d\n", n - (r - i + 1));
      else
        printf("%d\n", n - (r - i));
      break;
    }
    while (a[i] < 0) {
      a[i] += a[r];
      if (a[i] < 0) {
        r--;
        ori = a[r];
      } else {
        a[r] = a[i];
      }
    }
  }
}
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 1005
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int A[SIZE], B[SIZE];
int px[SIZE], py[SIZE];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]), A[i]--;
  for (int i = 0; i < m; i++)
    scanf("%d", &B[i]), B[i]--;
  memset(px, -1, sizeof(px));
  memset(py, -1, sizeof(py));
  ll ret = 1;
  for (int i = 0; i < n; i++) {
    if (px[A[i]] != -1) {
      puts("0");
      return 0;
    }
    px[A[i]] = i;
  }
  for (int i = 0; i < m; i++) {
    if (py[B[i]] != -1) {
      puts("0");
      return 0;
    }
    py[B[i]] = i;
  }
  ll zx = 0, zy = 0;
  for (int i = n * m - 1; i >= 0; i--) {
    if (px[i] != -1) {
      if (py[i] == -1) {
        ret = ret * zy % MOD;
        zx++;
      } else
        zx++, zy++;
    } else {
      if (py[i] != -1) {
        ret = ret * zx % MOD;
        zy++;
      } else {
        ll zan = zx * zy - (n * m - i - 1);
        ret = ret * zan % MOD;
      }
    }
  }
  printf("%lld\n", ret);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 1000005
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int A[SIZE], B[SIZE];
int px[SIZE], py[SIZE];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]), A[i]--;
  for (int i = 0; i < m; i++)
    scanf("%d", &B[i]), B[i]--;
  memset(px, -1, sizeof(px));
  memset(py, -1, sizeof(py));
  ll ret = 1;
  for (int i = 0; i < n; i++) {
    if (px[A[i]] != -1) {
      puts("0");
      return 0;
    }
    px[A[i]] = i;
  }
  for (int i = 0; i < m; i++) {
    if (py[B[i]] != -1) {
      puts("0");
      return 0;
    }
    py[B[i]] = i;
  }
  ll zx = 0, zy = 0;
  for (int i = n * m - 1; i >= 0; i--) {
    if (px[i] != -1) {
      if (py[i] == -1) {
        ret = ret * zy % MOD;
        zx++;
      } else
        zx++, zy++;
    } else {
      if (py[i] != -1) {
        ret = ret * zx % MOD;
        zy++;
      } else {
        ll zan = zx * zy - (n * m - i - 1);
        ret = ret * zan % MOD;
      }
    }
  }
  printf("%lld\n", ret);
  return 0;
}
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = n; i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= m; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
const double PI = 3.14159265358979323846;
using namespace std;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> vec(100100, {-1, -1});
  rep(i, n) {
    int a;
    cin >> a;
    vec[a].first = i;
  }
  rep(i, m) {
    int b;
    cin >> b;
    vec[b].second = i;
  }

  ll ans = 1;
  set<int> as, bs;
  for (int i = n * m; i > 0; i--) {
    int a, b;
    tie(a, b) = vec[i];

    if (a == -1 && b == -1) {
      ll total = n * m - i;
      ll now = (ll)(sz(as) * sz(bs)) - total;
      ans = (ans * now) % MOD;
    } else if (a == -1) {
      ll now = sz(as);
      ans = (ans * now) % MOD;
      bs.insert(b);
    } else if (b == -1) {
      ll now = sz(bs);
      ans = (ans * now) % MOD;
      as.insert(a);
    } else {
      as.insert(a);
      bs.insert(b);
    }
    // cout<<ans<<endl;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = n; i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= m; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
const double PI = 3.14159265358979323846;
using namespace std;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> vec(1001001, {-1, -1});
  rep(i, n) {
    int a;
    cin >> a;
    vec[a].first = i;
  }
  rep(i, m) {
    int b;
    cin >> b;
    vec[b].second = i;
  }

  ll ans = 1;
  set<int> as, bs;
  for (int i = n * m; i > 0; i--) {
    int a, b;
    tie(a, b) = vec[i];

    if (a == -1 && b == -1) {
      ll total = n * m - i;
      ll now = (ll)(sz(as) * sz(bs)) - total;
      ans = (ans * now) % MOD;
    } else if (a == -1) {
      ll now = sz(as);
      ans = (ans * now) % MOD;
      bs.insert(b);
    } else if (b == -1) {
      ll now = sz(bs);
      ans = (ans * now) % MOD;
      as.insert(a);
    } else {
      as.insert(a);
      bs.insert(b);
    }
    // cout<<ans<<endl;
  }
  cout << ans << endl;
}
Problem p03152 (C++)

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

using namespace std;

#define fi first
#define se second
typedef pair<int, int> ii;

const int N = 1e3 + 5, mod = 1e9 + 7;
int n, m, cntR[N], cntC[N], a[N], b[N], row, col, ans = 1;
ii pos[N * N];

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    ++cntR[a[i]];
    pos[a[i]].fi = i;
  }
  for (int i = 1; i <= m; ++i) {
    scanf("%d", b + i);
    ++cntC[b[i]];
    pos[b[i]].se = i;
  }

  for (int i = 1; i <= n * m; ++i)
    if (cntC[i] > 1 || cntR[i] > 1)
      return printf("0"), 0;

  for (int i = n * m; i > 0; --i) {
    if (pos[i].fi && pos[i].se) {

    } else if (pos[i].fi) {
      ans = 1LL * ans * col % mod;
    } else if (pos[i].se) {
      ans = 1LL * ans * row % mod;
    } else {
      ans = 1LL * ans * max(0, row * col - (n * m - i)) % mod;
    }
    row += (pos[i].fi > 0);
    col += (pos[i].se > 0);
    //        cout << "ans = " << ans << '\n';
  }

  printf("%d", ans);

  return 0;
}


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

using namespace std;

#define fi first
#define se second
typedef pair<int, int> ii;

const int N = 1e3 + 5, mod = 1e9 + 7;
int n, m, cntR[N * N], cntC[N * N], a[N], b[N], row, col, ans = 1;
ii pos[N * N];

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    ++cntR[a[i]];
    pos[a[i]].fi = i;
  }
  for (int i = 1; i <= m; ++i) {
    scanf("%d", b + i);
    ++cntC[b[i]];
    pos[b[i]].se = i;
  }

  for (int i = 1; i <= n * m; ++i)
    if (cntC[i] > 1 || cntR[i] > 1)
      return printf("0"), 0;

  for (int i = n * m; i > 0; --i) {
    if (pos[i].fi && pos[i].se) {

    } else if (pos[i].fi) {
      ans = 1LL * ans * col % mod;
    } else if (pos[i].se) {
      ans = 1LL * ans * row % mod;
    } else {
      ans = 1LL * ans * max(0, row * col - (n * m - i)) % mod;
    }
    row += (pos[i].fi > 0);
    col += (pos[i].se > 0);
    //        cout << "ans = " << ans << '\n';
  }

  printf("%d", ans);

  return 0;
}
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define INF 10e17 // 4倍しても(4回足しても)long longを溢れない
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_r(i, n, m) for (int i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())

void mul(ll &a, ll &b) { a = a * b % MOD; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  sortd(a);
  sortd(b);

  a.pb(-1);
  b.pb(-1);

  ll apt = 0, bpt = 0, num = n * m;
  ll ans = 1;
  while (num > 0) {
    if (a[apt] == num and b[bpt] == num) {
      apt++;
      bpt++;
    } else if (a[apt] == num) {
      apt++;
      mul(ans, bpt);
    } else if (b[bpt] == num) {
      bpt++;
      mul(ans, apt);
    } else {
      ll tmp = apt * bpt - (n * m - num);
      mul(ans, tmp);
    }
    num -= 1;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define INF 10e17 // 4倍しても(4回足しても)long longを溢れない
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_r(i, n, m) for (int i = m; i < n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())

void mul(ll &a, ll &b) { a = a * b % MOD; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  sortd(a);
  sortd(b);

  a.pb(-1);
  b.pb(-1);

  ll apt = 0, bpt = 0, num = n * m;
  ll ans = 1;
  while (num > 0) {
    if (a[apt] == num and b[bpt] == num) {
      apt++;
      bpt++;
    } else if (a[apt] == num) {
      apt++;
      mul(ans, bpt);
    } else if (b[bpt] == num) {
      bpt++;
      mul(ans, apt);
    } else {
      ll tmp = apt * bpt - (n * m - num);
      mul(ans, tmp);
    }
    num -= 1;
  }
  cout << ans << endl;
}
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/

const int MAXN = 1200;

int n, m;
int a[MAXN];
int b[MAXN];

vector<int> gc[MAXN], gr[MAXN];
const ll MOD = 1e9 + 7;

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    cin >> a[i], --a[i], gr[a[i]].push_back(i);
  for (int i = 0; i < m; ++i)
    cin >> b[i], --b[i], gc[b[i]].push_back(i);
  ll ans = 1;
  int r = 0;
  int c = 0;
  int cr = 0;
  for (int i = n * m - 1; i >= 0; --i) {
    if (gr[i].size() >= 2 || gc[i].size() >= 2) {
      cout << 0 << "\n";
      return 0;
    }
    if (gr[i].empty()) {
      if (gc[i].empty()) {
        ans = (ans * cr) % MOD;
        --cr;
      } else {
        ans = (ans * r) % MOD;
        cr += r - 1;
        ++c;
      }
    } else {
      if (gc[i].empty()) {
        ans = (ans * c) % MOD;
        cr += c - 1;
        ++r;
      } else {
        cr += r + c;
        ++r, ++c;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/

const int MAXN = 1200 * 1200;

int n, m;
int a[MAXN];
int b[MAXN];

vector<int> gc[MAXN], gr[MAXN];
const ll MOD = 1e9 + 7;

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    cin >> a[i], --a[i], gr[a[i]].push_back(i);
  for (int i = 0; i < m; ++i)
    cin >> b[i], --b[i], gc[b[i]].push_back(i);
  ll ans = 1;
  int r = 0;
  int c = 0;
  int cr = 0;
  for (int i = n * m - 1; i >= 0; --i) {
    if (gr[i].size() >= 2 || gc[i].size() >= 2) {
      cout << 0 << "\n";
      return 0;
    }
    if (gr[i].empty()) {
      if (gc[i].empty()) {
        ans = (ans * cr) % MOD;
        --cr;
      } else {
        ans = (ans * r) % MOD;
        cr += r - 1;
        ++c;
      }
    } else {
      if (gc[i].empty()) {
        ans = (ans * c) % MOD;
        cr += c - 1;
        ++r;
      } else {
        cr += r + c;
        ++r, ++c;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p03152 (C++)

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

#define int long long

const int N = 1e3 + 10, mod = 1e9 + 7;
int n, m, ans = 1, ar[2][N], mark[N * N];

int32_t main() {
  cin >> n >> m;
  for (int j = 0; j < n; j++) {
    cin >> ar[0][j];
    if (ar[0][j] > n * m || mark[ar[0][j]])
      return cout << 0, 0;
    mark[ar[0][j]] = 1;
  }

  memset(mark, 0, sizeof mark);
  for (int i = 0; i < m; i++) {
    cin >> ar[1][i];
    if (ar[1][i] > n * m || mark[ar[1][i]])
      return cout << 0, 0;
    mark[ar[1][i]] = 1;
  }

  sort(ar[0], ar[0] + n, [&](int a, int b) { return a > b; });
  sort(ar[1], ar[1] + m, [&](int a, int b) { return a > b; });

  int l = 0, r = 0;
  for (int i = m * n; i; i--) {
    while (l < n && ar[0][l] >= i)
      l++;
    while (r < m && ar[1][r] >= i)
      r++;
    assert(l && r);

    //		cout << l << ',' << r << " " << ar[0][l - 1] << ' ' << ar[1][r - 1] <<
    //endl;
    if (ar[0][l - 1] == i && ar[1][r - 1] == i)
      ;
    else if (ar[0][l - 1] == i)
      ans = (ans * r) % mod;
    else if (ar[1][r - 1] == i)
      ans = (ans * l) % mod;
    else
      ans = (ans * (l * r - (m * n - i))) % mod;
  }
  cout << ans;
}

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

#define int long long

const int N = 1e3 + 10, mod = 1e9 + 7;
int n, m, ans = 1, ar[2][N], mark[N * N];

int32_t main() {
  cin >> n >> m;
  for (int j = 0; j < n; j++) {
    cin >> ar[0][j];
    if (ar[0][j] > n * m || mark[ar[0][j]])
      return cout << 0, 0;
    mark[ar[0][j]] = 1;
  }

  memset(mark, 0, sizeof mark);
  for (int i = 0; i < m; i++) {
    cin >> ar[1][i];
    if (ar[1][i] > n * m || mark[ar[1][i]])
      return cout << 0, 0;
    mark[ar[1][i]] = 1;
  }

  sort(ar[0], ar[0] + n, [&](int a, int b) { return a > b; });
  sort(ar[1], ar[1] + m, [&](int a, int b) { return a > b; });

  int l = 0, r = 0;
  for (int i = m * n; i; i--) {
    while (l < n && ar[0][l] >= i)
      l++;
    while (r < m && ar[1][r] >= i)
      r++;
    if (!l || !r)
      return cout << 0, 0;
    //		cout << l << ',' << r << " " << ar[0][l - 1] << ' ' << ar[1][r - 1] <<
    //endl;
    if (ar[0][l - 1] == i && ar[1][r - 1] == i)
      ;
    else if (ar[0][l - 1] == i)
      ans = (ans * r) % mod;
    else if (ar[1][r - 1] == i)
      ans = (ans * l) % mod;
    else
      ans = (ans * (l * r - (m * n - i))) % mod;
  }
  cout << ans;
}
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 1010
#define ll long long
const ll mod = 1e9 + 7;

int n, m, s1, s2, cnt;
int a[N], b[N], h[N], l[N];
ll ans = 1;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (h[a[i]])
      return puts("0"), 0;
    h[a[i]] = i;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
    if (l[b[i]])
      return puts("0"), 0;
    l[b[i]] = i;
  }

  for (int i = n * m; i; i--) {
    if (h[i] && l[i])
      s1++, s2++;
    else if (h[i] || l[i]) {
      if (h[i])
        ans = ans * s2 % mod, s1++;
      else
        ans = ans * s1 % mod, s2++;
    } else
      ans = ans * (s1 * s2 - (n * m - i)) % mod, cnt++;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 1010
#define ll long long
const ll mod = 1e9 + 7;

int n, m, s1, s2, cnt;
int a[N], b[N], h[N * N], l[N * N];
ll ans = 1;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (h[a[i]])
      return puts("0"), 0;
    h[a[i]] = i;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
    if (l[b[i]])
      return puts("0"), 0;
    l[b[i]] = i;
  }

  for (int i = n * m; i; i--) {
    if (h[i] && l[i])
      s1++, s2++;
    else if (h[i] || l[i]) {
      if (h[i])
        ans = ans * s2 % mod, s1++;
      else
        ans = ans * s1 % mod, s2++;
    } else
      ans = ans * (s1 * s2 - (n * m - i)) % mod, cnt++;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1005, mod = 1e9 + 7;
ll r[N], c[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    if (r[x])
      return cout << "0", 0;
    r[x] = i;
  }
  for (ll i = 1; i <= m; i++) {
    ll x;
    cin >> x;
    if (c[x])
      return cout << "0", 0;
    c[x] = i;
  }
  ll ans = 1, x = 0, y = 0;
  for (ll val = n * m; val >= 1; val--) {
    if (r[val] || c[val]) {
      if (r[val] && c[val])
        x++, y++;
      else if (r[val]) {
        ans *= y;
        ans %= mod;
        x++;
      } else {
        ans *= x;
        ans %= mod;
        y++;
      }
    } else {
      ll tmp = x * y - (n * m - val);
      ans *= tmp;
      ans %= mod;
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e6 + 5, mod = 1e9 + 7;
int r[N], c[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    if (r[x])
      return cout << "0", 0;
    r[x] = i;
  }
  for (ll i = 1; i <= m; i++) {
    ll x;
    cin >> x;
    if (c[x])
      return cout << "0", 0;
    c[x] = i;
  }
  ll ans = 1, x = 0, y = 0;
  for (ll val = n * m; val >= 1; val--) {
    if (r[val] || c[val]) {
      if (r[val] && c[val])
        x++, y++;
      else if (r[val]) {
        ans *= y;
        ans %= mod;
        x++;
      } else {
        ans *= x;
        ans %= mod;
        y++;
      }
    } else {
      ll tmp = x * y - (n * m - val);
      ans *= tmp;
      ans %= mod;
    }
  }
  cout << ans;
}
Problem p03152 (C++)

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

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

int a[2003], b[2003];
int kn[2003], km[2003];
long long mod = 1000000007;
int main() {
  int n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    if (kn[a[i]]) {
      puts("0");
      return 0;
    }
    kn[a[i]] = 1;
  }
  for (int i = 1; i <= m; ++i) {
    b[i] = read();
    if (km[b[i]]) {
      puts("0");
      return 0;
    }
    km[b[i]] = 1;
  }
  int xn = 0, xm = 0;
  int c = 0;
  long long res = 1;
  for (int i = n * m; i >= 1; --i) {
    if (km[i] && kn[i]) {
      c += xn + xm;
      xn++, xm++;
    } else if (kn[i]) {
      if (!xm) {
        puts("0");
        return 0;
      }
      (res *= (long long)xm) %= mod;
      c += xm - 1;
      xn++;
    } else if (km[i]) {
      if (!xn) {
        puts("0");
        return 0;
      }
      (res *= (long long)xn) %= mod;
      c += xn - 1;
      xm++;
    } else {
      if (!c) {
        puts("0");
        return 0;
      }
      (res *= (long long)c) %= mod;
      c--;
    }
  }
  printf("%lld\n", res);
  return 0;
}

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

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

int a[2003], b[2003];
int kn[2000006], km[2000006];
long long mod = 1000000007;
int main() {
  int n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    if (kn[a[i]]) {
      puts("0");
      return 0;
    }
    kn[a[i]] = 1;
  }
  for (int i = 1; i <= m; ++i) {
    b[i] = read();
    if (km[b[i]]) {
      puts("0");
      return 0;
    }
    km[b[i]] = 1;
  }
  int xn = 0, xm = 0;
  int c = 0;
  long long res = 1;
  for (int i = n * m; i >= 1; --i) {
    if (km[i] && kn[i]) {
      c += xn + xm;
      xn++, xm++;
    } else if (kn[i]) {
      if (!xm) {
        puts("0");
        return 0;
      }
      (res *= (long long)xm) %= mod;
      c += xm - 1;
      xn++;
    } else if (km[i]) {
      if (!xn) {
        puts("0");
        return 0;
      }
      (res *= (long long)xn) %= mod;
      c += xn - 1;
      xm++;
    } else {
      if (!c) {
        puts("0");
        return 0;
      }
      (res *= (long long)c) %= mod;
      c--;
    }
  }
  printf("%lld\n", res);
  return 0;
}
Problem p03152 (C++)

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main() {
  ll i, j;
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (i = 0; i < n; ++i) {
    cin >> a.at(i);
  }
  vector<ll> b(m);
  for (i = 0; i < m; ++i) {
    cin >> b.at(i);
  }
  const ll MOD = 1e9 + 7;
  sort(all(a));
  sort(all(b));
  for (i = 0; i < n - 1; ++i) {
    if (a.at(i) == a.at(i + 1)) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (i = 0; i < m - 1; ++i) {
    if (b.at(i) == b.at(i + 1)) {
      cout << 0 << endl;
      return 0;
    }
  }
  ll ans = 1;
  for (i = n * m; i >= 1; --i) {
    ll indh = lower_bound(all(a), i) - a.begin();
    ll indw = lower_bound(all(b), i) - b.begin();
    if (i == a.at(indh) && i == b.at(indw))
      continue;
    else if (i == a.at(indh)) {
      ll tmp = m - indw;
      ans *= tmp;
      ans %= MOD;
    } else if (i == b.at(indw)) {
      ll tmp = n - indh;
      ans *= tmp;
      ans %= MOD;
    } else {
      ll tmp = (m - indw) * (n - indh);
      tmp -= n * m - i;
      if (tmp <= 0) {
        cout << 0 << endl;
        return 0;
      }
      ans *= tmp;
      ans %= MOD;
    }
  }
  cout << ans << endl;

  return 0;
}

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main() {
  ll i, j;
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (i = 0; i < n; ++i) {
    cin >> a.at(i);
  }
  vector<ll> b(m);
  for (i = 0; i < m; ++i) {
    cin >> b.at(i);
  }
  const ll MOD = 1e9 + 7;
  sort(all(a));
  sort(all(b));
  for (i = 0; i < n - 1; ++i) {
    if (a.at(i) == a.at(i + 1)) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (i = 0; i < m - 1; ++i) {
    if (b.at(i) == b.at(i + 1)) {
      cout << 0 << endl;
      return 0;
    }
  }
  ll ans = 1;
  for (i = n * m; i >= 1; --i) {
    ll indh = lower_bound(all(a), i) - a.begin();
    ll indw = lower_bound(all(b), i) - b.begin();
    if (indh == n || indw == m) {
      cout << 0 << endl;
      return 0;
    }
    if (i == a.at(indh) && i == b.at(indw))
      continue;
    else if (i == a.at(indh)) {
      ll tmp = m - indw;
      ans *= tmp;
      ans %= MOD;
    } else if (i == b.at(indw)) {
      ll tmp = n - indh;
      ans *= tmp;
      ans %= MOD;
    } else {
      ll tmp = (m - indw) * (n - indh);
      tmp -= n * m - i;
      if (tmp <= 0) {
        cout << 0 << endl;
        return 0;
      }
      ans *= tmp;
      ans %= MOD;
    }
  }
  cout << ans << endl;

  return 0;
}