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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

const ll INF = 1LL << 60;
const ll MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;

  vector<ll> A(N);
  for (ll i = 0; i < N; ++i)
    cin >> A[i];

  vector<ll> Bu;
  vector<ll> Bl;

  bool flag_up = true;
  bool enable = true;
  for (ll i = 0; i < N - 1; ++i) {
    if (A[i] > A[i + 1]) {
      flag_up = false;
      enable = true;
      break;
    }
    if (A[i] < A[i + 1]) {
      flag_up = true;
      enable = true;
      break;
    }
  }

  if (!enable) {
    std::cout << "1000" << endl;
    return 0;
  }

  if (flag_up) {
    Bl.push_back(A[0]);
  } else {
    Bu.push_back(A[0]);
  }
  bool iniial_flag_up = flag_up;

  for (ll i = 2; i < N; ++i) {
    ll dA = A[i] - A[i - 1];

    if (flag_up && dA < 0) {
      Bu.push_back(A[i - 1]);
      flag_up = false;
      continue;
    }

    if (!flag_up && dA > 0) {
      Bl.push_back(A[i - 1]);
      flag_up = true;
      continue;
    }
  }

  if (flag_up) {
    Bu.push_back(A[N - 1]);
  } else {
    Bl.push_back(A[N - 1]);
  }

  ll iMax = min(Bu.size(), Bl.size());
  ll money = 1000;
  ll stock = 0;
  if (iniial_flag_up) {
    for (ll i = 0; i < iMax; ++i) {
      stock += money / Bl[i];
      money = money % Bl[i];

      money += stock * Bu[i];
      stock = 0;
    }
  } else {
    for (ll i = 0; i < iMax - 1; ++i) {
      stock += money / Bl[i];
      money = money % Bl[i];

      money += stock * Bu[i + 1];
      stock = 0;
    }
  }

  cout << money << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

const ll INF = 1LL << 60;
const ll MOD = 1000000007;

vector<ll> A;
ll N;

ll f(const P &state, const ll day) {
  if (day == N + 1)
    return state.first;
  const ll value = A[day];

  ll rv0 = f(state, day + 1);
  ll rv1 = -INF;
  ll rv2 = -INF;
  if (state.first >= value) {
    rv1 =
        f(P(state.first % value, state.second + state.first / value), day + 1);
  }
  if (state.second > 0) {
    rv2 = f(P(state.first + state.second * value, 0), day + 1);
  }

  return max(rv0, max(rv1, rv2));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;

  A.resize(N + 1);
  A[0] = 0;
  for (ll i = 0; i < N; ++i)
    cin >> A[i + 1];

  ll ans = f(P(1000, 0), 1);

  cout << ans << endl;
}