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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

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;
}

using ll = long long;

ll N;
ll g[2], s[2], b[2];
ll dp[5][100010];

ll knap(const std::vector<ll> &w, const std::vector<ll> &v, ll W) {
  rep(i, 5) rep(j, W + 1) dp[i][j] = 0;

  rep(i, 3) rep(j, W + 1) {
    if (j - w[i] >= 0)
      chmax(dp[i + 1][j],
            std::max({dp[i + 1][j - w[i]] + v[i], dp[i][j - w[i]] + v[i]}));

    chmax(dp[i + 1][j], dp[i][j]);
  }

  ll ret = 0;

  rep(j, W + 1) chmax(ret, dp[3][j]);

  return W + ret;
}

int main() {
  std::cin >> N;

  rep(i, 2) std::cin >> g[i] >> s[i] >> b[i];

  ll M = knap({g[0], s[0], b[0]}, {g[1] - g[0], s[1] - s[0], b[1] - b[0]}, N);

  ll ans = knap({g[1], s[1], b[1]}, {g[0] - g[1], s[0] - s[1], b[0] - b[1]}, M);

  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

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;
}

using ll = long long;

ll N;
ll g[2], s[2], b[2];
ll dp[5][25000010];

ll knap(const std::vector<ll> &w, const std::vector<ll> &v, ll W) {
  rep(i, 5) rep(j, W + 1) dp[i][j] = 0;

  rep(i, 3) rep(j, W + 1) {
    if (j - w[i] >= 0)
      chmax(dp[i + 1][j],
            std::max({dp[i + 1][j - w[i]] + v[i], dp[i][j - w[i]] + v[i]}));

    chmax(dp[i + 1][j], dp[i][j]);
  }

  ll ret = 0;

  rep(j, W + 1) chmax(ret, dp[3][j]);

  return W + ret;
}

int main() {
  std::cin >> N;

  rep(i, 2) std::cin >> g[i] >> s[i] >> b[i];

  ll M = knap({g[0], s[0], b[0]}, {g[1] - g[0], s[1] - s[0], b[1] - b[0]}, N);

  ll ans = knap({g[1], s[1], b[1]}, {g[0] - g[1], s[0] - s[1], b[0] - b[1]}, M);

  std::cout << ans << std::endl;

  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define re return
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
ll dp[5010];
ll n;
ll a[10][10];
ll ans;
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - a[0][0] >= 0) {
      dp[i] = max(dp[i], dp[i - a[0][0]] + a[1][0] - a[0][0]);
    }
    if (i - a[0][1] >= 0) {
      dp[i] = max(dp[i], dp[i - a[0][1]] + a[1][1] - a[0][1]);
    }
    if (i - a[0][2] >= 0) {
      dp[i] = max(dp[i], dp[i - a[0][2]] + a[1][2] - a[0][2]);
    }
  }
  ans = n + dp[n];
  n = n + dp[n];
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - a[1][0] >= 0) {
      dp[i] = max(dp[i], dp[i - a[1][0]] + a[0][0] - a[1][0]);
    }
    if (i - a[1][1] >= 0) {
      dp[i] = max(dp[i], dp[i - a[1][1]] + a[0][1] - a[1][1]);
    }
    if (i - a[1][2] >= 0) {
      dp[i] = max(dp[i], dp[i - a[1][2]] + a[0][2] - a[1][2]);
    }
  }
  ans += dp[n];
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define re return
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
ll dp[5001 * 5001];
ll n;
ll a[10][10];
ll ans;
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - a[0][0] >= 0) {
      dp[i] = max(dp[i], dp[i - a[0][0]] + a[1][0] - a[0][0]);
    }
    if (i - a[0][1] >= 0) {
      dp[i] = max(dp[i], dp[i - a[0][1]] + a[1][1] - a[0][1]);
    }
    if (i - a[0][2] >= 0) {
      dp[i] = max(dp[i], dp[i - a[0][2]] + a[1][2] - a[0][2]);
    }
  }
  ans = n + dp[n];
  n = n + dp[n];
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - a[1][0] >= 0) {
      dp[i] = max(dp[i], dp[i - a[1][0]] + a[0][0] - a[1][0]);
    }
    if (i - a[1][1] >= 0) {
      dp[i] = max(dp[i], dp[i - a[1][1]] + a[0][1] - a[1][1]);
    }
    if (i - a[1][2] >= 0) {
      dp[i] = max(dp[i], dp[i - a[1][2]] + a[0][2] - a[1][2]);
    }
  }
  ans += dp[n];
  cout << ans << endl;
  return 0;
}
Problem p03008 (C++)

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

typedef long long ll;
ll n, ta[3], tb[3];
vector<ll> a[2], b[2];

ll f(ll p, ll q, ll r) {
  ll i, re = 0;
  if (r == a[p].size())
    return q;
  for (i = 0; i * a[p][r] <= q; i++) {
    re = max(re, f(p, q - i * a[p][r], r + 1) + i * b[p][r]);
  }
  return re;
}

int main() {
  ll i;
  cin >> n;
  for (i = 0; i < 3; i++)
    scanf("%lld", &ta[i]);
  for (i = 0; i < 3; i++)
    scanf("%lld", &tb[i]);
  for (i = 0; i < 3; i++) {
    if (ta[i] < tb[i])
      a[0].push_back(ta[i]), b[0].push_back(tb[i]);
    else if (ta[i] > tb[i])
      a[1].push_back(tb[i]), b[1].push_back(ta[i]);
  }
  cout << f(1, f(0, n, 0), 0) << endl;
  return 0;
}

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

typedef long long ll;
ll n, ta[3], tb[3];
vector<ll> a[2], b[2];

ll f(ll p, ll q, ll r) {
  ll i, re = 0;
  if (r == a[p].size())
    return q;
  if (r == a[p].size() - 1)
    return q / a[p][r] * b[p][r] + q % a[p][r];
  for (i = 0; i * a[p][r] <= q; i++) {
    re = max(re, f(p, q - i * a[p][r], r + 1) + i * b[p][r]);
  }
  return re;
}

int main() {
  ll i;
  cin >> n;
  for (i = 0; i < 3; i++)
    scanf("%lld", &ta[i]);
  for (i = 0; i < 3; i++)
    scanf("%lld", &tb[i]);
  for (i = 0; i < 3; i++) {
    if (ta[i] < tb[i])
      a[0].push_back(ta[i]), b[0].push_back(tb[i]);
    else if (ta[i] > tb[i])
      a[1].push_back(tb[i]), b[1].push_back(ta[i]);
  }
  cout << f(1, f(0, n, 0), 0) << endl;
  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1000003;

ll dp[5050], dp2[200010];
ll w[5], w2[5];
ll v[5], v2[5];

int main() {
  ll n;
  cin >> n;
  ll a[2][3];
  rep(i, 2) { rep(j, 3) cin >> a[i][j]; }
  rep(i, 3) {
    w[i] = a[0][i];
    v[i] = a[1][i];
    w2[i] = a[1][i];
    v2[i] = a[0][i];
  }
  v[3] = 1;
  w[3] = 1;
  v2[3] = 1;
  w2[3] = 1;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= n; j++) {
      if (j < w[i]) {
        dp[j] = dp[j];
      } else {
        dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
  }
  ll m = dp[n];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= m; j++) {
      if (j < w2[i]) {
        dp2[j] = dp2[j];
      } else {
        dp2[j] = max(dp2[j], dp2[j - w2[i]] + v2[i]);
      }
    }
  }
  cout << dp2[m] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1000003;

ll dp[5050], dp2[25000001];
ll w[5], w2[5];
ll v[5], v2[5];

int main() {
  ll n;
  cin >> n;
  ll a[2][3];
  rep(i, 2) { rep(j, 3) cin >> a[i][j]; }
  rep(i, 3) {
    w[i] = a[0][i];
    v[i] = a[1][i];
    w2[i] = a[1][i];
    v2[i] = a[0][i];
  }
  v[3] = 1;
  w[3] = 1;
  v2[3] = 1;
  w2[3] = 1;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= n; j++) {
      if (j < w[i]) {
        dp[j] = dp[j];
      } else {
        dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
  }
  ll m = dp[n];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= m; j++) {
      if (j < w2[i]) {
        dp2[j] = dp2[j];
      } else {
        dp2[j] = max(dp2[j], dp2[j - w2[i]] + v2[i]);
      }
    }
  }
  cout << dp2[m] << endl;
  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int dp[5005];
vector<int> dp2;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  int n;
  cin >> n;
  int a[3];
  int b[3];
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    cin >> b[i];
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < 3; j++) {
      if (i - a[j] >= 0)
        dp[i - a[j]] = max(dp[i - a[j]], dp[i] + (b[j] - a[j]));
    }
  }
  int mx = 0;
  for (int i = 0; i <= n; i++) {
    mx = max(mx, n + dp[i]);
  }
  n = mx;
  dp2.assign(n + 5, 0);
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < 3; j++) {
      if (i - b[j] >= 0)
        dp[i - b[j]] = max(dp[i - b[j]], dp[i] + (a[j] - b[j]));
    }
  }
  mx = 0;
  for (int i = 0; i <= n; i++) {
    mx = max(mx, n + dp2[i]);
  }
  cout << mx;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int dp[5005];
vector<int> dp2;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  int n;
  cin >> n;
  int a[3];
  int b[3];
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    cin >> b[i];
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < 3; j++) {
      if (i - a[j] >= 0)
        dp[i - a[j]] = max(dp[i - a[j]], dp[i] + (b[j] - a[j]));
    }
  }
  int mx = 0;
  for (int i = 0; i <= n; i++) {
    mx = max(mx, n + dp[i]);
  }
  n = mx;
  dp2.assign(n + 5, 0);
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < 3; j++) {
      if (i - b[j] >= 0)
        dp2[i - b[j]] = max(dp2[i - b[j]], dp2[i] + (a[j] - b[j]));
    }
  }
  mx = 0;
  for (int i = 0; i <= n; i++) {
    mx = max(mx, n + dp2[i]);
  }
  cout << mx;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[3], b[3], go[5005], back[5005];
int main() {
  scanf("%lld", &n);
  for (int i = 0; i < 3; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < 3; i++) {
    scanf("%lld", &b[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i - a[j] >= 0)
        go[i] = max(go[i], go[i - a[j]] + b[j] - a[j]);
    }
  }
  n += go[n];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i - b[j] >= 0)
        back[i] = max(back[i], back[i - b[j]] + a[j] - b[j]);
    }
  }
  n += back[n];
  printf("%lld", n);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[3], b[3], go[5005], back[5005 * 5005];
int main() {
  scanf("%lld", &n);
  for (int i = 0; i < 3; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < 3; i++) {
    scanf("%lld", &b[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i - a[j] >= 0)
        go[i] = max(go[i], go[i - a[j]] + b[j] - a[j]);
    }
  }
  n += go[n];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i - b[j] >= 0)
        back[i] = max(back[i], back[i - b[j]] + a[j] - b[j]);
    }
  }
  n += back[n];
  printf("%lld", n);
}
Problem p03008 (C++)

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

long long f[5000005];

int main() {
  int n;
  cin >> n;
  int a[3], b[3];
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    cin >> b[i];
  for (int i = 0; i < 3; i++)
    if (a[i] < b[i]) {
      for (int j = a[i]; j <= n; j++)
        f[j] = max(f[j], f[j - a[i]] + b[i]);
    }
  long long s = 0;
  for (int i = 0; i <= n; i++)
    s = max(s, f[i] - i);
  n += s;
  memset(f, 0, sizeof(f));
  for (int i = 0; i < 3; i++)
    if (a[i] > b[i]) {
      for (int j = b[i]; j <= n; j++)
        f[j] = max(f[j], f[j - b[i]] + a[i]);
    }
  s = 0;
  for (int i = 0; i <= n; i++)
    s = max(s, f[i] - i);
  printf("%lld\n", s + n);
  return 0;
}


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

long long f[25000005];

int main() {
  int n;
  cin >> n;
  int a[3], b[3];
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    cin >> b[i];
  for (int i = 0; i < 3; i++)
    if (a[i] < b[i]) {
      for (int j = a[i]; j <= n; j++)
        f[j] = max(f[j], f[j - a[i]] + b[i]);
    }
  long long s = 0;
  for (int i = 0; i <= n; i++)
    s = max(s, f[i] - i);
  n += s;
  memset(f, 0, sizeof(f));
  for (int i = 0; i < 3; i++)
    if (a[i] > b[i]) {
      for (int j = b[i]; j <= n; j++)
        f[j] = max(f[j], f[j - b[i]] + a[i]);
    }
  s = 0;
  for (int i = 0; i <= n; i++)
    s = max(s, f[i] - i);
  printf("%lld\n", s + n);
  return 0;
}
Problem p03008 (C++)

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

using namespace std;
const long long MAXN = 5e6 + 5;
long long a[5][5];
long long dp[MAXN];
long long m, ans;
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < 2; i++) {
    for (long long j = 1; j <= 3; j++) {
      cin >> a[i][j];
    }
  }
  for (long long i = 0; i <= n; i++) {
    dp[i] = n;
  }
  m = n;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= 3; j++) {
      if (a[1][j] > a[0][j] && i - a[0][j] >= 0) {
        dp[i] = max(dp[i], dp[i - a[0][j]] + a[1][j] - a[0][j]);
      }
      m = max(m, dp[i]);
    }
  }
  for (long long i = 0; i <= m; i++) {
    dp[i] = m;
  }

  for (long long i = 1; i <= m; i++) {
    for (long long j = 1; j <= 3; j++) {
      if (a[0][j] > a[1][j] && i - a[1][j] >= 0) {
        dp[i] = max(dp[i], dp[i - a[1][j]] + a[0][j] - a[1][j]);
      }
      ans = max(ans, dp[i]);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>

using namespace std;
const long long MAXN = 5e7 + 5;
long long a[5][5];
long long dp[MAXN];
long long m, ans;
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < 2; i++) {
    for (long long j = 1; j <= 3; j++) {
      cin >> a[i][j];
    }
  }
  for (long long i = 0; i <= n; i++) {
    dp[i] = n;
  }
  m = n;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= 3; j++) {
      if (a[1][j] > a[0][j] && i - a[0][j] >= 0) {
        dp[i] = max(dp[i], dp[i - a[0][j]] + a[1][j] - a[0][j]);
      }
      m = max(m, dp[i]);
    }
  }
  for (long long i = 0; i <= m; i++) {
    dp[i] = m;
  }

  for (long long i = 1; i <= m; i++) {
    for (long long j = 1; j <= 3; j++) {
      if (a[0][j] > a[1][j] && i - a[1][j] >= 0) {
        dp[i] = max(dp[i], dp[i - a[1][j]] + a[0][j] - a[1][j]);
      }
      ans = max(ans, dp[i]);
    }
  }
  cout << ans << endl;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll MOD = 1e9 + 7;
const ll N = 2e5 + 5;
const ld pi = 3.14159265359;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll) a.size()

ll n, a[3], b[3], dp[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  REP(i, 3) cin >> a[i];
  REP(i, 3) cin >> b[i];
  dp[0] = 0;
  REP1(i, n) {
    dp[i] = i;
    REP(j, 3) {
      if (i >= a[j])
        dp[i] = max(dp[i], dp[i - a[j]] + b[j]);
    }
  }
  n = dp[n];
  REP1(i, n) {
    dp[i] = i;
    REP(j, 3) {
      if (i >= b[j])
        dp[i] = max(dp[i], dp[i - b[j]] + a[j]);
    }
  }
  cout << dp[n] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll MOD = 1e9 + 7;
const ll N = 5000 * 5000 + 5;
const ld pi = 3.14159265359;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll) a.size()

ll n, a[3], b[3], dp[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  REP(i, 3) cin >> a[i];
  REP(i, 3) cin >> b[i];
  dp[0] = 0;
  REP1(i, n) {
    dp[i] = i;
    REP(j, 3) {
      if (i >= a[j])
        dp[i] = max(dp[i], dp[i - a[j]] + b[j]);
    }
  }
  n = dp[n];
  REP1(i, n) {
    dp[i] = i;
    REP(j, 3) {
      if (i >= b[j])
        dp[i] = max(dp[i], dp[i - b[j]] + a[j]);
    }
  }
  cout << dp[n] << "\n";
  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = (int)1e7;
int ex[2][87];
LL dp[MAXN];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      scanf("%d", &ex[i][j]);
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      int sub = ex[0][j];
      int add = ex[1][j];
      dp[i + sub] = max(dp[i + sub], dp[i] + add);
    }
  }
  LL nN = 0;
  for (int i = 0; i <= N; i++) {
    int tmp = N - i;
    nN = max(nN, tmp + dp[i]);
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= nN; i++) {
    for (int j = 0; j < 3; j++) {
      int sub = ex[1][j];
      int add = ex[0][j];
      dp[i + sub] = max(dp[i + sub], dp[i] + add);
    }
  }
  LL ans = 0;
  for (int i = 0; i <= nN; i++) {
    int tmp = nN - i;
    ans = max(ans, tmp + dp[i]);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = (int)3e7;
int ex[2][87];
LL dp[MAXN];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      scanf("%d", &ex[i][j]);
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      int sub = ex[0][j];
      int add = ex[1][j];
      dp[i + sub] = max(dp[i + sub], dp[i] + add);
    }
  }
  LL nN = 0;
  for (int i = 0; i <= N; i++) {
    int tmp = N - i;
    nN = max(nN, tmp + dp[i]);
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= nN; i++) {
    for (int j = 0; j < 3; j++) {
      int sub = ex[1][j];
      int add = ex[0][j];
      dp[i + sub] = max(dp[i + sub], dp[i] + add);
    }
  }
  LL ans = 0;
  for (int i = 0; i <= nN; i++) {
    int tmp = nN - i;
    ans = max(ans, tmp + dp[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

int solve(int N, vector<int> a, vector<int> b) {
  int ans = N;
  for (int cf = 0; cf * a[0] <= N; cf++) {
    if (cf > 0 && a[0] > b[0])
      break;
    for (int cs = 0; cf * a[0] + cs * a[1] <= N; cs++) {
      if (cs > 0 && a[1] > b[1])
        break;
      int ct = (N - cf * a[0] - cs * a[1]) / a[2];
      ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] +
                         (N - cf * a[0] - cs * a[1] - ct * a[2]));
      ct = 0;
      ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] +
                         (N - cf * a[0] - cs * a[1] - ct * a[2]));
    }
  }
  return ans;
}

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

  int N;
  cin >> N;
  vector<int> A(3);
  vector<int> B(3);
  for (auto &t : A) {
    cin >> t;
  }
  for (auto &t : B) {
    cin >> t;
  }
  N = solve(N, A, B);
  N = solve(N, B, A);
  cout << N << '\n';
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

int solve(int N, vector<int> a, vector<int> b) {
  int ans = N;
  vector<int> ind(3);
  iota(ind.begin(), ind.end(), 0);
  sort(ind.begin(), ind.end(),
       [&](int i, int j) { return b[i] - a[i] < b[j] - a[j]; });
  {
    vector<int> na(3);
    vector<int> nb(3);
    for (int i = 0; i < 3; i++) {
      na[i] = a[ind[i]];
      nb[i] = b[ind[i]];
    }
    a = na;
    b = nb;
  }
  for (int cf = 0; cf * a[0] <= N; cf++) {
    if (cf > 0 && a[0] > b[0])
      break;
    for (int cs = 0; cf * a[0] + cs * a[1] <= N; cs++) {
      if (cs > 0 && a[1] > b[1])
        break;
      int ct = (N - cf * a[0] - cs * a[1]) / a[2];
      ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] +
                         (N - cf * a[0] - cs * a[1] - ct * a[2]));
      ct = 0;
      ans = max(ans, cf * b[0] + cs * b[1] + ct * b[2] +
                         (N - cf * a[0] - cs * a[1] - ct * a[2]));
    }
  }
  return ans;
}

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

  int N;
  cin >> N;
  vector<int> A(3);
  vector<int> B(3);
  for (auto &t : A) {
    cin >> t;
  }
  for (auto &t : B) {
    cin >> t;
  }
  N = solve(N, A, B);
  N = solve(N, B, A);
  cout << N << '\n';
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e7 + 5;
int n, a, b, c, d, e, f, ma, dp[N];
signed main() {
  cin >> n;
  cin >> a >> b >> c >> d >> e >> f;
  memset(dp, -63, sizeof(dp));
  dp[0] = 0;
  for (int i = a; i <= n; i++)
    dp[i] = max(dp[i], dp[i - a] + d);
  for (int i = b; i <= n; i++)
    dp[i] = max(dp[i], dp[i - b] + e);
  for (int i = c; i <= n; i++)
    dp[i] = max(dp[i], dp[i - c] + f);
  ma = n;
  for (int i = 0; i <= n; i++)
    ma = max(ma, dp[i] + n - i);
  for (int i = d; i <= n; i++)
    dp[i] = max(dp[i], dp[i - d] + a);
  for (int i = e; i <= n; i++)
    dp[i] = max(dp[i], dp[i - e] + b);
  for (int i = f; i <= n; i++)
    dp[i] = max(dp[i], dp[i - f] + c);
  int ans = n;
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i] + n - i);
  memset(dp, -63, sizeof(dp));
  dp[0] = 0;
  n = ma;
  for (int i = d; i <= n; i++)
    dp[i] = max(dp[i], dp[i - d] + a);
  for (int i = e; i <= n; i++)
    dp[i] = max(dp[i], dp[i - e] + b);
  for (int i = f; i <= n; i++)
    dp[i] = max(dp[i], dp[i - f] + c);
  ans = max(ans, n);
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i] + n - i);
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5000 * 5000 + 5;
int n, a, b, c, d, e, f, ma, dp[N];
signed main() {
  cin >> n;
  cin >> a >> b >> c >> d >> e >> f;
  memset(dp, -63, sizeof(dp));
  dp[0] = 0;
  for (int i = a; i <= n; i++)
    dp[i] = max(dp[i], dp[i - a] + d);
  for (int i = b; i <= n; i++)
    dp[i] = max(dp[i], dp[i - b] + e);
  for (int i = c; i <= n; i++)
    dp[i] = max(dp[i], dp[i - c] + f);
  ma = n;
  for (int i = 0; i <= n; i++)
    ma = max(ma, dp[i] + n - i);
  for (int i = d; i <= n; i++)
    dp[i] = max(dp[i], dp[i - d] + a);
  for (int i = e; i <= n; i++)
    dp[i] = max(dp[i], dp[i - e] + b);
  for (int i = f; i <= n; i++)
    dp[i] = max(dp[i], dp[i - f] + c);
  int ans = n;
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i] + n - i);
  memset(dp, -63, sizeof(dp));
  dp[0] = 0;
  n = ma;
  for (int i = d; i <= n; i++)
    dp[i] = max(dp[i], dp[i - d] + a);
  for (int i = e; i <= n; i++)
    dp[i] = max(dp[i], dp[i - e] + b);
  for (int i = f; i <= n; i++)
    dp[i] = max(dp[i], dp[i - f] + c);
  ans = max(ans, n);
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i] + n - i);
  cout << ans;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;

int n;
int ga, sa, ba;
int gb, sb, bb;
ll dp[10000000];

int main() {
  cin >> n;
  cin >> ga >> sa >> ba;
  cin >> gb >> sb >> bb;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - ga >= 0)
      dp[i] = max(dp[i], dp[i - ga] + gb - ga);
    if (i - sa >= 0)
      dp[i] = max(dp[i], dp[i - sa] + sb - sa);
    if (i - ba >= 0)
      dp[i] = max(dp[i], dp[i - ba] + bb - ba);
  }
  n += dp[n];
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - gb >= 0)
      dp[i] = max(dp[i], dp[i - gb] + ga - gb);
    if (i - sb >= 0)
      dp[i] = max(dp[i], dp[i - sb] + sa - sb);
    if (i - bb >= 0)
      dp[i] = max(dp[i], dp[i - bb] + ba - bb);
  }
  cout << n + dp[n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;

int n;
int ga, sa, ba;
int gb, sb, bb;
ll dp[30000000];

int main() {
  cin >> n;
  cin >> ga >> sa >> ba;
  cin >> gb >> sb >> bb;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - ga >= 0)
      dp[i] = max(dp[i], dp[i - ga] + gb - ga);
    if (i - sa >= 0)
      dp[i] = max(dp[i], dp[i - sa] + sb - sa);
    if (i - ba >= 0)
      dp[i] = max(dp[i], dp[i - ba] + bb - ba);
  }
  n += dp[n];
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (i - gb >= 0)
      dp[i] = max(dp[i], dp[i - gb] + ga - gb);
    if (i - sb >= 0)
      dp[i] = max(dp[i], dp[i - sb] + sa - sb);
    if (i - bb >= 0)
      dp[i] = max(dp[i], dp[i - bb] + ba - bb);
  }
  cout << n + dp[n] << endl;
}
Problem p03008 (C++)

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

typedef long long int64;

#define MAX_N 200005

struct data {
  double ratio;
  int64 cost, profit;
};

void DFS(int depth, vector<data> &vs, int64 value, int64 now, int64 &res) {
  if (depth == 3) {
    res = max(res, value + now);
    return;
  }

  int64 upper = now / vs[depth].cost;
  if (vs[depth].ratio <= 1)
    upper = 0LL;
  for (int64 i = upper; i >= 0; i--) {
    int64 remain = now - i * vs[depth].cost;
    DFS(depth + 1, vs, value + i * vs[depth].profit, remain, res);
  }
}

int64 solve(int64 N, int64 a, int64 b, int64 c, int64 A, int64 B, int64 C) {
  vector<data> vs;
  vs.push_back(data{1.0 * A / a, a, A});
  vs.push_back(data{1.0 * B / b, b, B});
  vs.push_back(data{1.0 * C / c, c, C});
  sort(vs.begin(), vs.end(),
       [](auto &lhs, auto &rhs) { return lhs.ratio > rhs.ratio; });
  int64 res = 0LL;
  DFS(0, vs, 0LL, N, res);
  return res;
}

int main() {
  // freopen("input.txt", "r", stdin);

  int64 N, gA, sA, bA, gB, sB, bB;
  scanf("%lld", &N);
  scanf("%lld %lld %lld", &gA, &sA, &bA);
  scanf("%lld %lld %lld", &gB, &sB, &bB);
  int64 M = solve(N, gA, sA, bA, gB, sB, bB);
  int64 res = solve(M, gB, sB, bB, gA, sA, bA);
  printf("%lld\n", res);
  return 0;
}


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

typedef long long int64;

#define MAX_N 200005

struct data {
  double ratio;
  int64 cost, profit;
};

void DFS(int depth, vector<data> &vs, int64 value, int64 now, int64 &res) {
  if (depth == 3) {
    res = max(res, value + now);
    return;
  }

  if (value + now * max(1.0, vs[depth].ratio) < res)
    return;

  int64 upper = now / vs[depth].cost;
  if (vs[depth].ratio <= 1)
    upper = 0LL;
  for (int64 i = upper; i >= 0; i--) {
    int64 remain = now - i * vs[depth].cost;
    DFS(depth + 1, vs, value + i * vs[depth].profit, remain, res);
  }
}

int64 solve(int64 N, int64 a, int64 b, int64 c, int64 A, int64 B, int64 C) {
  vector<data> vs;
  vs.push_back(data{1.0 * A / a, a, A});
  vs.push_back(data{1.0 * B / b, b, B});
  vs.push_back(data{1.0 * C / c, c, C});
  sort(vs.begin(), vs.end(),
       [](auto &lhs, auto &rhs) { return lhs.ratio > rhs.ratio; });
  int64 res = 0LL;
  DFS(0, vs, 0LL, N, res);
  return res;
}

int main() {
  // freopen("input.txt", "r", stdin);

  int64 N, gA, sA, bA, gB, sB, bB;
  scanf("%lld", &N);
  scanf("%lld %lld %lld", &gA, &sA, &bA);
  scanf("%lld %lld %lld", &gB, &sB, &bB);
  int64 M = solve(N, gA, sA, bA, gB, sB, bB);
  int64 res = solve(M, gB, sB, bB, gA, sA, bA);
  printf("%lld\n", res);
  return 0;
}
Problem p03008 (C++)

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

int main() {
  int N;
  cin >> N;

  vector<int> gsb_A(3), gsb_B(3);
  cin >> gsb_A[0] >> gsb_A[1] >> gsb_A[2];
  cin >> gsb_B[0] >> gsb_B[1] >> gsb_B[2];

  // A -> B
  // dp[n] :=  with n acrons, chokudai can gain dp[n] profit
  // dp[n] = max(dp[n], dp[n - g_A] + (g_B - g_A));
  vector<long long> dp1(N + 1);
  vector<int> profits(3);
  for (int i = 0; i < 3; i++) {
    profits[i] = max(0, gsb_B[i] - gsb_A[i]);
  }
  for (int n = 0; n <= N; n++) {
    for (int i = 0; i < 3; i++) {
      if (n >= gsb_A[i]) {
        dp1[n] = max(dp1[n], dp1[n - gsb_A[i]] + profits[i]);
      }
    }
  }

  long long N2 = dp1[N] + N;
  for (int i = 0; i < 3; i++) {
    profits[i] = max(0, gsb_A[i] - gsb_B[i]);
  }
  vector<long long> dp2(N2);
  for (int n = 0; n <= N2; n++) {
    for (int i = 0; i < 3; i++) {
      if (n >= gsb_B[i]) {
        dp2[n] = max(dp2[n], dp2[n - gsb_B[i]] + profits[i]);
      }
    }
  }
  cout << dp2[N2] + N2 << endl;
}


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

int main() {
  int N;
  cin >> N;

  vector<int> gsb_A(3), gsb_B(3);
  cin >> gsb_A[0] >> gsb_A[1] >> gsb_A[2];
  cin >> gsb_B[0] >> gsb_B[1] >> gsb_B[2];

  // A -> B
  // dp[n] :=  with n acrons, chokudai can gain dp[n] profit
  // dp[n] = max(dp[n], dp[n - g_A] + (g_B - g_A));
  vector<long long> dp1(N + 1);
  vector<int> profits(3);
  for (int i = 0; i < 3; i++) {
    profits[i] = max(0, gsb_B[i] - gsb_A[i]);
  }
  for (int n = 0; n <= N; n++) {
    for (int i = 0; i < 3; i++) {
      if (n >= gsb_A[i]) {
        dp1[n] = max(dp1[n], dp1[n - gsb_A[i]] + profits[i]);
      }
    }
  }

  long long N2 = dp1[N] + N;
  for (int i = 0; i < 3; i++) {
    profits[i] = max(0, gsb_A[i] - gsb_B[i]);
  }
  vector<long long> dp2(N2 + 1);
  for (int n = 0; n <= N2; n++) {
    for (int i = 0; i < 3; i++) {
      if (n >= gsb_B[i]) {
        dp2[n] = max(dp2[n], dp2[n - gsb_B[i]] + profits[i]);
      }
    }
  }
  cout << dp2[N2] + N2 << endl;
}
Problem p03009 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 100010;
const ll P = 1000000007;
int n, m, D;
ll ans, n1;
ll f[maxn], s[maxn];
int main() {
  scanf("%d%d%d", &n, &m, &D);
  int i;
  ll tmp = 1;
  f[0] = s[0] = 1;
  for (i = 1; i <= n; i++)
    tmp = tmp * i % P, n1 = (n1 + tmp) % P;
  for (i = 1; i < m; i++) {
    f[i] = s[i - 1] - ((i <= D) ? 0 : s[i - D - 1]) + P;
    f[i] = f[i] * n1 % P;
    s[i] = (s[i - 1] + f[i]) % P;
  }
  printf("%lld\n", (s[m - 1] - s[m - D - 1] + P) * tmp % P);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1000010;
const ll P = 1000000007;
int n, m, D;
ll ans, n1;
ll f[maxn], s[maxn];
int main() {
  scanf("%d%d%d", &n, &m, &D);
  int i;
  ll tmp = 1;
  f[0] = s[0] = 1;
  for (i = 1; i <= n; i++)
    tmp = tmp * i % P, n1 = (n1 + tmp) % P;
  for (i = 1; i < m; i++) {
    f[i] = s[i - 1] - ((i <= D) ? 0 : s[i - D - 1]) + P;
    f[i] = f[i] * n1 % P;
    s[i] = (s[i - 1] + f[i]) % P;
  }
  printf("%lld\n", (s[m - 1] - s[m - D - 1] + P) * tmp % P);
  return 0;
}
Problem p03009 (C++)

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

long long fact[1000005];
long long dp[1000005];

const long long MOD = 1e9 + 7;

int main() {
  int N, H, D;
  scanf("%d%d%d", &N, &H, &D);

  long long retainWays = 0;
  fact[0] = 1;
  for (int i = 1; i <= N; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    retainWays += fact[i] % MOD;
  }
  retainWays %= MOD;

  // printf("retainWays=%lld\n", retainWays);

  dp[0] = fact[N];
  long long slidingWindowSum = dp[0];
  for (int i = 1; i <= H; i++) {
    dp[i] = slidingWindowSum;

    /*for(int j = 1; j <= min(i, D); j ++){
        dp[i] += dp[i-j];
    }*/

    dp[i] %= MOD;

    if (i == H) {
      break;
    }

    dp[i] = dp[i] * retainWays % MOD;
    slidingWindowSum += dp[i];
    if (slidingWindowSum >= 0) {
      slidingWindowSum -= dp[i - D];
    }
    slidingWindowSum = (slidingWindowSum + 2 * MOD) % MOD;

    // printf("dp[%d]=%lld\n", i, dp[i]);
  }

  printf("%lld", dp[H]);
  return 0;
}


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

long long fact[1000005];
long long dp[1000005];

const long long MOD = 1e9 + 7;

int main() {
  int N, H, D;
  scanf("%d%d%d", &N, &H, &D);

  long long retainWays = 0;
  fact[0] = 1;
  for (int i = 1; i <= N; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    retainWays += fact[i] % MOD;
  }
  retainWays %= MOD;

  // printf("retainWays=%lld\n", retainWays);

  dp[0] = fact[N];
  long long slidingWindowSum = dp[0];
  for (int i = 1; i <= H; i++) {
    dp[i] = slidingWindowSum;

    /*for(int j = 1; j <= min(i, D); j ++){
        dp[i] += dp[i-j];
    }*/

    dp[i] %= MOD;

    if (i == H) {
      break;
    }

    dp[i] = dp[i] * retainWays % MOD;
    slidingWindowSum += dp[i];
    if (i - D >= 0) {
      slidingWindowSum -= dp[i - D];
    }
    slidingWindowSum = (slidingWindowSum + 2 * MOD) % MOD;

    // printf("dp[%d]=%lld\n", i, dp[i]);
  }

  printf("%lld", dp[H]);
  return 0;
}
Problem p03009 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = x; i <= y; ++i)
#define repd(i, x, y) for (register int i = x; i >= y; --i)
using namespace std;
typedef long long ll;
template <typename T> inline void read(T &x) {
  char c;
  int sign = 1;
  x = 0;
  do {
    c = getchar();
    if (c == '-')
      sign = -1;
  } while (!isdigit(c));
  do {
    x = x * 10 + c - '0';
    c = getchar();
  } while (isdigit(c));
  x *= sign;
}

const int mod = 1e9 + 7, N = 2e5 + 50;
int f[N], fac[N], m, n, h, d;

inline int ksm(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = 1ll * ans * x % mod;
    y >>= 1;
    x = 1ll * x * x % mod;
  }
  return ans;
}
int main() {
  read(n);
  read(h);
  read(d);
  f[0] = 1;
  fac[0] = 1;
  rep(i, 1, n) fac[i] = 1ll * fac[i - 1] * i % mod;
  rep(i, 1, n) m = (m + fac[i]) % mod;

  rep(i, 1, h) {
    f[i] = 1ll * (f[i - 1] - (i - d - 1 >= 0 ? f[i - d - 1] : 0)) * m % mod;
    if (f[i] < 0)
      f[i] = f[i] + mod;

    f[i] = (f[i - 1] + f[i]) % mod;
  }

  cout << 1ll * (f[h] - f[h - 1] + mod) % mod * fac[n] % mod * ksm(m, mod - 2) %
              mod
       << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = x; i <= y; ++i)
#define repd(i, x, y) for (register int i = x; i >= y; --i)
using namespace std;
typedef long long ll;
template <typename T> inline void read(T &x) {
  char c;
  int sign = 1;
  x = 0;
  do {
    c = getchar();
    if (c == '-')
      sign = -1;
  } while (!isdigit(c));
  do {
    x = x * 10 + c - '0';
    c = getchar();
  } while (isdigit(c));
  x *= sign;
}

const int mod = 1e9 + 7, N = 1e6 + 50;
int f[N], fac[N], m, n, h, d;

inline int ksm(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = 1ll * ans * x % mod;
    y >>= 1;
    x = 1ll * x * x % mod;
  }
  return ans;
}
int main() {
  read(n);
  read(h);
  read(d);
  f[0] = 1;
  fac[0] = 1;
  rep(i, 1, n) fac[i] = 1ll * fac[i - 1] * i % mod;
  rep(i, 1, n) m = (m + fac[i]) % mod;

  rep(i, 1, h) {
    f[i] = 1ll * (f[i - 1] - (i - d - 1 >= 0 ? f[i - d - 1] : 0)) * m % mod;
    if (f[i] < 0)
      f[i] = f[i] + mod;

    f[i] = (f[i - 1] + f[i]) % mod;
  }

  cout << 1ll * (f[h] - f[h - 1] + mod) % mod * fac[n] % mod * ksm(m, mod - 2) %
              mod
       << endl;

  return 0;
}
Problem p03009 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;
const int N = 3e5 + 500;
const int LN = 21;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 57;

long long n, m, u, v, k, t, q, a, h, x, d;

long long fact[N];
long long dp[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> h >> d;

  fact[0] = 1;
  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    fact[i] = 1LL * i * fact[i - 1];
    fact[i] %= mod;
    sum += fact[i];
    if (sum >= mod)
      sum -= mod;
  }

  dp[0] = fact[n];
  for (int i = 1; i <= h - 1; ++i) {
    long long cur = dp[i - 1];
    if (i - d - 1 >= 0)
      cur -= dp[i - d - 1];
    if (cur < 0)
      cur += mod;
    dp[i] = (sum * cur) % mod;
    dp[i] += dp[i - 1];
    if (dp[i] >= mod)
      dp[i] -= mod;
  }

  long long cur = dp[h - 1];
  if (h - d - 1 >= 0)
    cur -= dp[h - d - 1];
  if (cur < 0)
    cur += mod;
  cout << cur << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;
const int N = 1e6 + 500;
const int LN = 21;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 57;

long long n, m, u, v, k, t, q, a, h, x, d;

long long fact[N];
long long dp[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> h >> d;

  fact[0] = 1;
  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    fact[i] = 1LL * i * fact[i - 1];
    fact[i] %= mod;
    sum += fact[i];
    if (sum >= mod)
      sum -= mod;
  }

  dp[0] = fact[n];
  for (int i = 1; i <= h - 1; ++i) {
    long long cur = dp[i - 1];
    if (i - d - 1 >= 0)
      cur -= dp[i - d - 1];
    if (cur < 0)
      cur += mod;
    dp[i] = (sum * cur) % mod;
    dp[i] += dp[i - 1];
    if (dp[i] >= mod)
      dp[i] -= mod;
  }

  long long cur = dp[h - 1];
  if (h - d - 1 >= 0)
    cur -= dp[h - d - 1];
  if (cur < 0)
    cur += mod;
  cout << cur << endl;
}
Problem p03011 (Python)

Buggy submission (status: Runtime Error):
s = map(int, input().split())
s.sort()
print(s[0] + s[1])


Fixed submission:
s = list(map(int, input().split()))
s.sort()
print(s[0] + s[1])
Problem p03011 (Python)

Buggy submission (status: Runtime Error):
p, q, r = map(int, input().split())
sorted_list = sorted(list[p, q, r])
ans = sorted_list[0] + sorted_list[1]
print(ans)


Fixed submission:
p, q, r = map(int, input().split())
sorted_list = sorted([p, q, r])
ans = sorted_list[0] + sorted_list[1]
print(ans)
Problem p03011 (Python)

Buggy submission (status: Runtime Error):
# coding: utf-8

p, q, r = [int(x) for x in input().rstrip().split(" ")]

s = []
s[0] = p + q
s[1] = q + r
s[2] = r + p

print(min(s))


Fixed submission:
# coding: utf-8

p, q, r = [int(x) for x in input().rstrip().split(" ")]

s = [0, 0, 0]
s[0] = p + q
s[1] = q + r
s[2] = r + p

print(min(s))
Problem p03011 (Python)

Buggy submission (status: Runtime Error):
A = [int(input()) for i in range(3)]

ans = sum(A) - max(A)
print(ans)


Fixed submission:
A = list(map(int, input().split()))
ans = sum(A) - max(A)
print(ans)
Problem p03011 (Python)

Buggy submission (status: Runtime Error):
p = list(map(int, input().split()))
print(p[0] + p[1] + [2] - max(p))


Fixed submission:
p = list(map(int, input().split()))
print(p[0] + p[1] + p[2] - max(p))
Problem p03011 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main() {
  vector<int> a(2);
  cin >> a.at(0) >> a.at(1) >> a.at(2);
  sort(a.begin(), a.end());
  cout << a.at(0) + a.at(1) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main() {
  vector<int> a(3);
  cin >> a.at(0) >> a.at(1) >> a.at(2);
  sort(a.begin(), a.end());
  cout << a.at(0) + a.at(1) << endl;
  return 0;
}
Problem p03011 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define pb push_back
#define sz size
#define f first
#define s second

int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int p, q, r;
  cin >> p >> q >> r;
  int sum = 0;
  sum += min(min((p + q), (p + r)), (r + q));
  cout << sum;

  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define pb push_back
#define sz size
#define f first
#define s second

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int p, q, r;
  cin >> p >> q >> r;
  int sum = 0;
  sum += min(min((p + q), (p + r)), (r + q));
  cout << sum;

  return 0;
}
Problem p03011 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int p, q, r;
  while (true) {
    cin >> p >> q >> r;
    cout << p + q + r - max({p, q, r}) << endl;
  }
}


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

int main() {
  int p, q, r;
  cin >> p >> q >> r;
  cout << p + q + r - max({p, q, r}) << endl;
}
Problem p03011 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main() {
  int P, Q, R;
  int min;
  while (scanf("%d %d %d", &P, &Q, &R)) {
    min = P + Q;
    if (P + R < min)
      min = P + R;
    if (Q + R < min)
      min = Q + R;
    printf("%d\n", min);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int P, Q, R, min, i;
  for (i = 0; i < 100; i++) {
    if (scanf("%d %d %d", &P, &Q, &R) != 3)
      break;
    min = P + Q;
    if (P + R < min)
      min = P + R;
    if (Q + R < min)
      min = Q + R;
    printf("%d\n", min);
  }
  return 0;
}