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

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll mod_pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll n;
ll a[310];
ll dp[301][90001][2];

ll dfs(ll i, ll s, bool f) {
  if (dp[i][s][f] != -1)
    return dp[i][s][f];
  if (i == n) {
    if (!f)
      return dp[n][s][f] = mod_pow(s, n);
    else
      return dp[n][s][f] = (mod - mod_pow(s, n)) % mod;
  }
  ll res = 0;
  if (s >= a[i]) {
    (res += dfs(i + 1, s - a[i], !f)) %= mod;
  }
  (res += dfs(i + 1, s, f)) %= mod;
  return dp[i][s][f] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  ll S;
  cin >> S;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, S, false) << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll mod_pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll n;
ll a[310];
int dp[301][90001][2];

ll dfs(ll i, ll s, bool f) {
  if (dp[i][s][f] != -1)
    return dp[i][s][f];
  if (i == n) {
    if (!f)
      return dp[n][s][f] = mod_pow(s, n);
    else
      return dp[n][s][f] = (mod - mod_pow(s, n)) % mod;
  }
  ll res = 0;
  if (s >= a[i]) {
    (res += dfs(i + 1, s - a[i], !f)) %= mod;
  }
  (res += dfs(i + 1, s, f)) %= mod;
  return dp[i][s][f] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  ll S;
  cin >> S;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, S, false) << endl;
  return 0;
}
Problem p01750 (C++)

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

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

using ll = long long;
const ll mod = 1000000007LL;

int l[310];
ll dp[310];

int main(void) {
  int d, s;
  cin >> d;
  rep(i, d) cin >> l[i];
  cin >> s;

  dp[s] = 1LL;
  rep(i, d) rep(j, s + 1) {
    if (j - l[i] >= 0)
      dp[j - l[i]] += mod - dp[j];
    dp[j] %= mod;
  }

  ll cut = 0LL;
  rep(i, s + 1) {
    ll tmp = dp[i];
    rep(j, d) tmp = (tmp * i) % mod;
    cut = (cut + tmp) % mod;
  }

  cout << cut << endl;
  return 0;
}

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

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

using ll = long long;
const ll mod = 1000000007LL;

int l[90010];
ll dp[90010];

int main(void) {
  int d, s;
  cin >> d;
  rep(i, d) cin >> l[i];
  cin >> s;

  dp[s] = 1LL;
  rep(i, d) rep(j, s + 1) {
    if (j - l[i] >= 0)
      dp[j - l[i]] += mod - dp[j];
    dp[j] %= mod;
  }

  ll cut = 0LL;
  rep(i, s + 1) {
    ll tmp = dp[i];
    rep(j, d) tmp = (tmp * i) % mod;
    cut = (cut + tmp) % mod;
  }

  cout << cut << endl;
  return 0;
}
Problem p01750 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
int dp[2][303][90909];
int d, s;
int l[303];
int mod_pow(int x, int n, int m) {
  int res = 1;
  while (n) {
    if (n & 1)
      (res *= x) %= m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
int dfs(int f, int x, int y) {
  if (~dp[f][x][y])
    return dp[f][x][y];
  int res = 0;
  if (x == d) {
    res = mod_pow(y, d, MOD);
    if (f)
      res = (MOD - res) % MOD;
    return dp[f][x][y] = res;
  }
  (res += dfs(f, x + 1, y)) %= MOD;
  if (l[x] < y)
    (res += dfs(!f, x + 1, y - l[x])) %= MOD;
  return dp[f][x][y] = res;
}
signed main() {
  cin >> d;
  for (int i = 0; i < d; i++)
    cin >> l[i];
  cin >> s;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 0, s) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
signed dp[2][303][90909];
int d, s;
int l[303];
int mod_pow(int x, int n, int m) {
  int res = 1;
  while (n) {
    if (n & 1)
      (res *= x) %= m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
int dfs(int f, int x, int y) {
  if (~dp[f][x][y])
    return dp[f][x][y];
  int res = 0;
  if (x == d) {
    res = mod_pow(y, d, MOD);
    if (f)
      res = (MOD - res) % MOD;
    return dp[f][x][y] = res;
  }
  (res += dfs(f, x + 1, y)) %= MOD;
  if (l[x] < y)
    (res += dfs(!f, x + 1, y - l[x])) %= MOD;
  return dp[f][x][y] = res;
}
signed main() {
  cin >> d;
  for (int i = 0; i < d; i++)
    cin >> l[i];
  cin >> s;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 0, s) << endl;
  return 0;
}
Problem p01750 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

ll mod_pow(ll a, ll x) {
  ll ret = 1;
  while (x > 0) {
    if (x & 1) {
      (ret *= a) %= MOD;
    }
    (a *= a) %= MOD;
    x >>= 1;
  }
  return ret;
}

int main() {
  ll d;
  cin >> d;
  vector<ll> a(d);
  for (auto &I : a) {
    cin >> I;
  }
  ll s;
  cin >> s;
  vector<ll> dp(s, 0);
  dp[0] = 1;
  for (auto &I : a) {
    for (ll t = s - 1; t >= 0; t--) {
      if (t + I < s) {
        dp[t + I] -= dp[t];
        dp[t + I] %= MOD;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < s; i++) {
    ans += dp[i] * mod_pow(s - i, d) % MOD;
    ans %= MOD;
  }
  if (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

ll mod_pow(ll a, ll x) {
  ll ret = 1;
  while (x > 0) {
    if (x & 1) {
      (ret *= a) %= MOD;
    }
    (a *= a) %= MOD;
    x >>= 1;
  }
  return ret;
}

int main() {
  ll d;
  cin >> d;
  vector<ll> a(d);
  for (auto &I : a) {
    cin >> I;
  }
  ll s;
  cin >> s;
  if (s == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> dp(s + 10, 0);
  dp[0] = 1;
  for (auto &I : a) {
    for (ll t = s - 1; t >= 0; t--) {
      if (t + I < s) {
        dp[t + I] -= dp[t];
        dp[t + I] %= MOD;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < s; i++) {
    ans += dp[i] * mod_pow(s - i, d) % MOD;
    ans %= MOD;
  }
  if (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;

  return 0;
}
Problem p01751 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n, m, b, d;
  long long c;
  bool a = false;
  cin >> n >> m >> b;
  c = b;
  for (int i = 0; i < 250000000; i++) {
    if (c % (n + m) <= n && a == false) {
      a = true;
      d = i;
    }
    c += 60;
  }
  if (a == false) {
    cout << "-1" << endl;
  } else {
    c = b + d * 60;
    cout << c << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, m, b, d;
  long long c;
  bool a = false;
  cin >> n >> m >> b;
  c = b;
  for (int i = 0; i < 150000000; i++) {
    if (c % (n + m) <= n && a == false) {
      a = true;
      d = i;
    }
    c += 60;
  }
  if (a == false) {
    cout << "-1" << endl;
  } else {
    c = b + d * 60;
    cout << c << endl;
  }
  return 0;
}
Problem p01751 (C++)

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

using namespace std;

int main() {
  int a, b, c;

  cin >> a >> b >> c;

  while (c < 1000000) {
    if (0 <= c % (a + b) && c % (a + b) <= a) {
      cout << c << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int a, b, c;

  cin >> a >> b >> c;

  while (c < 1000000) {
    if (0 <= c % (a + b) && c % (a + b) <= a) {
      cout << c << endl;
      return 0;
    }

    c += 60;
  }

  cout << -1 << endl;

  return 0;
}
Problem p01751 (C++)

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

using namespace std;

int main(void) {
  int a, b, c;
  cin >> a >> b >> c;

  for (int i = 0;; i += a + b) {
    if (i <= c && c <= i + a) {
      cout << i + c % 60 - i % 60 << endl;
      break;
    }
    if (60 < i && 60 % (a + b) == 0) {
      cout << -1 << endl;
      break;
    }
    if (i / 60 != (i + a + b) / 60)
      c += 60;
  }

  return 0;
}

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

using namespace std;

int main(void) {
  int a, b, c;
  cin >> a >> b >> c;

  for (int i = 0;; i += a + b) {
    if (i <= c && c <= i + a) {
      cout << i + c % 60 - i % 60 << endl;
      break;
    }
    // if(60<i && 60%(a+b)==0){
    if (100000 < i) {
      cout << -1 << endl;
      break;
    }
    if (i / 60 != (i + a + b) / 60)
      c += 60;
  }

  return 0;
}
Problem p01751 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int a, b, c;
  int d = 0;
  int n = 0;
  cin >> a >> b >> c;
  while (true) {
    if (d <= c && c <= d + a) {
      cout << n * 60 + c << endl;
      break;
    }
    d += a + b;
    if (d > 60) {
      n += d / 60;
      d %= 60;
    }
    if (d == 0) {
      cout << -1 << endl;
      break;
    }
  }
}

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

int main() {
  int a, b, c;
  int d = 0;
  int n = 0;
  cin >> a >> b >> c;
  while (true) {
    if (d <= c && c <= d + a) {
      cout << n * 60 + c << endl;
      break;
    }
    d += a + b;
    if (d >= 60) {
      n += d / 60;
      d %= 60;
    }
    if (d == 0) {
      cout << -1 << endl;
      break;
    }
  }
}
Problem p01751 (C++)

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

using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))

typedef long long ll;

int main() {
  ll a, b, c;
  cin >> a >> b >> c;
  ll t = 0;
  while (1) {
    if (c < (a + b) * t) {
      c += 60;
    } else if (c <= (a + b) * t + a) {
      cout << c << endl;
      return 0;
    } else {
      t++;
    }
  }
  cout << -1 << endl;
}

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

using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))

typedef long long ll;

int main() {
  ll a, b, c;
  cin >> a >> b >> c;
  ll t = 0;
  while (1) {
    if (c < (a + b) * t) {
      c += 60;
    } else if (c <= (a + b) * t + a) {
      cout << c << endl;
      return 0;
    } else {
      t++;
    }
    if (t > 1000)
      break;
  }
  cout << -1 << endl;
}
Problem p01751 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int st, et, r;
  int d[60] = {};
  for (st = r = 0, et = a;; st = et + b, et = st + a, d[st] = et) {
    if (st >= 60) {
      st -= 60;
      et -= 60;
      r++;
    }
    if (c >= st && c <= et) {
      cout << r * 60 + c << endl;
      break;
    }
    if (d[st] == et) {
      cout << "-1" << endl;
      break;
    }
  }
}

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int st, et, r;
  int d[60] = {};
  for (st = r = 0, et = a;; d[st] = et, st = et + b, et = st + a) {
    if (st >= 60) {
      st -= 60;
      et -= 60;
      r++;
    }
    if (c >= st && c <= et) {
      cout << r * 60 + c << endl;
      break;
    }
    if (d[st] == et) {
      cout << "-1" << endl;
      break;
    }
  }
}
Problem p01751 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int main(void) {
  int a, b, c;
  cin >> a >> b >> c;
  int t = 0;
  int res = 0;
  vector<vector<int>> visited(a + b + 1, vector<int>(2, 0));
  visited[0][0] = 1;
  bool ok = false;

  while (1) {
    int nt = t;
    nt += a;
    res += a;
    if ((t <= c and c <= nt) or (t <= c + 60 and c + 60 <= nt)) {
      res -= (nt - c) % 60;
      ok = true;
      break;
    }
    nt %= 60;
    if (visited[nt][1] != 0)
      break;
    visited[nt][1] = 1;

    t = nt;
    nt += b;
    res += b;
    nt %= 60;
    if (visited[nt][0] != 0)
      break;
    visited[nt][0] = 1;

    t = nt;
  }

  if (not ok)
    res = -1;
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int main(void) {
  int a, b, c;
  cin >> a >> b >> c;
  int t = 0;
  int res = 0;
  vector<vector<int>> visited(121, vector<int>(2, 0));
  visited[0][0] = 1;
  bool ok = false;

  while (1) {
    int nt = t;
    nt += a;
    res += a;
    if ((t <= c and c <= nt) or (t <= c + 60 and c + 60 <= nt)) {
      res -= (nt - c) % 60;
      ok = true;
      break;
    }
    nt %= 60;
    if (visited[nt][1] != 0)
      break;
    visited[nt][1] = 1;

    t = nt;
    nt += b;
    res += b;
    nt %= 60;
    if (visited[nt][0] != 0)
      break;
    visited[nt][0] = 1;

    t = nt;
  }

  if (not ok)
    res = -1;
  cout << res << endl;

  return 0;
}
Problem p01756 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 100001
using namespace std;
typedef pair<string, int> P;
typedef set<P>::iterator It;

void solve(string str, set<P> A, vector<int> &L) {
  for (int i = 0; i < str.size(); i++) {
    string t;
    t += str[i];
    It it;
    while ((it = A.lower_bound(P(t, -1))) != A.end()) {
      while (it != A.end() && it->first == t)
        L[it->second] = i, A.erase(it++);
      if (i + t.size() >= str.size())
        break;
      t += str[i + t.size()];
    }
  }
}

int main() {
  string str;
  cin >> str;
  int len = str.size(), n;
  cin >> n;
  set<P> A, B;
  string a[N], b[N];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    A.insert(P(a[i], i));
    reverse(b[i].begin(), b[i].end());
    B.insert(P(b[i], i));
  }
  vector<int> L(N, -1), R(N, -1);
  solve(str, A, L);
  reverse(str.begin(), str.end());
  solve(str, B, R);

  for (int i = 0; i < n; i++) {
    int cnt = 0, alen = a[i].size(), blen = b[i].size();
    while (cnt < min(alen, blen) && a[i][alen - cnt] == b[i][cnt])
      cnt++;
    if (L[i] == -1 || R[i] == -1 || len - R[i] < L[i] + alen - cnt)
      printf("0\n");
    else
      printf("%d\n", len - R[i] - L[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 100001
using namespace std;
typedef pair<string, int> P;
typedef set<P>::iterator It;

void solve(string str, set<P> A, vector<int> &L) {
  for (int i = 0; i < str.size(); i++) {
    string t;
    t += str[i];
    It it;
    while ((it = A.lower_bound(P(t, -1))) != A.end()) {
      if (it->first.substr(0, t.size()) != t)
        break;
      while (it != A.end() && it->first == t)
        L[it->second] = i, A.erase(it++);
      if (i + t.size() >= str.size())
        break;
      t += str[i + t.size()];
    }
  }
}

int main() {
  string str;
  cin >> str;
  int len = str.size(), n;
  cin >> n;
  set<P> A, B;
  string a[N], b[N];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    A.insert(P(a[i], i));
    reverse(b[i].begin(), b[i].end());
    B.insert(P(b[i], i));
  }
  vector<int> L(N, -1), R(N, -1);
  solve(str, A, L);
  reverse(str.begin(), str.end());
  solve(str, B, R);

  for (int i = 0; i < n; i++) {
    int cnt = 0, alen = a[i].size(), blen = b[i].size();
    while (cnt < min(alen, blen) && a[i][alen - cnt] == b[i][cnt])
      cnt++;
    if (L[i] == -1 || R[i] == -1 || len - R[i] < L[i] + alen - cnt)
      printf("0\n");
    else
      printf("%d\n", len - R[i] - L[i]);
  }
  return 0;
}
Problem p01764 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
using namespace std;
int b[310];
long long dp[310][310];
int sum[310];
int pow[10];
long long solve(int l, int r) {
  if (l == r)
    return 0;
  if (~dp[l][r])
    return dp[l][r];
  long long ret = 99999999999999LL;
  for (int i = l; i < r; i++) {
    int L = sum[i + 1] - sum[l];
    int R = sum[r + 1] - sum[i + 1];
    int c = 0;
    long long cost = solve(l, i) + solve(i + 1, r);
    while (L + R + c) {
      cost += (L % 10) * (R % 10);
      if (c + L % 10 + R % 10 >= 10) {
        c = 1;
        cost++;
      }
      L /= 10;
      R /= 10;
    }
    ret = min(ret, cost);
  }
  return dp[l][r] = ret;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++)
    scanf("%d", b + i);
  for (int i = 0; i < a; i++)
    sum[i + 1] = sum[i] + b[i];
  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 310; j++) {
      dp[i][j] = -1;
    }
  }
  pow[0] = 1;
  for (int i = 1; i < 10; i++)
    pow[i] = pow[i - 1] * 10;
  printf("%lld\n", solve(0, a - 1));
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int b[310];
long long dp[310][310];
int sum[310];
int pow[10];
long long solve(int l, int r) {
  if (l == r)
    return 0;
  if (~dp[l][r])
    return dp[l][r];
  long long ret = 99999999999999LL;
  for (int i = l; i < r; i++) {
    int L = sum[i + 1] - sum[l];
    int R = sum[r + 1] - sum[i + 1];
    int c = 0;
    long long cost = solve(l, i) + solve(i + 1, r);
    while (L + R + c) {
      cost += (L % 10) * (R % 10);
      if (c + L % 10 + R % 10 >= 10) {
        c = 1;
        cost++;
      } else
        c = 0;
      L /= 10;
      R /= 10;
    }
    ret = min(ret, cost);
  }
  return dp[l][r] = ret;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++)
    scanf("%d", b + i);
  for (int i = 0; i < a; i++)
    sum[i + 1] = sum[i] + b[i];
  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 310; j++) {
      dp[i][j] = -1;
    }
  }
  pow[0] = 1;
  for (int i = 1; i < 10; i++)
    pow[i] = pow[i - 1] * 10;
  printf("%lld\n", solve(0, a - 1));
}
Problem p01767 (C++)

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

using namespace std;

#define MAX 300001
typedef long long ll;

int main() {
  ll N, M, a;
  vector<ll> d(MAX, 0), e(MAX + 1, 0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    d[a]++;
  }
  for (int i = 0; i < MAX; i++) {
    d[i] *= (ll)i;
  }
  for (int i = 0; i < MAX; i++) {
    e[i + 1] = e[i] + d[i + 1];
  }
  cin >> M;
  vector<ll> b(M);
  for (int i = 0; i < M; i++) {
    cin >> a;
    b[i] = e[a];
  }
  for (int i = 0; i < M; i++) {
    cin >> a;
    cout << (b[i] >= a ? "Yes" : "No") << endl;
  }
  return 0;
}

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

using namespace std;

#define MAX 1000001
typedef long long ll;

int main() {
  ll N, M, a;
  vector<ll> d(MAX, 0), e(MAX + 1, 0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    d[a]++;
  }
  for (int i = 0; i < MAX; i++) {
    d[i] *= (ll)i;
  }
  for (int i = 0; i < MAX; i++) {
    e[i + 1] = e[i] + d[i + 1];
  }
  cin >> M;
  vector<ll> b(M);
  for (int i = 0; i < M; i++) {
    cin >> a;
    b[i] = e[a];
  }
  for (int i = 0; i < M; i++) {
    cin >> a;
    cout << (b[i] >= a ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01767 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define PB push_back
#define BE(c) c.begin(), c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef vector<ut> VI;
const int SIZE = 1 + 1e+7;
LL getly[SIZE];
VI nums;
LL b[SIZE];
int main() {
  LL N, M, a, c;
  cin >> N;
  REP(i, N) {
    scanf("%lld", &a);
    nums.PB(a);
  }
  sort(BE(nums));
  int now = 0;
  while (now != nums.size() && nums[now] == 0)
    now++;
  FOR(i, 1, SIZE) {
    getly[i] = getly[i - 1];
    while (now != nums.size() && nums[now] == i) {
      getly[i] += (LL)i;
      now++;
    }
  }
  cin >> M;
  REP(i, M)
  scanf("%lld", &b[i]);
  REP(i, M) {
    scanf("%lld", &c);
    if (getly[b[i]] >= c)
      printf("Yes\n");
    else
      printf("No\n");
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define PB push_back
#define BE(c) c.begin(), c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef vector<ut> VI;
const int SIZE = 1 + 1e+6;
LL getly[SIZE];
VI nums;
LL b[SIZE];
int main() {
  LL N, M, a, c;
  cin >> N;
  REP(i, N) {
    scanf("%lld", &a);
    nums.PB(a);
  }
  sort(BE(nums));
  int now = 0;
  while (now != nums.size() && nums[now] == 0)
    now++;
  FOR(i, 1, SIZE) {
    getly[i] = getly[i - 1];
    while (now != nums.size() && nums[now] == i) {
      getly[i] += (LL)i;
      now++;
    }
  }
  cin >> M;
  REP(i, M)
  scanf("%lld", &b[i]);
  REP(i, M) {
    scanf("%lld", &c);
    if (getly[b[i]] >= c)
      printf("Yes\n");
    else
      printf("No\n");
  }
}
Problem p01767 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

#include <cstdio>
#include <queue>
inline ll getLL() {
  ll s;
  scanf("%lld", &s);
  return s;
}
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>

using namespace std;

int main() {
  const int n = getInt();
  vector<ll> a(n);
  REP(i, n) a[i] = getLL();
  sort(a.begin(), a.end());
  vector<ll> as(n);
  as[0] = a[0];
  REP(i, n - 1) as[i + 1] = as[i] + a[i + 1];

  const int m = getInt();
  vector<ll> b(n);
  vector<ll> c(n);
  REP(i, m) b[i] = getLL();
  REP(i, m) c[i] = getLL();

  REP(i, m) {
    const int idx = lower_bound(a.begin(), a.end(), b[i] + 1) - a.begin();
    const ll g = idx == 0 ? 0 : as[idx - 1];
    puts(c[i] <= g ? "Yes" : "No");
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

#include <cstdio>
#include <queue>
inline ll getLL() {
  ll s;
  scanf("%lld", &s);
  return s;
}
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>

using namespace std;

int main() {
  const int n = getInt();
  vector<ll> a(n);
  REP(i, n) a[i] = getLL();
  sort(a.begin(), a.end());
  vector<ll> as(n);
  as[0] = a[0];
  REP(i, n - 1) as[i + 1] = as[i] + a[i + 1];

  const int m = getInt();
  vector<ll> b(m);
  vector<ll> c(m);
  REP(i, m) b[i] = getLL();
  REP(i, m) c[i] = getLL();

  REP(i, m) {
    const int idx = lower_bound(a.begin(), a.end(), b[i] + 1) - a.begin();
    const ll g = idx == 0 ? 0 : as[idx - 1];
    puts(c[i] <= g ? "Yes" : "No");
  }

  return 0;
}
Problem p01767 (C++)

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

int main() {
  int N, M, b[300000];
  long long Counter[1000001] = {}, c[300000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    Counter[a] += a;
  }
  for (int i = 1; i < 1000001; i++) {
    Counter[i] += Counter[i - 1];
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < M; i++) {
    if (Counter[b[i]] >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

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

int main() {
  int N, M, b[300000];
  static long long Counter[1000001] = {};
  long long c[300000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    Counter[a] += a;
  }
  for (int i = 1; i < 1000001; i++) {
    Counter[i] += Counter[i - 1];
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < M; i++) {
    if (Counter[b[i]] >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p01767 (C++)

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

class Fenwick_Tree {
private:
  vector<int64> p;

public:
  Fenwick_Tree(int size) { p.resize(size + 2, 0); }
  int64 sum(int x) {
    int64 ret = 0;
    for (++x; x > 0; x -= x & -x)
      ret += p[x];
    return (ret);
  }
  void add(int k, int a) {
    for (++k; k < p.size(); k += k & -k)
      p[k] += a;
  }
};

int main() {
  int N, M;
  Fenwick_Tree BIT(300004);

  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    BIT.add(a[i], a[i]);
  }
  cin >> M;
  vector<int64> b(M), c(M);
  for (int i = 0; i < M; i++)
    cin >> b[i];
  for (int i = 0; i < M; i++)
    cin >> c[i];

  for (int i = 0; i < M; i++) {
    int64 get = BIT.sum(b[i]);
    if (get >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

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

class Fenwick_Tree {
private:
  vector<int64> p;

public:
  Fenwick_Tree(int size) { p.resize(size + 2, 0); }
  int64 sum(int x) {
    int64 ret = 0;
    for (++x; x > 0; x -= x & -x)
      ret += p[x];
    return (ret);
  }
  void add(int k, int a) {
    for (++k; k < p.size(); k += k & -k)
      p[k] += a;
  }
};

int main() {
  int N, M;
  Fenwick_Tree BIT(1000004);

  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    BIT.add(a[i], a[i]);
  }
  cin >> M;
  vector<int64> b(M), c(M);
  for (int i = 0; i < M; i++)
    cin >> b[i];
  for (int i = 0; i < M; i++)
    cin >> c[i];

  for (int i = 0; i < M; i++) {
    int64 get = BIT.sum(b[i]);
    if (get >= c[i])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p01768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};
string a[5000];
int x[5000];
int Min[5000], cnt[5000];
int main() {
  int n;
  scanf("%d", &n);
  map<string, int> mp;
  rep(i, n) {
    cin >> a[i] >> x[i];
    mp[a[i]] = mp.size();
  }
  int m;
  scanf("%d", &m);
  UnionFind uf(n);
  rep(i, m) {
    string s, t;
    cin >> s >> t;
    uf.unite(mp[s], mp[t]);
  }
  memset(Min, 0x3f, sizeof(Min));
  rep(i, n) {
    Min[uf.find(i)] = min(Min[uf.find(i)], x[i]);
    cnt[uf.find(i)]++;
  }
  int ans = 0;
  rep(i, n) ans += Min[i] * cnt[i];
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};
string a[5000];
int x[5000];
int Min[5000], cnt[5000];
int main() {
  int n;
  scanf("%d", &n);
  map<string, int> mp;
  rep(i, n) {
    cin >> a[i] >> x[i];
    int b = mp.size();
    mp[a[i]] = b;
  }
  int m;
  scanf("%d", &m);
  UnionFind uf(n);
  rep(i, m) {
    string s, t;
    cin >> s >> t;
    uf.unite(mp[s], mp[t]);
  }
  memset(Min, 0x3f, sizeof(Min));
  rep(i, n) {
    Min[uf.find(i)] = min(Min[uf.find(i)], x[i]);
    cnt[uf.find(i)]++;
  }
  int ans = 0;
  rep(i, n) ans += Min[i] * cnt[i];
  cout << ans << endl;
}
Problem p01768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string, int> cost;
multimap<string, string> rule;
set<string> his, his2;
int flg;

void update(string a) {
  his2.insert(a);
  multimap<string, string>::iterator it = rule.find(a);
  while (it != rule.end() && it->first == a) {
    if (his2.count(it->second) == 0 && cost[it->second] > cost[a])
      cost[it->second] = cost[a], update(it->second);
    it++;
  }
}

int mk_rule(string a) {
  if (his.count(a) == 0) {
    his.insert(a);
    multimap<string, string>::iterator it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      cost[a] = min(cost[a], mk_rule(it->second));
      it++;
    }
    update(a);
    it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      if (cost[it->second] > cost[a])
        exit(1);
      it++;
    }
  }
  return cost[a];
}

int main() {
  int n, m, b;
  cin >> n;
  string a, c, ls[5002];
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cost[a] = b, ls[i] = a;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> c;
    rule.insert(pair<string, string>(a, c));
    rule.insert(pair<string, string>(c, a));
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += mk_rule(ls[i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string, int> cost;
multimap<string, string> rule;
set<string> his, his2;
int flg;

void update(string a) {
  his2.insert(a);
  multimap<string, string>::iterator it = rule.find(a);
  while (it != rule.end() && it->first == a) {
    if (his2.count(it->second) == 0 && cost[it->second] > cost[a])
      cost[it->second] = cost[a], update(it->second);
    it++;
  }
}

int mk_rule(string a) {
  if (his.count(a) == 0) {
    his.insert(a);
    multimap<string, string>::iterator it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      cost[a] = min(cost[a], mk_rule(it->second));
      it++;
    }
    his2.clear();
    update(a);
    it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      if (cost[it->second] > cost[a])
        exit(1);
      it++;
    }
  }
  return cost[a];
}

int main() {
  int n, m, b;
  cin >> n;
  string a, c, ls[5002];
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cost[a] = b, ls[i] = a;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> c;
    rule.insert(pair<string, string>(a, c));
    rule.insert(pair<string, string>(c, a));
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += mk_rule(ls[i]);
  cout << ans << endl;
  return 0;
}
Problem p01768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#define f first
#define s second
using namespace std;
typedef pair<string, int> P;
map<string, P> mp;

string find(string x) {
  if (mp[x].f != x)
    mp[x] = mp[find(mp[x].f)];
  return mp[x].f;
}

bool unite(string x, string y) {
  bool flg = 0;
  mp[x] = mp[find(x)], mp[y] = mp[find(y)];
  if (mp[x] != mp[y])
    flg = 1;
  if (mp[x].s < mp[y].s)
    swap(mp[x], mp[y]);
  mp[x] = mp[y];
  return flg;
}

int main() {
  int n, m, a;
  cin >> n;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str >> a;
    mp[str].f = str;
    mp[str].s = a;
  }
  cin >> m;
  string in[2][1001];
  for (int i = 0; i < m; i++)
    cin >> in[0][i] >> in[1][i], unite(in[0][i], in[1][i]);

  for (int i = 0; i < m; i++) {
    bool flg = 0;
    if (unite(in[0][i], in[1][i]))
      flg = 1;
    if (flg)
      i = -1;
  }
  int ans = 0;
  map<string, P>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++)
    ans += mp[find(it->f)].s;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#define f first
#define s second
using namespace std;
typedef pair<string, int> P;
map<string, P> mp;

string find(string x) {
  if (mp[x].f != x)
    mp[x] = mp[find(mp[x].f)];
  return mp[x].f;
}

bool unite(string x, string y) {
  bool flg = 0;
  mp[x] = mp[find(x)], mp[y] = mp[find(y)];
  if (mp[x].s != mp[y].s)
    flg = 1;
  if (mp[x].s < mp[y].s)
    swap(mp[x], mp[y]);
  mp[x] = mp[y];
  return flg;
}

int main() {
  int n, m, a;
  cin >> n;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str >> a;
    mp[str].f = str;
    mp[str].s = a;
  }
  cin >> m;
  string in[2][1001];
  for (int i = 0; i < m; i++)
    cin >> in[0][i] >> in[1][i], unite(in[0][i], in[1][i]);

  for (int i = 0; i < m; i++) {
    bool flg = 0;
    if (unite(in[0][i], in[1][i]))
      flg = 1;
    if (flg)
      i = -1;
  }
  int ans = 0;
  map<string, P>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++)
    ans += mp[find(it->f)].s;
  cout << ans << endl;
  return 0;
}
Problem p01769 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int dp[5010][5010] = {0};
int main() {
  int n, l;
  cin >> n >> l;
  int MOD = 1000000007;
  vi x(n), a(n);
  rep(i, n) cin >> x[i];
  rep(i, n) cin >> a[i];
  rep(i, 5010) dp[0][i] = 1;
  rep(i, n) {
    for (int j = x[i] + 1; j <= l; j++) {
      if ((j - x[i] - 1) % a[i] == 0)
        dp[i + 1][j] = (dp[i + 1][j] + dp[i][j - 1]) % MOD;
      dp[i + 1][j] = (dp[i + 1][j] + dp[i + 1][j - 1]) % MOD;
    }
  }
  cout << dp[n][l] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int dp[5010][5010] = {0};
int main() {
  int n, l;
  cin >> n >> l;
  int MOD = 1000000007;
  vi x(n), a(n);
  rep(i, n) cin >> x[i];
  rep(i, n) cin >> a[i];
  rep(i, 5010) dp[0][i] = 1;
  rep(i, n) {
    for (int j = x[i] + 1; j <= l; j++) {
      if (j == x[i] + 1 || a[i] && (j - x[i] - 1) % a[i] == 0)
        dp[i + 1][j] = (dp[i + 1][j] + dp[i][j - 1]) % MOD;
      dp[i + 1][j] = (dp[i + 1][j] + dp[i + 1][j - 1]) % MOD;
    }
  }
  cout << dp[n][l] << endl;
}
Problem p01769 (C++)

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

int dp[5010][5010];

signed main() {
  int n, l, p = 1000000007;
  cin >> n >> l;
  vector<int> x(n), a(n);
  for (int i = 0; i < n; i++)
    cin >> x[n - 1 - i];
  for (int i = 0; i < n; i++)
    cin >> a[n - 1 - i];
  if (a[0] == 0)
    dp[0][x[0]] = 1;
  else
    for (int i = x[0]; i < l; i += a[0])
      dp[0][i] = 1;
  for (int i = 1; i < n; i++) {
    int sum = 0;
    for (int j = l - 1; j >= x[i]; j--) {
      if ((j - x[i]) % a[i] == 0) {
        if (a[i] > 0 || x[i] == j) {
          dp[i][j] += sum;
          dp[i][j] %= p;
        }
      }
      sum += dp[i - 1][j];
      sum %= p;
    }
  }
  int ans = 0;
  for (int i = 0; i < l; i++) {
    ans += dp[n - 1][i];
    ans %= p;
  }
  cout << ans << endl;
  return 0;
}


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

int dp[5010][5010];

signed main() {
  int n, l, p = 1000000007;
  cin >> n >> l;
  vector<int> x(n), a(n);
  for (int i = 0; i < n; i++)
    cin >> x[n - 1 - i];
  for (int i = 0; i < n; i++)
    cin >> a[n - 1 - i];
  if (a[0] == 0)
    dp[0][x[0]] = 1;
  else
    for (int i = x[0]; i < l; i += a[0])
      dp[0][i] = 1;
  for (int i = 1; i < n; i++) {
    int sum = 0;
    for (int j = l - 1; j >= x[i]; j--) {
      if ((a[i] > 0 && (j - x[i]) % a[i] == 0) || x[i] == j) {
        dp[i][j] += sum;
        dp[i][j] %= p;
      }
      sum += dp[i - 1][j];
      sum %= p;
    }
  }
  int ans = 0;
  for (int i = 0; i < l; i++) {
    ans += dp[n - 1][i];
    ans %= p;
  }
  cout << ans << endl;
  return 0;
}
Problem p01769 (C++)

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

using namespace std;

int main() {
  int N, L;
  int x[5010], a[5010];

  scanf("%d %d", &N, &L);

  for (int i = 0; i < N; i++)
    scanf("%d", x + i);
  for (int i = 0; i < N; i++)
    scanf("%d", a + i);

  int dp[2][5010] = {{0}};
  int prev = 0, now = 1;

  dp[prev][L] = 1;
  for (int i = N - 1; i >= 0; i--) {
    for (int j = L; j >= 0; j--) {
      dp[now][j] = 0;
      dp[prev][j] = (dp[prev][j] + dp[prev][j + 1]) % 1000000007;
    }
    for (int j = x[i]; j < L; j += a[i]) {
      dp[now][j] = dp[prev][j + 1];
    }
    now = 1 - now;
    prev = 1 - prev;
  }

  int ans = 0;

  for (int i = 0; i < L; i++)
    ans = (ans + dp[prev][i]) % 1000000007;

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

  return (0);
}

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

using namespace std;

int main() {
  int N, L;
  int x[5010], a[5010];

  scanf("%d %d", &N, &L);

  for (int i = 0; i < N; i++)
    scanf("%d", x + i);
  for (int i = 0; i < N; i++)
    scanf("%d", a + i);

  int dp[2][5010] = {{0}};
  int prev = 0, now = 1;

  dp[prev][L] = 1;
  for (int i = N - 1; i >= 0; i--) {
    for (int j = L; j >= 0; j--) {
      dp[now][j] = 0;
      dp[prev][j] = (dp[prev][j] + dp[prev][j + 1]) % 1000000007;
    }
    for (int j = x[i]; j < L; j += a[i]) {
      dp[now][j] = dp[prev][j + 1];
      if (!a[i])
        break;
    }
    now = 1 - now;
    prev = 1 - prev;
  }

  int ans = 0;

  for (int i = 0; i < L; i++)
    ans = (ans + dp[prev][i]) % 1000000007;

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

  return (0);
}
Problem p01769 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())
#define ten(x) ((int)1e##x)
#define tenll(x) ((ll)1e##x)

int x[5000], a[5000];
int dp[2][5010];
const int MOD = ten(9) + 7;

int main() {
  int n, l;
  scanf("%d%d", &n, &l);
  FOR(i, n) scanf("%d", x + i);
  FOR(i, n) scanf("%d", a + i);
  FOR(i, n) x[i]++;

  FOR(i, l + 1) dp[0][i] = 1;
  auto pv = dp[0], nt = dp[1];
  FOR(i, n) {
    memset(nt, 0, sizeof(dp[0]));
    for (int dist = 0; x[i] + dist <= l; dist += a[i]) {
      int cur = x[i] + dist;
      nt[cur] = pv[cur - 1];
    }
    FOR(j, l) {
      nt[j + 1] += nt[j];
      if (nt[j + 1] >= MOD)
        nt[j + 1] -= MOD;
    }
    swap(pv, nt);
  }
  cout << pv[l] << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())
#define ten(x) ((int)1e##x)
#define tenll(x) ((ll)1e##x)

int x[5000], a[5000];
int dp[2][5010];
const int MOD = ten(9) + 7;

int main() {
  int n, l;
  scanf("%d%d", &n, &l);
  FOR(i, n) scanf("%d", x + i);
  FOR(i, n) scanf("%d", a + i);
  FOR(i, n) x[i]++;

  FOR(i, l + 1) dp[0][i] = 1;
  auto pv = dp[0], nt = dp[1];
  FOR(i, n) {
    memset(nt, 0, sizeof(dp[0]));
    if (a[i]) {
      for (int dist = 0; x[i] + dist <= l; dist += a[i]) {
        int cur = x[i] + dist;
        nt[cur] = pv[cur - 1];
      }
    } else {
      nt[x[i]] = pv[x[i] - 1];
    }
    FOR(j, l) {
      nt[j + 1] += nt[j];
      if (nt[j + 1] >= MOD)
        nt[j + 1] -= MOD;
    }
    swap(pv, nt);
  }
  cout << pv[l] << endl;
}
Problem p01769 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static const int MOD = 1000000007;
static int dp[5001][5001];

int main() {
  ios_base::sync_with_stdio(false);
  int n, l;
  cin >> n >> l;
  vector<int> x(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(dp, 0, sizeof(dp));
  dp[n][l] = 1;
  for (int i = n - 1; i >= 0; --i) {
    int sum = dp[i + 1][l];
    for (int j = l - 1; j >= x[i]; --j) {
      if ((j - x[i]) % a[i] == 0) {
        dp[i][j] = (dp[i][j] + sum) % MOD;
      }
      sum = (sum + dp[i + 1][j]) % MOD;
    }
  }
  int answer = 0;
  for (int i = 0; i <= l; ++i) {
    answer = (answer + dp[0][i]) % MOD;
  }
  cout << answer << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static const int MOD = 1000000007;
static int dp[5001][5001];

int main() {
  ios_base::sync_with_stdio(false);
  int n, l;
  cin >> n >> l;
  vector<int> x(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(dp, 0, sizeof(dp));
  dp[n][l] = 1;
  for (int i = n - 1; i >= 0; --i) {
    int sum = dp[i + 1][l];
    for (int j = l - 1; j >= x[i]; --j) {
      if (a[i] == 0) {
        if (j == x[i]) {
          dp[i][j] = (dp[i][j] + sum) % MOD;
        }
      } else if ((j - x[i]) % a[i] == 0) {
        dp[i][j] = (dp[i][j] + sum) % MOD;
      }
      sum = (sum + dp[i + 1][j]) % MOD;
    }
  }
  int answer = 0;
  for (int i = 0; i <= l; ++i) {
    answer = (answer + dp[0][i]) % MOD;
  }
  cout << answer << endl;
  return 0;
}
Problem p01769 (C++)

Buggy submission (status: Memory Limit Exceeded):
long long dp[5000][5000];
int main() {
  for (int i = 0; i < 5000; i++)
    for (int j = 0; j < 5000; j++)
      dp[i][j] = 1;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\t" #x " = " << (x) << endl

const int MOD = 1000000007;
int n, l;
int x[5010], a[5010];

int dp[5010][5010];

signed main() {
  while (cin >> n >> l) {
    rep(i, n) cin >> x[i], ++x[i];
    rep(i, n) cin >> a[i], a[i] ? 0 : a[i] = l;
    memset(dp, 0, sizeof dp);
    for (int i = 0; i <= l; ++i)
      dp[0][i] = 1;
    for (int i = 1; i <= n; ++i) {
      int a = ::a[i - 1], x = ::x[i - 1];
      for (int j = x; j <= l; ++j) {
        if (j == x) {
          dp[i][j] = dp[i - 1][j - 1];
        } else {
          if ((j - x) % a == 0) {
            dp[i][j] += dp[i - 1][j - 1];
          }
          dp[i][j] += dp[i][j - 1];
          dp[i][j] %= MOD;
        }
      }
    }

    // for(int i = 0; i <= n; ++i){
    //     for(int j = 0; j <= l; ++j){
    //         cout << dp[i][j] << ' ';
    //     }
    //     cout << endl;
    // }

    cout << dp[n][l] << endl;
  }
}