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

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

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

int main() {
  // cout << fixed << setprecision(5);

  lli n;
  cin >> n;

  deque<lli> a;
  REP(i, 0, n) {
    lli aa;
    cin >> aa;
    if (i == 0)
      a.push_back(aa);
    else {
      if (a[a.size() - 1] == aa)
        continue;
      a.push_back(aa);
    }
  }

  if (a.size() == 1) {
    cout << 1000 << endl;
    return 0;
  }

  a.push_front(INF);
  a.push_back(-INF);

  lli now = 1000;
  lli nowKabu = 0;
  REP(i, 1, a.size() - 1) {
    if (nowKabu == 0) {
      if (a[i - 1] > a[i] && a[i] < a[i + 1]) {
        nowKabu = now / a[i];
        now %= a[i];
      }
    } else {
      if (a[i - 1] < a[i] && a[i] > a[i + 1]) {
        now += nowKabu * a[i];
        nowKabu = 0;
      }
    }
  }
  cout << now << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

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

lli ans = 0;
lli n;
vector<lli> A;

map<pair<lli, pair<lli, lli>>, bool> vis;

void func(lli now, lli money, lli kabu) {
  if (vis[{now, {money, kabu}}])
    return;
  if (now == n) {
    chmax(ans, money);
    return;
  }

  vis[{now, {money, kabu}}] = true;
  // no buy
  func(now + 1, money, kabu);
  // all sell
  func(now + 1, money + kabu * A[now], 0);
  // all in
  lli nowMoney = money;
  func(now + 1, money % A[now], kabu + nowMoney / A[now]);
}

void func() {

  lli now = 1000;
  lli kabu = 0;

  func(0, 1000, 0);

  cout << ans << endl;
}

int main() {
  // cout << fixed << setprecision(5);

  scanf("%lld", &n);
  A.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &A[i]);
  }
  func();
  return 0;
}