alexjercan/bugnet huggingface dataset:
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bit>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>

#include <algorithm>
#include <fstream>
#include <vector>

#define ll long long int
#define MOD 1000000007

using namespace std;

bool isPrime(ll n) {
  if (n == 1) {
    return false;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }

  return true;
}

int main(void) {

  ll n, res, t, k;
  vector<ll> a;

  res = 1000;
  k = 0;

  cin >> n;

  for (ll i = 0; i < n; i++) {
    cin >> t;
    a.push_back(t);
  }

  for (ll i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1]) {
      k += res / a[i];
      ll tmp = res / a[i];
      res -= a[i] * tmp;
    } else if (a[i] > a[i + 1]) {
      res += a[i] * k;
      k = 0;
    }
  }

  for (; k > 0;) {
    k--;
    res += a[n - 1];
  }

  cout << res;

  return 0;
}

Error: TLE

Fixed submission:

#include <bit>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>

#include <algorithm>
#include <fstream>
#include <vector>

#define ll long long int
#define MOD 1000000007

using namespace std;

bool isPrime(ll n) {
  if (n == 1) {
    return false;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }

  return true;
}

int main(void) {

  ll n, res, t, k;
  vector<ll> a;

  res = 1000;
  k = 0;

  cin >> n;

  for (ll i = 0; i < n; i++) {
    cin >> t;
    a.push_back(t);
  }

  for (ll i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1]) {
      for (; res >= a[i];) {
        k++;
        res -= a[i];
      }
    } else if (a[i] > a[i + 1]) {
      for (; k > 0;) {
        k--;
        res += a[i];
      }
    }
  }

  for (; k > 0;) {
    k--;
    res += a[n - 1];
  }

  cout << res;

  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[110];
#define int long long int
main() {
  int i, n, k, ans = 1000;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (i = 1; i <= n; i++) {
    if (a[i + 1] > a[i]) {
      k = (ans) / a[i];
      ans = ans % a[i] + (k * a[i + 1]);
    }
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
const long long MAXN = 105;
long long p[MAXN];
int main() {
  long long T, M = 1000;
  scanf("%d", &T);
  for (long long i = 1; i <= T; ++i) {
    scanf("%d", p + i);
  }
  for (long long i = 1; i <= T; ++i) {
    if (p[i] < p[i + 1]) {
      long long tot = M / p[i];
      if (tot == 0) {
        continue;
      }
      M %= p[i];
      if (i < T && p[i] < p[i + 1]) {
        ++i;
      }
      M += tot * p[i];
    }
  }
  printf("%d\\n", M);
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793
#define MOD 1000000007
typedef long long ll;
typedef long double ld;

int main() {
  ll N, K;
  ll ans = 1000;
  ll kabu = 0;
  cin >> N;
  vector<ll> x(N, 0);
  for (int i = 0; i < N; i++)
    cin >> x[i];

  for (int i = 0; i < N - 1; i++) {

    if (x[i] < x[i + 1]) {

      if (ans - x[i] >= 0) {
        kabu += ans / x[i];
        ans -= kabu * x[i];
      }
    }

    else {
      if (kabu > 0) {
        ans += kabu * x[i];
        kabu = 0;
      }
    }
  }
  if (kabu > 0) {
    ans += x[N - 1] * kabu;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793
#define MOD 1000000007
typedef long long ll;
typedef long double ld;

int main() {
  ll N, K;
  ll ans = 1000;
  ll kabu = 0;
  cin >> N;
  vector<ll> x(N, 0);
  for (int i = 0; i < N; i++)
    cin >> x[i];

  for (int i = 0; i < N - 1; i++) {

    if (x[i] < x[i + 1]) {

      if (ans - x[i] >= 0) {
        while (ans - x[i] >= 0) {
          ans -= x[i];
          kabu++;
        }
      }
    }

    else {
      if (kabu > 0) {
        while (kabu > 0) {
          ans += x[i];
          kabu--;
        }
      }
    }
  }
  if (kabu > 0) {
    ans += x[N - 1] * kabu;
  }
  cout << ans << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long n, m = 1000;
  cin >> n;
  vector<long long> a(n);
  for (long long &c : a) {
    cin >> c;
  }
  for (int i = 0; i < n - 1; ++i) {
    if (a.at(i) < a.at(i + 1)) {
      long long c = m / a.at(i);
      m += c * (a.at(i + 1) - a.at(i));
    }
  }
  cout << m << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, shojikin = 1000, shojikabu = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  vector<int> joge(n);
  int age = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a.at(i) < a.at(i + 1)) {
      joge.at(i) = 0;
      age++;
    }
    if (a.at(i) == a.at(i + 1)) {
      joge.at(i) = 1;
    }
    if (a.at(i) > a.at(i + 1)) {
      joge.at(i) = 2;
    }
  }

  if (age == 0) {
    cout << "1000" << endl;

  } else {
    int ji = 0, start = 0;
    while (ji < n - 1) {
      bool agefind = false;
      while (agefind == false) {

        if (joge.at(ji) == 0) {
          agefind = true;
        }
        ji++;
      }

      int saiyasune = a.at(ji);
      for (int i = start; i < ji; i++) {
        if (a.at(i) < saiyasune) {
          saiyasune = a.at(i);
        }
      }

      shojikabu = shojikin / saiyasune;
      shojikin -= shojikin / saiyasune * saiyasune;

      shojikin += shojikabu * a.at(ji);
      shojikabu = 0;
      start = ji + 2;
      ji++;
    }
    cout << shojikin << endl;
  }
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define endl '\\n'
using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  ll ans = 1000;
  ll i = 0;
  for (i = 0; i + 1 < n; ++i) {
    if (v[i] < v[i + 1]) {
      ll m = ans / v[i];
      ans -= m * v[i];
      ans += m * v[i + 1];
    }
  }
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define endl '\\n'
using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<ll> v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
  }
  ll ans = 1000;
  ll amt = 0;
  ll i = 0;
  while (i < n + 1) {
    if (v[i] < v[i + 1] && ans >= v[i]) {
      for (int j = i; j < n - 1; j++) {
        if (v[j] > v[j + 1]) {
          i = j;
          break;
        }
        amt += ans / v[i];
        ans %= v[i];
      }
    } else if (v[i] > v[i + 1] && amt > 0) {
      for (ll j = i; j < n - 1; j++) {
        if (v[j] < v[j + 1]) {
          i = j;
          break;
        }
        ans += amt * v[i];
        amt = 0;
      }
    }
  }
  if (amt > 0) {
    ans += v[n - 1] * amt;
  }

  cout << ans;

  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  long long n, m = 1000;
  cin >> n;
  long long arr[n];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long s;
  for (ll i = 0; i < n - 1; i++) {
    if (arr[i] >= arr[i + 1])
      continue;
    else {
      s = m / arr[i];
      m = m % arr[i];
      m = m + s * arr[i + 1];
    }
  }
  cout << m << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  long long n, a[n], b = 1000;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    long long c = 0;
    if (a[i] < a[i + 1])
      c = b / a[i];
    b += (a[i + 1] - a[i]) * c;
  }
  cout << b;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
#define op1(x) cout << "Scenario #" << x << ": "
#define endl "\\n"
using namespace std;
const ll mod = 1000000007;
const ll INF = 1e18;
const int N = 20;

ll n;
ll a[81];

void solve(int t) {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll b[n + 1] = {0};
  b[0] = 0;

  for (int i = 1; i < n; i++) {
    if (a[i] >= a[i - 1]) {
      b[i] = b[i - 1] + 1;
    }
  }
  /*
  for (int i = 0; i < n; i++)
  {
          cout << b[i] << " ";
  }*/

  ll rem = 1000;
  ll d = 0;
  for (int i = 0; i < n; i++) {
    if (b[i] == 0 and b[i + 1] == 1) {
      d = rem / a[i];
      rem -= d * a[i];
    } else if (b[i] >= 1 and b[i + 1] == 0) {
      rem += d * a[i];
      d = 0;
    }
  }

  cout << rem << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
#define op1(x) cout << "Scenario #" << x << ": "
#define endl "\\n"
using namespace std;
const ll mod = 1000000007;
const ll INF = 1e18;
const int N = 20;

ll n;
ll a[81];

ll dp[81][2001][201][2];

ll foo(ll idx, ll rem, ll num, ll cn) {

  if (cn == 2) {
    return -INF;
  }

  if (idx == n) {
    return rem;
  }

  if (dp[idx][rem][num][cn] != -1)
    return dp[idx][rem][num][cn];

  ll res = 0;

  ll p = 0;
  ll q = 0;
  for (int i = 0; i * a[idx] <= rem; i++) {
    res = max(foo(idx + 1, rem - i * a[idx], num + i, 0), res);
    res = max(foo(idx, rem - i * a[idx], num + i, cn + 1), res);
  }

  for (int i = 0; i <= num; i++) {
    res = max(foo(idx + 1, rem + i * a[idx], num - i, 0), res);
    res = max(foo(idx, rem + i * a[idx], num - i, cn + 1), res);
  }

  return dp[idx][rem][num][cn] = res;
}

void solve(int t) {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));

  cout << foo(0, 1000, 0, 0) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000009;
const long long INF = 1LL << 60;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  ll dp[90]{};
  dp[0] = 1000;
  for (int i = 1; i < N; ++i) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < i; ++j) {
      ll v = dp[j] / A[j];
      ll w = dp[j] + (A[i] - A[j]) * v;
      dp[i] = max(dp[i], w);
    }
  }
  cout << dp[N - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000009;
const long long INF = 1LL << 60;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  ll dp[90]{};
  dp[0] = 1000;
  for (int i = 1; i < N; ++i) {
    dp[i] = dp[i - 1];
    for (int j = 2; j << i - 1; ++j) {
      ll v = dp[j] / A[j];
      ll w = dp[j] + (A[i] - A[j]) * v;
      dp[i] = max(dp[i], w);
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  ll money = 1000;
  ll stock = 0;
  for (ll i = 0; i < n; i++) {
    money += a[i] * stock;
    stock = 0;
    if (i != n - 1) {
      if (a[i] < a[i + 1]) {
        stock = money / a[i];
        money = money % a[i];
      }
    }
  }
  cout << money << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  ll money = 1000;
  ll stock = 0;
  for (ll i = 0; i < n; i++) {
    money += a[i] * stock;
    stock = 0;
    if (i != n - 1) {
      if (a[i] < a[i + 1]) {
        while (0 <= money - a[i]) {
          money -= a[i];
          stock++;
        }
      }
    }
  }
  cout << money << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

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

typedef long long ll;

ll a[82];

int main() {
  int n;
  ll s = 0, x = 1000;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  n = unique(a, a + n) - a;
  for (int i = 0; i < n; i++) {
    if (i < n - 1 && a[i + 1] > a[i] && (i == 0 || a[i - 1] > a[i])) {
      ll y = x / a[i];
      x -= a[i] * y, s += y;
    }
    if (i && a[i - 1] < a[i] && (i == n - 1 || a[i + 1] < a[i])) {
      x += s * a[i];
      s = 0;
    }
  }
  printf("%lld\\n", x);
  return 0;
}

Error: -11

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

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

int n, a[82], dp[82][82][17000];

int f(int i, int s, int m) {
  if (i == n)
    return m;
  if (dp[i][s][m] == -1) {
    int x = 0, y = 0, z = f(i + 1, s, m);
    for (int j = 1; j * a[i] <= m; j++)
      x = max(x, f(i + 1, s + j, m - j * a[i]));
    for (int j = 1; j <= s; j++)
      y = max(y, f(i + 1, s - j, m + j * a[i]));
    dp[i][s][m] = max({x, y, z});
  }
  return dp[i][s][m];
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\\n", f(0, 0, 1000));
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const ll MOD = 1000000007;
const ll INF = 1LL << 60;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll money = 1000;
  ll kabu = 0;
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 0) {
      ll store = 0;
      if (a[i] > a[i - 1]) {
        store = money / a[i - 1];
      }
      money += (a[i] - a[i - 1]) * store;
    }
  }
  cout << money << endl;
}

Error: -8

Fixed submission:

#include <iostream>
#include <vector>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int money = 1000;
  int kabu = 0;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > a[i - 1]) {
      int store = money / a[i - 1];
      money += (a[i] - a[i - 1]) * store;
    }
  }
  cout << money << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  ll rem, tmp, cur = 1000;
  for (ll i = 1; i < n; i++) {
    if (arr[i] >= arr[i - 1]) {
      tmp = cur / arr[i - 1];
      rem = cur % arr[i - 1];
      cur = (tmp * arr[i]) + rem;
    }
  }
  cout << cur;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define assem99                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define ld long double
#define endll '\\n'
#define sz(a) (int)(a).size()
#define RWFile                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
int dx[] = {0, -1, 0, 1, -1, 1, -1, 1};
int dy[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int MOD = 1e9 + 7, N = 2e6 + 5, oo = 1e9;
const double pi = acos(-1);
using namespace std;

ll n, arr[81];
ll solve(int id, ll money, ll candies) {
  if (id == n)
    return 0;
  ll ans = 0;
  // if(~dp[id][money][candies]) return dp[id][money][candies];
  ans = max(ans, solve(id + 1, money, candies));
  if (money >= arr[id])
    ans = max(ans, solve(id + 1, money - ((money / arr[id]) * arr[id]),
                         candies + (money / arr[id])) -
                       ((money / arr[id]) * arr[id]));
  if (candies)
    ans = max(ans, solve(id + 1, money + (candies * arr[id]), 0) +
                       (candies * arr[id]));
  return ans;
}

int main() {
  assem99
          // memset(dp, -1, sizeof dp);
          cin >>
      n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << 1000 + solve(0, 1000, 0);

  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

/* 	* In the name of GOD
 * Thanks God */
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define int long long
const int N = 88;
int a[N];

int32_t main() {
  int n, ans = 1000;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    int j;
    for (j = i + 1; j < n; j++) {
      if (a[j] != a[i]) {
        break;
      }
    }
    if (j == n)
      break;
    if (a[j] > a[i]) {
      int x = ans / a[i];
      ans += (x * (a[j] - a[i]));
    }
    i = j - 1;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

/* 	* In the name of GOD
 * Thanks God */
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define int long long
const int N = 80;
int a[N], n;
map<int, int> ans[N];

inline int solve(int p, int l) {
  if (ans[l][p]) {
    return ans[l][p];
  }
  int mx = p;
  for (int i = l + 1; i < n; i++) {
    int ans = p;
    if (a[i] > a[l]) {
      int x = ans / a[l];
      ans -= (x * a[l]);
      ans += (x * a[i]);
    }
    mx = max(mx, solve(ans, i));
  }
  ans[l][p] = mx;
  return mx;
}

int32_t main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << solve(1000, 0);
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false), cin.tie(0);

  long long n;
  cin >> n;

  long long money = 1000;
  long long stocks = 0;
  int a[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a[n] = 0;
  long long high = 1000;
  int prec = 2000;
  for (int i = 0; i < n; i++) {
    if (a[i] < prec) {
      long long temp = money / a[i];
      money = money % a[i];
      stocks += temp;
    }
    if (a[i] > a[i + 1]) {
      money += a[i] * stocks;
      stocks = 0;
    }
    if (money > high)
      high = money;
    //		cout << money << endl;
  }
  cout << high << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false), cin.tie(0);

  long long n;
  cin >> n;

  long long money = 1000;
  long long stocks = 0;
  long long a[n + 1];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  a[n] = 0;
  long long high = 1000;
  long long prec = 2000;
  for (long long i = 0; i < n; i++) {
    if (a[i] < prec) {
      while (money >= a[i]) {
        money -= a[i];
        //				cout << "buy " << a[i] << endl;
        stocks += 1;
      }
    }
    if (a[i] > a[i + 1]) {
      while (stocks >= 1) {
        money += a[i];
        //				cout << "sell " << a[i] << endl;
        stocks -= 1;
      }
    }
    if (money > high)
      high = money;
    //		cout << money << endl;
  }
  cout << high << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
vector<int> A;
long func(int n, long y, long k, int a, int m) {
  if (n == m + 1) {
    // cout << '!' << endl;
    return y + A.at(n - 2) * k;
  } else {
    // cout << 'o' ;
    if (A.at(n - 1) > A.at(n)) {
      return func(n + 1, y + A.at(n - 1) * k, k - k, A.at(n), m);
    } else if (A.at(n - 1) < A.at(n)) {
      return func(n + 1, y - A.at(n - 1) * (long(y / a)), k + (long(y / a)),
                  A.at(n), m);
    } else {
      return func(n + 1, y, k, A.at(n), m);
    }
  }
}
int main() {
  int N, Y, K;
  Y = 1000;
  K = 0;
  cin >> N;
  int hold;
  for (int i = 0; i < N; i++) {
    cin >> hold;
    A.push_back(hold);
  }
  A.push_back(0);
  // cout << 'o' << endl;
  cout << func(1, Y, K, A.at(0), N) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int func(int n, int y, int k, int a, int m) {
  if (n == m + 1) {
    // cout << '!' << endl;
    return y;
  } else {
    int buy, sell;
    buy = 0;
    sell = 0;
    // cout << 'o' ;
    buy = max(y, func(n + 1, y - A.at(n - 1) * int(y / a), k + int(y / a),
                      A.at(n), m));
    sell = max(y, func(n + 1, y + A.at(n - 1) * k, k - k, A.at(n), m));
    return max(buy, sell);
  }
}
int main() {
  int N, Y, K;
  Y = 1000;
  K = 0;
  cin >> N;
  int hold;
  for (int i = 0; i < N; i++) {
    cin >> hold;
    A.push_back(hold);
  }
  A.push_back(0);
  // cout << 'o' << endl;
  cout << func(1, Y, K, A.at(0), N) << endl;
}