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

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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>>
    vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18
const int INF = pow(2, 30) - 1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll ans = 1000;
vector<ll> A(100);

/*void dfs(ll money, ll kabu, ll i,ll N)
{
    if(i==N){
        ans = max(ans, money);
        return;
    }

    dfs(money, kabu, i + 1,N);


    ll tmp = money / A.at(i);
    if (tmp>=1){
        dfs(money - A.at(i) * tmp, kabu + tmp, i + 1, N);
    }
    if(kabu>=1){
        ll tmp1 = kabu * A.at(i);
        dfs(money + tmp1, 0, i + 1, N);
    }

}*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll N;
  string S;
  cin >> N;

  REP(i, N) { cin >> A.at(i); }

  // dfs(1000, 0, 0, N);

  ll money, kabu;

  money = 1000;
  kabu = 0;

  REPS(i, N - 2) {
    if (A.at(i - 1) <= A.at(i) && A.at(i) >= A.at(i + 1)) {
      money += kabu * A.at(i);
      kabu = 0;
    }
    if (A.at(i - 1) >= A.at(i) && A.at(i) <= A.at(i + 1)) {
      ll tmp = money / A.at(i);
      kabu += tmp;
      money -= tmp * A.at(i);
    }
  }

  money += kabu * A.at(N - 1);

  ans = max(ans, money);

  kabu = 1000 / A.at(0);
  money = 1000 - kabu * A.at(0);

  REPS(i, N - 2) {
    if (A.at(i - 1) <= A.at(i) && A.at(i) >= A.at(i + 1)) {
      money += kabu * A.at(i);
      kabu = 0;
    }
    if (A.at(i - 1) >= A.at(i) && A.at(i) <= A.at(i + 1)) {
      ll tmp = money / A.at(i);
      kabu += tmp;
      money -= tmp * A.at(i);
    }
  }

  money += kabu * A.at(N - 1);
  ans = max(ans, money);

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>>
    vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18
const int INF = pow(2, 30) - 1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll ans = 0;
vector<ll> A(100);

void dfs(ll money, ll kabu, ll i, ll N) {
  if (i == N) {
    ans = max(ans, money);
    return;
  }

  dfs(money, kabu, i + 1, N);

  ll tmp = money / A.at(i);
  if (tmp >= 1) {
    dfs(money - A.at(i) * tmp, kabu + tmp, i + 1, N);
  }
  if (kabu >= 1) {
    ll tmp1 = kabu * A.at(i);
    dfs(money + tmp1, 0, i + 1, N);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll N;
  string S;
  cin >> N;

  REP(i, N) { cin >> A.at(i); }

  dfs(1000, 0, 0, N);
  cout << ans << endl;
}