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

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")

int main() {
  int N;
  vecl(A);
  ll ans = 1000;

  cin >> N;
  fx(N) { vecp(A); }
  int start = 0;
  fx(N - 1) {
    if (A[x] <= A[x + 1]) {
      continue;
    }
    ans = ans / A[start] * A[x] + ans % A[start];
    start = x + 1;
  }

  ce(max(ans, ans / A[start] * A[N - 1] + ans % A[start]));
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int N;
vecl(A);
ll ans = 1000;

void search2(int day, ll money, ll stock) {
  if (day == N) {
    ans = max(ans, money);
    return;
  }

  search2(day + 1, money, stock);
  if (money >= A[day]) {
    search2(day + 1, money % A[day], stock + money / A[day]);
  }
  if (stock > 0) {
    search2(day + 1, money + stock * A[day], 0LL);
  }
}

int main() {
  cin >> N;
  fx(N) { vecp(A); }

  search2(0, 1000, 0);
  ce(ans);
  return 0;
}