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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> as(N);
  F0R(i, N) { cin >> as[i]; }

  auto kau = [&](int day, i64 &yen, i64 &kabu) {
    i64 n = yen / as[day];
    yen -= as[day] * n;
    kabu += n;
    // cout << "kau " << a << "\\n";
  };
  auto uru = [&](int day, i64 &yen, i64 &kabu) {
    yen += as[day] * kabu;
    kabu = 0;
    // cout << "uru " << a << "\\n";
  };

  vector<i64> yms(N, 0);
  vector<pair<i64, i64>> kms(N);

  i64 result = 0;
  auto dfs = [&](auto dfs, int day, i64 yen, i64 kabu) -> void {
    if (day == N) {
      chmax(result, yen);
      return;
    }

    bool updated = false;
    if (yen > yms[day]) {
      yms[day] = yen;
      updated = true;
    }
    if (kabu > kms[day].first) {
      kms[day] = {kabu, yen};
      updated = true;
    } else if (kabu == kms[day].first && yen > kms[day].second) {
      kms[day] = {kabu, yen};
      updated = true;
    }
    if (!updated)
      return;

    if (day == N - 1) {
      uru(day, yen, kabu);
      dfs(dfs, day + 1, yen, kabu);
    } else {
      // なにもしない
      dfs(dfs, day + 1, yen, kabu);

      if (kabu) {
        if (as[day - 1] < as[day]) {
          uru(day, yen, kabu);
          dfs(dfs, day + 1, yen, kabu);
        }
      } else {
        if (day == 0 || as[day - 1] > as[day]) {
          kau(day, yen, kabu);
          dfs(dfs, day + 1, yen, kabu);
        }
      }
    }
  };
  dfs(dfs, 0, 1000, 0);

  cout << result;
}
#endif


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> as(N);
  F0R(i, N) { cin >> as[i]; }

  auto kau = [&](int day, i64 &yen, i64 &kabu) {
    i64 n = yen / as[day];
    yen -= as[day] * n;
    kabu += n;
    // cout << "kau " << a << "\\n";
  };
  auto uru = [&](int day, i64 &yen, i64 &kabu) {
    yen += as[day] * kabu;
    kabu = 0;
    // cout << "uru " << a << "\\n";
  };

  i64 result = 0;
  auto dfs = [&](auto dfs, int day, i64 yen, i64 kabu) -> void {
    if (day == N) {
      chmax(result, yen);
      return;
    } else if (day == N - 1) {
      uru(day, yen, kabu);
      dfs(dfs, day + 1, yen, kabu);
    } else {
      // なにもしない
      dfs(dfs, day + 1, yen, kabu);

      if (kabu) {
        if (as[day - 1] < as[day]) {
          uru(day, yen, kabu);
          dfs(dfs, day + 1, yen, kabu);
        }
      } else {
        if (day == 0 || as[day - 1] > as[day]) {
          kau(day, yen, kabu);
          dfs(dfs, day + 1, yen, kabu);
        }
      }
    }
  };
  dfs(dfs, 0, 1000, 0);

  cout << result;
}
#endif