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

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
const llint INF = 100000000;

int main() {
  int n;
  cin >> n;
  hei(llint) a(n);
  rep(i, 0, n) cin >> a[i];
  llint now = 1000;
  llint kabu = 0;
  rep(i, 0, n - 1) {
    now += kabu * a[i];
    kabu = 0;
    if (a[i] < a[i + 1]) {
      kabu = now / a[i];
      now %= a[i];
    }
  }
  llint ans = a[n - 1] * kabu + now;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
const llint INF = 100000000;

int n;
int ans = 0;
void dfs(int v, int k, int c, hei(int) & a) {
  ans = max(ans, c + (k * a[v]));
  if (v == n - 1)
    return;
  rep(i, 1, k + 1) { dfs(v + 1, k - i, c + a[v] * i, a); }
  rep(i, 1, 1 + (c / a[v])) { dfs(v + 1, k + i, c - a[v] * i, a); }
}

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