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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define setbits(x) __builtin_popcountll(x)
#define lp(a, n) for (ll i = a; i < n; i++)
#define lpi(a, n) for (int i = a; i < n; i++)
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define inf 1e9
#define LB lower_bound
#define UB upper_bound
const int mod = 1000000007;
using namespace std;
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int prime[100007];
void SieveOfEratosthenes(int n) {
  memset(prime, -1, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {

    if (prime[p] == -1) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = p;
    }
  }
}
int lps(string s) {
  int len = 0;
  int n = s.length();
  int dp[n];
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    if (s[i] == s[len]) {
      len++;
      dp[i] = len;
      continue;
    }
    if (len != 0) {
      len = dp[len - 1];
      i--;
      continue;
    }
    dp[i] = 0;
  }
  return dp[n - 1];
}

ll power(unsigned ll x, unsigned ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;

    y = y >> 1;
    x = x * x;
  }
  return res;
}

// to calculate p1^x  *  p2^x2  *p3^x3 ----------
// i.e. prime factorization and store pi and xi in vector
vector<pair<ll, ll>> pFactor;

void factorize(long long n) {
  int count = 0;

  // count the number of times 2 divides
  while (!(n % 2)) {
    n >>= 1; // equivalent to n=n/2;
    count++;
  }

  // if 2 divides it
  if (count)
    pFactor.pb({2, count});

  // check for all the possible numbers that can
  // divide it
  for (long long i = 3; i <= sqrt(n); i += 2) {
    count = 0;
    while (n % i == 0) {
      count++;
      n = n / i;
    }
    if (count)
      pFactor.pb({i, count});
  }

  // if n at the end is a prime number.
  if (n > 2)
    pFactor.pb({n, 1});
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  lp(0, n) cin >> a[i];
  ll ans = 1000;
  ll prev = a[0];
  lp(1, n) {
    if (a[i] > prev) {
      ans += (ans / prev) * (a[i] - prev);
    }
    prev = a[i];
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define setbits(x) __builtin_popcountll(x)
#define lp(a, n) for (ll i = a; i < n; i++)
#define lpi(a, n) for (int i = a; i < n; i++)
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define inf 1e9
#define LB lower_bound
#define UB upper_bound
const int mod = 1000000007;
using namespace std;
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int prime[100007];
void SieveOfEratosthenes(int n) {
  memset(prime, -1, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {

    if (prime[p] == -1) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = p;
    }
  }
}
int lps(string s) {
  int len = 0;
  int n = s.length();
  int dp[n];
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    if (s[i] == s[len]) {
      len++;
      dp[i] = len;
      continue;
    }
    if (len != 0) {
      len = dp[len - 1];
      i--;
      continue;
    }
    dp[i] = 0;
  }
  return dp[n - 1];
}

ll power(unsigned ll x, unsigned ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;

    y = y >> 1;
    x = x * x;
  }
  return res;
}

// to calculate p1^x  *  p2^x2  *p3^x3 ----------
// i.e. prime factorization and store pi and xi in vector
vector<pair<ll, ll>> pFactor;

void factorize(long long n) {
  int count = 0;

  // count the number of times 2 divides
  while (!(n % 2)) {
    n >>= 1; // equivalent to n=n/2;
    count++;
  }

  // if 2 divides it
  if (count)
    pFactor.pb({2, count});

  // check for all the possible numbers that can
  // divide it
  for (long long i = 3; i <= sqrt(n); i += 2) {
    count = 0;
    while (n % i == 0) {
      count++;
      n = n / i;
    }
    if (count)
      pFactor.pb({i, count});
  }

  // if n at the end is a prime number.
  if (n > 2)
    pFactor.pb({n, 1});
}

int find(vector<vi> &dp, int a[], int i, int n, int money) {
  if (i >= n) {
    return 0;
  }
  int ans = 0;
  for (int j = i; j < n; j++) {
    for (int k = j; k < n; k++) {
      int profit = 0;
      if (a[k] > a[j]) {
        profit = (money / a[j]) * dp[j][k];
        profit += find(dp, a, k + 1, n, money + profit);
      }
      ans = max(ans, profit);
    }
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  lp(0, n) cin >> a[i];
  vector<vi> dp(n, vi(n, 0));

  lp(0, n) {
    for (int j = i; j < n; j++) {
      dp[i][j] = a[j] - a[i];
    }
  }
  int ans = 0;
  ans = find(dp, a, 0, n, 1000);
  cout << ans + 1000 << endl;
}