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

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, b, a) for (ll i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair<ll, ll>
#define vi vector<ll>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
string repeat(string s, ll n) {
  string s1 = "";
  for (ll i = 0; i < n; i++)
    s1 += s;
  return s1;
}
string getString(char x) {
  string s(1, x);
  return s;
}

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
vector<ll> v(10000005, 1);
void solve() {
  ll n;
  cin >> n;
  ll a[n];
  ll money = 1000;
  rep(i, 0, n) cin >> a[i];
  ll mx = money;
  ll stocks = 0;
  ll buy = 1;
  for (ll i = 0; i < n;) {

    if (buy) {
      while (i != n - 1 && a[i] >= a[i + 1]) {
        i++;
      }
      if (i == n - 1) {
        money += stocks * a[n - 1];
        mx = max(mx, money);
        cout << mx << endl;
        return;
      }
      // cout
      stocks += money / a[i];
      // cout<<stocks<<endl;
      money %= a[i];
      buy = 0;
    } else {
      while (i != n - 1 && a[i] <= a[i + 1]) {
        i++;
      }
      if (i == n - 1) {
        money += stocks * a[n - 1];
        mx = max(mx, money);
        cout << mx << endl;
        return;
      }
      money += stocks * a[i];
      stocks = 0;
      mx = max(mx, money);
      buy = 1;
    }
    // cout<<stocks<<endl;
  }
  // cout<<stocks<<endl;
  money += stocks * a[n - 1];
  cout << mx << endl;
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
string repeat(string s, int n) {
  string s1 = "";
  for (int i = 0; i < n; i++)
    s1 += s;
  return s1;
}
string getString(char x) {
  string s(1, x);
  return s;
}

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
vector<ll> v(10000005, 1);
void solve() {
  ll n;
  cin >> n;
  const ll N = 10000;
  ll money = 1000;
  ll a[n + 1];
  rep(i, 1, n + 1) cin >> a[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(N, -1));
  dp[1][0] = money;
  ll mx = money;
  rep(i, 1, N) {
    if (dp[1][i - 1] >= a[1]) {
      dp[1][i] = dp[1][i - 1] - a[1];
      mx = max(mx, dp[1][i]);
    }
  }
  rep(i, 2, n + 1) {
    rep(j, 0, N) {
      rep(k, j, N) {
        if (dp[i - 1][k] != -1)
          dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i] * (k - j));
        mx = max(mx, dp[i][j]);
      }
    }
    rep(k, 1, N) {
      if (dp[i][k - 1] >= a[i]) {
        dp[i][k] = max(dp[i][k], dp[i][k - 1] - a[i]);
        mx = max(mx, dp[i][k]);
      }
    }
  }
  cout << mx << endl;
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}