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

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
#define all(x) x.begin(), x.end()
#define trav(i, a) for (auto &i : a)
inline int in() {
  int x;
  scanf("%lld", &x);
  return x;
}
int32_t main() {
  int n = in();
  vector<int> a;
  int x = in();
  a.push_back(x);
  for (int i = 1; i < n; i++) {
    x = in();
    if (x == a.back())
      continue;
    a.push_back(x);
  }

  // 1000 yen
  vector<int> df;
  vector<pair<int, int>> costs;
  n = a.size();
  for (int i = 0; i < n - 1; i++) {
    x = a[i + 1] - a[i];
    if (x < 0)
      continue;
    if (x > 0 and df.empty()) {
      df.push_back(x);
      costs.push_back({a[i], a[i + 1]});
    } else if (x >= 0) {
      df.push_back(x);
      costs.push_back({a[i], a[i + 1]});
    }
  }
  int cur = 1000;

  auto buysell = [&](int buy, int sell) {
    int stocks = cur / buy;
    int cost = stocks * buy;
    cur -= cost;
    int profit = stocks * sell;
    cur += profit;
  };

  if (costs.size() == 0) {
    cout << "1000";
    return 0;
  }
  for (auto i : costs) {
    buysell(i.first, i.second);
    // cout<<cur<<" "<<i.first<<" "<<i.second<<"\\n";
  }
  cout << cur;
}

Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
#define all(x) x.begin(), x.end()
#define trav(i, a) for (auto &i : a)
inline int in() {
  int x;
  scanf("%lld", &x);
  return x;
}
int32_t main() {
  int n = in();
  vector<int> a;
  int x = in();
  a.push_back(x);
  for (int i = 1; i < n; i++) {
    x = in();
    if (x == a.back())
      continue;
    a.push_back(x);
  }

  // 1000 yen
  vector<int> df;
  vector<pair<int, int>> costs;
  for (int i = 0; i < n - 1; i++) {
    x = a[i + 1] - a[i];
    if (x < 0)
      continue;
    if (x > 0 and df.empty()) {
      df.push_back(x);
      costs.push_back({a[i], a[i + 1]});
    } else if (x >= df.back()) {
      df.push_back(x);
      costs.push_back({a[i], a[i + 1]});
    }
  }
  int cur = 1000;

  auto buysell = [&](int buy, int sell) {
    int stocks = cur / buy;
    int cost = stocks * buy;
    cur -= cost;
    int profit = stocks * sell;
    cur += profit;
  };

  if (costs.size() == 0) {
    cout << "1000";
    return 0;
  }
  for (auto i : costs) {
    buysell(i.first, i.second);
  }
  cout << cur;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  ll dp[100], n, a[100], i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = 0;
  }
  dp[0] = 1000;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      dp[j] = max(dp[j], dp[i] / a[i] * a[j] + dp[i] % a[i]);
      dp[j] = max(dp[j], dp[j - 1]);
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  ll n, dp[100][1010], i, j, k, l, a[100];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < 1000; j++) {
      dp[i][j] = -1;
    }
  }
  for (j = 0; j <= 10; j++) {
    if (1000 - a[0] * j >= 0) {
      dp[0][j] = 1000 - a[0] * j;
    }
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < 1000; j++) {
      if (dp[i][j] == -1) {
        break;
      }
      for (k = 0; k <= dp[i][j] / a[i + 1]; k++) {
        dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] - a[i + 1] * k);
        for (l = 0; l <= j; l++) {
          if (dp[i + 1][j + k] == -1) {
            break;
          }
          dp[i + 1][j + k - l] =
              max(dp[i + 1][j + k - l], dp[i + 1][j + k] + a[i + 1] * l);
        }
      }
    }
  }
  cout << dp[n - 1][0] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;

int main() {
  ll n;
  cin >> n;

  vl a(n);
  rep(i, n) { cin >> a[i]; }

  vl dp(n + 1); // dp[i+1] : i日目に株の売却が終了した時点での所持金の最大値
  dp[0] = 1000;
  rep(i, n) {
    chmax(dp[i + 1], dp[i]);
    rep(j, i) { chmax(dp[i + 1], (dp[j] / a[j]) * a[i] + dp[j] % a[j]); }
  }

  out(dp[n]);
  re0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;

ll dp[200010];

int main() {
  ll n;
  cin >> n;

  vl a(n + 1);
  rep(i, n) { cin >> a[i]; }

  ll now = 1000;
  rep(i, n) {
    rep(j, 200010) { dp[j] = -INF; }
    dp[0] = 0;
    ll to = now;
    exrep(j, 0, now) { chmax(dp[j + a[i]], dp[j] + a[i + 1]); }
    exrep(j, 0, now) { chmax(to, dp[j] + now - j); }
    now = to;
  }

  out(now);
  re0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int64_t n, c, d, e;
  cin >> n;
  c = 0; // 0のかず
  d = 1000;
  e = 0;
  vector<int> a(n);
  vector<int> b(n - 1);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 1; i < n; i++) {
    if (a.at(i) < a.at(i - 1)) {
      b.at(i - 1) = -1;
    } else if (a.at(i) > a.at(i - 1)) {
      b.at(i - 1) = 1;
    } else {
      b.at(i - 1) = 0;
      c++;
    }
  }
  vector<int> x(n - c);
  x.at(0) = a.at(0);
  int j = 1;
  for (int i = 0; i < n - 1; i++) {
    if (b.at(i) == 0) {
      continue;
    } else {
      x.at(j) = a.at(i + 1);
      j++;
    }
  }
  if (n - c != 1) {
    if (x.at(1) > x.at(0)) {
      e = d / x.at(0);
      d = d % x.at(0);
    }
  }
  for (int i = 1; i < n - c - 1; i++) {
    if (x.at(i) < x.at(i - 1) && x.at(i) < x.at(i + 1)) {
      e = d / x.at(i);
      d = d % x.at(i);
    } else if (x.at(i) > x.at(i - 1) && x.at(i) > x.at(i + 1)) {
      d = d + x.at(i) * e;
      e = 0;
    }
  }
  cout << d + e * x.at(n - c - 1) << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int n, c, d, e;
  cin >> n;
  c = 0; // 0のかず
  d = 1000;
  e = 0;
  vector<int> a(n);
  vector<int> b(n - 1);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (int i = 1; i < n; i++) {
    if (a.at(i) < a.at(i - 1)) {
      b.at(i - 1) = -1;
    } else if (a.at(i) > a.at(i - 1)) {
      b.at(i - 1) = 1;
    } else {
      b.at(i - 1) = 0;
      c++;
    }
  }
  vector<int> x(n - c);
  x.at(0) = a.at(0);
  int j = 1;
  for (int i = 0; i < n - 1; i++) {
    if (b.at(i) == 0) {
      continue;
    } else {
      x.at(j) = a.at(i + 1);
      j++;
    }
  }
  if (x.at(1) > x.at(0)) {
    e = d / x.at(0);
    d = d % x.at(0);
  }
  for (int i = 1; i < n - c - 1; i++) {
    if (x.at(i) < x.at(i - 1) && x.at(i) < x.at(i + 1)) {
      e = d / x.at(i);
      d = d % x.at(i);
    } else if (x.at(i) > x.at(i - 1) && x.at(i) > x.at(i + 1)) {
      d = d + x.at(i) * e;
      e = 0;
    }
  }
  cout << d + e * x.at(n - c - 1) << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define Matrix vector<vector<int>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\\n'

constexpr int INF = 2e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

// int dp[100][10000];
vector<int> arr;

// int f(int idx, int s /*, int d*/) {

//   if (idx == arr.size())
//     return 0;

//   if (dp.count(ii(idx, s /*, d*/)))
//     return dp[ii(idx, s /*, d*/)];

//   int &ret = dp[idx][s];
//   if(~ret)
//     return ret;

//   // last
//   ret = s * arr[idx];

//   // buy here
//   ret = arr[idx] / s;
//   for(int i = idx + 1; i < n; ++i) {

//   }

//   // buy all
//   ret = f(idx + 1, s + d / arr[idx] /*, d % arr[idx]*/);

//   // sell all
//   ret = max(ret, f(idx + 1, 0 /*, d + s * arr[idx]*/));

//   // do nothing
//   ret = max(ret, f(idx + 1, s /*, d*/));

//   return ret;
// }

// #define MULTIPLE_TEST_CASES
void solve() {
  int n;
  cin >> n;

  arr.resize(n);
  for (int &x : arr)
    cin >> x;

  arr.eb(-INF);
  int ans = 1000;
  for (int j = 0; j < n; ++j) {
    int cur = 1000;
    int s = 0;
    for (int i = j; i < n; ++i) {
      if (arr[i + 1] < arr[i]) {
        // sell
        cur += s * arr[i];
        s = 0;
      } else {
        // buy
        s += cur / arr[i];
        cur = cur % arr[i];
      }
      ans = max(ans, cur);
    }
  }

  cout << ans << endl;
}

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

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define Matrix vector<vector<int>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\\n'

constexpr int INF = 2e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

map<tt, int> dp;
vector<int> arr;

int f(int idx, int s, int d) {

  if (idx == arr.size())
    return d;

  if (dp.count(tt(idx, s, d)))
    return dp[tt(idx, s, d)];

  int &ret = dp[tt(idx, s, d)];

  // buy all
  ret = f(idx + 1, s + d / arr[idx], d % arr[idx]);

  // sell all
  ret = max(ret, f(idx + 1, 0, d + s * arr[idx]));

  // do nothing
  ret = max(ret, f(idx + 1, s, d));

  return ret;
}

// #define MULTIPLE_TEST_CASES
void solve() {
  int n;
  cin >> n;

  arr.resize(n);
  for (int &x : arr) {
    cin >> x;
  }

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

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

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<long long> A;
  int count = 0;
  for (int n = 0; n < N; ++n) {
    int num = 0;
    cin >> num;
    if (0 == n) {
      A.push_back(num);
      count++;
    } else {
      if (A[count - 1] != num) {
        A.push_back(num);
        count++;
      }
    }
  }
  long long now = 1000;
  long long num = 0;
  long long num2 = 0;
  int size = A.size();
  {
    if (A[0] < A[1]) {
      num = now / A[0];
      now -= A[0] * (num);
      num2 = A[0];
    }
  }
  for (int n = 1; n < size - 1; ++n) {
    if (A[n] > A[n - 1] && A[n] > A[n + 1]) {
      now += A[n] * num;
      num = 0;
      num2 = A[n];
    }
    if (A[n] < A[n - 1] && A[n] < A[n + 1]) {
      num = now / A[n];
      now -= A[n] * num;
      num2 = A[n];
    }
    // cout << now <<" " <<num<<" "<<num2<<endl;
  }
  { now += max(A[size - 1], num2) * num; }
  // 最終日は別処理
  cout << now << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }
  int now = 1000;
  vector<vector<int>> dp(N + 1, vector<int>(30000, -1));
  dp[0][0] = 1000;
  for (int n = 0; n < N; ++n) {
    for (int m = 0; m < 20000; ++m) {
      if (-1 != dp[n][m]) {
        // cout << n << "☆" << m << endl;
        // 買う
        for (int l = 0; l <= dp[n][m] / A[n]; ++l) {
          // cout << l << "☆"  << endl;
          dp[n + 1][m + l] = max(dp[n + 1][m + l], dp[n][m] - A[n] * l);
        }
        // 売る
        for (int l = 0; l <= m; ++l) {
          // cout << l << "☆" << endl;
          dp[n + 1][m - l] = max(dp[n + 1][m - l], dp[n][m] + A[n] * l);
        }
      }
    }
    /*
    for (int m = 0; m < 30; ++m) {
            cout << dp[n][m] <<"_";
    }
    cout << endl;*/
  }
  int ans = 0;
  for (int m = 0; m < 30; ++m) {
    ans = max(ans, dp[N][m]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  /*for(int i=0; i<a.size(); i++){
      cout << a[i] << endl;
      cout << a.size() << endl;
  }*/

  ll mymoney = 1000;
  for (int i = 0; i < n - 1; i++) {
    long long stocks = 0;
    if (a[i] < a[i + 1]) {
      stocks = mymoney / a[i];
      mymoney += (a[i + 1] - a[i]) * stocks;
    }
  }

  cout << mymoney << endl;
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

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

  /*for(int i=0; i<a.size(); i++){
      cout << a[i] << endl;
      cout << a.size() << endl;
  }*/

  int mymoney = 1000;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1]) {
      int tax = 0;
      while (mymoney - a[i] >= 0) {
        tax++;
        mymoney -= a[i];
      }
      mymoney += tax * a[i + 1];
    }
  }

  cout << mymoney << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define ill long long
#define fcout cout << fixed << setprecision(10)
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ill> a(n);
  rep(i, 0, n) cin >> a[i];
  ill sum = 1000, kabu = 0;
  rep(i, 0, n - 1) {
    if (a[i] < a[i + 1]) {
      kabu += sum / a[i];
      sum = sum % a[i];
      /*while(1) {
        if(sum >= a[i]) {
          kabu++;
          sum -= a[i];
        } else {
          break;
        }
      }*/
      // cout << i << " " << sum << " " << kabu << "\\n";
    } else if (a[i] > a[i + 1]) {
      sum += kabu * a[i];
      kabu = 0;
      /*while(1) {
        if(kabu > 0) {
          kabu--;
          sum += a[i];
        } else {
          break;
        }
      }*/
      // cout << i << " " << sum << " " << kabu << "\\n";
    }
  }
  /*while(1) {
    if(kabu > 0) {
      kabu--;
      sum += a[n-1];
    } else {
      break;
    }
  }*/
  sum += kabu * a[n - 1];
  kabu = 0;
  cout << sum << "\\n";
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define ill long long
#define fcout cout << fixed << setprecision(10)
using namespace std;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e9;

#define mod 1000000007
#define what_is(x) cerr << #x << " is " << x << "\\n";
#define read(x) freopen("in.txt", "r", stdin);
#define write(x) freopen("out.txt", "w", stdout);
#define Fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define sz 81

int n;
ll arr[sz];
ll solve(ll taka) {

  ll stock;

  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i + 1]) {
      stock = taka / arr[i];
      taka -= (arr[i] * stock);
      taka += (arr[i + 1] * stock);
    }
  }
  return taka;
}

int main() {
  Fast; /// using Fast I/O
  int a, b, c, i, j, k, t, m;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> arr[i];
  cout << solve(1000) << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e9;

#define mod 1000000007
#define what_is(x) cerr << #x << " is " << x << "\\n";
#define read(x) freopen("in.txt", "r", stdin);
#define write(x) freopen("out.txt", "w", stdout);
#define Fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define sz 85

int n;
ll arr[sz];
ll dp[sz][1000][1601];

ll solve(int pos, ll stock, ll taka) {
  if (pos > n)
    return taka;
  if (dp[pos][stock][taka] != -1)
    return dp[pos][stock][taka];

  ll a, b, c;
  ll sell = stock * arr[pos];
  ll buy = taka / arr[pos];
  ll rem = taka - (buy * arr[pos]);

  a = solve(pos + 1, stock, taka);
  b = solve(pos + 1, buy, rem);
  c = solve(pos + 1, 0, taka + sell);

  return dp[pos][stock][taka] = max(a, max(b, c));
}

int main() {
  Fast; /// using Fast I/O
  int a, b, c, i, j, k, t, m;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> arr[i];
  memset(dp, -1, sizeof(dp));
  cout << solve(1, 0, 1000) << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (ll &x : a)
    cin >> x;

  ll kane = 1000;
  ll kabu = 0;
  rep(i, 0, n - 1) {
    if (a[i] < a[i + 1]) {
      kabu += kane / a[i];
      kane = kane % a[i];
    }
    if (a[i] > a[i + 1]) {
      kane += kabu * a[i];
      kabu = 0;
    }
  }
  kane += kabu * a[n - 1];
  cout << kane << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

constexpr ll MOD = 0x3b9aca07;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

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

  map<tuple<ll, ll, ll>, bool> used;
  ll n, k;
  cin >> n;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];
  // 80000
  queue<tuple<ll, ll, ll>> q;
  ll ans = 0;
  q.emplace(0, 0, 1000);
  while (q.size()) {
    ll i, j, k;
    tie(i, j, k) = q.front();
    q.pop();
    if (i == n) {
      ans = max(ans, k);
      continue;
    }
    if (j > 0) {
      if (!used[{i + 1, 0, k + a[i] * j}]) {
        q.emplace(i + 1, 0, k + a[i] * j);
        used[{i + 1, 0, k + a[i] * j}] = true;
      }
    }
    if (!used[{i + 1, j, k}]) {
      q.emplace(i + 1, j, k);
      used[{i + 1, j, k}] = true;
    }

    ll num = k / a[i];
    if (!used[{i + 1, j + num, k - num * a[i]}]) {
      q.emplace(i + 1, j + num, k - num * a[i]);
      used[{i + 1, j + num, k - num * a[i]}] = true;
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 200 + 5;
ll a[maxn], n;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  ll now = 1000;
  a[0] = 1e9;
  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i - 1]) {
      ll g = now / a[i - 1];
      now -= g * a[i - 1];
      now += g * a[i];
    }
  }
  cout << now << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 200 + 5;
int a[maxn], n;
int dp[85][4005][55];
int dfs(int step, int m, int s) {
  // if(step < 0 || m < 0 || s < 0 || m > 2000 || s > 20) cout << "gg" << endl;
  int &x = dp[step][m][s];
  if (step == n + 1)
    return x = m;
  if (~x)
    return x;
  int ans = 0;
  // 什么都不做
  ans = dfs(step + 1, m, s);
  // 买
  int t = m / a[step];
  for (int i = 1; i <= t; i++) {
    ans = max(ans, dfs(step + 1, m - i * a[step], s + i));
  }
  // 卖
  for (int i = 1; i <= s; i++) {
    ans = max(ans, dfs(step + 1, m + i * a[step], s - i));
  }
  return x = ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << dfs(1, 1000, 0) << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (long long &e : a)
    cin >> e;
  long long ans = 1000;
  for (int i = 1; i < n; ++i)
    if (a[i - 1] < a[i])
      ans = ans - ans / a[i - 1] * a[i - 1] + ans / a[i - 1] * a[i];
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (long long &e : a)
    cin >> e;
  long long maxi = 1000;
  for (int i = 0; i < n - 1; ++i) {
    long long dp[500000] = {};
    for (int j = 1; j <= maxi; ++j)
      dp[j] = j;
    for (int j = a[i]; j <= maxi; ++j)
      dp[j] = max(dp[j], dp[j - a[i]] + a[i + 1]);
    maxi = *max_element(dp, dp + maxi + 1);
  }
  cout << maxi << endl;

  return 0;
}