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

#include <bits/stdc++.h>

using namespace std;

const int inf = 1000000000;

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

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

  long long m = 1000;
  for (int i = 0; i + 1 < n; ++i) {
    if (a[i] < a[i + 1]) {
      long long x = m / a[i];
      long long y = m % a[i];
      m = x * a[i + 1] + y;
    }
  }

  cout << m << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int inf = 1000000000;

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

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

  int m = 1000;

  vector<int> dp(1000001, -inf);
  for (int i = 0; i <= m; ++i) {
    dp[i] = 0;
  }
  for (int i = 0; i + 1 < n; ++i) {
    dp.assign(dp.size(), 0);
    for (int j = 0; j <= m; ++j) {
      dp[j] = j;
    }
    for (int j = a[i]; j <= m; ++j) {
      dp[j] = max(dp[j], dp[j - a[i]] + a[i + 1]);
    }
    m = max(m, *max_element(dp.begin(), dp.end()));
  }

  cout << m << '\\n';

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

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;
int n;
int m;
vector<int> vt;

int main() {
  std ::ios ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cout << 81 * 80 * 201 << endl;
  cin >> n;
  vt.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> vt[i];
  }
  long long ans = 1000;
  for (int i = 0; i + 1 < n; ++i) {
    if (vt[i] >= vt[i + 1]) {
      continue;
    }
    long long dp = ans / vt[i];
    ans -= dp * vt[i];
    ans += vt[i + 1] * dp;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;
int n;
int m;
vector<int> vt;
map<tuple<int, int, int>, int> dp;

int rec(int pos, int amt, int stock) {
  if (pos == n) {
    return amt;
  }
  if (dp.count({pos, amt, stock})) {
    return dp[{pos, amt, stock}];
  }
  int ret = rec(pos + 1, amt, stock);
  for (int i = 1;; ++i) {
    if (amt - i * vt[pos] < 0) {
      break;
    }
    ret = max(ret, rec(pos + 1, amt - i * vt[pos], stock + i));
  }
  for (int i = 1; i <= stock; ++i) {
    // if(pos == 2) {
    //     cout << "amt : " << amt + i * vt[pos] << endl;
    // }
    ret = max(ret, rec(pos + 1, amt + i * vt[pos], stock - i));
  }
  dp[{pos, amt, stock}] = ret;
  return ret;
}

int main() {
  std ::ios ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cout << 81 * 80 * 201 << endl;
  cin >> n;
  vt.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> vt[i];
  }
  int ans = rec(0, 1000, 0);
  cout << ans << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

signed main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int CurrentMoney = 1000;
  rep(i, n - 1) {
    if (a[i] < a[i + 1]) {
      int stocks = CurrentMoney / a[i];
      CurrentMoney += (a[i + 1] - a[i]) * stocks;
    }
  }
  print(CurrentMoney);
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

signed main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int dp[11][81];
  // DP初期条件: dp[0][w] = 0
  rep(w, 81) {
    dp[0][w] = 0;
    if (1000 - a[0] * w >= 0) {
      dp[1][w] = 1000 - a[0] * w;
    } else {
      dp[1][w] = -INF;
    }
  }
  dp[0][0] = dp[1][0] = 1000;
  // DPループ
  FOR(i, 2, n + 1) { // i日目
    rep(j, 11) {
      dp[i][j] = dp[i - 1][j];
      rep(k, 11) {
        dp[i][j] = max(dp[i][j], (dp[i - 1][k] - (j - k) * a[i - 1]));
      }
    }
  }
  cout << dp[n][0] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long N, A[87], CurrentMoney = 1000;
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= N - 1; i++) {
    long long Stocks = 0;
    if (A[i] < A[i + 1])
      Stocks = CurrentMoney / A[i];
    CurrentMoney += (A[i + 1] - A[i]) * Stocks;
  }
  cout << CurrentMoney << endl;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)


Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> a(N, 0);
  vector<int> toshi;
  for (int i = 0; i < N; i++)
    cin >> a.at(i);

  bool up = false;
  if (a.at(0) < a.at(1)) {
    up = true;
    toshi.push_back(0);
  }

  for (int i = 1; i < N - 1; i++) {
    if (up == true) {
      if (a.at(i - 1) > a.at(i)) {
        toshi.push_back(i - 1);
        up = false;
      }
    } else {
      if (a.at(i - 1) < a.at(i)) {
        toshi.push_back(i - 1);
        up = true;
      }
    }
  }
  if (a.at(N - 1) > a.at(N - 2))
    toshi.push_back(N - 1);

  int sum = 1000;
  int m;
  int size = toshi.size();
  for (int i = 0; i < size; i++) {
    if (i % 2 == 0) {
      m = toshi.at(i);
      sum = sum + sum / a.at(m) * (a.at(toshi.at(i + 1)) - a.at(m));
    }
  }
  cout << sum << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define mod 1000000007
typedef long long ll;

int main() {
  ll money = 1000;
  ll kabu = 0;
  ll n;
  cin >> n;
  vector<ll> vec(n);
  rep(i, n) cin >> vec[i];
  bool up = false;
  rep(i, n - 1) {
    bool cup;

    if (vec[i + 1] > vec[i])
      cup = true;
    else if (vec[i + 1] < vec[i])
      cup = false;
    else
      continue;

    if (up && cup)
      continue;
    else if (!up && cup) {
      kabu = money / vec[i];
      money %= vec[i];
    } else if (up && !cup) {
      money += kabu * vec[i];
      kabu = 0;
    } else
      continue;

    up = cup;
  }
  if (up)
    money += kabu * vec.back();
  cout << money << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define mod 1000000007
typedef long long ll;

int main() {
  ll money = 1000;
  ll kabu = 0;
  ll n;
  cin >> n;
  vector<ll> vec(n);
  rep(i, n) cin >> vec[i];
  bool up = false;
  rep(i, n - 1) {
    bool cup;

    if (vec[i + 1] > vec[i])
      cup = true;
    else if (vec[i + 1] < vec[i])
      cup = false;
    else
      continue;

    if (up && cup)
      continue;
    else if (!up && cup) {
      while (money - vec[i] >= 0) {
        money -= vec[i];
        kabu++;
      }
    } else if (up && !cup) {
      money += kabu * vec[i];
      kabu = 0;
    } else
      continue;

    up = cup;
  }
  if (up)
    money += kabu * vec.back();
  cout << money << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mp make_pair
#define endl "\\n"
#define vi vector<int>
#define vc vector<char>
#define vll vector<ll>
#define pi pair<int, int>
const int MOD = 1e9 + 7;
const ll inf = 1e18;

int main() {
  int n;
  cin >> n;
  vll a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  ll ans = 1000LL;
  // int max_so_far = 0 ;
  for (int i = 1; i < n; ++i) {
    if (a[i] >= a[i - 1]) {
      // ans %= a[i-1] ;
      ll tmp = ans / a[i - 1];
      ans %= a[i - 1];
      ans += (ll)tmp * a[i];
    }
    // cout << ans << endl ;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mp make_pair
#define endl "\\n"
#define vi vector<int>
#define vc vector<char>
#define vll vector<ll>
#define pi pair<int, int>
const int MOD = 1e9 + 7;
const ll inf = 1e18;

int n;
vi a(100000);

int rec(int cur, int pos, int cost, int stocks) {
  if (pos >= n)
    return INT_MIN;
  if (stocks < 0)
    return INT_MIN;
  int i = pos;
  if (pos == n - 1) {
    return stocks + cur * a[i];
  }

  // option 0 pass
  int t0 = rec(cur, pos + 1, cost, stocks);
  // option 1 buy
  int t1 = rec(cur + (stocks / a[i]), pos + 1, cost, stocks % a[i]);
  // option 2 sell
  int t2 = rec(0, pos + 1, cost + cur * a[i], stocks + cur * a[i]);

  // option 3 sell and buy
  int new_stocks = stocks + cur * a[i];
  int t3 = rec(new_stocks / a[pos], pos + 1, cost, new_stocks % a[i]);

  return max(max(t0, t1), max(t2, t3));
}

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

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

int main(void) {
  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];
  ll cash = 1000;
  ll Ns = 0;
  rep(i, N - 1) {
    cash += A[i] * Ns;
    if (A[i + 1] > A[i]) {
      Ns = cash / A[i];
      cash -= A[i] * Ns;
    } else {
      Ns = 0;
    }
  }
  cash += A[N - 1] * Ns;
  cout << cash << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];
  int cash = 1000;
  int Ns = 0;
  rep(i, N - 1) {
    /*
    cash += A[i]*Ns;
    if (A[i+1] > A[i]){
            Ns = cash/A[i];
    }
    cash -= A[i]*Ns;
    cout << Ns << " " << cash <<  cash+A[i]*Ns << endl;
*/
    int sisan_n = cash + A[i + 1] * Ns;
    int Nsell = 0, Nbuy = 0;
    rep(s, Ns + 1) {
      int buy_max = (cash + A[i] * s) / A[i];
      rep(b, buy_max + 1) {
        int tmp = cash + A[i] * (s - b) + A[i + 1] * (Ns - s + b);
        if (tmp >= sisan_n) {
          Nsell = s;
          Nbuy = b;
          sisan_n = tmp;
        }
      }
    }
    Ns = Ns - Nsell + Nbuy;
    cash = cash + A[i] * (Nsell - Nbuy);
  }
  cash += A[N - 1] * Ns;
  cout << cash << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  unordered_map<long long int, long long int> um1, um2;
  um1[0] = 1000;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    um2 = um1;
    for (auto p : um1) {
      long long int s = p.first, m = p.second;
      um2[0] = max(um2[0], m + a * s);
      um2[s + m / a] = max(um2[s + m / a], m % a);
    }
    um1 = um2;
  }
  cout << um1[0] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  unordered_map<int, int> um1, um2;
  um1[0] = 1000;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    um2 = um1;
    for (auto p : um1) {
      int s = p.first, m = p.second;
      for (int j = 0; j < s; j++) {
        um2[j] = max(um2[j], m + a * (s - j));
      }
      while (m >= 0) {
        um2[s] = max(um2[s], m);
        m -= a;
        s++;
      }
    }
    um1 = um2;
  }
  cout << um1[0] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, n) for (int i = (a); i < (n); ++i)
using namespace std;
using ll = long long;
using uint = unsigned int;
const ll mod = (ll)1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll money = 1000;
  ll stock = 0;

  for (int i = 0; i < n - 1; i++) {
    if (a[i] >= a[i + 1])
      continue;
    stock = money / a[i];
    money += stock * (a[i + 1] - a[i]);
  }
  cout << money << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <numeric>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, n) for (int i = (a); i < (n); ++i)
using namespace std;
using ll = long long;
using uint = unsigned int;
const ll mod = (ll)1e9 + 7;

vector<int> a;
int n;
int ans = 0;
void dfs(int money, int stock, int it, bool up) {
  if (up) {
    while (it < n - 1 && a[it] <= a[it + 1])
      it++;
    if (it == n - 1) {
      money += stock * a[it];
      ans = max(money, ans);
      return;
    }
    dfs(money, stock, it, false);
    money += stock * a[it];
    stock = 0;
    dfs(money, stock, it, false);
  } else {
    while (it < n - 1 && a[it] >= a[it + 1])
      it++;
    if (it == n - 1) {
      ans = max(ans, money);
      return;
    }
    dfs(money, stock, it, true);
    int ds = money / a[it];
    stock += ds;
    money -= ds * a[it];
    dfs(money, stock, it, true);
  }
}

int main() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  dfs(1000, 0, 0, false);
  cout << ans << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
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;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> money(n + 1, 1000);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      money[i] = max(money[i], money[j] - ((money[j] / a[j]) * a[j]) +
                                   ((money[j] / a[j]) * a[i]));
    }
    money[i] = max(money[i], money[i - 1]);
  }
  ll res = 1000;
  for (int i = 0; i < n; i++) {
    // cout << money[i] << endl;
    res = max(res, money[i]);
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
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;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll tmp = a[0];
  bool down = true;
  for (int i = 1; i < n; i++) {
    if (down) {
      if (tmp >= a[i]) {
        tmp = a[i];
      } else {
        b.pb(tmp);
        down = 0;
      }
    } else {
      if (tmp <= a[i]) {
        tmp = a[i];
      } else {
        b.pb(tmp);
        down = 1;
      }
    }
  }
  if ((down == 1 && b[b.size() - 1] > a[n - 1]) ||
      (down == 0 && b[b.size() - 1] < a[n - 1])) {
    b.pb(a[n - 1]);
  }
  // for(int i = 0; i < b.size(); i++) {
  // 	cout << b[i] << endl;
  // }

  ll res = 1000;
  ll money = 1000;
  ll stock = 0;
  for (int i = 0; i < int(b.size()); i++) {
    if (i % 2 == 0) {
      stock = money / b[i];
      money = money - (money / b[i]) * b[i];
    } else {
      money += b[i] * stock;
      stock = 0;
      res = max(res, money);
    }
  }
  cout << res << endl;

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

#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)

LL cash(LL money, LL max, LL min) {
  // 株価の端数で現金が余る
  LL mod = money % min;
  LL stock = (money / min);
  return stock * max + mod;
}
int N;
vector<LL> A;
int rec(int money, int stock, int day, int ops) {
  if (day >= N)
    return money;
  // 購入
  if (ops == 1) {
    stock += money / A[day];
    money = money % A[day];
  } else if (ops == 2) {
    money += stock * A[day];
    stock = 0;
  }
  money =
      max(max(money, rec(money, stock, day + 1, 0)),
          max(rec(money, stock, day + 1, 1), rec(money, stock, day + 1, 2)));
  return money;
}
int main() {
  scanf("%d", &N);
  A = vector<LL>(N + 1, (LL)0);
  vector<LL> T = vector<LL>(N + 1, (LL)0);
  vector<int> max_ind;
  rep(i, N) scanf("%d ", &A[i]);

  T[0] = 1000;
  rep(i, N) {
    // kは買う日
    for (int k = 0; k < i + 1; k++) {
      // jは売る日
      for (int j = k; j < i + 1; j++) {
        LL tmp = cash(T[k], A[j], A[k]);
        if (T[i + 1] < tmp)
          T[i + 1] = tmp;
      }
    }
  }
  LL maxval = 0;
  rep(i, N + 1) { maxval = max(T[i], maxval); }
  cout << maxval << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)

int cash(int money, int max, int min) {
  // 株価の端数で現金が余る
  int mod = money % min;
  int stock = (money / min);
  return stock * max + mod;
}
int N;
vector<int> A;
int rec(int money, int stock, int day, int ops) {
  if (day >= N)
    return money;
  // 購入
  if (ops == 1) {
    stock += money / A[day];
    money = money % A[day];
  } else if (ops == 2) {
    money += stock * A[day];
    stock = 0;
  }
  money =
      max(max(money, rec(money, stock, day + 1, 0)),
          max(rec(money, stock, day + 1, 1), rec(money, stock, day + 1, 2)));
  return money;
}
int main() {
  scanf("%d", &N);
  A = vector<int>(N + 1, 0);
  vector<int> max_ind;
  rep(i, N) scanf("%d ", &A[i]);
  int money = rec(1000, 0, -1, 0);
  cout << money << endl;

  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 < (n); i++)
#define contains(v, t) (find(v.begin(), v.end(), t) != v.end())
#define MOD 1000000007
using ll = long long;
using ull = unsigned long long;
using Pii = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 100);
  rep(i, n) { cin >> a[i]; }
  a[n + 1] = a[n];

  ull m = 1000;
  ull c = 0;

  rep(i, n) {
    // cout << i << "日目: " << a[i] << endl;
    int cur = a[i];
    int pos = 0;
    for (int j = i + 1; j <= n; j++) {
      if (cur < a[j]) {
        pos = 1;
        break;
      }
      if (cur > a[j]) {
        pos = -1;
        break;
      }
    }
    ull curm = cur;
    if (pos == 1 && m >= curm) {
      c = m / cur;
      m = m - c * cur;
    } else if (pos == -1) {
      m = m + c * cur;
      c = 0;
    }
    // cout << m << " " << c << endl;
  }
  cout << m;
}


Error: WA

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define contains(v, t) (find(v.begin(), v.end(), t) != v.end())
#define MOD 1000000007
using ll = long long;
using ull = unsigned long long;
using Pii = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  rep(i, n) { cin >> a[i]; }
  a[n + 1] = a[n];

  ull m = 1000;
  ull c = 0;

  rep(i, n) {
    // cout << i << "日目: " << a[i] << endl;
    int cur = a[i];
    int pos = 0;
    for (int j = i + 1; j <= n; j++) {
      if (cur < a[j]) {
        pos = 1;
        break;
      }
      if (cur > a[j]) {
        pos = -1;
        break;
      }
    }
    ull curm = cur;
    if (pos == 1 && m >= curm) {
      c = m / cur;
      m = m - c * cur;
    } else if (pos == -1) {
      m = m + c * cur;
      c = 0;
    }
    // cout << m << " " << c << endl;
  }
  cout << m;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a[100], now_money = 1000;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < n - 1; i++) {
    long long stock = 0;
    if (a[i] < a[i + 1]) {
      stock = now_money / a[i];
      now_money += (a[i + 1] - a[i]) * stock;
    }
  }
  cout << now_money << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a[78], now_money = 1000;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < n - 1; i++) {
    int stock = 0;
    if (a[i] < a[i + 1]) {
      stock = now_money / a[i];
      now_money += (a[i + 1] - a[i]) * stock;
    }
  }
  cout << now_money << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
ll a[85];
int main() {
#ifdef LOCAL
  freopen("in3.txt", "r", stdin);
  freopen("out3.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll m = 1000;
  for (int i = 0; i < n - 1; i++) {
    if (a[i + 1] - a[i] > 0)
      m += m / a[i] * (a[i + 1] - a[i]);
  }
  cout << m << endl;
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
int dp[1000010];
int a[100];
int main() {
#ifdef LOCAL
  freopen("in1.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int b = 1000;
  for (int i = 0; i < n; i++) {
    memset(dp, 0, sizeof(dp));
    for (int j = a[i]; j <= b; j++) {
      dp[j] = max(dp[j], dp[j - a[i]] + a[i + 1] - a[i]);
    }
    b += dp[b];
  }
  cout << b << endl;
  return 0;
}