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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
#define rep1(i, N) for (int i = 1; i < int(N); ++i)
#define all(a) (a).begin(), (a).end()
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }
#define printpair(v)                                                           \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << "{" << _.first << "," << _.second << "}"                         \\
           << ", ";                                                            \\
    cerr << "]" << endl;                                                       \\
  }
#define dump(x) cerr << #x << ": " << x << endl;
#define bit(k) (1LL << (k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "}";
  return os;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18 + 1;
const ll MOD = (ll)1e9 + 7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

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

  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  A.emplace_back(0);
  ll ans = 1000;
  ll cnt = 0;
  ll minn = INFLL;
  rep(i, N) {
    if (A[i] < A[i + 1]) {
      chmin(minn, A[i]);
    } else {
      cnt += ans / minn;
      ans %= minn;

      ans += cnt * A[i];
      cnt = 0;
      minn = INFLL;
    }
    // dump(ans);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
#define rep1(i, N) for (int i = 1; i < int(N); ++i)
#define all(a) (a).begin(), (a).end()
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }
#define printpair(v)                                                           \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << "{" << _.first << "," << _.second << "}"                         \\
           << ", ";                                                            \\
    cerr << "]" << endl;                                                       \\
  }
#define dump(x) cerr << #x << ": " << x << endl;
#define bit(k) (1LL << (k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "}";
  return os;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18 + 1;
const ll MOD = (ll)1e9 + 7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/
const int M = 10000;
ll dp[100][M + 5][2];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  rep(i, N + 1) rep(j, M + 1) rep(k, 2) dp[i][j][k] = -INF;
  ;
  dp[0][0][0] = 1000;
  rep(i, N) rep(j, M) rep(k, 2) {
    // x個売る
    rep(x, j + 1) {
      if (dp[i][j][k] >= 0) {
        chmax(dp[i + 1][j - x][1], dp[i][j][k] + A[i] * x);
      }
    }
    // x個買う
    rep(x, dp[i][j][k] / A[i] + 1) {
      chmax(dp[i + 1][j + x][0], dp[i][j][k] - A[i] * x);
    }
  }
  ll ans = 0;
  rep(i, N + 1) rep(j, M + 1) rep(k, 2) chmax(ans, dp[i][j][k]);
  cout << ans << endl;
}