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

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
#define endl '\\n'
#define ld long double
#define all(a) a.begin(), a.end()
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define sz(v) (int)v.size()
#define UB upper_bound
#define LB lower_bound
#define BP(x) __builtin_popcountll(x)
#define PQS priority_queue<int, vector<int>, greater<int>>
#define OST                                                                    \\
  tree<pii, null_type, less<pii>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

const int INF = 1e18 + 0;
const int mod = 1e9 + 7;
// const int mod = 998244353;

int n;
int A[85];
// map <pair <int, pii>, int > dp, vis;

// int go(int ind, int money, int stocks) {
//   if(ind > n) return money;

//   int &x = dp[{ind, {money, stocks}}];
//   if(vis[{ind,{money, stocks}}]) return x;

//   int kuchni = go(ind+1, money, stocks);
//   int nm = money + stocks*A[ind];
//   int sell = go(ind+1, nm, 0);
//   nm = money%A[ind]; int ns = money/A[ind];
//   int buy = go(ind+1, nm, ns);

//   vis[{ind,{money, stocks}}] = 1;
//   return (x = max(kuchni, max(buy, sell)));
// }

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  int money = 1000, stocks = 0;
  A[n + 1] = -INF;
  for (int i = 1; i <= n; i++) {
    if (A[i] < A[i + 1]) {
      stocks += (money / A[i]);
      money %= A[i];
    } else {
      money += (stocks * A[i]);
      stocks = 0;
    }
  }
  cout << money << '\\n';
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1; // cin >> t;
  for (int i = 1; i <= t; i++) {
    //  cout << "Case #" << i << ": ";
    solve();
  }
}

/*

*/

Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
#define endl '\\n'
#define ld long double
#define all(a) a.begin(), a.end()
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define sz(v) (int)v.size()
#define UB upper_bound
#define LB lower_bound
#define BP(x) __builtin_popcountll(x)
#define PQS priority_queue<int, vector<int>, greater<int>>
#define OST                                                                    \\
  tree<pii, null_type, less<pii>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

const int INF = 1e18 + 0;
const int mod = 1e9 + 7;
// const int mod = 998244353;

int n;
int A[85];
map<pair<int, pii>, int> dp, vis;

int go(int ind, int money, int stocks) {
  if (ind > n)
    return money;

  int &x = dp[{ind, {money, stocks}}];
  if (vis[{ind, {money, stocks}}])
    return x;

  int kuchni = go(ind + 1, money, stocks);
  int nm = money + stocks * A[ind];
  int sell = go(ind + 1, nm, 0);
  nm = money % A[ind];
  int ns = money / A[ind];
  int buy = go(ind + 1, nm, ns);

  vis[{ind, {money, stocks}}] = 1;
  return (x = max(kuchni, max(buy, sell)));
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  int ans = go(1, 1000, 0);
  cout << ans << '\\n';
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1; // cin >> t;
  for (int i = 1; i <= t; i++) {
    //  cout << "Case #" << i << ": ";
    solve();
  }
}

/*

*/