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

#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define MAXINF (1e18L)
#define INF (1e9L)
#define EPS (1e-9)
#define MOD ((ll)(1e9 + 7))
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define ALL(v) v.begin(), v.end()
#define FIND(v, x) (binary_search(ALL(v), (x)))
#define SORT(v) sort(ALL(v))
#define RSORT(v)                                                               \\
  sort(ALL(v));                                                                \\
  reverse(ALL(v))
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \\
  cerr << #v << ":";                                                           \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cerr << " " << v[i];                                                       \\
  cerr << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define pb push_back
#define fi first
#define se second
using namespace std;
template <class A> void pr(A a) { cout << (a) << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
template <class T> void pr_vec(vector<T> &v, char ep = ' ') {
  int n = v.size();
  REP(ni, n) {
    cout << v[ni];
    if (ni != n - 1)
      cout << ep;
  }
  cout << endl;
};
template <class T> inline bool chmin(T &a, T b) {
  return a > b ? a = b, true : false;
}
template <class T> inline bool chmax(T &a, T b) {
  return a < b ? a = b, true : false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  vector<ll> dp(N);
  dp[0] = 1000;
  Rep(i, 1, N) {
    dp[i] = dp[i - 1];
    REP(j, i) { chmax(dp[i], A[i] * (dp[j] / A[j]) + (dp[j] % A[j])); }
  }
  pr(dp[N - 1]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define MAXINF (1e18L)
#define INF (1e9L)
#define EPS (1e-9)
#define MOD ((ll)(1e9 + 7))
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define ALL(v) v.begin(), v.end()
#define FIND(v, x) (binary_search(ALL(v), (x)))
#define SORT(v) sort(ALL(v))
#define RSORT(v)                                                               \\
  sort(ALL(v));                                                                \\
  reverse(ALL(v))
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \\
  cerr << #v << ":";                                                           \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cerr << " " << v[i];                                                       \\
  cerr << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define pb push_back
#define fi first
#define se second
using namespace std;
template <class A> void pr(A a) { cout << (a) << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
template <class T> void pr_vec(vector<T> &v, char ep = ' ') {
  int n = v.size();
  REP(ni, n) {
    cout << v[ni];
    if (ni != n - 1)
      cout << ep;
  }
  cout << endl;
};
template <class T> inline bool chmin(T &a, T b) {
  return a > b ? a = b, true : false;
}
template <class T> inline bool chmax(T &a, T b) {
  return a < b ? a = b, true : false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N;
vector<int> A;

int dfs(int n, int k, int m, int prev) {
  if (n == N)
    return m;

  if (n == 0)
    return max(
        {dfs(n + 1, k + (m / A[n]), m % (A[n]), A[n]), dfs(n + 1, k, m, prev)});
  else {
    int ret = m;
    if (k > 0 && prev < A[n]) {
      chmax(ret, dfs(n + 1, 0, m + (k * A[n]), 0));
    }
    if (m >= A[n]) {
      chmax(ret, dfs(n + 1, k + (m / A[n]), m % (A[n]), A[n]));
    }
    chmax(ret, dfs(n + 1, k, m, prev));
    return ret;
  }
}

int main(void) {
  cin >> N;
  A.assign(N, 0);
  REP(i, N) cin >> A[i];

  pr(dfs(0, 0, 1000, 0));
}