Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef ll Weight;
typedef vector<vector<Weight>> Graph;
void add_edge(vector<vector<Weight>> &g, Weight from, Weight to,
              bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

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

int n;

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

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

  ll kabu = 0, money = 1000;
  rep(i, n - 1) {
    if (a[i] >= a[i + 1]) {
      money += kabu * a[i];
      kabu = 0;
    } else if (a[i] <= a[i + 1]) {
      kabu += money / a[i];
      money = money % a[i];
    }

    // cerr << i << " " << money << " " << kabu << endl;
  }

  cout << money + a[n - 1] * kabu << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef ll Weight;
typedef vector<vector<Weight>> Graph;
void add_edge(vector<vector<Weight>> &g, Weight from, Weight to,
              bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

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

int n;

ll dfs(int money, int kabu, int ix, bool buy, vector<ll> &a) {
  // cout << ix << " " << money << " "<< kabu << endl;

  ll ret = money + kabu * a[ix];
  if (a[ix] < money)
    for (int i = ix + 1; i < n; ++i)
      chmax(ret, dfs(money % a[ix], kabu + money / a[ix], i, !buy, a));
  if (kabu > 0)
    for (int i = ix + 1; i < n; ++i)
      chmax(ret, dfs(money + kabu * a[ix], 0, i, !buy, a));

  return ret;
}

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

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

  cout << max(dfs(1000, 0, 0, false, a), dfs(1000, 0, 0, true, a)) << endl;
}