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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

int main(void) {
  ll N;
  ll money = 1000;
  ll kabu = 0;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  A.erase(unique(A.begin(), A.end()), A.end());
  rep(i, A.size()) {
    ll temp = i;
    ll temp2 = i;
    rep2(j, i + 1, A.size()) {
      if (A[i] > A[j])
        temp = j;
      else
        break;
    }
    rep2(j, i + 1, A.size()) {
      if (A[i] < A[j])
        temp2 = j;
      else
        break;
    }
    if (i == temp2) {
      money += A[i] * kabu;
      kabu = 0;
    } else if (i == temp) {
      ll temp = money / A[i];
      kabu += temp;
      money -= temp * A[i];
    }
  }
  cout << money << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

ll N;
vector<ll> A;
ll maxi = 0;

void dfs(ll index, ll kabu, ll money) {
  if (index == N - 1) {
    maxi = max(maxi, money + A.back() * kabu);
    return;
  }
  {
    ll temp = money / A[index];
    dfs(index + 1, kabu + temp, money - temp * A[index]);
  }
  { dfs(index + 1, 0, money + A[index] * kabu); }
}

int main(void) {
  cin >> N;
  A.resize(N);
  rep(i, N) cin >> A[i];
  dfs(0, 0, 1000);
  cout << maxi << endl;
  // ll money = 1000;
  // ll kabu = 0;
  // rep(i, N) {
  //     bool flag = false;
  //     rep2(j, i + 1, N) {
  //         if(A[i] < A[j]) {
  //             flag = true;
  //             break;
  //         }
  //     }
  //     if(flag) {
  //         ll temp = money / A[i];
  //         kabu += temp;
  //         money -= temp * A[i];
  //     } else {
  //         money += A[i] * kabu;
  //         kabu = 0;
  //     }
  //     cout << money << endl;
  // }
  // cout << money << endl;
  return 0;
}