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

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("avx,avx2")
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")

// # include <x86intrin.h>
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 1e8 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

int rnd(int l, int r) { return uniform_int_distribution<int>(l, r)(gen); }
int a[88];

int main() {
  SpeedForce;

  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  ll ans = 1000;
  int curmn = 200;
  for (int i = 1; i <= n; ++i) {
    if (a[i] > curmn) {
      ans = (ans / curmn) * a[i] + (ans % curmn);
      curmn = a[i];
    } else {
      curmn = a[i];
    }
  }
  cout << ans << '\\n';
  return Accepted;
}

// B...a

Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("avx,avx2")
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")

// # include <x86intrin.h>
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 1e8 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

int rnd(int l, int r) { return uniform_int_distribution<int>(l, r)(gen); }
const int K = 20000;
int dp[88][K + 5];

int main() {
  SpeedForce;

  for (int i = 0; i < 88; ++i)
    for (int j = 0; j <= K; ++j)
      dp[i][j] = -inf;

  dp[0][0] = 1000;
  int n;
  cin >> n;
  for (int i = 1, x; i <= n; ++i) {
    cin >> x;
    for (int j = 0; j <= K; ++j)
      if (dp[i - 1][j] >= 0) {
        for (int s = 0; s <= K; ++s) {
          if (s * x <= dp[i - 1][j])
            dp[i][j + s] = max(dp[i][j + s], dp[i - 1][j] - s * x);
          if (s <= j) {
            dp[i][j - s] = max(dp[i][j - s], dp[i - 1][j] + s * x);
          }
        }
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      }
  }

  cout << dp[n][0] << '\\n';
  return Accepted;
}

// B...a