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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define debug(n) cout << "DEBUG1 " << n << "\\n";
#define debug2(n, m) cout << "DEBUG2 " << n << ' ' << m << "\\n";
#define debug3(n, m, x) cout << "DEBUG3 " << n << ' ' << m << ' ' << x << "\\n";
#define debug4(n, m, x, y)                                                     \\
  cout << "DEBUG3 " << n << ' ' << m << ' ' << x << ' ' << y << "\\n";
#define ss_def                                                                 \\
  stringstream ss // stringstream, do ss_def(str), then ss >> n (n is int)
#define upzero(n)                                                              \\
  if (n < 0)                                                                   \\
  n = 0
#pragma GCC optimize("Ofast")
using namespace std;
ll int BtoPmodM(ll int b, ll int p, ll int m) { // (b ^ p) % m (Big Mod)
  b %= m;
  if (p == 0)
    return 1;
  else if (p % 2 == 0)
    return (BtoPmodM(b, p / 2, m) * BtoPmodM(b, p / 2, m)) %
           m; // a^(p/2) * a^(p/2) = a^p
  else if (p % 2 == 1)
    return (BtoPmodM(b, (p - 1) / 2, m) * BtoPmodM(b, (p - 1) / 2, m) * b) %
           m; // +1
  else
    return 0; // a function must return something
}
ll int gcd(ll int a, ll int b) { // Euclidean Algorithm
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
bool coprime(ll int a, ll int b) {
  if (gcd(a, b) == 1)
    return true;
  else
    return false;
}
ll int factModP(ll int n, ll int p) {
  ll int fac[n - 1];
  fac[0] = 1;
  for (ll int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % p;
  }
  return fac[n];
}
ll int power(ll int x, ll int y, ll int p) { // x ^ y % p
  ll int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y /= 2;
    x = (x * x) % p;
  }
  return res;
}
ll int modInverse(ll int n, ll int p) { return power(n, p - 2, p); }
ll int nCrModP(ll int n, ll int r, ll int p) {
  if (r == 0)
    return 1;
  else if (r > n)
    return 0;
  ll int fac[n + 1];
  fac[0] = 1;
  for (ll int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % p;
  }
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
ll int nCr(ll int n, ll int r) {
  ll int ans = 1;
  if (r >= n - r) {
    for (ll int i = r + 1; i <= n; i++)
      ans *= i;
    for (ll int i = 1; i <= n - r; i++)
      ans /= i;
  } else {
    for (ll int i = n - r + 1; i <= n; i++)
      ans *= i;
    for (ll int i = 1; i <= r; i++)
      ans /= i;
  }
  return ans;
}
ll int string_to_int(string str) {
  int strl = str.length();
  ll int ans = 0;
  if (str[0] == '-') {
    for (int i = 1; i < strl; i++)
      ans += pow(10, strl - i - 1) * (str[i] - 48);
    ans = 0 - ans;
    return ans;
  }
  for (int i = 0; i < strl; i++)
    ans += pow(10, strl - i - 1) * (str[i] - 48);
  return ans;
}
struct edge_stru {
  int u, v, w; // node 1, node 2, weight
};
edge_stru make_edge_stru(int u, int v, int w) {
  edge_stru a = {u, v, w};
  return a;
}
class cmp { // sorting edges
public:
  bool operator()(edge_stru a, edge_stru b) {
    return a.w > b.w; // sort in ascending order of weight
  }
};
void make_set(int v, int *parent, int *rank) {
  parent[v] = v; // set as the root
  rank[v] = 0;   // first rank is 0
  return;
}
int find_set(int v, int *parent) {
  if (v == parent[v])
    return v; // finds root
  //    return parent[v] = find_set(parent[v], parent); // Path compression
  //    optimization
  return find_set(parent[v], parent);
}
void union_sets(int a, int b, int *parent, int *rank) {
  //    a = find_set(a, parent); // find root of a
  //    b = find_set(b, parent); // find root of b
  if (rank[a] > rank[b])
    swap(a, b);  // let a be the bigger rank (root a < root b)
  parent[b] = a; // set node b as root a's parent
  if (rank[a] == rank[b])
    rank[b]++; // if same rank set b as the child of a
  return;
}
bool binSearch(ll *arr, ll n, ll target) {
  ull l = 0, r = n - 1;
  while (l < r) {
    int mid = (l + r) / 2;
    if (arr[mid] >= target) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  if (arr[l] == target) {
    return true;
  } else {
    return false;
  }
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll int tot = 1000;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] <= a[i]) {
      ll int cnt = tot / a[i - 1];
      tot += (a[i] - a[i - 1]) * cnt;
    }
  }
  cout << tot << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // implementation
  solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define debug(n) cout << "DEBUG1 " << n << "\\n";
#define debug2(n, m) cout << "DEBUG2 " << n << ' ' << m << "\\n";
#define debug3(n, m, x) cout << "DEBUG3 " << n << ' ' << m << ' ' << x << "\\n";
#define debug4(n, m, x, y)                                                     \\
  cout << "DEBUG3 " << n << ' ' << m << ' ' << x << ' ' << y << "\\n";
#define ss_def                                                                 \\
  stringstream ss // stringstream, do ss_def(str), then ss >> n (n is int)
#define upzero(n)                                                              \\
  if (n < 0)                                                                   \\
  n = 0
#pragma GCC optimize("Ofast")
using namespace std;
ll int BtoPmodM(ll int b, ll int p, ll int m) { // (b ^ p) % m (Big Mod)
  b %= m;
  if (p == 0)
    return 1;
  else if (p % 2 == 0)
    return (BtoPmodM(b, p / 2, m) * BtoPmodM(b, p / 2, m)) %
           m; // a^(p/2) * a^(p/2) = a^p
  else if (p % 2 == 1)
    return (BtoPmodM(b, (p - 1) / 2, m) * BtoPmodM(b, (p - 1) / 2, m) * b) %
           m; // +1
  else
    return 0; // a function must return something
}
ll int gcd(ll int a, ll int b) { // Euclidean Algorithm
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
bool coprime(ll int a, ll int b) {
  if (gcd(a, b) == 1)
    return true;
  else
    return false;
}
ll int factModP(ll int n, ll int p) {
  ll int fac[n - 1];
  fac[0] = 1;
  for (ll int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % p;
  }
  return fac[n];
}
ll int power(ll int x, ll int y, ll int p) { // x ^ y % p
  ll int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y /= 2;
    x = (x * x) % p;
  }
  return res;
}
ll int modInverse(ll int n, ll int p) { return power(n, p - 2, p); }
ll int nCrModP(ll int n, ll int r, ll int p) {
  if (r == 0)
    return 1;
  else if (r > n)
    return 0;
  ll int fac[n + 1];
  fac[0] = 1;
  for (ll int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % p;
  }
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
ll int nCr(ll int n, ll int r) {
  ll int ans = 1;
  if (r >= n - r) {
    for (ll int i = r + 1; i <= n; i++)
      ans *= i;
    for (ll int i = 1; i <= n - r; i++)
      ans /= i;
  } else {
    for (ll int i = n - r + 1; i <= n; i++)
      ans *= i;
    for (ll int i = 1; i <= r; i++)
      ans /= i;
  }
  return ans;
}
ll int string_to_int(string str) {
  int strl = str.length();
  ll int ans = 0;
  if (str[0] == '-') {
    for (int i = 1; i < strl; i++)
      ans += pow(10, strl - i - 1) * (str[i] - 48);
    ans = 0 - ans;
    return ans;
  }
  for (int i = 0; i < strl; i++)
    ans += pow(10, strl - i - 1) * (str[i] - 48);
  return ans;
}
struct edge_stru {
  int u, v, w; // node 1, node 2, weight
};
edge_stru make_edge_stru(int u, int v, int w) {
  edge_stru a = {u, v, w};
  return a;
}
class cmp { // sorting edges
public:
  bool operator()(edge_stru a, edge_stru b) {
    return a.w > b.w; // sort in ascending order of weight
  }
};
void make_set(int v, int *parent, int *rank) {
  parent[v] = v; // set as the root
  rank[v] = 0;   // first rank is 0
  return;
}
int find_set(int v, int *parent) {
  if (v == parent[v])
    return v; // finds root
  //    return parent[v] = find_set(parent[v], parent); // Path compression
  //    optimization
  return find_set(parent[v], parent);
}
void union_sets(int a, int b, int *parent, int *rank) {
  //    a = find_set(a, parent); // find root of a
  //    b = find_set(b, parent); // find root of b
  if (rank[a] > rank[b])
    swap(a, b);  // let a be the bigger rank (root a < root b)
  parent[b] = a; // set node b as root a's parent
  if (rank[a] == rank[b])
    rank[b]++; // if same rank set b as the child of a
  return;
}
bool binSearch(ll *arr, ll n, ll target) {
  ull l = 0, r = n - 1;
  while (l < r) {
    int mid = (l + r) / 2;
    if (arr[mid] >= target) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  if (arr[l] == target) {
    return true;
  } else {
    return false;
  }
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll int tot = 1000;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] <= a[i]) {
      ll int cnt = 0;
      while (tot >= a[i - 1]) {
        tot -= a[i - 1];
        cnt++;
      }
      tot += cnt * a[i];
    }
  }
  cout << tot << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // implementation
  solve();
}