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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

ll dp[85];

int main() {
  int n;
  cin >> n;
  vint a(n);
  rep(i, n) cin >> a[i];
  dp[0] = 1000;
  REP(i, n + 1) {
    chmax(dp[i], dp[i - 1]);
    for (int j = 1; j < i; j++) {
      ll n_stock = dp[j] / a[j - 1];
      chmax(dp[i], n_stock * a[i - 1] + dp[j] - n_stock * a[j - 1]);
    }
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int n;
vint a;

int dfs(int date = 0, int n_stock = 0, int posses = 1000) {
  if (date == n)
    return posses;
  int res = -1;
  if (n_stock == 0) {
    int n_stock_ = n_stock + posses / a[date];
    int posses_ = posses - n_stock_ * a[date];
    res = max(res, dfs(date + 1, n_stock_, posses_));
  } else {
    int posses_ = posses + n_stock * a[date];
    res = max(res, dfs(date + 1, 0, posses_));
  }
  res = max(res, dfs(date + 1, n_stock, posses));
  return res;
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  a.erase(unique(all(a)), a.end());
  n = a.size();
  int ans = dfs();
  cout << ans << endl;
}