alexjercan/bugnet huggingface dataset:
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long INF = 1LL << 60;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A.at(i);
  int64_t M = 1000, S = 0;
  for (int i = 0; i < N - 1; i++) {
    if (A.at(i) <= A.at(i + 1)) {
      S = M / A.at(i);
      M += S * (A.at(i + 1) - A.at(i));
    }
  }
  cout << M;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long INF = 1LL << 60;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A.at(i);
  vector<int> T;
  if (A.at(0) < A.at(1))
    T.push_back(A.at(0));
  for (int i = 1; i < N - 1; i++) {
    if (A.at(i) == A.at(i + 1))
      continue;
    if (A.at(i) >= A.at(i - 1) && A.at(i) > A.at(i + 1))
      T.push_back(A.at(i));
    if (A.at(i) <= A.at(i - 1) && A.at(i) < A.at(i + 1))
      T.push_back(A.at(i));
  }
  if (A.at(N - 1) >= A.at(N - 2))
    T.push_back(A.at(N - 1));
  int S = T.size();
  if (S > 1 && T.at(S - 1) < T.at(S - 2)) {
    T.pop_back();
    S--;
  }
  if (S > 2 && T.at(0) > T.at(1)) {
    rep(i, S - 1) { T.at(i) = T.at(i + 1); }
    T.pop_back();
    S--;
  }
  if (S <= 1)
    cout << 1000;
  else {
    pair<int64_t, int64_t> M(1000 / T.at(0), 1000 % T.at(0));
    int i = 1;
    while (i < S - 1) {
      auto a = M.first, b = M.second; // aが株の数、bが現金
      auto c = a * T.at(i) + b;
      if (a * T.at(i + 2) + b <
          (c / T.at(i + 1)) * T.at(i + 2) + c % T.at(i + 1)) {
        M.first = c / T.at(i + 1);
        M.second = c % T.at(i + 1);
      }
      i += 2;
    }
    cout << M.second + M.first * T.at(S - 1);
  }
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cerr << elem;
  }
  cerr << endl;
}

template <class Type_a, class Type_b> bool chmax(Type_a &a, const Type_b &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class Type_a, class Type_b> bool chmin(Type_a &a, const Type_b &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

  vector<llong> a(n + 3);
  REP(i, n) cin >> a[i + 1];
  a[n + 1] = 1;
  a[n + 2] = INF;

  llong ans = 1000;
  llong num = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] < a[i + 1]) {
      num += ans / a[i];
      ans %= a[i];
    } else if (a[i] >= a[i + 1]) {
      ans += a[i] * num;
      num = 0;
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cerr << elem;
  }
  cerr << endl;
}

template <class Type_a, class Type_b> bool chmax(Type_a &a, const Type_b &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class Type_a, class Type_b> bool chmin(Type_a &a, const Type_b &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

  vector<llong> a(n + 1);
  REP(i, n) cin >> a[i + 1];

  int hoge = 1e4 + 1;
  vector<vector<llong>> dp(n + 1,
                           vector<llong>(hoge, -1)); // dp[day][num]=money.
  dp[0][0] = 1000;
  for (int i = 1; i <= n; ++i) {
    REP(j, hoge) {
      if (dp[i - 1][j] < 0)
        break;
      REP(k, j + dp[i - 1][j] / a[i] + 1) {
        llong tmp = a[i] * (k - j);
        chmax(dp[i][k], dp[i - 1][j] - tmp);
      }
    }
    // line(dp[i]);
  }

  llong ans = -1;
  REP(i, hoge) chmax(ans, dp[n][i]);

  cout << ans << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
long long N, A[87], CurrentMoney = 1000;
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= N - 1; i++) {
    long long Stocks = 0;
    if (A[i] < A[i + 1])
      Stocks = CurrentMoney / A[i];

    CurrentMoney += (A[i + 1] - A[i]) * Stocks;
  }

  cout << CurrentMoney << endl;
}

Error: -11

Fixed submission:

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

int makeMaxProfit(vector<int> &arr, int n, int amt) {
  long long int currentMoney = amt;
  for (int i = 0; i < n - 1; ++i) {
    if (arr[i] < arr[i + 1]) {
      // Find the number of stocks
      // that can be purchased.
      long long int numStocks = currentMoney / arr[i];
      // Profit made by that
      // transaction.
      currentMoney += 1LL * (arr[i + 1] - arr[i]) * numStocks;
    }
  }
  return currentMoney;
}

int main() {
  int initialAmount = 1000;
  // vector<int> arr = {100 ,130, 130, 130, 115, 115, 150};
  //     vector<int> arr = {
  // 200, 180, 160, 140, 120, 100};
  int n;
  cin >> n;
  vector<int> arr;
  for (auto &it : arr)
    cin >> it;
  // int n = sizeof(arr) / sizeof(arr[0]);
  cout << makeMaxProfit(arr, n, initialAmount);
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

int main() {

  ll n;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  vector<ll> dp(n + 1, 0);
  dp[0] = 1000;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (int j = 1; j < i; j++) {
      dp[i] = max(dp[i], dp[j] + (v[i - 1] - v[j - 1]) * (dp[j] / v[j - 1]));
    }
  }
  cout << dp[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

int main() {

  ll n;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(10000000, -1));
  dp[0][0] = 1000;
  rep(i, n) rep(j, 10000000) {
    if (dp[i][j] == -1)
      continue;
    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    if (j + (dp[i][j] / v[i]) < 10000000)
      dp[i + 1][j + (dp[i][j] / v[i])] =
          max(dp[i + 1][j + (dp[i][j] / v[i])], (dp[i][j] % v[i]));
    dp[i + 1][0] = max(dp[i + 1][0], dp[i][j] + j * v[i]);
  }
  cout << (ll)dp[n][0] << endl;

  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <deque>     // deque
#include <iomanip>   // setprecision
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<long long>;
using pii = pair<int, int>;
using psi = pair<string, int>;

int main() {
  int N;
  cin >> N;
  vi data(N);
  for (int i = 0; i < N; i++) {
    cin >> data[i];
  }
  ll k = 0;
  ll money = 1000;
  if (data[0] <= data[1]) {
    k = floor(money / data[0]);
    money -= k * data[0];
  }
  vi vec(N, 0);
  for (int i = 1; i < N - 1; i++) {
    if (data[i] >= data[i - 1] && data[i] >= data[i + 1]) {
      vec[i] = 1;
      continue;
    }
    if (data[i] <= data[i - 1] && data[i] <= data[i + 1]) {
      vec[i] = 2;
    }
  }
  for (int i = 1; i < N - 1; i++) {
    if (vec[i] == 1) {
      money += k * data[i];
      k = 0;
      continue;
    }
    if (vec[i] == 2) {
      ll a = floor(money / data[i]);
      k += a;
      money -= a * data[i];
    }
  }
  money += k * data[N - 1];
  cout << money << endl;
}


Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <deque>     // deque
#include <iomanip>   // setprecision
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<long long>;
using pii = pair<int, int>;
using psi = pair<string, int>;

ll f(ll a, ll b) {
  ll ans = 0;
  while (a >= b) {
    ans++;
    a -= b;
  }
  return ans;
}

int main() {
  ll N;
  cin >> N;
  vi data(N);
  for (ll i = 0; i < N; i++) {
    cin >> data[i];
  }
  ll k = 0;
  ll money = 1000;
  if (data[0] <= data[1]) {
    k = f(money, data[0]);
    money -= k * data[0];
  }
  for (ll i = 1; i < N - 1; i++) {
    if (max({data[i], data[i - 1], data[i + 1]}) == data[i]) {
      money += k * data[i];
      k = 0;
    } else if (min({data[i], data[i - 1], data[i + 1]}) == data[i]) {
      ll a = f(money, data[i]);
      k += a;
      money -= a * data[i];
    }
  }
  money += k * data[N - 1];
  cout << money << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define RREP(i, a, n) for (int i = n - 1; i >= a; --i)

int n;
int a[101];
int dp[101010];
int main(void) {
  cin >> n;
  REP(i, 0, n) cin >> a[i];

  long long sum = 1000, stock;
  REP(i, 0, n - 1) {
    if (a[i] < a[i + 1] && sum >= a[i]) {
      stock = sum / a[i];
      sum -= (a[i] * stock);
      sum += (a[i + 1] * stock);
    }
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define RREP(i, a, n) for (int i = n - 1; i >= a; --i)

int n;
int a[101];
int dp[101010];
int main(void) {
  cin >> n;
  REP(i, 0, n) cin >> a[i];

  int sum = 1000;
  REP(i, 0, n - 1) {
    REP(j, 0, sum + 1) dp[j] = j;

    REP(j, 0, sum + 1) {
      if (j - a[i] >= 0)
        dp[j] = max(dp[j], dp[j - a[i]] + a[i + 1]);
    }
    int mx = 0;
    REP(j, 0, sum + 1) mx = max(mx, dp[j]);
    sum = mx;
  }
  cout << sum << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = 1000;
  ll st = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      ans += a[i] * st;
      st = 0;
    } else if (a[i] < a[i + 1]) {
      st += ans / a[i];
      ans %= a[i];
    }
  }
  ans += a[n - 1] * st;
  st = 0;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = 1000;
  ll st = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      while (st > 0) {
        ans += a[i];
        st--;
      }
    } else if (a[i] < a[i + 1]) {
      while (ans >= a[i]) {
        ans -= a[i];
        st++;
      }
    }
  }
  while (st > 0) {
    ans += a[n - 1];
    st--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

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

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int cur = a[0];
  ll ans = 1000;
  ll S = 1000;
  ll c = 0;
  bool flag = true;
  for (int i = 1; i < n; ++i) {
    if (flag) {
      if (a[i] <= cur)
        cur = a[i];
      else {
        ll buy = S / cur;
        c += buy;
        S -= buy * cur;
        cur = a[i];
        flag = false;
      }
    } else {
      if (a[i] >= cur)
        cur = a[i];
      else {
        S += cur * c;
        cur = a[i];
        c = 0;
        flag = true;
        chmax(ans, S);
      }
    }
  }
  if (c > 0)
    S += c * a.back();
  chmax(ans, S);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

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

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  vector<vector<int>> dp(n + 1, vector<int>(2005, -1));
  dp[0][0] = 1000;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 2000; ++j) {
      if (dp[i][j] == -1)
        continue;
      for (int k = 0; k <= j; ++k) {
        chmax(dp[i + 1][j - k], dp[i][j] + k * a[i]);
      }
      if (dp[i][j] > 0) {
        for (int k = 0; k <= dp[i][j] / a[i]; ++k) {
          chmax(dp[i + 1][j + k], dp[i][j] - k * a[i]);
        }
      }
    }
    // for(int j = 0; j <= 10; ++j){
    //     cerr << dp[i + 1][j] << ' ';
    // }
    // cerr << '\\n';
  }
  cout << dp[n][0] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (int)(n); i++)
#define put(i) cout << fixed << i << endl
using namespace std;
using ll = long long;

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

  ll en = 1000, stock = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i + 1] > a[i]) {
      en += stock * a[i];
      stock = 0;
      stock = en / a[i];
      en -= stock * a[i];
    } else if (a[i + 1] < a[i]) {
      en += stock * a[i];
      stock = 0;
    }
  }
  en += stock * a[n - 1];
  put(en);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (int)(n); i++)
#define put(i) cout << fixed << i << endl
using namespace std;
using ll = long long;

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

  vector<ll> topIndex, underIndex;

  ll downF = 1, upF = 1;
  for (long long i = 0; i < n - 1; i++) {
    if (a[i + 1] < a[i]) {
      downF = 1;
      if (upF) {
        topIndex.push_back(i);
        upF = 0;
      }
    } else if (a[i + 1] > a[i]) {
      upF = 1;
      if (downF) {
        underIndex.push_back(i);
        downF = 0;
      }
    }
  }

  if (a[n - 1] > a[n - 2]) {
    topIndex.push_back(n - 1);
  }

  ll en = 1000, stock = 0;
  ll t = 0, u = 0;
  for (long long i = 0; i < underIndex.size(); i++) {
    stock = en / a[underIndex[i]];
    en -= stock * a[underIndex[i]];
    en += stock * a[topIndex[i]];
    stock = 0;
  }

  put(en);

  // for(auto x:topIndex) cout << x << " "; cout << "top" << endl;
  // for(auto x:underIndex) cout << x << " "; cout << "under" << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<int64_t> data(n);
  for (int i = 0; i < n; ++i) {
    cin >> data[i];
  }

  int fin = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (data[i] < data[i + 1]) {
      fin = i;
    }
  }

  int64_t g = 1000LL;
  int64_t k = 0LL;
  for (int i = 0; i < n; ++i) {
    if (i > fin) {
      g += (int64_t)data[i] * k;
      k = 0ll;
      break;
    } else if (data[i] < data[i + 1]) {
      // while(g >= data[i]) {
      //   g -= data[i];
      //   k ++;
      // }
      int64_t num = g / data[i];
      int64_t u = num * (int64_t)data[i];
      g -= u;
      k += num;
    } else if (data[i] > data[i + 1]) {
      g += int64_t(data[i]) * k;
      k = 0LL;
    }
  }

  cout << g;
  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<int64_t> data(n);
  for (int i = 0; i < n; ++i) {
    cin >> data[i];
  }

  int fin = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (data[i] < data[i + 1]) {
      fin = i;
    }
  }

  int64_t g = 1000LL;
  int64_t k = 0LL;
  for (int i = 0; i < n; ++i) {
    if (i > fin) {
      g += (int64_t)data[i] * k;
      k = 0ll;
      break;
    } else if (data[i] < data[i + 1]) {
      while (g >= data[i]) {
        g -= data[i];
        k++;
      }
      // int num = g / data[i];
      // g -= num * (int64_t)data[i];
      // k += num;
    } else if (data[i] > data[i + 1]) {
      g += int64_t(data[i]) * k;
      k = 0LL;
    }
  }

  cout << g;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

  long long int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  long long int mon = 1000;
  long long int kabu = 0;

  for (int i = 0; i < n - 1; i++) {
    if (A[i] < A[i + 1] and kabu == 0 and mon != 0) {
      kabu = kabu + (mon / A[i]);
      mon = mon - (mon / A[i]) * A[i];
    } else if (A[i] > A[i + 1] and kabu != 0) {
      mon = mon + kabu * A[i];
      kabu = 0;
    }
  }

  if (kabu != 0) {
    mon += kabu * A[n - 1];
  }

  cout << mon << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n;
long long int A[80];
long long int ans = 0;

void dfs(int i, long long int mon, int kabu) {

  if (i == n) {
    ans = max(ans, mon);
  } else {
    dfs(i + 1, mon, kabu);
    dfs(i + 1, mon - (mon / A[i]) * A[i], kabu + (mon / A[i]));
    dfs(i + 1, mon + (kabu * A[i]), 0);
  }
}

int main() {
  cin >> n;

  // long long int mon=1000;
  // int kabu=0;

  for (int i = 0; i < n; i++)
    cin >> A[i];

  dfs(0, 1000, 0);
  cout << ans << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  /*int mx = *max_element(all(a));
  if (a[0] == mx) {
      cout << 1000 << endl;
      return 0;
  }*/
  vector<int> buy, sell;
  if (a[0] <= a[1])
    buy.push_back(0);
  rep(i, n - 2) {
    if (a[i] > a[i + 1] && a[i + 1] <= a[i + 2])
      buy.push_back(i + 1);
    if (a[i] <= a[i + 1] && a[i + 1] > a[i + 2])
      sell.push_back(i + 1);
  }
  if (a[n - 2] <= a[n - 1])
    sell.push_back(n - 1);
  int l = min(buy.size(), sell.size());
  /*
  cout << "buy:";
  rep(i, buy.size()) printf("%d ", buy[i] + 1);
  cout << endl <<  "sell:";
  rep(i, sell.size()) printf("%d ", sell[i] + 1);
  cout << endl;
  */

  ll m = 1000;
  ll stock = 0;
  rep(i, l) {
    stock = m / a[buy[i]];
    m -= stock * a[buy[i]];
    // cout << m << endl;
    m += stock * a[sell[i]];
    stock = 0;
    // cout << m << endl;
  }
  cout << m << endl;

  return 0;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  /*int mx = *max_element(all(a));
  if (a[0] == mx) {
      cout << 1000 << endl;
      return 0;
  }*/
  vector<int> buy, sell;
  if (a[0] < a[1])
    buy.push_back(0);
  rep(i, n - 2) {
    if (a[i] > a[i + 1] && a[i + 1] <= a[i + 2])
      buy.push_back(i + 1);
    if (a[i] < a[i + 1] && a[i + 1] >= a[i + 2])
      sell.push_back(i + 1);
  }
  if (a[n - 2] < a[n - 1])
    sell.push_back(n - 1);
  int l = sell.size();
  /*
  cout << "buy:";
  rep(i, buy.size()) printf("%d ", buy[i] + 1);
  cout << endl <<  "sell:";
  rep(i, l) printf("%d ", sell[i] + 1);
  cout << endl;
  */

  ll m = 1000;
  ll stock = 0;
  rep(i, l) {
    stock = m / a[buy[i]];
    m -= stock * a[buy[i]];
    // cout << m << endl;
    m += stock * a[sell[i]];
    stock = 0;
    // cout << m << endl;
  }
  cout << m << endl;

  return 0;
}