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

#include <bits/stdc++.h>

//{{{
#if ((_WIN32 || __WIN32__) && __cplusplus < 201103L)
#define lld I64d
#else
#define lld lld
#endif
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, int> pii;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
// clang-format off
#define debug(args...) {vector<string> _v = split(#args, ','); err(_v.begin(), args); cerr<<endl;}
 vector<string> split(const string& s, char c) {vector<string> v; stringstream ss(s); string x;
 while (getline(ss, x, c)) v.push_back(x); return v;} void err(vector<string>::iterator) {}
 template<typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args)
{cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; err(++it, args...);}
// clang-format on
//}}}
//

int T;

const int mod = 1e9 + 7;
const int N = 1e2 + 10;
int x;
int n;
int a[N];

// LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
// LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }

int head[N], e;
struct E {
  int v, next;
} edge[N];

void addedge(int u, int v) {
  edge[e].v = v;
  edge[e].next = head[u];
  head[u] = e++;
}

LL dp[111];
bool inque[111];
queue<int> q;

int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif

  while (cin >> n) {
    for (int i = 1; i <= n; i++)
      cin >> a[i];

    clr(dp, 0);
    dp[1] = 1000;
    inque[1] = 1;
    q.push(1);

    while (!q.empty()) {
      int day = q.front();
      LL money = dp[day];
      q.pop();

      inque[day] = 0;

      for (int i = day + 1; i <= n; i++) {
        if (a[i] > a[day]) {
          LL d = money % a[day];
          LL v = money / a[day] * a[i];
          LL tot = v + d;
          dp[i] = max(dp[i], tot);

          if (!inque[i]) {
            inque[i] = true;
            q.push(i);
          }
        } else {
          dp[i] = max(dp[i], money);
          if (!inque[i]) {
            inque[i] = true;
            q.push(i);
          }
        }
        // debug(i, dp[i]);
      }
    }

    cout << dp[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

//{{{
#if ((_WIN32 || __WIN32__) && __cplusplus < 201103L)
#define lld I64d
#else
#define lld lld
#endif
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, int> pii;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
// clang-format off
#define debug(args...) {vector<string> _v = split(#args, ','); err(_v.begin(), args); cerr<<endl;}
 vector<string> split(const string& s, char c) {vector<string> v; stringstream ss(s); string x;
 while (getline(ss, x, c)) v.push_back(x); return v;} void err(vector<string>::iterator) {}
 template<typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args)
{cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; err(++it, args...);}
// clang-format on
//}}}
//

int T;

const int mod = 1e9 + 7;
const int N = 1e2 + 10;
int x;
int n;
int a[N];

// LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
// LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }

int head[N], e;
struct E {
  int v, next;
} edge[N];

void addedge(int u, int v) {
  edge[e].v = v;
  edge[e].next = head[u];
  head[u] = e++;
}

LL dp[111];
bool vis[111];
priority_queue<pii> q;

int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif

  while (cin >> n) {
    for (int i = 1; i <= n; i++)
      cin >> a[i];

    clr(dp, 0);
    dp[1] = 1000;
    q.push({1000, 1});

    while (!q.empty()) {
      auto [money, day] = q.top();
      q.pop();

      vis[day] = true;

      for (int i = day + 1; i <= n; i++) {
        if (a[i] > a[day]) {
          LL d = money % a[day];
          LL v = money / a[day] * a[i];
          LL tot = v + d;
          dp[i] = max(dp[i], tot);
        } else {
          dp[i] = max(dp[i], money);
        }
        // debug(i, dp[i]);
        q.push({dp[i], i});
      }
    }

    cout << dp[n] << endl;
  }

  return 0;
}