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

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}

void solve() {
  ll N;
  cin >> N;
  ll A[85];
  rep(i, 0, N) cin >> A[i];
  ll dp[85] = {};
  dp[0] = 1000;
  rep(i, 0, N) {
    rep(j, i + 1, N) {
      dp[j] = max(dp[j], dp[i]);
      if (A[i] >= A[j])
        continue;
      ll n = dp[i] / A[i];
      ll money = dp[i] % A[i] + n * A[j];
      dp[j] = max(dp[j], money);
    }
  }
  print(dp[N - 1]);
}

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

Error: 0

Fixed submission:

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}

void solve() {
  ll N;
  cin >> N;
  ll A[85];
  rep(i, 0, N) cin >> A[i];
  ll dp[85][2010] = {};
  rep(i, 0, N + 1) {
    rep(j, 0, 2001) { dp[i][j] = -INF; }
  }
  dp[0][1000] = 0;

  rep(i, 0, N) {
    rep(j, 0, 2001) {
      if (dp[i][j] == -INF)
        continue;
      ll num = dp[i][j];
      rep(sell, 0, num + 1) {
        ll money = sell * A[i];
        dp[i + 1][j + money] = max(dp[i + 1][j + money], dp[i][j] - sell);
      }
      rep(buy, 0, 21) {
        ll money = buy * A[i];
        if (j < money)
          break;
        dp[i + 1][j - money] = max(dp[i + 1][j - money], dp[i][j] + buy);
      }
    }
  }
  rrep(i, 2000, 0) {
    if (dp[N][i] >= 0) {
      print(i);
      return;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)

#define LL long long int
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 3e5 + 5;
const int oo = 1e9 + 5;
const LL ooll = (LL)1e18 + 5;
const int MOD = 1e9 + 7;
const int M = 20005;

clock_t start = clock();

LL v[105];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  LL have = 1000;
  multiset<pair<LL, LL>> stock;
  for (int i = 0; i < n; ++i) {
    while (!stock.empty() && stock.begin()->first < v[i]) {
      stock.insert({v[i], stock.begin()->second});
      stock.erase(stock.begin());
    }
    while (!stock.empty()) {
      auto it = stock.end();
      --it;
      if (it->first > v[i]) {
        have += it->first * it->second;
        stock.erase(it);
      } else {
        break;
      }
    }
    LL go = have / v[i];
    stock.insert({v[i], go});
    have -= go * v[i];
  }
  for (auto it : stock)
    have += it.first * it.second;
  cout << have << '\\n';
}

int main() {
  FASTIO;
  int T = 1;
  // pre();
  // cin >> T;
  while (T--) {
    solve();
  }
  // cout << fixed << setprecision(10);
  // cout << (clock() - start) / ((double)(CLOCKS_PER_SEC)) << " s\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)

#define LL long long int
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 3e5 + 5;
const int oo = 1e9 + 5;
const LL ooll = (LL)1e18 + 5;
const int MOD = 1e9 + 7;
const int M = 20005;

clock_t start = clock();

int v[N];
void solve() {
  int n;
  cin >> n;
  vector<int> dp(M, -oo), dq;
  dp[1000] = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    // cout << i << " here\\n";
    dq = dp;
    fill(dp.begin(), dp.end(), -oo);
    for (int j = 0; j < M; ++j) {
      for (int k = 0; k * x <= j; ++k) {
        // assert(j-k*x >= 0);
        dp[j - k * x] = max(dp[j - k * x], dq[j] + k);
      }
      dp[j] = max(dp[j], dq[j]);
      int have = dq[j];
      for (int k = 0; k <= have; ++k) {
        // assert(j+k*x < M);
        dp[j + k * x] = max(dp[j + k * x], have - k);
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < M; ++i)
    if (dp[i] >= 0)
      ans = i;
  cout << ans << '\\n';
}

int main() {
  // FASTIO;
  int T = 1;
  // pre();
  // cin >> T;
  while (T--) {
    solve();
  }
  // cout << fixed << setprecision(10);
  // cout << (clock() - start) / ((double)(CLOCKS_PER_SEC)) << " s\\n";
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define modd 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int main() {
  fast;
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 1000;
  for (ll i = 0; i < n - 1; i++) {
    ll stocks = ans / a[i];
    if (a[i] < a[i + 1]) {
      ans += abs(a[i + 1] - a[i]) * stocks;
    }
  }
  cout << ans << "\\n";
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define modd 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll n;
ll a[10000];
ll ans = 0;
ll dp[50][30000][81];
ll fun(ll curs, ll curm, ll i) {
  if (dp[curs][curm][i] != -1) {
    ans = max(ans, dp[curs][curm][i]);
    return 0;
  }
  ans = max(ans, curm);
  if (curm < 0 || i >= n || curs < 0) {
    return 0;
  }

  ll z = curm;
  ll k1 = fun(curs, curm, i + 1);

  ll stock = a[i];
  ll cnt = 1;
  while (z >= cnt * stock) {
    fun(curs + cnt, z - stock * cnt, i + 1);

    cnt++;
  }
  cnt = 1;
  while (cnt <= curs) {
    fun(curs - cnt, z + cnt * stock, i + 1);
    cnt++;
  }
  if (dp[curs][curm][i] == -1) {
    dp[curs][curm][i] = ans;
  }
  return 0;
}
int main() {
  fast;
  cin >> n;
  memset(dp, -1, sizeof dp);
  // cout<<dp[0][0]<<"\\n";
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  fun(0, 1000, 0);
  cout << ans << "\\n";
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
ll n, a[85], memo[85];
typedef pair<ll, ll> ii;
priority_queue<ii, vector<ii>, greater<ii>> pq;

/*
int dp(int day, int money){
    printf("%d %d\\n", day, money);
    if (day == n) return money;
    int tmp = 0;
    for (int i = day; i < n; ++i){
        int profit = (money / a[day]) * (a[i] - a[day]);
        tmp = max(tmp, dp(i + 1, money + profit));
    }
    return memo[day][money] = tmp;
}*/

int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  memset(memo, -1, sizeof memo);
  pq.push(ii(1, -1000));
  while (!pq.empty()) {
    ll day, money;
    tie(day, money) = pq.top();
    pq.pop();
    // printf("%lld %lld\\n", day, money);
    money = -money;
    if (memo[day] != -1)
      continue;
    memo[day] = money;
    for (int i = day; i <= n; ++i) {
      pq.push(ii(i + 1, -(money + (money / a[day]) * (a[i] - a[day]))));
    }
  }
  ll ans = memo[n + 1];
  printf("%lld", ans);
}


Error: WA

Fixed submission:

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

#define MAX_M 40005
int n, a[85], memo[85][MAX_M];
#define INF 1023456789

int dp(int day, int money) {
  if (day == n)
    return money;
  int tmp = dp(day + 1, money);
  for (int i = day + 1; i < n; ++i) {
    int profit = (money / a[day]) * (a[i] - a[day]);
    if (profit < 0)
      continue;
    tmp = max(tmp, dp(i + 1, money + profit));
  }
  return memo[day][money] = tmp;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  memset(memo, -1, sizeof memo);
  int ans = dp(0, 1000);
  printf("%d", ans);
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL
ll n;
ll ans;
vector<ll> aa(100);
vector<ll> maxk(100);

int main() {
  ll a, b, c, d, h, i, j, k, l, m, x, y;
  ans = 1000;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> aa[i];
  maxk[0] = 1000;
  for (i = 1; i <= n; i++) {
    maxk[i] = maxk[i - 1];
    for (j = 1; j < i; j++) {
      a = (maxk[j] / aa[j]) * aa[i] + (maxk[j] % aa[j]);
      maxk[i] = max(maxk[i], a);
    }
  }
  cout << maxk[n] << endl;
  return 0;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL
ll n;
ll ans;
vector<ll> aa(100);
void calc(ll day, ll yen, ll kabu) {
  // printf("day=%d yen=%d kabu=%d\\n", day,yen,kabu);
  if (day > n) {
    ans = max(ans, yen);
    // printf("yen=%d ans=%d\\n",yen,ans);
    return;
  }
  ll y = yen;
  ll k = kabu;
  while (y >= aa[day]) {
    y -= aa[day];
    k++;
    calc(day + 1, y, k);
  }
  y = yen;
  k = kabu;
  while (k > 0) {
    y += aa[day];
    k--;
    calc(day + 1, y, k);
  }
  calc(day + 1, yen, kabu);
}
int main() {
  ll a, b, c, h, i, j, k, l, m, x, y;
  ans = 1000;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> aa[i];

  calc(1, 1000, 0);
  cout << ans << endl;
  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 REP2(i, x, n) for (int i = x; i < (n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
static const double PI = acos(-1);
static const int INF = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  vector<int> sell;
  vector<int> buy;
  bool hold = false;

  REP(i, n - 1) {
    if (hold) {
      if (a[i] > a[i + 1]) {
        sell.push_back(a[i]);
        hold = false;
      }
    }

    if (!hold) {
      if (a[i] < a[i + 1]) {
        buy.push_back(a[i]);
        hold = true;
      }
    }
  }

  if (buy.empty()) {
    cout << 1000 << endl;
    return 0;
  }

  if (sell.size() != buy.size()) {
    sell.push_back(a[n - 1]);
  }

  //
  // for(auto x: buy) cout << x << " ";
  // cout << endl;
  //
  // for(auto x: sell) cout << x <<" ";
  // cout <<  endl;

  ll sum = 1000;
  ll nmb = 0;

  REP(i, sell.size()) {
    nmb = sum / buy[i];
    sum %= buy[i];
    // sell
    sum += nmb * sell[i];
    nmb = 0;
  }

  cout << sum << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP2(i, x, n) for (int i = x; i < (n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
static const double PI = acos(-1);
static const int INF = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  vector<int> sell;
  vector<int> buy;
  bool hold = false;

  REP(i, n - 1) {
    if (hold) {
      if (a[i] > a[i + 1])
        sell.push_back(a[i]);
      hold = false;
    }
    if (!hold) {
      if (a[i] < a[i + 1])
        buy.push_back(a[i]);
      hold = true;
    }
  }

  if (sell.empty() || *sell.rbegin() < a[n - 1]) {
    // sell.pop_back();
    sell.push_back(a[n - 1]);
  }
  // for(auto x: buy) cout << x << " ";
  // cout << endl;
  //
  // for(auto x: sell) cout << x <<" ";
  // cout <<  endl;

  if (buy.empty()) {
    cout << 1000 << endl;
    return 0;
  }

  ll sum = 1000;
  ll nmb = 0;
  REP(i, sell.size()) {
    nmb = sum / buy[i];
    sum %= buy[i];
    // sell
    sum += nmb * sell[i];
    nmb = 0;
  }

  cout << sum << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vl v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  ll prof = 0;
  ll buy = v[0];
  ll cash = 1000;
  ll sell = 0;
  int f = 0;
  for (ll i = 1; i < n; i++) {
    if (v[i] < v[i - 1]) {
      if (f == 1) {
        cash += (sell - buy) * (cash / buy);
        buy = v[i];
        f = 0;
        continue;
      }
      buy = v[i];
    } else if (v[i] == v[i - 1]) {
      if (f == 1)
        sell = v[i];
      else
        buy = v[i];
    } else {
      if (buy == 0) {
        buy = v[i];
        continue;
      }
      sell = v[i];
      f = 1;
    }
  }
  if (f == 1)
    cash += (sell - buy) * (cash / buy);
  cout << cash << "\\n";
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vl v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  ll prof = 0;
  ll buy = v[0];
  ll cash = 1000;
  ll sell = 0;
  int f = 0;
  for (ll i = 1; i < n; i++) {
    if (v[i] <= v[i - 1]) {
      if (f == 1) {
        cash += (sell - buy) * (cash / buy);
        buy = 0;
        f = 0;
        continue;
      }
      buy = v[i];
    } else {
      sell = v[i];
      f = 1;
    }
  }
  if (f == 1)
    cash += (sell - buy) * (cash / buy);
  cout << cash << "\\n";
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring> // memset()
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define For(i, a, b) for (int i = (int)(a), sz = (int)(b); i < sz; i++)
#define rFor(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fill(a, b) memset(a, b, sizeof(a))
#define IO ios_base::sync_with_stdio(0), cin.tie(0)

const int MAX = 2e5 + 5;

int main() {
  IO;
  ll n, a, have = 0, c = 1000, prev;
  cin >> n >> prev;
  For(i, 1, n) {
    cin >> a;
    if (a > prev) {
      have = c / prev, c %= prev;
      c += have * a;
    }
    prev = a;
  }
  printf("%lld\\n", c);
  return 0;
}

Error: -11

Fixed submission:

#define ll long long int

#include <bits/stdc++.h>

using namespace std;
const int N = 16005;
int arr[N];

string convert(ll n) {
  string temp = "";
  while (n) {
    if (n % 2) {
      temp += '1';
    } else
      temp += '0';
    n = n / 2;
  }
  reverse(temp.begin(), temp.end());
  return temp;
}

ll power(ll n) {
  ll res = 1;
  while (n) {
    res = res * 2;
    n--;
  }
  return res;
}

ll binary(string s) {
  ll res = 0;
  ll cnt = 0;
  for (int i = s.size() - 1; i >= 0; --i) {
    if (s[i] == '1')
      res += power(cnt);
    cnt++;
  }
  return res;
}

int n;
ll dp[81][N][101];

ll go(int i, int have, int candy) {

  if (i == n) {
    return 1000;
  }
  ll &ret = dp[i][have][candy];
  if (~ret)
    return ret;

  ret = -1e9;
  ret = max(ret, go(i + 1, have, candy));

  if (candy > 0) {
    ret = max(ret, go(i, have + arr[i], candy - 1) + arr[i]);
  }
  if (have >= arr[i]) {
    ret = max(ret, go(i, have - arr[i], candy + 1) - arr[i]);
  }
  //  cout << i <<" " << have <<endl;
  return ret;
}

int main() {

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

  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << go(0, 1000, 0) << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool change(T &a, T b) {
  if (a != b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
typedef long long ll;

// 株・金
map<ll, ll> dp[100];
ll a[100];
ll n;

int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dp[1][0] = 1000;
  for (ll i = 1; i <= n; i++) {
    for (const auto &[j, mon] : dp[i]) {
      chmax(dp[i + 1][j], mon);
      chmax(dp[i + 1][0], mon + a[i] * j);
      chmax(dp[i + 1][j + mon / a[i]], mon % a[i]);
    }
  }
  cout << dp[n + 1][0] << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool change(T &a, T b) {
  if (a != b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
typedef long long ll;

ll dp[100][2000000];
ll a[100];
ll n;

int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dp[1][0] = 1000;
  ll maxm = 2000000;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j < maxm; j++)
      dp[i][j] = -1;
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < maxm; j++) {
      if (dp[i][j] != -1) {
        chmax(dp[i + 1][j], dp[i][j]);
        chmax(dp[i + 1][0], dp[i][j] + a[i] * j);
        chmax(dp[i + 1][j + dp[i][j] / a[i]], dp[i][j] % a[i]);
      }
    }
  }
  cout << dp[n + 1][0] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, a[87] = {0};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll money = 1000;
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < a[i + 1]) {
      cnt += money / a[i];
      ll num = money / a[i];
      money -= a[i] * num;
    } else {
      money += cnt * a[i];
      cnt = 0;
    }
  }
  cout << money << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, a[87] = {0};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll money = 1000;
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < a[i + 1]) {
      cnt += money / a[i];
      while (money >= a[i])
        money -= a[i];
    } else {
      money += cnt * a[i];
      cnt = 0;
    }
  }
  cout << money << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll stock = 0, money = 1000;
  rep(i, n) {
    // 購入
    {
      ll mv = a[i];
      bool exchange = false;
      reps(j, i + 1, n) {
        mv = min(mv, a[j]);
        if (a[j] > a[i]) {
          exchange = true;
          break;
        }
      }
      if ((exchange) && (mv == a[i])) {
        ll cnt = money / a[i];
        stock += cnt;
        money -= cnt * a[i];
      }
    }
    // 売却
    {
      ll mv = a[i];
      reps(j, i + 1, n) {
        mv = max(mv, a[j]);
        if (a[j] < a[i])
          break;
      }
      if (mv == a[i]) {
        money += stock * a[i];
        stock = 0;
      }
    }
  }
  cout << money << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<map<ll, ll>> dp(n + 1);
  dp[0][0] = 1000;
  rep(i, n) {
    for (auto &x : dp[i]) {
      rep(j, x.first) {
        dp[i + 1][j] = max(dp[i + 1][j], x.second + (x.first - j) * a[i]);
      }
      rep(j, x.second / a[i] + 1) {
        dp[i + 1][x.first + j] =
            max(dp[i + 1][x.first + j], x.second - a[i] * j);
      }
    }
  }
  ll ans = 0;
  for (auto &x : dp[n])
    ans = max(ans, x.second);
  cout << ans << endl;
  return 0;
}