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

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

ll N;
vector<ll> A;

void solve() {
  vector<int> p(N - 1);
  rep(i, N - 1) { p.at(i) = A.at(i + 1) - A.at(i); }

  ll c = 1000; // cash
  ll s = 0;    // stock count
  for (int i = 0; i < N - 1; i++) {
    if (p.at(i) > 0) { // buy
      ll cnt = floor(c / A.at(i));
      c -= A.at(i) * cnt;
      s += cnt;
    } else if (p.at(i) < 0) { // sell
      c += A.at(i) * s;
      s = 0;
    }
  }
  c += s * A.at(N - 1);

  cout << c << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  scanf("%lld", &N);
  A.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

ll N;
vector<ll> A;

// cash, stock count, left days
ll f(ll c, ll s, ll d) {
  if (d == N)
    return c;

  ll mx = 0;

  // buy
  ll g = floor(c / A.at(d));
  if (g != 0)
    mx = max(mx, f(c - A.at(d) * g, s + g, d + 1));

  // sell
  mx = max(mx, f(c + A.at(d) * s, 0, d + 1));

  // nothing
  mx = max(mx, f(c, s, d + 1));

  return mx;
}

void solve() {
  ll ans = f(1000, 0, 0);
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  scanf("%lld", &N);
  A.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve();
  return 0;
}