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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b;
  rep(i, n) {
    cin >> a[i];
    if (i == 0)
      b.emplace_back(a[i]);
    else if (a[i - 1] != a[i])
      b.emplace_back(a[i]);
  }
  ll money = 1000, kabu = 0;
  int m = b.size();
  rep(i, m) {
    if (i == 0) {
      if (b[i] > b[i + 1])
        continue;
      else { // buy
        kabu += money / b[i];
        money %= b[i];
      }
    } else if (i == m - 1) {
      money += kabu * b[i];
      kabu = 0;
    } else {
      if (b[i - 1] < b[i] and b[i] > b[i + 1]) { // sell
        money += kabu * b[i];
        kabu = 0;
      } else if (b[i - 1] > b[i] and b[i] < b[i + 1]) { // buy
        kabu += money / b[i];
        money %= b[i];
      }
    }
  }
  cout << money << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

int n;

int dfs(int j, int v, int k, const vector<int> &a) {
  if (j == n)
    return v;
  int res1 = 0;
  rep(i, v / a[j] + 1) { // i個買う
    if (v - a[j] * i < 0)
      break;
    chmax(res1, dfs(j + 1, v - a[j] * i, k + i, a));
  }
  int res2 = 0;
  rep(i, k + 1) { // i個売る
    chmax(res2, dfs(j + 1, v + a[j] * i, k - i, a));
  }
  return max(res1, res2);
}

int main() {
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  cout << dfs(0, 1000, 0, a) << endl;
  return 0;
}