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

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

// const int M = 100000;
const int M = 20;

int main() {
  int N;
  cin >> N;
  vector<lint> A(N, 0);
  rep(i, N) cin >> A[i];
  A.push_back(-1);

  lint sum = 1000;
  lint k = 0;

  for (int i = 0; i < N; i++) {
    if (A[i + 1] <= A[i]) {
      continue;
    }

    for (int j = i + 1; j < N; j++) {
      if (A[j] > A[i] and A[j] > A[j + 1]) {
        k = sum / A[i];
        sum -= A[i] * k;

        sum += k * A[j];
        k = 0;
        i = j;
        break;
      }
    }
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

// const int M = 100000;
const int M = 20;

int main() {
  int N;
  cin >> N;
  vector<lint> A(N, 0);
  rep(i, N) cin >> A[i];
  A.push_back(-1);

  lint sum = 1000;
  lint k = 0;

  for (int i = 0; i < N; i++) {
    while (A[i + 1] <= A[i]) {
      continue;
    }

    for (int j = i + 1; j < N; j++) {
      if (A[j] > A[i] and A[j] > A[j + 1]) {
        k = sum / A[i];
        sum -= A[i] * k;

        sum += k * A[j];
        k = 0;
        i = j;
        break;
      }
    }
  }

  cout << sum << endl;

  return 0;
}