alexjercan/bugnet huggingface dataset:
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int N;
  long long int O, S;
  O = 1000;
  S = 0;
  cin >> N;
  long long int A[N + 1];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  A[N] = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] < A[i + 1]) {
      S = S + O / A[i];
      O = O % A[i];
    }
    if (A[i] > A[i + 1]) {
      O = O + S * A[i];
      S = 0;
    }
    //  cout << O << ":" << S <<endl;
  }

  cout << O << endl;

  return 0;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}


Error: TLE

Fixed submission:

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

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int N;
  long long int O, S;
  O = 1000;
  S = 0;
  cin >> N;
  long long int A[N + 1];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  A[N] = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] < A[i + 1]) {
      while (A[i] <= O) {
        O = O - A[i];
        S++;
      }
    }
    if (A[i] > A[i + 1]) {
      while (S > 0) {
        O = O + A[i];
        S--;
      }
    }
    //  cout << O << ":" << S <<endl;
  }

  cout << O << endl;

  return 0;
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int kabu = 0, money = 1000;
  rep(i, 0, n) {
    if (i != n - 1) {
      if (a[i] < a[i + 1]) {
        kabu += money / a[i];
        money %= a[i];
      } else if (a[i] == a[i + 1]) {
        continue;
      } else {
        money += a[i] * kabu;
        kabu = 0;
      }
    } else {
      money += a[i] * kabu;
      kabu = 0;
    }
    // cout << money << " " << kabu << "\\n";
  }
  cout << money << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int dp[n + 1][3010]; // dp[i][j] 持ってる最大のお金
  rep(i, 0, n + 1) rep(j, 0, 3005) dp[i][j] = 0;
  dp[0][0] = 1000;
  bool chk[n + 1][3010];
  rep(i, 0, n + 1) rep(j, 0, 3005) chk[i][j] = 0; // 株を持っているか否か
  rep(i, 0, n) {
    dp[i + 1][0] = max(dp[i + 1][0], (ll)1000);
    rep(j, 0, 1000) {
      for (int k = 1; k * a[i] <= dp[i][j]; k++) {
        dp[i + 1][j + k] = max(dp[i][j] - a[i] * k, dp[i + 1][j + k]);
        chk[i + 1][j + k] = 1;
      }
      if (chk[i][j] && j != 0) {
        rep(k, 0, j + 1) dp[i + 1][k] =
            max(dp[i + 1][k], dp[i][j] + a[i] * (j - k));
      }
    }
  }
  /*
  rep(i, 0, n + 1) {
      rep(j, 0, 11) cout << dp[i][j] << " ";
      cout << "\\n";
  }
  */
  int ans = 1000;
  rep(i, 0, 3000) ans = max(ans, dp[n][i]);
  cout << ans << "\\n";
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\\n"
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <template <typename, typename...> class ContainerType,
          typename ValueType, typename... Args>
void print(const ContainerType<ValueType, Args...> &c) {
  for (const auto &v : c)
    cout << v << ' ';
  cout << '\\n';
}
void print() { cout << '\\n'; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipower(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

// 8====================================D
int main() {
  int n;
  cin >> n;
  vector<lli> a(n);
  fori(n) cin >> a[i];
  a.pb(0);

  lli s = 0;
  lli m = 1000;
  for (lli i = 0; i < n; i++) {
    if (a[i + 1] > a[i]) {
      s += m / a[i];
      m = m % a[i];
    } else if (a[i + 1] < a[i]) {
      m += s * a[i];
      s = 0;
    }
  }
  m += s * a[n - 1];
  s = 0;

  cout << max(m, 0LL) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\\n"
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}
template <template <typename, typename...> class ContainerType,
          typename ValueType, typename... Args>
void print(const ContainerType<ValueType, Args...> &c) {
  for (const auto &v : c)
    cout << v << ' ';
  cout << '\\n';
}
void print() { cout << '\\n'; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipower(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

// 8====================================D
int main() {
  int n;
  cin >> n;
  vector<lli> a(n);
  fori(n) cin >> a[i];
  a.pb(0);

  lli s = 0;
  lli m = 1000;
  for (lli i = 0; i < n; i++) {
    if (a[i + 1] > a[i]) {
      while (m >= a[i]) {
        s++;
        m -= a[i];
      }
    } else if (a[i + 1] < a[i]) {
      while (s > 0) {
        s--;
        m += a[i];
      }
    }
  }

  cout << max(m, 0LL) << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

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

  ll m = 1000;
  rep(i, n - 1) {
    ll s = 0;
    if (a[i] < a[i + 1]) {
      s = m / a[i];
      m += (a[i + 1] - a[i]) * s;
    }
  }
  cout << m << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

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

  ll m = 1000, s = 0;
  rep(i, n - 1) {
    if (a[i] < a[i + 1]) {
      while (m - a[i] >= 0) {
        m -= a[i];
        s++;
      }
      while (s - 1 >= 0) {
        m += a[i + 1];
        s--;
      }
    }
  }
  cout << m << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define F first
#define S second

using ll = long long;
using P = pair<ll, ll>;
using PP = pair<P, P>;

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

  vector<int> arr(n + 1, 0);
  rep(i, n) { cin >> arr[i]; }

  ll res = 1000;
  queue<PP> que;
  que.push(PP(P(1000, 0), P(0, 0)));

  vector<ll> memo(n + 1, -1);

  vector<bool> tab(n, false);
  rep(i, n) {
    bool f = false;
    for (int j = i + 1; j < n; j++) {
      if (arr[i] < arr[j]) {
        f = true;
        break;
      }
    }
    tab[i] = f;
  }

  while (!que.empty()) {
    PP t = que.front();
    que.pop();

    ll m = t.F.F;
    ll s = t.F.S;
    ll d = t.S.F;
    ll b = t.S.S;

    if (memo[d] > m + s * arr[d])
      continue;
    memo[d] = m + s * arr[d];

    if (m > res) {
      res = m;
    }
    if (d + 1 > n) {
      continue;
    }

    que.push(PP(P(m, s), P(d + 1, b)));
    if (m >= arr[d] && tab[d]) {
      ll k = m / arr[d];
      que.push(PP(P(m - arr[d] * k, s + k), P(d + 1, arr[d])));
    }
    if (s > 0 && arr[d] > arr[d + 1] && arr[d] > b) {
      que.push(PP(P(m + arr[d] * s, 0), P(d + 1, 0)));
    }
  }

  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define F first
#define S second

using ll = long long;
using P = pair<ll, ll>;
using PP = pair<P, P>;

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

  vector<int> arr(n + 1, 0);
  rep(i, n) { cin >> arr[i]; }

  ll res = 1000;
  queue<PP> que;
  que.push(PP(P(1000, 0), P(0, 0)));

  vector<ll> memo(n + 1, -1);

  vector<bool> tab(n, false);
  rep(i, n) {
    bool f = false;
    for (int j = i + 1; j < n; j++) {
      if (arr[i] < arr[j]) {
        f = true;
        break;
      }
    }
    tab[i] = f;
  }

  while (!que.empty()) {
    PP t = que.front();
    que.pop();

    ll m = t.F.F;
    ll s = t.F.S;
    ll d = t.S.F;
    ll b = t.S.S;

    if (memo[d] > m + s * arr[d])
      continue;
    memo[d] = m + s * arr[d];

    if (m > res) {
      res = m;
    }
    if (d + 1 > n) {
      continue;
    }

    que.push(PP(P(m, s), P(d + 1, b)));
    {
      ll mt = m;
      ll st = s;
      if (mt >= arr[d] && tab[d]) {
        ll k = mt / arr[d];
        mt -= arr[d] * k;
        st += k;
        que.push(PP(P(mt, st), P(d + 1, arr[d])));
      }
    }
    {
      ll mt = m;
      ll st = s;
      if (st > 0 && arr[d] > b) {
        mt += arr[d] * st;
        st = 0;
        que.push(PP(P(mt, st), P(d + 1, 0)));
      }
    }
  }

  cout << res << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007

int main() {
  int kabuka[85];
  int n;
  cin >> n;
  REP(i, n) { cin >> kabuka[i]; }

  ll money = 1000;
  ll kabu = 0;
  REP(i, n - 1) {
    if (kabuka[i] < kabuka[i + 1]) {
      kabu = (money / kabuka[i]);
      money += (kabu * (kabuka[i + 1] - kabuka[i]));
    }
  }
  cout << money << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007

int kabuka[85];
int n;

ll loop(ll money, ll kabu, int day) {
  if (day == n - 1) {
    return (money + kabu * kabuka[day]);
  }
  ll buy_able_kabu = money / kabuka[day];
  ll sell_able_money = kabu * kabuka[day];
  /*
    ll money1 = 0;
    ll money2 = 0;

    if (buy_able_kabu >= 1) {
      money1 =
    }

    if (sell_able_money > 0) {
      money2 =
    }
    */
  if (buy_able_kabu >= 1 && sell_able_money >= 0) {
    return max(max(money, loop(money - (kabuka[day] * buy_able_kabu),
                               kabu + buy_able_kabu, day + 1)),
               loop(money + sell_able_money, 0, day + 1));
  } else if (buy_able_kabu >= 1) {
    return max(money, loop(money - (kabuka[day] * buy_able_kabu),
                           kabu + buy_able_kabu, day + 1));
  } else if (sell_able_money >= 0) {
    return max(money, loop(money + sell_able_money, 0, day + 1));
  } else {
    return loop(money, kabu, day + 1);
  }
}

int main() {
  cin >> n;
  REP(i, n) { cin >> kabuka[i]; }
  cout << loop(1000, 0, 0) << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  long long M = 1000, S;
  vector<long long> A(87);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < N; i++) {
    if (M >= 1000) {
      if (A.at(i) < A.at(i + 1)) {
        S = M / A.at(i);
        M %= A.at(i);
      }
    } else {
      if (A.at(i) < A.at(i + 1))
        continue;
      else {
        M += S * A.at(i);
        S = 0;
      }
    }
  }
  cout << M << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int N, S;
  cin >> N;
  long long int M = 1000;
  vector<long int> A(80);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < N; i++) {
    if (M >= 1000) {
      if (A.at(i) < A.at(i + 1)) {
        S = M / A.at(i);
        M %= A.at(i);
      }
    } else {
      if (A.at(i) < A.at(i + 1))
        continue;
      else {
        M += S * A.at(i);
        S = 0;
      }
    }
  }
  cout << M << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
#define ll long long
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x) scanf("%d", &x)
#define scall(x) scanf("%lld", &x)
ll a[200005];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 210;
  a[n + 1] = 0;
  ll num = 0;
  ll mon = 1000;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= a[i - 1] && a[i] >= a[i + 1]) {
      mon += (num * a[i]);
      num = 0;
    }
    if (a[i] <= a[i - 1] && a[i] <= a[i + 1]) {
      {
        num += (mon / a[i]);
        mon -= (mon / a[i] * a[i]);
      }
    }
    // cout<<num<<endl;
  }
  cout << mon << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
#define ll long long
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x) scanf("%d", &x)
#define scall(x) scanf("%lld", &x)
ll a[200005];
int maxn, n;
int mm;
void dfs(int sum, int x, int num) {
  if (x == n + 1) {
    maxn = max(maxn, sum);
    return;
  }
  if (num == 0 && sum < 1000)
    return;
  if (sum + num * mm < 1000)
    return;
  if (num == 0) {
    dfs(sum - sum / a[x] * a[x], x + 1, sum / a[x]);
    dfs(sum, x + 1, num);
  } else {
    dfs(sum, x + 1, num);
    dfs(sum + a[x] * num, x + 1, 0);
  }
}
int main() {
  cin >> n;
  maxn = 0;
  mm = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (a[i] > mm)
      mm = a[i];

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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

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

  vec<ll> A(N);
  rep(i, N) cin >> A[i];

  vec<vec<ll>> dp(N + 2, vec<ll>(2, -1));
  dp[0][0] = 1000; // 日数、枚数、金額

  ll amari = 0;
  rep(i, N) rep(j, 3) {
    ll price = A[i];
    dp[i + 1][0] = max(dp[i][0], dp[i][1] * price + amari);

    if (dp[i][0] / price > dp[i][1]) {
      dp[i + 1][1] = dp[i][0] / price;
      amari = dp[i][0] % price;
    } else if (dp[i][0] / price < dp[i][1]) {
      dp[i + 1][1] = dp[i][1];
    } else {
      dp[i + 1][1] = dp[i][1];
      amari = max(amari, dp[i][0] % price);
    }
  }

  cout << dp[N][0] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

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

  vec<ll> A(N);
  rep(i, N) cin >> A[i];

  vec<vec<ll>> dp(N + 2, vec<ll>(1501, -1));
  dp[0][0] = 1000; // 日数、枚数、金額

  rep(i, N) rep(j, 1000) {
    if (dp[i][j] == -1)
      continue;
    ll price = A[i];

    // 株買うパターン
    for (ll k = 0; dp[i][j] - k * price >= 0; k++) {
      dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] - k * price);
    }

    // 株売るパターン
    for (ll k = 0; k <= j; k++) {
      dp[i + 1][j - k] = max(dp[i + 1][j - k], dp[i][j] + k * price);
    }
  }

  cout << dp[N][0] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

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

#define PI acos(-1)
#define pb push_back
#define int long long int
#define mp make_pair
#define pi pair<int, int>
#define pii pair<pi, int>
#define fir first
#define sec second
#define MAXN 10000
#define MAXL 100
#define mod 1000000007

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, ans = 1000;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 1; i < n; i++) {
    int at = ans / v[i - 1];
    int ans2 = ans - (at * v[i - 1]);
    ans2 = ans2 + (at * v[i]);
    ans = max(ans, ans2);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define PI acos(-1)
#define pb push_back
#define int long long int
#define mp make_pair
#define pi pair<int, int>
#define pii pair<pi, int>
#define fir first
#define sec second
#define MAXN 10000
#define MAXL 100
#define mod 1000000007

int dp[81][MAXN];

int solve(int i, int j, int n, vector<int> v, int sum) {
  if (i == n)
    return dp[i][sum] = sum;
  if (dp[i][sum] != -1)
    return dp[i][sum];
  int curr = 0, best = sum, cnt = 0;
  best = max(best, solve(i + 1, j, n, v, sum));
  while (1) {
    curr += v[i], cnt++;
    if (curr > sum) {
      curr -= v[i];
      cnt--;
      break;
    }
  }
  if (cnt > 0)
    best = max(best, solve(i + 1, j + cnt, n, v, sum - curr));
  curr = 0, cnt = 0;
  while (1) {
    curr += v[i], cnt++;
    if (cnt > j) {
      curr -= v[i];
      cnt--;
      break;
    }
  }
  if (cnt > 0)
    best = max(best, solve(i + 1, j - cnt, n, v, sum + curr));
  return dp[i][sum] = best;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0, n, v, 1000) << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll m = 1000;
  ll n;
  cin >> n;
  vector<ll> a(87);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n - 1; i++) {
    ll s = 0;
    if (a[i] < a[i + 1])
      s = m / a[i];
    m += (a[i + 1] - a[i]) * s;
  }

  cout << m << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int m = 1000;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n - 1; i++) {
    ll s = 0;
    if (a[i] < a[i + 1])
      s = m / a[i];
    m += (a[i + 1] - a[i]) * s;
  }

  cout << m << endl;

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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int, int>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; /// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

ll niz[100];
int n;
ll dp[100];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  ff(i, 1, n) cin >> niz[i];
  dp[1] = 1000;
  ff(i, 2, n + 1) {
    dp[i] = dp[i - 1];
    ff(j, 1, i - 1) {
      ff(k, j + 1, i - 1) {
        ll sta = dp[j] / niz[j];
        sta = sta * niz[k];
        sta += dp[j] % niz[j];
        dp[i] = max(dp[i], sta);
      }
    }
  }
  cout << dp[n + 1] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int, int>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; /// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int niz[100];
int n;
ll dp[100][50005];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  ff(i, 1, n) cin >> niz[i];
  dp[1][0] = 1000;

  ff(i, 1, n) { ff(j, 0, 10000) dp[i][j] = -1e9; }
  dp[1][0] = 1000;

  ff(i, 1, n) {
    ff(j, 0, 10000) {
      if (dp[i][j] < 0)
        continue;
      ff(k, 0, j) {
        dp[i + 1][j - k] = max(dp[i + 1][j - k], dp[i][j] + k * niz[i]);
      }
      ff(k, 0, dp[i][j] / niz[i]) {
        dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] - k * niz[i]);
      }
      // dp[i + 1][0] = max(dp[i + 1][0], dp[i][j] + j*niz[i]);
      // dp[i + 1][j + dp[i][j] / niz[i]] = max(dp[i + 1][j + dp[i][j] /
      // niz[i]], dp[i][j]%niz[i]);
    }
  }
  int res = 0;
  cout << dp[n + 1][0] << "\\n";
  return 0;
}