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

/*Author : Sambhu show(jgec)*/
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define itc                                                                    \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define PI 3.141592653589793238
#define Max(a, b, c) max(a, max(b, c));
#define Min(a, b, c) min(a, min(b, c));
#define all(v) v.begin(), v.end()
#define ll long long int
#define ld long double
#define pb push_back
#define eb emplace_back
#define gcd(a, b) __gcd(a, b)
#define fi first
#define se second
#define endl "\\n"
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int XX[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int YY[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
ll price[85];
ll n;
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> price[i];

  ll s = 0, sum = 1000;
  for (int i = 1; i < n; i++) {
    if (price[i] > price[i - 1]) {
      ll buy = sum / price[i - 1];
      s += buy;
      sum -= (buy * price[i - 1]);
      sum += price[i] * s;
      s = 0;
    }
  }
  cout << sum;
}
int main() {
  fio
  // itc
  {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

/*Author : Sambhu show(jgec)*/
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define itc                                                                    \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define PI 3.141592653589793238
#define Max(a, b, c) max(a, max(b, c));
#define Min(a, b, c) min(a, min(b, c));
#define all(v) v.begin(), v.end()
#define ll long long int
#define ld long double
#define pb push_back
#define eb emplace_back
#define gcd(a, b) __gcd(a, b)
#define fi first
#define se second
#define endl "\\n"
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int XX[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int YY[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
ll price[85];
ll n;
ll dp[30][81][80001];
ll maximumsum(ll stock, ll indx, ll sum) {
  if (indx >= n)
    return sum;

  if (stock < 30 && indx <= 80 && sum <= 80000) {
    if (dp[stock][indx][sum] != -1)
      return dp[stock][indx][sum];
  }

  ll ans = sum;
  for (int i = 0; i <= sum / price[indx]; i++)
    ans = max(ans, maximumsum(stock + i, indx + 1,
                              sum - (i * price[indx]))); // buying

  for (int i = 0; i <= stock; i++)
    ans = max(ans, maximumsum(stock - i, indx + 1, sum + (i * price[indx])));

  if (stock < 30 && indx <= 80 && sum <= 80000)
    return dp[stock][indx][sum] = ans;
  else
    return ans;
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> price[i];

  memset(dp, -1, sizeof(dp));
  cout << maximumsum(0, 0, 1000);
}
int main() {
  fio
  // itc
  {
    solve();
  }
  return 0;
}