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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < (NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()

#define cMOD 1000000007ULL
#define cINF ((1ll << 62) - 1)
#define cINFINT ((1 << 30) - 1)

ll knapsackNonLimit(const vector<ll> &v, const vector<ll> &w, ll maxW) {
  ll n = min(v.size(), w.size());
  vector<vector<ll>> dp(n + 1, vector<ll>(maxW + 1, 0));
  REP(i, n) REP(j, maxW + 1) {
    if (j < w[i])
      dp[i + 1][j] = dp[i][j];
    else
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
  }
  return dp[n][maxW];
}

int main() {
  ull n = 0;
  cin >> n;
  vector<ll> a(3, 0), b(3, 0);
  cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1] >> b[2];

  auto v0 = b;
  v0.push_back(1);
  auto w0 = a;
  w0.push_back(1);
  auto v1 = a;
  v1.push_back(1);
  auto w1 = b;
  w1.push_back(1);

  ll n1 = knapsackNonLimit(v0, w0, n);
  ll n2 = knapsackNonLimit(v1, w1, n1);

  cout << n2 << endl;
  return 0;
}

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < (NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()

#define cMOD 1000000007ULL
#define cINF ((1ll << 62) - 1)
#define cINFINT ((1 << 30) - 1)

ll knapsackNonLimit(const vector<ll> &v, const vector<ll> &w, ll maxW) {
  ll n = min(v.size(), w.size());
  vector<ll> dp(maxW + 1, 0);
  REP(i, n)
  REP(j, maxW + 1) if (j >= w[i]) dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
  return dp[maxW];
}

int main() {
  ull n = 0;
  cin >> n;
  vector<ll> a(3, 0), b(3, 0);
  cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1] >> b[2];

  auto v0 = b;
  v0.push_back(1);
  auto w0 = a;
  w0.push_back(1);
  auto v1 = a;
  v1.push_back(1);
  auto w1 = b;
  w1.push_back(1);

  ll n1 = knapsackNonLimit(v0, w0, n);
  ll n2 = knapsackNonLimit(v1, w1, n1);

  cout << n2 << endl;
  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define loop(n, i) for (int i = 0; i < n; i++)
#define loop1(n, i) for (int i = 1; i <= n; i++)
#define rloop(n, i) for (int i = n; i; i++)
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;

const int MAX = 5000 * 5000 + 10;
ll G[2][3];
ll dp[MAX];

int main() {
  int n;
  cin >> n;
  loop(2, i) loop(3, j) cin >> G[i][j];

  fill(dp, dp + MAX, 0);
  loop(n, i) {
    dp[i + 1] = max(dp[i + 1], dp[i] + 1);
    loop(3, j) {
      int k = i + G[0][j];
      dp[k] = max(dp[k], dp[i] + G[1][j]);
    }
  }

  int m = dp[n];
  fill(dp, dp + MAX, 0);
  loop(m, i) {
    dp[i + 1] = max(dp[i + 1], dp[i] + 1);
    loop(3, j) {
      int k = i + G[1][j];
      dp[k] = max(dp[k], dp[i] + G[0][j]);
    }
  }

  cout << dp[m] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define loop(n, i) for (int i = 0; i < n; i++)
#define loop1(n, i) for (int i = 1; i <= n; i++)
#define rloop(n, i) for (int i = n; i; i++)
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;

const int MAX = 5000 * 5100;
ll G[2][3];
ll dp[MAX];

int main() {
  int n;
  cin >> n;
  loop(2, i) loop(3, j) cin >> G[i][j];

  fill(dp, dp + MAX, 0);
  loop(n, i) {
    dp[i + 1] = max(dp[i + 1], dp[i] + 1);
    loop(3, j) {
      int k = i + G[0][j];
      dp[k] = max(dp[k], dp[i] + G[1][j]);
    }
  }

  int m = dp[n];
  fill(dp, dp + MAX, 0);
  loop(m, i) {
    dp[i + 1] = max(dp[i + 1], dp[i] + 1);
    loop(3, j) {
      int k = i + G[1][j];
      dp[k] = max(dp[k], dp[i] + G[0][j]);
    }
  }

  cout << dp[m] << endl;
  return 0;
}
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;

using ll = long long;

int main() {
  int N, a[2][3], b[3];
  cin >> N;
  rep(i, 2) rep(j, 3) cin >> a[i][j];
  rep(i, 3) b[i] = a[1][i] - a[0][i];
  vector<vector<ll>> dp1(4, vector<ll>(N + 1, 0));
  rep(i, 3) {
    rep(j, N + 1) {
      dp1[i + 1][j] = dp1[i][j];
      if (j >= a[0][i]) {
        ll x = dp1[i + 1][j - a[0][i]] + b[i];
        if (x > dp1[i + 1][j])
          dp1[i + 1][j] = x;
      }
    }
  }
  N += dp1[3][N];
  vector<vector<ll>> dp2(4, vector<ll>(N + 1, 0));
  rep(i, 3) {
    rep(j, N + 1) {
      dp2[i + 1][j] = dp2[i][j];
      if (j >= a[1][i]) {
        ll x = dp2[i + 1][j - a[0][i]] - b[i];
        if (x > dp2[i + 1][j])
          dp2[i + 1][j] = x;
      }
    }
  }
  cout << N + dp2[3][N] << endl;
}

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

using ll = long long;

int main() {
  int N, a[2][3], b[3];
  cin >> N;
  rep(i, 2) rep(j, 3) cin >> a[i][j];
  rep(i, 3) b[i] = a[1][i] - a[0][i];
  vector<vector<ll>> dp1(4, vector<ll>(N + 1, 0));
  rep(i, 3) {
    rep(j, N + 1) {
      dp1[i + 1][j] = dp1[i][j];
      if (j >= a[0][i]) {
        ll x = dp1[i + 1][j - a[0][i]] + b[i];
        if (x > dp1[i + 1][j])
          dp1[i + 1][j] = x;
      }
    }
  }
  N += dp1[3][N];
  vector<vector<ll>> dp2(4, vector<ll>(N + 1, 0));
  rep(i, 3) {
    rep(j, N + 1) {
      dp2[i + 1][j] = dp2[i][j];
      if (j >= a[1][i]) {
        ll x = dp2[i + 1][j - a[1][i]] - b[i];
        if (x > dp2[i + 1][j])
          dp2[i + 1][j] = x;
      }
    }
  }
  cout << N + dp2[3][N] << endl;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <unordered_map>
#include <vector>

std::vector<int64_t> dp;
int main(void) {
  int64_t n;
  std::vector<int64_t> g(2), s(2), b(2);
  std::cin >> n;
  for (int i = 0; i < 2; i++) {
    std::cin >> g[i] >> s[i] >> b[i];
  }
  dp.resize(5002);
  int64_t gain[3];
  gain[0] = g[1] - g[0];
  gain[1] = s[1] - s[0];
  gain[2] = b[1] - b[0];

  for (int i = 0; i <= dp.size(); i++)
    dp[i] = n;
  for (int i = 0; i <= n; i++) {
    dp[(i + g[0]) % 5001] =
        std::max(dp[(i + g[0]) % 5001], dp[i % 5001] + gain[0]);
    dp[(i + s[0]) % 5001] =
        std::max(dp[(i + s[0]) % 5001], dp[i % 5001] + gain[1]);
    dp[(i + b[0]) % 5001] =
        std::max(dp[(i + b[0]) % 5001], dp[i % 5001] + gain[2]);
    if (i != n)
      dp[i % 5001] = n;
  }
  n = dp[n % 5001];
  for (int i = 0; i < 3; i++)
    gain[i] *= -1;
  for (int i = 0; i <= dp.size(); i++)
    dp[i] = n;
  for (int i = 0; i <= n; i++) {
    dp[(i + g[1]) % 5001] =
        std::max(dp[(i + g[1]) % 5001], dp[i % 5001] + gain[0]);
    dp[(i + s[1]) % 5001] =
        std::max(dp[(i + s[1]) % 5001], dp[i % 5001] + gain[1]);
    dp[(i + b[1]) % 5001] =
        std::max(dp[(i + b[1]) % 5001], dp[i % 5001] + gain[2]);
    if (i != n)
      dp[i % 5001] = n;
  }
  std::cout << dp[n] << std::endl;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <unordered_map>
#include <vector>

std::vector<int64_t> dp;
int main(void) {
  int64_t n;
  std::vector<int64_t> g(2), s(2), b(2);
  std::cin >> n;
  for (int i = 0; i < 2; i++) {
    std::cin >> g[i] >> s[i] >> b[i];
  }
  dp.resize(5002);
  int64_t gain[3];
  gain[0] = g[1] - g[0];
  gain[1] = s[1] - s[0];
  gain[2] = b[1] - b[0];

  for (int i = 0; i <= dp.size(); i++)
    dp[i] = n;
  for (int i = 0; i <= n; i++) {
    dp[(i + g[0]) % 5001] =
        std::max(dp[(i + g[0]) % 5001], dp[i % 5001] + gain[0]);
    dp[(i + s[0]) % 5001] =
        std::max(dp[(i + s[0]) % 5001], dp[i % 5001] + gain[1]);
    dp[(i + b[0]) % 5001] =
        std::max(dp[(i + b[0]) % 5001], dp[i % 5001] + gain[2]);
    if (i != n)
      dp[i % 5001] = n;
  }
  n = dp[n % 5001];
  for (int i = 0; i < 3; i++)
    gain[i] *= -1;
  for (int i = 0; i <= dp.size(); i++)
    dp[i] = n;
  for (int i = 0; i <= n; i++) {
    dp[(i + g[1]) % 5001] =
        std::max(dp[(i + g[1]) % 5001], dp[i % 5001] + gain[0]);
    dp[(i + s[1]) % 5001] =
        std::max(dp[(i + s[1]) % 5001], dp[i % 5001] + gain[1]);
    dp[(i + b[1]) % 5001] =
        std::max(dp[(i + b[1]) % 5001], dp[i % 5001] + gain[2]);
    if (i != n)
      dp[i % 5001] = n;
  }
  std::cout << dp[n % 5001] << std::endl;
}
Problem p03008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

int solve(int n, vector<int> a, vector<int> b) {

  vector<int> r(3);
  for (int i = 0; i < 3; ++i) {
    if (a[i] >= b[i]) {
      r[i] = 0;
    } else {
      r[i] = n / a[i];
    }
  }

  int ans = n;
  for (int i = 0; i <= r[0]; ++i) {
    for (int j = 0; i * a[0] + j * a[1] <= n && j <= r[1]; ++j) {

      int h = n - i * a[0] - j * a[1];
      int k = h / a[2];
      ans = max(ans,
                n + i * (b[0] - a[0]) + j * (b[1] - a[1]) + k * (b[2] - a[2]));
      ans = max(ans, n + i * (b[0] - a[0]) + j * (b[1] - a[1]));
    }
  }
  return ans;
}

signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  int n;
  cin >> n;
  vector<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];

  n = solve(n, a, b);
  n = solve(n, b, a);
  cout << n << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

int solve(int n, vector<int> a, vector<int> b) {

  auto comp = [&](int i, int j) { return b[i] - a[i] < b[j] - a[j]; };

  vector<int> per;
  for (int i = 0; i < 3; ++i)
    per.app(i);
  sort(all(per), comp);

  vector<int> a1(3), b1(3);
  for (int i = 0; i < 3; ++i) {
    a1[i] = a[per[i]];
    b1[i] = b[per[i]];
  }
  a = a1;
  b = b1;

  vector<int> r(3);
  for (int i = 0; i < 3; ++i) {
    if (a[i] >= b[i]) {
      r[i] = 0;
    } else {
      r[i] = n / a[i];
    }
  }

  int ans = n;
  for (int i = 0; i <= r[0]; ++i) {
    for (int j = 0; i * a[0] + j * a[1] <= n && j <= r[1]; ++j) {

      int h = n - i * a[0] - j * a[1];
      int k = h / a[2];
      ans = max(ans,
                n + i * (b[0] - a[0]) + j * (b[1] - a[1]) + k * (b[2] - a[2]));
      ans = max(ans, n + i * (b[0] - a[0]) + j * (b[1] - a[1]));
    }
  }
  return ans;
}

signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  int n;
  cin >> n;
  vector<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];

  n = solve(n, a, b);
  n = solve(n, b, a);
  cout << n << endl;
}
Problem p03008 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]


def count(n, a, b):
    a0, a1, a2 = a
    b0, b1, b2 = b
    dp = [0] * (n + 1)
    for i in range(n + 1):
        c0 = dp[i - a0] + b0 if i - a0 >= 0 else 0
        c1 = dp[i - a1] + b1 if i - a1 >= 0 else 0
        c2 = dp[i - a2] + b2 if i - a2 >= 0 else 0
        dp[i] = max([i, c0, c1, c2])
    return dp[n]


print(count(count(N, A, B), B, A))


Fixed submission:
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]


def count(n, a, b):
    a0, a1, a2 = a
    b0, b1, b2 = b
    dp = [0] * (n + 1)
    for i in range(n + 1):
        c0 = dp[i - a0] + b0 if i - a0 >= 0 else 0
        c1 = dp[i - a1] + b1 if i - a1 >= 0 else 0
        c2 = dp[i - a2] + b2 if i - a2 >= 0 else 0
        dp[i] = max(i, c0, c1, c2)
    return dp[n]


print(count(count(N, A, B), B, A))
Problem p03008 (C++)

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

int main() {
  long n, n1 = 0, n2 = 0, g1, s1, b1, g2, s2, b2;
  cin >> n >> g1 >> s1 >> b1 >> g2 >> s2 >> b2;

  for (long i = 0; i * g1 <= n; i++) {
    for (long j = 0; i * g1 + j * s1 <= n; j++) {
      n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1));
      n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1) / b1 * b2 +
                       (n - i * g1 - j * s1) % b1);
    }
  }
  for (long i = 0; i * g2 <= n1; i++) {
    for (long j = 0; i * g2 + j * s2 <= n1; j++) {
      n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2));
      n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2) / b2 * b1 +
                       (n1 - i * g2 - j * s2) % b2);
    }
  }
  cout << n2 << endl;
}

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

int main() {
  long n, n1 = 0, n2 = 0, g1, s1, b1, g2, s2, b2;
  cin >> n >> g1 >> s1 >> b1 >> g2 >> s2 >> b2;

  for (long i = 0; i * g1 <= n; i++) {
    for (long j = 0; i * g1 + j * s1 <= n; j++) {
      n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1));
      n1 = max(n1, i * g2 + j * s2 + (n - i * g1 - j * s1) / b1 * b2 +
                       (n - i * g1 - j * s1) % b1);
    }
  }
  vector<long> v2, v1;
  if (g2 < g1) {
    v2.push_back(g2);
    v1.push_back(g1);
  }
  if (s2 < s1) {
    v2.push_back(s2);
    v1.push_back(s1);
  }
  if (b2 < b1) {
    v2.push_back(b2);
    v1.push_back(b1);
  }
  if (v2.size() == 0)
    n2 = n1;
  if (v2.size() == 1)
    n2 = n1 / v2[0] * v1[0] + n1 % v2[0];
  if (v2.size() == 2) {
    for (long i = 0; i * v2[0] <= n1; i++) {
      n2 = max(n2, i * v1[0] + (n1 - i * v1[0]));
      n2 = max(n2, i * v1[0] + (n1 - i * v2[0]) / v2[1] * v1[1] +
                       (n1 - i * v2[0]) % v2[1]);
    }
  }
  if (v2.size() == 3) {
    for (long i = 0; i * g2 <= n1; i++) {
      for (long j = 0; i * g2 + j * s2 <= n1; j++) {
        n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2));
        n2 = max(n2, i * g1 + j * s1 + (n1 - i * g2 - j * s2) / b2 * b1 +
                         (n1 - i * g2 - j * s2) % b2);
      }
    }
  }
  cout << n2 << endl;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

ll N;
ll n[2][3];
ll dp[2500001];

int main() {
  cin >> N;
  rep(i, 2) rep(j, 3) cin >> n[i][j];
  dp[N] = N;
  ll ans = 0;
  rep(i, 3) if (n[0][i] < n[1][i]) {
    for (int j = N; j >= n[0][i]; j--)
      dp[j - n[0][i]] = max(dp[j - n[0][i]], dp[j] + n[1][i] - n[0][i]);
  }
  rep(i, N + 1) ans = max(ans, dp[i]);
  rep(i, N + 1) dp[i] = 0;
  dp[ans] = ans;
  rep(i, 3) if (n[0][i] > n[1][i]) {
    for (int j = ans; j >= n[1][i]; j--)
      dp[j - n[1][i]] = max(dp[j - n[1][i]], dp[j] + n[0][i] - n[1][i]);
  }

  ll X = ans;
  rep(i, X + 1) ans = max(ans, dp[i]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

ll N;
ll n[2][3];
ll dp[25000001];

int main() {
  cin >> N;
  rep(i, 2) rep(j, 3) cin >> n[i][j];
  dp[N] = N;
  ll ans = 0;
  rep(i, 3) if (n[0][i] < n[1][i]) {
    for (int j = N; j >= n[0][i]; j--)
      dp[j - n[0][i]] = max(dp[j - n[0][i]], dp[j] + n[1][i] - n[0][i]);
  }
  rep(i, N + 1) ans = max(ans, dp[i]);
  rep(i, N + 1) dp[i] = 0;
  dp[ans] = ans;
  rep(i, 3) if (n[0][i] > n[1][i]) {
    for (int j = ans; j >= n[1][i]; j--)
      dp[j - n[1][i]] = max(dp[j - n[1][i]], dp[j] + n[0][i] - n[1][i]);
  }

  ll X = ans;
  rep(i, X + 1) ans = max(ans, dp[i]);
  cout << ans << endl;
}
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[5][5050], dp2[5][100010];
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[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
  ll m = dp[4][n];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= m; j++) {
      if (j < w2[i]) {
        dp2[i + 1][j] = dp2[i][j];
      } else {
        dp2[i + 1][j] = max(dp2[i][j], dp2[i + 1][j - w2[i]] + v2[i]);
      }
    }
  }
  cout << dp2[4][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[5][5050], dp2[5][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[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
  ll m = dp[4][n];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= m; j++) {
      if (j < w2[i]) {
        dp2[i + 1][j] = dp2[i][j];
      } else {
        dp2[i + 1][j] = max(dp2[i][j], dp2[i + 1][j - w2[i]] + v2[i]);
      }
    }
  }
  cout << dp2[4][m] << endl;
  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int A[2][3];
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> A[i][j];
    }
  }

  int mx = 0;
  int s;
  for (int z = 0; z < 2; z++) {
    mx = 0;
    int tt = 1 + (N / A[z][0]);
    for (int i = 0; i <= tt; i++) {
      for (int j = 0; j <= N; j++) {
        int r = N - i * A[z][0] - j * A[z][1];
        if (r < 0) {
          break;
        } else {
          s = A[1 - z][0] * i + A[1 - z][1] * j;
          mx = max(mx, r + s);

          int k = r / A[z][2];
          r -= k * A[z][2];

          mx = max(mx, r + s + A[1 - z][2] * k);
        }
      }
    }
    N = mx;
  }

  cout << N << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int A[2][3];
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> A[i][j];
    }
  }

  int mx = 0;
  int s;
  for (int z = 0; z < 2; z++) {
    mx = 0;
    int tt = 1 + (N / A[z][0]);
    for (int i = 0; i <= tt; i++) {
      if (i == 5001)
        i = max(i, tt - 5001);

      int zz = 1 + ((N - i * A[z][0]) / A[z][1]);
      for (int j = 0; j <= zz; j++) {
        if (j == 5001)
          j = max(j, zz - 5001);

        int r = N - i * A[z][0] - j * A[z][1];
        if (r < 0) {
          break;
        } else {
          s = A[1 - z][0] * i + A[1 - z][1] * j;
          mx = max(mx, r + s);

          int k = r / A[z][2];
          r -= k * A[z][2];

          mx = max(mx, r + s + A[1 - z][2] * k);
        }
      }
    }
    N = mx;
  }

  cout << N << endl;
}
Problem p03008 (C++)

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

int MAX_N = (1 << 25);
long long N, SA, SB, SC, TA, TB, TC;
long long dp[1 << 25], dp2[1 << 25];

int main() {
  cin >> N >> SA >> SB >> SC >> TA >> TB >> TC;

  for (int i = 1; i < MAX_N; i++)
    dp[i] = -(1 << 30);
  dp[0] = N;
  for (int i = 0; i < MAX_N - TA; i++)
    dp[i + TA] = max(dp[i + TA], dp[i] - SA);
  for (int i = 0; i < MAX_N - TB; i++)
    dp[i + TB] = max(dp[i + TB], dp[i] - SB);
  for (int i = 0; i < MAX_N - TC; i++)
    dp[i + TC] = max(dp[i + TC], dp[i] - SC);

  for (int i = 1; i < MAX_N; i++)
    dp2[i] = -(1 << 30);
  dp2[0] = 0;
  for (int i = 0; i < MAX_N - TA; i++)
    dp2[i + TA] = max(dp2[i + TA], dp2[i] + SA);
  for (int i = 0; i < MAX_N - TB; i++)
    dp2[i + TB] = max(dp2[i + TB], dp2[i] + SB);
  for (int i = 0; i < MAX_N - TC; i++)
    dp2[i + TC] = max(dp2[i + TC], dp2[i] + SC);
  for (int i = 0; i < MAX_N; i++)
    dp2[i + 1] = max(dp2[i + 1], dp2[i] + 1LL);

  long long maxn = 0;
  for (int i = 0; i < MAX_N; i++) {
    if (dp[i] < 0)
      continue;
    maxn = max(maxn, dp2[i + dp[i]]);
  }
  cout << maxn << endl;
  return 0;
}

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

int MAX_N = (1 << 25);
long long N, SA, SB, SC, TA, TB, TC;
long long dp[1 << 25], dp2[1 << 25];

int main() {
  cin >> N >> SA >> SB >> SC >> TA >> TB >> TC;

  for (int i = 1; i < MAX_N; i++)
    dp[i] = -(1 << 30);
  dp[0] = N;
  for (int i = 0; i < MAX_N - TA; i++)
    dp[i + TA] = max(dp[i + TA], dp[i] - SA);
  for (int i = 0; i < MAX_N - TB; i++)
    dp[i + TB] = max(dp[i + TB], dp[i] - SB);
  for (int i = 0; i < MAX_N - TC; i++)
    dp[i + TC] = max(dp[i + TC], dp[i] - SC);

  for (int i = 1; i < MAX_N; i++)
    dp2[i] = -(1 << 30);
  dp2[0] = 0;
  for (int i = 0; i < MAX_N - TA; i++)
    dp2[i + TA] = max(dp2[i + TA], dp2[i] + SA);
  for (int i = 0; i < MAX_N - TB; i++)
    dp2[i + TB] = max(dp2[i + TB], dp2[i] + SB);
  for (int i = 0; i < MAX_N - TC; i++)
    dp2[i + TC] = max(dp2[i + TC], dp2[i] + SC);
  for (int i = 0; i < MAX_N - 1; i++)
    dp2[i + 1] = max(dp2[i + 1], dp2[i] + 1LL);

  long long maxn = 0;
  for (int i = 0; i < MAX_N; i++) {
    if (dp[i] < 0)
      continue;
    maxn = max(maxn, dp2[i + dp[i]]);
  }
  cout << maxn << endl;
  return 0;
}
Problem p03008 (C++)

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

using namespace std;

long long int N, g[3], s[3], b[3];

long long int dp[5555];
long long int dp2[3000000];

int main(void) {

  cin >> N;

  for (int i = 1; i <= 2; i++) {
    cin >> g[i] >> s[i] >> b[i];
  }

  long long int M = 0;

  for (int i = 1; i <= N; i++) {
    dp[i] = i;
    if (i - g[1] >= 0) {
      dp[i] = max(dp[i], dp[i - g[1]] + g[2]);
    }
    if (i - s[1] >= 0) {
      dp[i] = max(dp[i], dp[i - s[1]] + s[2]);
    }
    if (i - b[1] >= 0) {
      dp[i] = max(dp[i], dp[i - b[1]] + b[2]);
    }
    M = max(M, dp[i]);
  }

  //=============================
  long long int ans = 0;

  for (long long int i = 1; i <= M; i++) {
    dp2[i] = i;
    if (i - g[2] >= 0) {
      dp2[i] = max(dp2[i], dp2[i - g[2]] + g[1]);
    }
    if (i - s[2] >= 0) {
      dp2[i] = max(dp2[i], dp2[i - s[2]] + s[1]);
    }
    if (i - b[2] >= 0) {
      dp2[i] = max(dp2[i], dp2[i - b[2]] + b[1]);
    }

    ans = max(ans, dp2[i]);
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

long long int N, g[3], s[3], b[3];

long long int dp[5555];
long long int dp2[30000000];

int main(void) {

  cin >> N;

  for (int i = 1; i <= 2; i++) {
    cin >> g[i] >> s[i] >> b[i];
  }

  long long int M = 0;

  for (int i = 1; i <= N; i++) {
    dp[i] = i;
    if (i - g[1] >= 0) {
      dp[i] = max(dp[i], dp[i - g[1]] + g[2]);
    }
    if (i - s[1] >= 0) {
      dp[i] = max(dp[i], dp[i - s[1]] + s[2]);
    }
    if (i - b[1] >= 0) {
      dp[i] = max(dp[i], dp[i - b[1]] + b[2]);
    }
    M = max(M, dp[i]);
  }

  //=============================
  long long int ans = 0;

  for (long long int i = 1; i <= M; i++) {
    dp2[i] = i;
    if (i - g[2] >= 0) {
      dp2[i] = max(dp2[i], dp2[i - g[2]] + g[1]);
    }
    if (i - s[2] >= 0) {
      dp2[i] = max(dp2[i], dp2[i - s[2]] + s[1]);
    }
    if (i - b[2] >= 0) {
      dp2[i] = max(dp2[i], dp2[i - b[2]] + b[1]);
    }

    ans = max(ans, dp2[i]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#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_popcountll

#define INF 1e16
#define mod 1000000007

ll n;
ll g[2], s[2], b[2];
ll dp[30000000];

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

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

  rep(i, 2) {
    ll j = 1 - i;
    rep(k, n + 1) dp[k] = -INF;
    dp[0] = 0;
    rep(k, n + 1) {
      if (k - g[i] >= 0)
        maxch(dp[k], dp[k - g[i]] + max(g[i], g[j]));
      if (k - s[i] >= 0)
        maxch(dp[k], dp[k - s[i]] + max(s[i], s[j]));
      if (k - b[i] >= 0)
        maxch(dp[k], dp[k - b[i]] + max(b[i], b[j]));
    }
    ll nxt = dp[n];
    n = nxt;
  }
  cout << n << 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_popcountll

#define INF 1e16
#define mod 1000000007

ll n;
ll g[2], s[2], b[2];
ll dp[30000000];

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

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

  rep(i, 2) {
    ll j = 1 - i;
    rep(k, n + 1) dp[k] = k;
    dp[0] = 0;
    rep(k, n + 1) {
      if (k - g[i] >= 0)
        maxch(dp[k], dp[k - g[i]] + max(g[i], g[j]));
      if (k - s[i] >= 0)
        maxch(dp[k], dp[k - s[i]] + max(s[i], s[j]));
      if (k - b[i] >= 0)
        maxch(dp[k], dp[k - b[i]] + max(b[i], b[j]));
    }
    ll nxt = dp[n];
    n = nxt;
  }
  cout << n << endl;

  return 0;
}
Problem p03008 (C++)

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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, memo[10][10], dp[10000];

signed main() {
  cin >> n;
  rep(i, 2) rep(j, 3) cin >> memo[j][i];
  rep(i, 3) {
    rep(j, n - memo[i][0] + 1) {
      dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]);
    }
  }
  rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1);
  n = dp[n];
  rep(i, 6000) dp[i] = 0;
  rep(i, 3) swap(memo[i][0], memo[i][1]);
  rep(i, 3) {
    rep(j, n - memo[i][0] + 1) {
      dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]);
    }
  }
  rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1);
  cout << dp[n] << endl;
}


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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, memo[10][10], dp[25000000];

signed main() {
  cin >> n;
  rep(i, 2) rep(j, 3) cin >> memo[j][i];
  rep(i, 3) {
    rep(j, n - memo[i][0] + 1) {
      dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]);
    }
  }
  rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1);
  n = dp[n];
  rep(i, 6000) dp[i] = 0;
  rep(i, 3) swap(memo[i][0], memo[i][1]);
  rep(i, 3) {
    rep(j, n - memo[i][0] + 1) {
      dp[j + memo[i][0]] = max(dp[j + memo[i][0]], dp[j] + memo[i][1]);
    }
  }
  rep1(i, n) dp[i] = max(dp[i], dp[i - 1] + 1);
  cout << dp[n] << endl;
}
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
// by xxj
#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define ll long long
#define pii pair<int, int>
#define lowbit(x) x & -x
const int inf = 1e9 + 7;
const double eps = 1e-10;
const ll linf = 1e18 + 7;
const ll hh = 523;
// const int mod=;
ll dp[5007];
int a[3], b[3];
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  scanf("%d", &n);
  for (int i = 0; i < 3; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < 3; i++) {
    scanf("%d", b + i);
  }
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  ll mx = n;
  for (int i = 0; i < 3; i++) {
    for (int j = a[i]; j <= n; j++) {
      if (dp[j - a[i]] != -1) {
        dp[j] = max(dp[j], dp[j - a[i]] + b[i]);
        mx = max(mx, n - j + dp[j]);
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  ll ans = mx;
  for (int i = 0; i < 3; i++) {
    for (int j = b[i]; j <= mx; j++) {
      if (dp[j - b[i]] != -1) {
        dp[j] = max(dp[j], dp[j - b[i]] + a[i]);
        ans = max(ans, dp[j] + mx - j);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
/*
input:
*/


Fixed submission:
// by xxj
#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define ll long long
#define pii pair<int, int>
#define lowbit(x) x & -x
const int inf = 1e9 + 7;
const double eps = 1e-10;
const ll linf = 1e18 + 7;
const ll hh = 523;
// const int mod=;
ll dp[25000007];
int a[3], b[3];
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  scanf("%d", &n);
  for (int i = 0; i < 3; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < 3; i++) {
    scanf("%d", b + i);
  }
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  ll mx = n;
  for (int i = 0; i < 3; i++) {
    for (int j = a[i]; j <= n; j++) {
      if (dp[j - a[i]] != -1) {
        dp[j] = max(dp[j], dp[j - a[i]] + b[i]);
        mx = max(mx, n - j + dp[j]);
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  ll ans = mx;
  for (int i = 0; i < 3; i++) {
    for (int j = b[i]; j <= mx; j++) {
      if (dp[j - b[i]] != -1) {
        dp[j] = max(dp[j], dp[j - b[i]] + a[i]);
        ans = max(ans, dp[j] + mx - j);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
/*
input:
*/
Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<LL, LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x) * (x))
#define pdd pair<double, double>
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
#define N 200005
#define rank Rank
#define index Index
LL dp[2500005];
int main() {
  LL n;
  scanf("%lld", &n);
  pll p[4];
  scanf("%lld %lld %lld %lld %lld %lld", &p[0].x, &p[1].x, &p[2].x, &p[0].y,
        &p[1].y, &p[2].y);
  p[3] = mp(1, 1);
  MEM(dp);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= n - p[i].x; j++) {
      dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y);
    }
    swap(p[i].x, p[i].y);
  }
  LL temp = 0;
  for (int i = 0; i <= n; i++)
    temp = max(temp, dp[i]);
  // printf("%lld\n",temp);
  n = temp;
  MEM(dp);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= n - p[i].x; j++) {
      dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y);
    }
    // printf("%d %d\n",p[i].x,p[i].y);
  }
  printf("%lld\n", dp[n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<LL, LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x) * (x))
#define pdd pair<double, double>
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
#define N 200005
#define rank Rank
#define index Index
LL dp[25000005];
int main() {
  LL n;
  scanf("%lld", &n);
  pll p[4];
  scanf("%lld %lld %lld %lld %lld %lld", &p[0].x, &p[1].x, &p[2].x, &p[0].y,
        &p[1].y, &p[2].y);
  p[3] = mp(1, 1);
  MEM(dp);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= n - p[i].x; j++) {
      dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y);
    }
    swap(p[i].x, p[i].y);
  }
  LL temp = 0;
  for (int i = 0; i <= n; i++)
    temp = max(temp, dp[i]);
  // printf("%lld\n",temp);
  n = temp;
  MEM(dp);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= n - p[i].x; j++) {
      dp[j + p[i].x] = max(dp[j + p[i].x], dp[j] + p[i].y);
    }
    // printf("%d %d\n",p[i].x,p[i].y);
  }
  printf("%lld\n", dp[n]);
  return 0;
}
Problem p03008 (C++)

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

int main() {
  int n;
  ll a[3], b[3];
  cin >> n;
  for (int i = 0; i < 3; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < 3; i++) {
    cin >> b[i];
  }
  ll maxa2b = n;
  for (int x = 0; x <= n; x++) {
    if (a[0] * x > n)
      break;
    for (int y = 0; y <= n; y++) {
      if (a[1] * y > n - a[0] * x)
        break;
      int z = (n - a[0] * x - a[1] * y) / a[2];
      ll change =
          b[0] * x + b[1] * y + b[2] * z + (n - a[0] * x - a[1] * y) % a[2];
      maxa2b = max(maxa2b, change);
    }
  }
  ll maxb2a = maxa2b;
  for (int x = 0; x <= maxa2b; x++) {
    if (b[0] * x > maxa2b)
      break;
    for (int y = 0; y <= maxa2b; y++) {
      if (b[1] * y > maxa2b - b[0] * x)
        break;
      int z = (maxa2b - b[0] * x - b[1] * y) / b[2];
      ll change = a[0] * x + a[1] * y + a[2] * z +
                  (maxa2b - b[0] * x - b[1] * y) % b[2];
      maxb2a = max(maxb2a, change);
    }
  }
  cout << maxb2a << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  int n;
  ll a[3], b[3];
  cin >> n;
  for (int i = 0; i < 3; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < 3; i++) {
    cin >> b[i];
  }
  ll maxa2b = n;
  for (int x = 0; x <= n; x++) {
    if (a[0] * x > n)
      break;
    for (int y = 0; y <= n; y++) {
      if (a[1] * y > n - a[0] * x)
        break;
      int z = (n - a[0] * x - a[1] * y) / a[2];
      ll change =
          b[0] * x + b[1] * y + b[2] * z + (n - a[0] * x - a[1] * y) % a[2];
      maxa2b = max(maxa2b, change);
    }
  }
  ll maxb2a = maxa2b;
  if (a[0] < b[0] && a[1] < b[1] && a[2] < b[2]) {
    cout << maxb2a << endl;
    return 0;
  }
  if (a[0] < b[0]) {
    for (int y = 0; y <= maxa2b; y++) {
      if (b[1] * y > maxa2b)
        break;
      int z = (maxa2b - b[1] * y) / b[2];
      ll change = a[1] * y + a[2] * z + (maxa2b - b[1] * y) % b[2];
      maxb2a = max(maxb2a, change);
    }
    cout << maxb2a << endl;
    return 0;
  }
  if (a[1] < b[1]) {
    for (int x = 0; x <= maxa2b; x++) {
      if (b[0] * x > maxa2b)
        break;
      int z = (maxa2b - b[0] * x) / b[2];
      ll change = a[0] * x + a[2] * z + (maxa2b - b[0] * x) % b[2];
      maxb2a = max(maxb2a, change);
    }
    cout << maxb2a << endl;
    return 0;
  }
  if (a[2] < b[2]) {
    for (int y = 0; y <= maxa2b; y++) {
      if (b[1] * y > maxa2b)
        break;
      int x = (maxa2b - b[1] * y) / b[0];
      ll change = a[1] * y + a[0] * x + (maxa2b - b[1] * y) % b[0];
      maxb2a = max(maxb2a, change);
    }
    cout << maxb2a << endl;
    return 0;
  }
  for (int x = 0; x <= maxa2b; x++) {
    if (b[0] * x > maxa2b)
      break;
    for (int y = 0; y <= maxa2b; y++) {
      if (b[1] * y > maxa2b - b[0] * x)
        break;
      int z = (maxa2b - b[0] * x - b[1] * y) / b[2];
      ll change = a[0] * x + a[1] * y + a[2] * z +
                  (maxa2b - b[0] * x - b[1] * y) % b[2];
      maxb2a = max(maxb2a, change);
    }
  }
  cout << maxb2a << endl;
}
Problem p03008 (C++)

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

long long trade(long long n, int g_A, int s_A, int b_A, int g_B, int s_B,
                int b_B) {
  long long n_Max = n;

  for (int g = 0; g * g_A <= n; g++) {
    if ((g_B - g_A <= 0) && (g > 0)) {
      break;
    }

    for (int s = 0; g * g_A + s * s_A <= n; s++) {
      if ((s_B - s_A <= 0) && (s > 0)) {
        break;
      }

      int b = 0;
      if (b_B - b_A > 0) {
        b = (n - g * g_A - s * s_A) / b_A;
      }

      long long n_New = n + (long long)g * (g_B - g_A) +
                        (long long)s * (s_B - s_A) + (long long)b * (b_B - b_A);

      if (n_New > n_Max) {
        n_Max = n_New;
      }
    }
  }

  return n_Max;
}

int main(int argc, char *argv[]) {
  long long n;
  int g_A, g_B;
  int s_A, s_B;
  int b_A, b_B;
  int ans;

  std::cin >> n;
  std::cin >> g_A >> s_A >> b_A;
  std::cin >> g_B >> s_B >> b_B;

  // 取引所 A -> B
  n = trade(n, g_A, s_A, b_A, g_B, s_B, b_B);
  ;

  // 取引所 B -> A
  n = trade(n, g_B, s_B, b_B, g_A, s_A, b_A);

  std::cout << n << std::endl;

  return 0;
}


Fixed submission:
#include <iostream>

long long trade(long long n, int g_A, int s_A, int b_A, int g_B, int s_B,
                int b_B) {
  long long n_Max = n;

  for (int g = 0; g * g_A <= n; g++) {
    if ((g_B - g_A <= 0) && (g > 0)) {
      break;
    }

    for (int s = 0; g * g_A + s * s_A <= n; s++) {
      if ((s_B - s_A <= 0) && (s > 0)) {
        break;
      }

      int b = 0;
      if (b_B - b_A > 0) {
        b = (n - g * g_A - s * s_A) / b_A;
      } else {
        s = (n - g * g_A) / s_A;
      }

      long long n_New = n + (long long)g * (g_B - g_A) +
                        (long long)s * (s_B - s_A) + (long long)b * (b_B - b_A);

      if (n_New > n_Max) {
        n_Max = n_New;
      }
    }
  }

  return n_Max;
}

int main(int argc, char *argv[]) {
  long long n;
  int g_A, g_B;
  int s_A, s_B;
  int b_A, b_B;
  int ans;

  std::cin >> n;
  std::cin >> g_A >> s_A >> b_A;
  std::cin >> g_B >> s_B >> b_B;

  // 取引所 A -> B
  n = trade(n, g_A, s_A, b_A, g_B, s_B, b_B);
  ;

  // 取引所 B -> A
  n = trade(n, g_B, s_B, b_B, g_A, s_A, b_A);

  std::cout << n << std::endl;

  return 0;
}
Problem p03008 (C++)

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

using namespace std;

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

  int N;
  cin >> N;
  long ga, sa, ba, gb, sb, bb;
  cin >> ga >> sa >> ba >> gb >> sb >> bb;
  struct Stat {
    long f, t;
  };
  Stat stats[3] = {
      Stat{min(ga, gb), max(ga, gb)},
      Stat{min(sa, sb), max(sa, sb)},
      Stat{min(ba, bb), max(ba, bb)},
  };
  bool st[3] = {ga <= gb, sa <= sb, ba <= bb};

  long NN = 0;
  for (int i = N / stats[0].f; i >= 0; i--) {
    if (!st[0])
      i = 0;
    auto nn = N - stats[0].f * i;
    long ss = stats[0].t * i;
    for (int j = nn / stats[1].f; j >= 0; j--) {
      if (!st[1])
        j = 0;
      auto nnn = nn - stats[1].f * j;
      auto k = nnn / stats[2].f;
      if (!st[2])
        k = 0;
      long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k;
      NN = max(NN, score);
    }
  }
  long ans = 0;
  for (int i = NN / stats[0].f; i >= 0; i--) {
    if (st[0])
      i = 0;
    auto nn = NN - stats[0].f * i;
    long ss = stats[0].t * i;
    for (int j = nn / stats[1].f; j >= 0; j--) {
      if (st[1])
        j = 0;
      auto nnn = nn - stats[1].f * j;
      auto k = nnn / stats[2].f;
      if (st[2])
        k = 0;
      long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k;
      ans = max(ans, score);
    }
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

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

  int N;
  cin >> N;
  long ga, sa, ba, gb, sb, bb;
  cin >> ga >> sa >> ba >> gb >> sb >> bb;
  struct Stat {
    long f, t;
  };
  Stat stats[3] = {
      Stat{min(ga, gb), max(ga, gb)},
      Stat{min(sa, sb), max(sa, sb)},
      Stat{min(ba, bb), max(ba, bb)},
  };
  bool st[3] = {ga <= gb, sa <= sb, ba <= bb};

  long NN = 0;
  for (int i = N / stats[0].f; i >= 0; i--) {
    if (!st[0])
      i = 0;
    auto nn = N - stats[0].f * i;
    long ss = stats[0].t * i;
    for (int j = nn / stats[1].f; j >= 0; j--) {
      if (!st[1])
        j = 0;
      auto nnn = nn - stats[1].f * j;
      auto k = nnn / stats[2].f;
      if (!st[2])
        k = 0;
      long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k;
      NN = max(NN, score);
    }
  }
  long ans = 0;
  for (int i = NN / stats[0].f; i >= 0; i--) {
    if (st[0])
      i = 0;
    auto nn = NN - stats[0].f * i;
    long ss = stats[0].t * i;
    for (int j = nn / stats[1].f; j >= 0; j--) {
      if (st[1])
        j = 0;
      auto nnn = nn - stats[1].f * j;
      auto k = nnn / stats[2].f;
      if (st[2])
        k = 0;
      long score = ss + stats[1].t * j + stats[2].t * k + nnn - stats[2].f * k;
      ans = max(ans, score);
      if (st[2])
        j = 0;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;

ll get(ll n, vector<ll> buy, vector<ll> sell) {
  ll ret = n;
  for (int i = 0; i * buy[0] <= n; i++) {
    for (int j = 0; i * buy[0] + j * buy[1] <= n; j++) {
      ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1]));
      int k = (n - i * buy[0] - j * buy[1]) / buy[2];
      ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1]) +
                         k * (sell[2] - buy[2]));
    }
  }
  return ret;
}

void solve() {
  ll n;
  vector<ll> a, b;
  a.resize(3);
  b.resize(3);
  cin >> n;
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    cin >> b[i];
  n = get(n, a, b);
  n = get(n, b, a);
  cout << n << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  /*
  ll t;
  cin >> t;
  for(ll i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
  */
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;

ll get(ll n, vector<ll> buy, vector<ll> sell) {
  ll ret = n;
  vector<int> good;
  for (int i = 0; i < buy.size(); i++) {
    if (buy[i] < sell[i])
      good.push_back(i);
  }
  if (good.size() == 3) {
    for (int i = 0; i * buy[0] <= n; i++) {
      if (i > 0 && buy[0] >= sell[0])
        continue;
      for (int j = 0; i * buy[0] + j * buy[1] <= n; j++) {
        if (j > 0 && buy[1] >= sell[1])
          continue;
        ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1]));
        int k = (n - i * buy[0] - j * buy[1]) / buy[2];
        ret = max(ret, n + i * (sell[0] - buy[0]) + j * (sell[1] - buy[1]) +
                           k * (sell[2] - buy[2]));
      }
    }
  }
  if (good.size() == 1) {
    ll amt = ret / buy[good[0]];
    ret = max(ret, n + amt * (sell[good[0]] - buy[good[0]]));
  }
  if (good.size() == 2) {
    for (int i = 0; i * buy[good[0]] <= n; i++) {
      int j = n - i * (buy[good[0]]);
      j /= buy[good[1]];
      ret = max(ret, n + i * (sell[good[0]] - buy[good[0]]) +
                         j * (sell[good[1]] - buy[good[1]]));
    }
  }
  return ret;
}

void solve() {
  ll n;
  vector<ll> a, b;
  a.resize(3);
  b.resize(3);
  cin >> n;
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    cin >> b[i];
  n = get(n, a, b);
  n = get(n, b, a);
  cout << n << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  /*
  ll t;
  cin >> t;
  for(ll i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
  */
}