alexjercan/bugnet huggingface dataset:
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using vi = vector<int>;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

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

  int n;
  ll x = 1000, kabu = 0;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int cnt = 0;
  while (1) {
    if (cnt == n - 1) {
      x += kabu * a[n - 1];
      break;
    }
    if (a[cnt] < a[cnt + 1]) {
      int cnt1 = cnt + 1;
      kabu += x / a[cnt];
      x -= a[cnt] * (x / a[cnt]);
      while (1) {
        if (a[cnt1] > a[cnt1 + 1] || cnt1 == n - 1) {
          x += kabu * a[cnt1];
          kabu = 0;
          cnt = cnt1;
          break;
        } else {
          ++cnt1;
        }
      }
    } else {
      ++cnt;
    }
  }
  cout << x << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using vi = vector<int>;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

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

  int n, x = 1000, kabu = 0;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int cnt = 0;
  while (1) {
    if (cnt == n - 1) {
      x += kabu * a[n - 1];
      break;
    }
    if (a[cnt] < a[cnt + 1]) {
      int cnt1 = cnt + 1;
      kabu += x / a[cnt];
      x -= a[cnt] * (x / a[cnt]);
      while (1) {
        if (a[cnt1] > a[cnt1 + 1]) {
          x += kabu * a[cnt1];
          kabu = 0;
          cnt = cnt1;
          break;
        } else {
          ++cnt1;
        }
      }
    } else {
      ++cnt;
    }
  }
  cout << x << "\\n";
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
ll a[1005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  ll m = 1000;
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      m = m % a[i - 1] + (m / a[i - 1]) * a[i];
    // cout<<m<<endl;
  }
  printf("%lld", m);
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t, n, m, a[1005], dp[1000005];
int main() {
  scanf("%d", &n);
  m = 1000;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i < n; i++) {
    memset(dp, 0, sizeof(dp));
    for (int j = a[i]; j <= m; j++) {
      dp[j] = max(dp[j], dp[j - a[i]] + a[i + 1] - a[i]);
    }
    m += dp[m];
  }
  printf("%d", m);
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) { cin >> A[i]; }
  vector<long long> dp(N, 0);
  dp[0] = 1000;
  rep(ni, N) rep(i, N) {
    if (ni <= i)
      continue;

    long long buy_count = dp[i] / A[i];
    long long next_money = dp[i];
    next_money -= buy_count * A[i];
    next_money += buy_count * A[ni];
    dp[ni] = max({dp[ni], next_money, dp[i]});
  }

  long long ans = 0;
  rep(i, N) { ans = max(dp[i], ans); }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) { cin >> A[i]; }
  vector<long long> dp(N + 1, 0);
  dp[0] = 1000;
  rep(ni, N + 1) rep(i, N + 1) {
    if (ni < i)
      continue;

    dp[ni] = max(dp[ni], dp[i]);
    long long buy_count = dp[i] / A[i];
    long long next_money = dp[i] + buy_count * (A[ni] - A[i]);
    dp[ni] = max(dp[ni], next_money);
  }

  long long ans = 0;
  rep(i, N + 1) { ans = max(dp[i], ans); }

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

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> ii;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  ll m = 1000;
  int mn = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] <= mn)
      mn = arr[i];
    else {
      ll candy = m / mn;
      m %= mn;
      m += candy * arr[i];
      mn = arr[i];
    }
  }
  cout << m;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*
 *  JUST KEEP GOING
 */
#define endl '\\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define For(i, s) for (auto i : s)
#define Forr(i, s) for (auto &i : s)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define vii vector<ii>
#define vll vector<pair<ll, ll>>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
void run() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 1e5 + 5, mod = 1e9 + 7, mod2 = 998244353;
int n, arr[88];
bool vis[80][80 * 200 + 1000][1001];
int ans = 1000;
void solve(int i, int money, int have) {

  if (i == n) {
    ans = max(ans, money);
    return;
  }

  if (vis[i][money][have])
    return;
  vis[i][money][have] = 1;
  solve(i + 1, money, have);
  solve(i + 1, money + arr[i] * have, 0);
  solve(i + 1, money - money / arr[i] * arr[i], have + money / arr[i]);
}
int main() {
  run();
  cin >> n;
  lp(i, n) cin >> arr[i];
  solve(0, 1000, 0);
  cout << ans;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, money = 1000;
  cin >> n;
  vector<long long> A(n);
  for (int i = 0; i < n; i++)
    cin >> A.at(i);
  for (int i = 0; i < n - 1; i++) {
    long long stock = 0;
    if (A.at(i) < A.at(i + 1))
      stock = money / A.at(i);
    money += (A.at(i + 1) - A.at(i)) * stock;
  }
  cout << money << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, stock = 0, money = 1000, a = 0, b = 0;
  cin >> n;
  vector<int> A(0);
  cin >> b;
  for (int i = 0; i < n - 1; ++i) {
    cin >> a;
    if (A.size() % 2 == 0 && a > b)
      A.push_back(b);
    else if (A.size() % 2 == 1 && a < b)
      A.push_back(b);
    if (i == n - 2) {
      if (A.size() % 2 == 0 && a < b)
        A.push_back(a);
      else if (A.size() % 2 == 1 && a > b)
        A.push_back(a);
    }
    b = a;
  }
  for (int i = 0; i < A.size() - 1; ++i) {
    if (A.at(i) < A.at(i + 1)) {
      stock = money / A.at(i);
      money %= A.at(i);
    } else if (A.at(i) > A.at(i + 1)) {
      money += stock * A.at(i);
      stock = 0;
    }
    if (i == A.size() - 2)
      money += stock * max(A.at(i), A.at(i + 1));
  }
  cout << money << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  ll mon = 1000, K = 0;
  vector<ll> A(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < N; i++) {
    K += mon / A[i];
    mon %= A[i];
    if (A[i] >= A[i + 1]) {
      mon += K * A[i];
      K = 0;
    }
  }
  cout << mon << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1000000007;

int dp[5100][84];

void chmax(int &x, int y) { x = max(x, y); }

int main() {
  for (int i = 0; i < 5100; i++) {
    for (int j = 0; j < 84; j++) {
      dp[i][j] = -1;
    }
  }
  dp[1000][0] = 0;
  int N, A;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    for (int j = 5099; j >= 0; j--) {
      if (dp[j][i] < 0)
        continue;
      for (int k = 1; k * A <= j; k++) {
        // 株買う
        chmax(dp[j - k * A][i + 1], dp[j][i] + k);
      }
      for (int k = dp[j][i]; k >= 0; k--) {
        // 株売る
        chmax(dp[j + k * A][i + 1], dp[j][i] - k);
      }
      chmax(dp[j][i + 1], dp[j][i]);
    }
  }
  int ans = 0;
  for (int i = 5099; i >= 0; i--) {
    if (dp[i][N] >= 0) {
      cout << i << endl;
      return 0;
    }
  }
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll, ll>

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

  int n;
  cin >> n;
  // cout<<"hi\\n";
  ll a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  // cout<<"hi\\n";
  ll dp[n + 1];
  for (int i = 1; i <= n; i++)
    dp[i] = 0;
  dp[1] = 1000;
  for (int i = 1; i <= n; i++) {
    ll num = dp[i] / a[i];
    ll div = dp[i] % a[i];
    for (int j = i + 1; j <= n; j++) {
      dp[j] = max(dp[j], dp[i]);
      dp[j] = max(dp[j], num * a[j] + div);
    }
  }
  cout << dp[n];
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll, ll>

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

  int n;
  cin >> n;
  // cout<<"hi\\n";
  ll a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  // cout<<"hi\\n";
  ll dp[n + 1][8001];
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < 8001; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 1000;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 8001; j++) {
      if (dp[i - 1][j] == -1)
        break;
      // cout<<i-1<<" "<<j<<" "<<dp[i-1][j]<<"\\n";
      ll cost = dp[i - 1][j];
      dp[i][j] = max(dp[i][j], cost);
      int num = j + 1;
      while (cost - a[i] >= 0) {
        cost = cost - a[i];
        dp[i][num] = max(dp[i][num], cost);
        num++;
      }
      cost = dp[i - 1][j];
      num = j - 1;
      while (num >= 0) {
        cost += a[i];
        dp[i][num] = max(dp[i][num], cost);
        num--;
      }
    }
  }

  cout << dp[n][0];

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


// TO KAISE HAI AAP LOG!! //

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

#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define F first
#define S second
#define all(v) v.begin(), v.end()
ll mod = 1e9 + 7;

/*/----------------------------- Code begins -----------------------------/*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1, tc;
  // cin>>t;
  for (tc = 1; tc <= t; tc++) {
    ll n, k, i, j, s = 1000, f, m, c, pos;
    cin >> n;
    ll a[n + 5];
    for (i = 0; i < n; i++)
      cin >> a[i];
    for (i = 0; i < n - 1;) {
      f = 0;
      m = a[i];
      j = i + 1;
      while (j < n) {
        if (a[j] > m) {
          f = 1;
          pos = j;
          break;
        }
        m = min(a[j], m);
        j++;
      }
      if (f == 0)
        break;
      c = 0;
      ll x = (s / m);
      s = s - (x * m);
      s = s + (x * a[pos]);
      i = j;
    }
    cout << s;
  }
  return 0;
}

Error: TLE

Fixed submission:


// TO KAISE HAI AAP LOG!! //

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

#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define F first
#define S second
#define all(v) v.begin(), v.end()
ll mod = 1e9 + 7;

/*/----------------------------- Code begins -----------------------------/*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1, tc;
  // cin>>t;
  for (tc = 1; tc <= t; tc++) {
    ll n, k, i, j, s = 1000, f, m, c, pos;
    cin >> n;
    ll a[n + 5];
    for (i = 0; i < n; i++)
      cin >> a[i];
    for (i = 0; i < n - 1;) {
      f = 0;
      m = a[i];
      j = i + 1;
      while (j < n) {
        if (a[j] > m) {
          f = 1;
          pos = j;
          break;
        }
        m = min(a[j], m);
        j++;
      }
      if (f == 0)
        break;
      c = 0;
      while (s - m >= 0) {
        s -= m;
        c++;
      }
      while (c--) {
        s += a[pos];
      }
      i = j;
    }
    cout << s;
  }
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define LL long long

using namespace std;

const int maxn = 85;
int n, a[maxn];
LL m;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  m = 1000;
  for (int i = 1; i < n; ++i) {
    if (a[i] < a[i + 1])
      m += (LL)(a[i + 1] - a[i]) * (m / a[i]);
  }
  printf("%lld", m);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 85;
int n, m, a[maxn];
int d[180000];

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

#include <bits/stdc++.h>
using namespace std;
const int N = 82;
int A[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  long c = 1e3;
  for (int i = 1; i + 1 <= n; i++) {
    if (A[i + 1] <= A[i])
      continue;
    long s = c / A[i];
    c -= s * A[i];
    c += s * A[i + 1];
  }
  cout << c;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 81;
const int M = 16e3 + 10;
int memo[N][M][N];
int A[N];
int n;
int dp(int idx, int c, int s) {
  if (idx > n)
    return c;
  int &ret = memo[idx][c][s];
  if (ret != -1)
    return ret;
  ret = 0;
  if (A[idx] <= c) {
    ret = max(ret, dp(idx, c - A[idx], s + 1));
    ret = max(ret, dp(idx + 1, c - A[idx], s + 1));
  }
  if (s) {
    ret = max(ret, dp(idx, c + A[idx], s - 1));
    ret = max(ret, dp(idx + 1, c + A[idx], s - 1));
  }
  ret = max(ret, dp(idx + 1, c, s));
  return ret;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  memset(memo, -1, sizeof(memo));
  cout << dp(1, 1000, 0);
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using gt = greater<int>;
using minq = priority_queue<int, vector<int>, gt>;
using P = pair<ll, ll>;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  set<int> tani, yama;
  if (a[0] < a[1])
    tani.insert(0);
  yama.insert(n - 1);
  for (int i = 1; i < n - 1; i++) {
    if (a[i] <= a[1 + i] && a[i] <= a[i - 1] &&
        (a[i + 1] != a[i] || a[i] != a[i - 1]))
      tani.insert(i);
    if (a[i] >= a[i + 1] && a[i] >= a[i - 1] &&
        (a[i + 1] != a[i] || a[i] != a[i - 1]))
      yama.insert(i);
  }
  ll ans = 1000;
  ll rest = 1000;
  ll stock = 0;
  rep(i, n) {
    if (tani.find(i) != tani.end()) {
      ll maxBuy = rest / a[i];
      rest -= maxBuy * a[i];
      stock += maxBuy;
    } else if (yama.find(i) != yama.end()) {
      rest += stock * a[i];
      stock = 0;
      chmax(ans, rest);
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using gt = greater<int>;
using minq = priority_queue<int, vector<int>, gt>;
using P = pair<ll, ll>;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

ll dp[10000][85];

int main() {
  int n;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  vi a(n);
  rep(i, n) cin >> a[i];
  // ll rest = 1000;
  dp[0][0] = 1000;
  ll maxRest = 1000;
  ll maxStock = 0;
  rep(i, n) {
    int maxBuy = maxRest / a[i];
    for (int j = 0; j <= maxBuy; j++) {
      rep(s, maxStock + 1) {
        if (dp[s][i] == -1)
          break;
        ll ns = s + j;
        chmax(dp[ns][i + 1], dp[s][i] - j * a[i]);
        chmax(maxStock, ns);
      }
    }
    for (int j = 0; j <= maxStock; j++) {
      if (dp[j][i] == -1)
        break; // TODO: iffy
      rep(sell, j + 1) {
        ll nm = dp[j][i] + sell * a[i];
        int nj = j - sell;
        assert(nj >= 0);
        assert(nj < 10000);
        chmax(dp[nj][i + 1], nm);
        chmax(maxRest, nm);
      }
    }
  }
  printf("%lld\\n", maxRest);
  // rep(i, a) {
  //   rep()
  // }
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  int n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll mon = 1000, ans = 1000;
  int i = 0;
  while (i < n - 1) {
    while ((i < n - 1) && (a[i + 1] <= a[i]))
      i++;
    if (i == n - 1)
      break;
    ll stocks = mon / a[i] * 1LL;
    int buy = i++;
    while ((i < n) && (stocks * a[i] >= stocks * a[i - 1]))
      i++;
    int sell = i - 1;
    mon += stocks * (a[sell] - a[buy]);
    ans = max(ans, mon);
  }
  cout << mon;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int a[100];
int solve(int st, int en, int mon) {
  if (st >= en)
    return 0;
  int p = 0;
  for (int i = st; i <= en; i++) {
    if (a[i] > mon)
      continue;
    int stocks = mon / a[i];
    for (int j = i + 1; j <= en; j++) {
      if (a[j] > a[i]) {
        int cur = (a[j] - a[i]) * stocks +
                  solve(st, i - 1, mon + (a[j] - a[i]) * stocks) +
                  solve(j + 1, en, mon + (a[j] - a[i]) * stocks);
        p = max(p, cur);
      }
    }
  }
  return p;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << solve(0, n - 1, 1000) + 1000;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

int main() {

  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  vector<ll> b(n + 1);
  rep(i, n) {
    cin >> a[i];
    if (i > 0) {
      if (a[i] > a[i - 1]) {
        b[i] = 1;
      } else {
        b[i] = 0;
      }
    }
  }
  b[n] = 0;
  b[0] = 0;

  ll ans = 1000;
  ll kabu = 0;
  rep(i, n) {
    if (b[i] == 0 && b[i + 1] == 1) {
      kabu = (ans / a[i]);
      ans -= (kabu * a[i]);
    } else if (b[i] == 1 && b[i + 1] == 0) {
      ans += (kabu * a[i]);
      kabu = 0;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

int main() {

  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) {
    cin >> a[i];
    if (i > 0) {
      if (a[i] > a[i - 1]) {
        b[i] = true;
      } else {
        b[i] = false;
      }
    }
  }
  b[n] = false;
  b[0] = false;

  ll ans = 1000;
  ll kabu = 0;
  rep(i, n) {
    if (b[i] == false && b[i + 1] == true) {
      kabu = (ans / a[i]);
      ans -= (kabu * a[i]);
    } else if (b[i] == true && b[i + 1] == false) {
      ans += (kabu * a[i]);
      kabu = 0;
    }
  }

  cout << ans << endl;

  return 0;
}