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

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\\n"
#define dump(x) cout << #x << " = " << (x) << "\\n"
#define endl "\\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
using TU = tuple<int, int, long long int>;
using edge = struct {
  int to;
  int cost;
};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  REP(i, n) cin >> a[i + 1];
  ll money = 1000;

  for (int i = 1; i < n; i++) {
    ll stocks = 0;
    if (a[i] < a[i + 1])
      stocks = money / a[i];
    money += (a[i + 1] - a[i]) * stocks;
  }
  COUT(money);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\\n"
#define dump(x) cout << #x << " = " << (x) << "\\n"
#define endl "\\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
using edge = struct {
  int to;
  int cost;
};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int n;
vector<int> a;
vector<ll> visited;
ll ans = 1000;

void dfs(int d, int s, ll money) {
  if (d >= n) {
    ll re = s * a[d] + money;
    // cout << s << " " << re << endl;
    if (re > ans)
      ans = re;
    return;
  }

  ll tot = money + s * a[d];
  // cout << money << " " << tot << " " << visited[d] << endl;
  if (tot < visited[d])
    return;

  // 全部購入
  int b_s = s + money / a[d];
  ll b_money = money - a[d] * (money / a[d]);
  ll b_tot = b_s * a[d] + b_money;
  // cout << "1: " << b_s << " " << tot << " " << b_tot << endl;
  if (tot <= b_tot) {
    dfs(d + 1, b_s, b_money);
  }

  // 全部売却
  ll s_money = money + a[d] * s;
  if (tot <= s_money) {
    dfs(d + 1, 0, s_money);
  }
}

int main() {
  IOS;
  cin >> n;
  a.resize(n + 1);
  visited.resize(n + 1);
  REP(i, n) cin >> a[i + 1];

  dfs(1, 0, 1000);

  COUT(ans);
  return 0;
}