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

#include <bits/stdc++.h>
#define roop0(n) for (long long i = 0; i < n; i++)
#define roop1(n) for (long long i = 1; i < n; i++)
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << "\\n"
#define debug(x) std::cerr << #x << ": " << x << "\\n"
#define debug2(x, y)                                                           \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << "\\n"
#define debug3(x, y, z)                                                        \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z        \\
            << ": " << z << "\\n"

using namespace std;
typedef long long ll;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvs = vector<vector<string>>;

int main() {
  int n;
  cin >> n;
  ll A[n];
  roop0(n) cin >> A[i];

  ll money = 1000;
  ll kabu = 0;
  for (int i = 0; i < n - 1; i++) {
    money += A[i] * kabu;
    kabu = 0;
    if (A[i] < A[i + 1]) // 今日が安いから買う
    {

      ll buyable = money / A[i];
      money -= buyable * A[i];
      kabu += buyable;
    }
  }
  if (kabu > 0)
    money += kabu * A[n - 1];
  cout << money << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define roop0(n) for (long long i = 0; i < n; i++)
#define roop1(n) for (long long i = 1; i < n; i++)
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << "\\n"

using namespace std;
using vi = vector<int>;
using vll = vector<long long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vvs = vector<vector<string>>;

const static string el = "\\n";
long long result = 0;
vi A;
int n;

void dfs(long long money, long long kabu, int date) {
  if (date > n) {
    if (result < money) {
      result = money;
    }
  } else {
    for (int i = 0; i < 2; i++) {
      if (i == 0) { // i == 0の時株式購入をするとする
        if (A[date - 1] > A[date])
          continue; // 今日より明日のほうが安ければスキップする
        int buyable = money / A[date - 1];
        for (int k = 0; k <= buyable; k++) { // kは購入する株の数
          long long tmp_money = money - k * A[date - 1];
          long long tmp_kabu = kabu + k;
          dfs(tmp_money, tmp_kabu, date + 1);
        }
      } else { // i == 2の時株式売買をするとする
        if (A[date - 1] < A[date])
          continue; // 今日より明日のほうが高ければスキップする
        int sellable = kabu;
        for (int k = 0; k <= sellable; k++) { // ここでのkは売れる株の数
          long long tmp_money = money + k * A[date - 1];
          long long tmp_kabu = kabu - k;
          dfs(tmp_money, tmp_kabu, date + 1);
        }
      }
    }
  }
}

int main() {
  cin >> n;
  A = vi(n);
  roop0(n) {
    int tmp;
    cin >> tmp;
    A[i] = tmp;
  }

  long long money = 1000;
  long long kabu = 0;
  dfs(money, kabu, 1);
  cout << result << endl;
  return 0;
}