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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_N = 80;
const int MAX_C = 25000000;

int N;
int A[MAX_N + 1];
int dp[MAX_N + 1];
int ans;

signed main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  dp[0] = 1000;
  for (int i = 0; i < N; ++i) {
    chmax(dp[i + 1], dp[i]);
    for (int j = 0; j <= i; ++j) {
      int pl = (dp[j] / A[j]) * A[i];
      int mn = (dp[j] / A[j]) * A[j];
      chmax(dp[i + 1], dp[j] - mn + pl);
    }
  }
  for (int i = 0; i <= N; ++i)
    chmax(ans, dp[i]);
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_N = 80;
const int MAX_C = 25000000;

int N;
int A[MAX_N + 1];
int dp[2][MAX_C + 5];
int ans;

void init() {
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < MAX_C + 5; ++j)
      dp[i][j] = -1;
}

signed main() {
  init();
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  dp[0][0] = 1000;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= MAX_C; ++j) {
      if (dp[i % 2][j] == -1)
        continue;
      chmax(dp[(i + 1) % 2][j], dp[i % 2][j]);
      chmax(dp[(i + 1) % 2][0], dp[i % 2][j] + A[i] * j);
      int k = dp[i % 2][j] / A[i];
      if (j + k <= MAX_C)
        chmax(dp[(i + 1) % 2][j + k], dp[i % 2][j] - A[i] * k);
      chmax(ans, dp[(i + 1) % 2][j]);
      chmax(ans, dp[(i + 1) % 2][0]);
      chmax(ans, dp[(i + 1) % 2][j + k]);
    }
  }
  cout << ans << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
//	Table is the complete reverse of dp
//	Always think of the pattern (LCM, GCD)
//	Be careful of unsigned
//	double epsilon = FLT_EPSILON;
//	Think of a solution that gets TLE and optimize it
int n, *x;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cin >> n;
  x = new int[n];
  for (int i = 0; i < n; i++)
    cin >> x[i];
  ll ans = 1000;
  for (int i = 0; i < n;) {
    int maxi = -1;
    for (int j = i + 1; j < n; j++)
      if (x[j] > x[i]) {
        maxi = j;
        break;
      }
    if (!~maxi) {
      i++;
      continue;
    }
    int mini = i;
    for (int j = i + 1; j < maxi; j++)
      if (x[j] < x[i]) {
        mini = j;
        break;
      }
    if (mini != i)
      i = mini;
    else {
      ll buy = ans / x[i];
      ans += buy * (x[maxi] - x[i]);
      i = maxi;
    }
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
//	Table is the complete reverse of dp
//	Always think of the pattern (LCM, GCD)
//	Be careful of unsigned
//	double epsilon = FLT_EPSILON;
//	Think of a solution that gets TLE and optimize it
int n, *x;
map<pair<ll, ll>, ll> *dp;
ll solve(int i = 0, ll sum = 1000, ll stock = 0) {
  if (i == n)
    return sum;
  if (dp[i].find({sum, stock}) != dp[i].end())
    return dp[i][{sum, stock}];
  ll &ret = dp[i][{sum, stock}];
  ret = solve(i + 1, sum - sum / x[i] * x[i], stock + sum / x[i]);
  ret = max(ret, solve(i + 1, sum, stock));
  ret = max(ret, solve(i + 1, sum + stock * x[i], 0));
  return ret;
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cin >> n;
  x = new int[n];
  for (int i = 0; i < n; i++)
    cin >> x[i];
  dp = new map<pair<ll, ll>, ll>[n];
  cout << solve();
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int siz = 2e5;
const long inf = 1e9;
int main() {
  int N;
  cin >> N;
  int a, t;
  cin >> t;
  long v = 1000;
  int k = 0;
  rep(i, 1, N) {
    cin >> a;
    if (t < a)
      v += (a - t) * (v / t);
    t = a;
  }
  cout << v << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int siz = 2e5;
const long inf = 1e9;
int main() {
  int N;
  cin >> N;
  int dp[N + 1][200];
  rep(i, 0, N + 1) rep(j, 0, 200) dp[i][j] = -1;
  dp[0][0] = 1000;
  int a;
  rep(i, 1, N + 1) {
    cin >> a;
    rep(j, 0, 200) {
      rep(k, 0, j + 1) {
        if (dp[i - 1][j] == -1)
          break;
        dp[i][j - k] = max(dp[i][j - k], dp[i - 1][j] + a * k);
      }
      rep(k, 0, dp[i - 1][j] / a + 1) {
        dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] - a * k);
      }
    }
  }
  int ans = 0;
  rep(i, 0, 200) ans = max(ans, dp[N][i]);
  cout << ans << endl; // pprint(dp,,N,10);
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define ii pair<ll, ll>
#define sz(v) ((int)((v).size()))
#define lp(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf = 2e9, MOD = 998244353, MAX = 1e2 + 2;

ll n;
ll a[82];

int main() {
  IOS;
  cin >> n;
  ll res = 1000, mn = inf;
  lp(i, 0, n) {
    cin >> a[i];
    if (i) {
      if (a[i] <= a[i - 1])
        mn = min(mn, a[i]);
      else {
        ll cnt = res / mn;
        res = res - cnt * mn;
        res = res + cnt * a[i];
        mn = a[i];
      }

    } else
      mn = a[i];
  }
  cout << res;
}
/*

*/


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define ii pair<ll, ll>
#define sz(v) ((int)((v).size()))
#define lp(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf = 2e7, MOD = 998244353, MAX = 1e2 + 2;

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

int sol(int idx, int cnt, int money) {
  if (idx >= n)
    return money;
  int &ret = dp[idx][cnt][money];
  if (~ret)
    return ret;
  ret = sol(idx + 1, cnt, money);
  if (money >= a[idx])
    ret = max(ret, max(sol(idx, cnt + 1, money - a[idx]),
                       sol(idx + 1, cnt + 1, money - a[idx])));
  if (cnt > 0)
    ret = max(ret, max(sol(idx, cnt - 1, money + a[idx]),
                       sol(idx + 1, cnt - 1, money + a[idx])));
  /*ret=max(sol(idx,cnt+1,money-a[idx]),max(sol(idx+1,cnt+1,money-a[idx]),max(
     sol(idx,cnt-1,money+a[idx]),max( sol(idx+1,cnt-1,money+a[idx]),ret))));*/
  return ret;
}

int main() {
  IOS;
  cin >> n;
  lp(i, 0, n) cin >> a[i];
  memset(dp, -1, sizeof dp);
  cout << sol(0, 0, 1000);
}
/*

*/
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int balance = 1000;
  vector<int> min;
  vector<int> max;
  if (a[0] < a[1]) {
    min.push_back(0);
  }

  for (int i = 1; i < n - 1; i++) {
    if (a[i] <= a[i - 1] && a[i] < a[i + 1]) {
      min.push_back(i);
    }
  }
  for (int i = 1; i < n - 1; i++) {
    if (a[i] > a[i - 1] && a[i] >= a[i + 1]) {
      max.push_back(i);
    }
  }
  if (a[n - 1] > a[n - 2]) {
    max.push_back(n - 1);
  }

  int x = min.size();
  int y = max.size();

  for (int i = 0; i < x; i++) {

    for (int j = 0; j < y; j++) {
      if (max[j] > min[i]) {
        long long int p = balance / a[min[i]];

        long long int q = p * (a[max[j]] - a[min[i]]);
        balance += q;

        j = y;
      }
    }
  }
  cout << balance << endl;
}


Error: 0

Fixed submission:

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int balance = 1000;
  vector<int> min;
  vector<int> max;
  if (a[0] <= a[1]) {
    min.push_back(0);
  }

  for (int i = 1; i < n - 1; i++) {
    if (a[i] <= a[i - 1] && a[i] < a[i + 1]) {
      min.push_back(i);
    }
  }
  for (int i = 1; i < n - 1; i++) {
    if (a[i] >= a[i - 1] && a[i] > a[i + 1]) {
      max.push_back(i);
    }
  }
  if (a[n - 1] >= a[n - 2]) {
    max.push_back(n - 1);
  }

  for (int i = 0; i < (int)min.size(); i++) {

    for (int j = 0; j < (int)max.size(); j++) {
      if (max[j] > min[i]) {
        int p = balance / a[min[i]];

        int q = p * (a[max[i]] - a[min[i]]);
        balance += q;

        j = (int)max.size();
      }
    }
  }
  cout << balance << endl;
}
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 repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  // dp[i]:i日めが始まるタイミングでの所持金(株0)
  vl dp(n + 1);
  dp[0] = 1000;
  dp[1] = 1000;

  // i日めに買ってj日めに売る
  rep(i, n - 1) {

    ll kabu = dp[i] / a[i];
    ll money = dp[i] % a[i];

    ll m = dp[i];
    repa(j, i + 1, n) {
      cmax(dp[j + 1], max(money + kabu * a[j], m));
      cmax(m, dp[j + 1]);
    }
  }

  cout << dp[n] << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  // int m=a[n-1];
  // vi amax(n);
  // amax[n-1]=0;
  // rrep(i,n-2){
  //     amax[i]=m;
  //     cmax(m,a[i]);
  // }

  // vc<vc<pi>> dp(n+1,vc<pi>(2));
  // dp[0][0].first=0;
  // dp[0][0].second=1000;

  // rep(i,n+1){

  // }

  map<string, ll> dp;
  dp["0"] = 1000;
  ll ans = 1000;

  rep(i, n) {
    for (auto it = dp.begin(); it != dp.end(); it++) {

      ll kabu = stoi((*it).first);

      // 売る
      if (dp["0"] < (*it).second + kabu * a[i])
        dp["0"] = (*it).second + kabu * a[i];
      if (ans < dp["0"])
        ans = dp["0"];
      // かう
      ll cb = (*it).second / a[i];
      if (dp[to_string(kabu + cb)] < ((*it).second - cb * a[i]))
        dp[to_string(kabu + cb)] = ((*it).second - cb * a[i]);
    }
  }

  cout << ans << "\\n";

  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++)
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> dp(N, 1000);
  REP(i, N) {
    ll money = dp[i]; // 今日の所持金
    ll kabu = money / A[i];
    money = money % A[i];
    if (kabu == 0)
      continue;
    for (int j = i + 1; j < N; j++) {
      if (dp[j] < money + kabu * A[j]) {
        dp[j] = money + kabu * A[j];
      }
    }
    for (int j = i + 1; j < N; j++) {
      if (dp[j] < dp[i]) {
        dp[j] = dp[i];
      }
    }
  }
  cout << dp[N - 1] << endl;
}

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>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> dp(N);
  REP(i, N + 1) dp[i] = 1000;
  REP(i, N) {
    ll money = dp[i]; // 今日の所持金
    ll kabu = money / A[i];
    money = money % A[i];
    if (kabu == 0)
      continue;
    for (int j = i + 1; j < N; j++) {
      chmax(dp[j], money + kabu * A[j]);
    }
    for (int j = i + 1; j < N; j++) {
      chmax(dp[j], dp[i]);
    }
  }
  cout << dp[N - 1] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

signed main() {
  int n, a[80];
  long long money, kabu, ans;

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

  ans = 1000;
  for (int i = 1; i < n; i++) {
    money = 1000, kabu = 0;
    for (int j = 0; j < i; j++) {
      if (a[j] < a[j + 1]) {
        kabu += money / a[j];
        money = money % a[j];
      } else if (a[j] > a[j + 1]) {
        money += a[j] * kabu;
        kabu = 0;
      }
    }
    ans = max(money + kabu * a[i], ans);
  }

  cout << ans << endl;

  return (0);
}

Error: TLE

Fixed submission:

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

int table[81][30001];
signed main() {
  int n, a[81], ans = 1000, kabu, kabuman, O;
  cin >> n;

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

  table[0][0] = 1000;

  kabu = 0;
  kabuman = 0;
  O = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= kabuman; j++) {
      for (int k = 0; k <= table[i - 1][j] / a[i]; k++) {
        table[i][j + k] = max(table[i][j + k], table[i - 1][j] - k * a[i]);
        if (table[i][j + k] > ans) {
          ans = table[i][j + k];
        }
        kabu = max(kabu, j + k);

        O++;
      }

      for (int k = j; k >= 0; k--) {
        table[i][k] = max(table[i][k], table[i - 1][j] + (j - k) * a[i]);
        ans = max(table[i][k], ans);

        O++;
      }
    }
    kabuman = kabu;
  }

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

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

void buy(long long &money, long long &stock, int price) {
  long long count = money / price;
  stock += count;
  money -= price * count;
}

void sell(long long &money, long long &stock, int price) {
  money += stock * price;
  stock = 0;
}

int main(void) {
  int N;

  cin >> N;

  vector<long long> A(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  long long money = 1000;
  long long stock = 0;

  int mode = 0;
  for (int i = 0; i < N - 1; i++) {
    if (mode == 0) {
      if (A[i] <= A[i + 1]) {
        buy(money, stock, A[i]);
        mode = 1;
      }
    } else if (mode == 1) {
      if (A[i] >= A[i + 1]) {
        sell(money, stock, A[i]);
        mode = 0;
      }
    }
  }

  sell(money, stock, A[N - 1]);
  cout << money << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

void next(vector<int> &A) {
  A[0]++;
  for (int i = 0; i < A.size() - 1; i++) {
    if (A[i] > 2) {
      A[i + 1]++;
      A[i] = 0;
    } else {
      break;
    }
  }
}

void buy(int &money, int &stock, int price) {
  int count = money / price;
  stock += count;
  money -= price * count;
}

void sell(int &money, int &stock, int price) {
  money += stock * price;
  stock = 0;
}

int main(void) {
  int N;

  cin >> N;

  vector<int> A(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int money = 1000, stock = 0, max_money = -1;

  vector<int> flag(N, 0);

  for (; flag[N - 1] <= 2; next(flag)) {
    money = 1000;
    stock = 0;

    for (int i = 0; i < N; i++) {
      if (flag[i] == 0) {
        buy(money, stock, A[i]);
      } else if (flag[i] == 1) {
        sell(money, stock, A[i]);
      }
    }

    if (max_money < money) {
      max_money = money;
    }
  }

  cout << max_money << endl;

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

/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ld x, ld y, ld a, ld b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (ll k = 0; k < n; k++) {
    cerr << a[k] << " ";
  }
  cout << "\\n";
}
#define PI 3.14159265358979323846
#define FF first
#define SS second

ll n;
ll a[80];

int main() {
  FASTIO;
  cin >> n;
  parseArray(a, n);
  ll k = 1;
  ll money = 1000;
  while (k < n) {
    while (k < n && a[k] < a[k - 1])
      k++;
    ll buy = money / a[k - 1];
    money -= buy * a[k - 1];
    while (k < n && a[k] >= a[k - 1])
      k++;
    ll sell = buy * a[k - 1];
    money += sell;
  }
  cout << money << endl;
  return 0;
}

Error: -11

Fixed submission:

/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ld x, ld y, ld a, ld b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (ll k = 0; k < n; k++) {
    cerr << a[k] << " ";
  }
  cout << "\\n";
}
#define PI 3.14159265358979323846
#define FF first
#define SS second

ll n;
ll a[80];
ll memo[80][1000 + 200 * 80][1000 / 100];
ll dp(ll day, ll money, ll shares) {
  if (day == n)
    return money;
  if (memo[day][money][shares] != -1)
    return memo[day][money][shares];
  ll ans = money;
  ll amount = 0;
  for (int k = 0; k <= shares; k++)
    ans = max(ans, dp(day + 1, money + k * a[day], shares - k));
  while (amount * a[day] <= money) {
    ans = max(ans, dp(day + 1, money - amount * a[day], shares + amount));
    amount++;
  }
  return memo[day][money][shares] = ans;
}

int main() {
  FASTIO;
  cin >> n;
  parseArray(a, n);
  memset(memo, -1, sizeof memo);
  cout << dp(0, 1000, 0) << endl;
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  long long N, Money = 1000, stock = 0;
  cin >> N;
  int i, A[81];
  for (i = 1; i <= N; i++)
    cin >> A[i];
  for (i = 1; i <= N; i++) {
    Money += stock * A[i];
    stock = 0;
    if (i < N && A[i] < A[i + 1]) {
      stock = Money / A[i];
      Money -= stock * A[i];
    }
  }
  cout << Money << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int N, A[81];

int f(int j, int money, int stock) {
  if (j == N + 1)
    return money;
  int k, max = 0;
  for (k = -stock; k <= (money / A[j]); k++) {
    if ((money - k * A[j]) >= 0 &&
        max < f(j + 1, money - k * A[j], stock + k)) {
      max = f(j + 1, money - k * A[j], stock + k);
    }
  }
  return max;
}

int main() {
  int i, ans;
  cin >> N;
  for (i = 1; i <= N; i++) {
    cin >> A[i];
  }
  ans = f(1, 1000, 0);
  cout << ans << endl;
  return 0;
}