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

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
const ll Mod = 1000000007;
const ll INF = 999999999999999999;
const ll NN = (ll)(1e6 + 5);
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
ll n;
vector<ll> v;
// map <pair<ll,pair<ll,ll>>,ll> dp;
// ll func(ll ind,ll money,ll stock)
// {
//     if(ind==n-1)
//      return money+stock*v[ind];
//     if(dp[{ind,{money,stock}}])
//      return dp[{ind,{money,stock}}];
//     return
//     dp[{ind,{money,stock}}]=max(func(ind+1,money-((money/v[ind])*v[ind]),stock+(money/v[ind])),func(ind+1,(money+stock*v[ind]),0));
// }
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    cin >> n;
    v.resize(n);
    ll stock = 0, money = 1000, last = n, i;
    for (auto &u : v)
      cin >> u;
    // ll ans=func(0,1000,0);
    for (i = 0; i < n - 1; i++) {
      // cout<<v[i+1]<<" "<<v[i]<<"\\n";
      if (v[i + 1] > v[i]) {
        // cout<<"yo\\n";
        stock += money / v[i];
        money -= (v[i] * stock);
        last = i;
        break;
      }
    }
    // cout<<last<<"\\n";
    for (i = last + 1; i < n; i++) {
      if (v[i] <= v[i - 1] && v[i] < v[i + 1]) {
        stock += money / v[i];
        money -= (money / v[i]) * v[i];
        last = i;
      } else if (v[i] >= v[i - 1] && ((v[i] > v[i + 1]) || (v[i] == n - 1))) {
        money += stock * v[i];
        stock = 0;
        last = i;
      }
    }
    if (stock)
      money += stock * v[n - 1];
    cout << money;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
const ll Mod = 1000000007;
const ll INF = 999999999999999999;
const ll NN = (ll)(1e6 + 5);
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
ll n;
vector<ll> v;
map<pair<ll, pair<ll, ll>>, ll> dp;
ll func(ll ind, ll money, ll stock) {
  if (ind == n - 1)
    return money + stock * v[ind];
  if (dp[{ind, {money, stock}}])
    return dp[{ind, {money, stock}}];
  return dp[{ind, {money, stock}}] =
             max(func(ind + 1, money - ((money / v[ind]) * v[ind]),
                      stock + (money / v[ind])),
                 func(ind + 1, (money + stock * v[ind]), 0));
}
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    cin >> n;
    v.resize(n);
    for (auto &u : v)
      cin >> u;
    ll ans = func(0, 1000, 0);
    cout << ans;
  }
}