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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <deque>
#define pi 3.141592653589793238
#include <chrono>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast;
  ll T = 1, i, j;

  // cin >> T;
  while (T--) {
    ll n;
    cin >> n;
    vector<ll> v(n);
    for (i = 0; i < n; i++) {
      cin >> v[i];
    }
    // dp[i][j]  is max money left if he has j stocks on the ith day
    vector<vector<ll>> dp(n + 1, vector<ll>(2, -INF));
    dp[1][0] = 1000;
    dp[1][1] = 1000 - (1000 / v[0]) * v[0];
    for (i = 1; i < n; i++) {
      ll prev = dp[i][0];
      ll prevstocks = dp[i][0] / v[i - 1];
      dp[i + 1][0] = max(dp[i][0], dp[i][1] + prevstocks * v[i]);
      dp[i + 1][1] = dp[i + 1][0] - (dp[i + 1][0] / v[i]) * v[i];
    }
    cout << dp[n][0];
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <deque>
#define pi 3.141592653589793238
#include <chrono>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast;
  ll T = 1, i, j;

  // cin >> T;
  while (T--) {
    ll n;
    cin >> n;
    vector<ll> v(n);
    for (i = 0; i < n; i++) {
      cin >> v[i];
    }
    // dp[i][j]  is max money left if he has j stocks on the ith day
    vector<vector<ll>> dp(n + 1, vector<ll>(100000, -INF));
    for (i = 0; i < 100000; i++) {
      dp[1][i] = 1000 - v[0] * (i);
      if (dp[1][i] < 0) {
        dp[1][i] = -INF;
      }
    }
    for (i = 1; i < n; i++) {
      for (j = 0; j < 100000; j++) {
        for (ll it = 0; it < 100000; it++) {
          if (dp[i][it] < 0) {
            continue;
          }

          dp[i + 1][j] = max(dp[i + 1][j], dp[i][it] - (j - it) * v[i]);
        }
      }
    }
    cout << dp[n][0];
  }
  return 0;
}