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

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  for (int i(0); i < N; ++i)
    std::cin >> A[i];
  long long ans(1000), cur(1000), st(0);
  bool decre(true);
  for (int k(0); k < N; ++k) {
    cur = 1000;
    for (int i(k); i < N; ++i) {
      std::vector<std::pair<int, int>> add;
      if (decre && (i < N - 1 && A[i] < A[i + 1])) {
        decre = false;
        st += cur / A[i];
        cur %= A[i];
      } else if (i == N - 1 || (!decre && A[i] > A[i + 1])) {
        decre = true;
        cur += st * A[i];
        st = 0;
        chmax(ans, cur);
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  for (int i(0); i < N; ++i)
    std::cin >> A[i];
  // first = cur, second = st
  std::vector<std::pair<int, int>> state, add;
  state.emplace_back(1000, 0);
  bool decre(true);
  for (int i(0); i < N; ++i) {
    std::vector<std::pair<int, int>> add;
    if (decre && i < N - 1) {
      if (A[i] < A[i + 1])
        decre = false;
      for (int j(0), j_len(state.size()); j < j_len; ++j) {
        std::pair<int, int> e;
        e.first = state[j].first % A[i];
        e.second = state[j].second + state[j].first / A[i];
        add.emplace_back(e);
      }
    } else if (i == N - 1 || !decre) {
      if (i < N - 1 && A[i] > A[i + 1])
        decre = true;
      for (int j(0), j_len(state.size()); j < j_len; ++j) {
        std::pair<int, int> e;
        e.first = state[j].first + state[j].second * A[i];
        e.second = 0;
        add.emplace_back(e);
      }
    }

    for (int j(0), j_len(add.size()); j < j_len; ++j) {
      state.emplace_back(add[j]);
    }
    std::sort(std::begin(state), std::end(state));
    int prv(INF), mx(-1);
    for (int j(state.size() - 1); j <= 0; --j) {
      if (mx >= state[j].second) {
        state.erase(std::begin(state) + j);
      } else
        mx = state[j].second;
    }
  }
  std::cout << (*std::rbegin(state)).first << std::endl;
  return 0;
}