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

#ifdef __APPLE__
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<long long int> vll;
typedef vector<pair<int, int>> vpi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

void solve() {
  int n;
  cin >> n;
  vll a(n);

  for (auto &x : a)
    cin >> x;
  a.resize(std::distance(a.begin(), std::unique(a.begin(), a.end())));
  n = sz(a);

  ll ans = 1000, s = 0;

  FOR(i, 0, n) {
    if ((i == 0 or a[i] < a[i - 1]) and (i < (n - 1) and a[i] < a[i + 1])) {
      s += ans / a[i], ans %= a[i];
    }

    if ((i == 0 or a[i] > a[i - 1]) and (i == (n - 1) or a[i] > a[i + 1])) {
      ans += a[i] * s, s = 0;
    }
  }
  assert(s == 0);
  cout << ans << "\\n";
}

int main() {
#ifdef LOCAL
  auto t1 = std::chrono::high_resolution_clock::now();
#endif

  io;
  solve();

#ifdef LOCAL
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();

  std::cout << "Time Taken: " << duration << "\\n";
#endif
}
// check for edge cases in limits
// printing double may print 1.223232e9 like values, beware of that
// carefully observe simulation of test cases


Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")

#ifdef __APPLE__
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<long long int> vll;
typedef vector<pair<int, int>> vpi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head
typedef gp_hash_table<int, int, hash<int>> ht;
int n;

int recurse(int i, int j, int k, vector<vector<ht>> &dp, vi &a) {
  if (i == n)
    return k;
  auto it = dp[i][j].find(k);
  if (it != dp[i][j].end())
    return it->second;

  int ans = recurse(i + 1, j, k, dp, a);
  int add = a[i], sub = a[i];
  FOR(sell, 1, j + 1)
  ans = max(ans, recurse(i + 1, j - sell, k + add, dp, a)), add += a[i];

  FOR(pur, 1, k / a[i] + 1)
  ans = max(ans, recurse(i + 1, j + pur, k - sub, dp, a)), sub += a[i];

  dp[i][j][k] = ans;
  return ans;
}

void solve() {
  cin >> n;

  vi a(n);
  for (auto &x : a)
    cin >> x;

  vector<vector<ht>> dp(n + 1, vector<ht>(11 * n));
  cout << recurse(0, 0, 1000, dp, a) << "\\n";
}

int main() {
#ifdef LOCAL
  auto t1 = std::chrono::high_resolution_clock::now();
#endif

  io;
  solve();

#ifdef LOCAL
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();

  std::cout << "Time Taken: " << duration << "\\n";
#endif
}
// check for edge cases in limits
// printing double may print 1.223232e9 like values, beware of that
// carefully observe simulation of test cases