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

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 80 + 5;
int a[N], n;
int main() {
  GO();
  int T;
  // cin>>T;
  T = 1;
  while (T--) {

    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    ll bal = 1000, cnt = 0;
    for (int i = 0; i < n; i++) {

      if (i + 1 < n && a[i + 1] >= a[i]) {
        cnt += bal / a[i];
        bal %= a[i];
        bal += cnt * a[i + 1];
        cnt = 0;
      } else {

        ;
      }
    }
    cout << bal << "\\n";
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 80 + 5;
int a[N], n, mem[N][2000 + 5][N][5];
int solve(int pos, int bal, int cnt, int fl) {
  //  cout<<pos<<" "<<bal<<" "<<cnt<<endl;
  if (pos == n) {
    return bal;
  }
  int &ret = mem[pos][bal][cnt][fl];
  if (ret != -1) {
    return ret;
  }
  int ans = 0;
  if (bal >= a[pos] && fl != 0) {
    ans = max(ans, solve(pos, bal - a[pos], cnt + 1, 1));
  }
  if (cnt > 0 && fl != 1) {
    ans = max(ans, solve(pos, bal + a[pos], cnt - 1, 0));
  }

  ans = max(ans, solve(pos + 1, bal, cnt, 2));
  return ret = ans;
}
int main() {
  GO();
  int T;
  // cin>>T;
  T = 1;
  while (T--) {

    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    clr(mem, -1);
    cout << solve(0, 1000, 0, 2) << "\\n";
  }
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;

void IO() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("test.txt", "r", stdin);
#endif
}

const int N = 2e6 + 3, M = 5e3 + 5, mod = 1e9 + 7, dx[] = {1, -1, 0, 0},
          dy[] = {0, 0, 1, -1};
const double eps = 1e-9;

int n, m, k, tc, ans, a[N];

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


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;

void IO() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("test.txt", "r", stdin);
#endif
}

const int N = 2e6 + 3, M = 5e3 + 5, mod = 1e9 + 7, dx[] = {1, -1, 0, 0},
          dy[] = {0, 0, 1, -1};
const double eps = 1e-9;

int n, m, k, tc, ans, a[N];

int dp[81][10005][205];

int solve(int idx = 0, int cur = 1000, int stock = 0) {
  if (idx >= n)
    return cur;
  int &r = dp[idx][cur][stock];
  if (~r)
    return r;
  r = max(solve(idx + 1, cur, stock), solve(idx + 1, cur + a[idx] * stock, 0));
  r = max(r, solve(idx + 1, cur % a[idx], stock + cur / a[idx]));
  return r;
}

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

#include <bits/stdc++.h>

#define lol long long
#define gcd(x, y) __gcd(x, y)
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
using namespace std;
using pii = pair<int, int>;
template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
template <class A> inline A abs(A &a) { return (a < 0 ? -a : a); }
bool inLine(int x, int y, int mx, int my) {
  return (x >= 0 && y >= 0 && x < mx && y < my);
}
const lol inf = (1LL << 62);
const lol MOD = (1e9) + 7;
const lol mod = 998244353;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
lol n, m, k, r;
int a[500];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<lol, lol> ma[100];
  ma[0][0] = 1000;
  for (int i = 0; i < n; i++) {
    for (auto o : ma[i]) {
      chmax(ma[i + 1][o.fi], o.se);                      // 何もしない
      chmax(ma[i + 1][o.fi + o.se / a[i]], o.se % a[i]); // 株を買う
      chmax(ma[i + 1][0], o.se + o.fi * a[i]);           // 株を売る
      chmax(ma[i + 1][o.se / a[i]], o.fi * a[i] + o.se % a[i]);
    }
  }
  lol ans = 0;
  for (auto o : ma[n]) {
    chmax(ans, o.se);
  }
  cout << ans << '\\n';
  return (0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define lol long long
#define gcd(x, y) __gcd(x, y)
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
using namespace std;
using pii = pair<int, int>;
template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
template <class A> inline A abs(A &a) { return (a < 0 ? -a : a); }
bool inLine(int x, int y, int mx, int my) {
  return (x >= 0 && y >= 0 && x < mx && y < my);
}
const lol inf = (1LL << 62);
const lol MOD = (1e9) + 7;
const lol mod = 998244353;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
lol n, m, k, r;
int a[500];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<int, int> ma[100];
  ma[0][0] = 1000;
  for (int i = 0; i < n; i++) {
    for (auto o : ma[i]) {
      chmax(ma[i + 1][o.fi], o.se); // 何もしない
      // chmax(ma[i+1][o.fi+o.se/a[i]],o.se%a[i]);//株を買う
      chmax(ma[i + 1][0], o.se + o.fi * a[i]); // 株を売る
      int j = 1;
      o.se -= a[i];
      while (o.se >= 0) {
        chmax(ma[i + 1][o.fi + j], o.se);
        o.se -= a[i];
        j++;
      }
    }
  }
  lol ans = 0;
  for (auto o : ma[n]) {
    chmax(ans, o.se);
  }
  cout << ans << '\\n';
  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++)
#define rep1(i, n) for (int i = 1; i < (n + 1); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      money += a[i] * stock;
      stock = 0;

    } else if (a[i] < a[i + 1]) {
      stock += money / a[i];
      money %= a[i];
    }
    mx = max(mx, money);
    // cout << money << endl;
  }
  money += a[n - 1] * stock;
  stock = 0;
  mx = max(mx, money);
  cout << mx << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      while (stock > 0) {
        money += a[i];
        stock--;
      }
    } else if (a[i] < a[i + 1]) {
      while (true) {
        if (money - a[i] >= 0) {
          money -= a[i];
          stock++;
        } else {
          break;
        }
      }
    }
    mx = max(mx, money);
    // cout << money << endl;
  }
  while (stock > 0) {
    money += a[n - 1];
    stock--;
  }
  mx = max(mx, money);
  cout << mx << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 998244353;
signed main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> b(0);
  b.pb(a[0]);
  REP(i, 1, n) {
    if (a[i] == a[i - 1])
      continue;
    b.pb(a[i]);
  }
  if (b.size() == 1) {
    cout << 1000 << endl;
    return 0;
  }
  ll c = 0;
  ll now = 1000;
  ll s = 0;
  if (b[1] > b[0]) {
    c += now / b[0];
    now = now % b[0];
    s = 1;
  }
  b.pb(-inf);
  REP(i, 1, b.size() - 1) {
    if (s) {
      if (b[i + 1] > b[i])
        continue;
      else {
        now += b[i] * c;
        c = 0;
        s = 0;
      }
    } else {
      if (b[i + 1] < b[i])
        continue;
      else {
        c += now / b[i];
        now = now % b[i];
        s = 1;
      }
    }
  }
  cout << now << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 998244353;
signed main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll m = 810;
  vector<vector<ll>> dp(n + 1, vector<ll>(m, -1));
  dp[0][0] = 1000;
  rep(i, n) {
    rep(j, m) {
      if (dp[i][j] == -1)
        continue;
      ll t = 1;
      while (dp[i][j] >= a[i] * t) {
        dp[i + 1][j + t] = max(dp[i + 1][j + t], dp[i][j] - a[i] * t);
        t++;
      }
      rep(k, j + 1) {
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + a[i] * (j - k));
      }
    }
  }
  cout << dp[n][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 REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  int N;
  cin >> N;
  vector<ll> A(N + 1);
  REP(i, N) { cin >> A[i]; }
  ll m = 1000, k = 0;
  REP(i, N) {
    if (k == 0) {
      while (i < N && A[i] >= A[i + 1]) {
        ++i;
      }
      if (i == N) {
        break;
      }
      k = m / A[i];
      m -= k * A[i];
    } else {
      while (i < N && A[i] <= A[i + 1]) {
        ++i;
      }
      m += A[i] * k;
      k = 0;
    }
  }
  cout << m << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dp[81][1000];

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

  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  using T = tuple<int, int, int>;
  queue<T> qu;
  qu.emplace(0, 0, 1000);
  int res = 0;
  while (!qu.empty()) {
    int i, j, k;
    tie(i, j, k) = qu.front();
    qu.pop();
    if (i == N) {
      res = max(res, k);
    } else {
      qu.emplace(i + 1, 0, k + A[i] * j);
      qu.emplace(i + 1, j + k / A[i], k - k / A[i] * A[i]);
    }
  }
  cout << res << endl;

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

#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

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

  ll money = 1000;
  for (int i = 0; i < n - 1; i++) {
    if (a[i + 1] > a[i]) {
      ll c = money / a[i];
      money += c * (a[i + 1] - a[i]);
    }
  }

  cout << money << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;

int n;
ll ans = 0;
vi a;

void calc(int i, int yen, int stocks) {
  yen += stocks * a[i];
  stocks = 0;
  ans = max(ans, (ll)yen);

  for (; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      calc(j, yen % a[i], yen / a[i]);
    }
  }
}

void solve() {
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    calc(i, 1000, 0);
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include<atcoder/all>
using namespace std;
// using namespace atcoder;
#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;
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 <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const ll MOD = 1000000007;
const ll LINF = 1ll << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll now = 1000, t = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] < a[i]) {
      t = now / a[i - 1];
      now += (a[i] - a[i - 1]) * t;
    }
  }
  cout << now << 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;
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 <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 1000000007;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n;
  cin >> n;
  vector<LL> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<vector<bool>> dp(n, vector<bool>(300 * 100, false));
  int mx = 300 * 80;
  dp[0][1000] = true;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      for (int k = mx; k >= 0; k--) {
        if (not dp[i][k])
          continue;
        int t = k / a[i];
        if (k + t * (a[j] - a[i]) >= 0)
          dp[j][k + t * (a[j] - a[i])] = true;
        dp[j][k] = true;
      }
    }
  }
  for (int i = mx; i >= 0; i--) {
    if (dp[n - 1][i]) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  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]) {
      chmax(dp[i + 1][0], x.second + a[i] * x.first);
      ll now = x.second / a[i];
      chmax(dp[i + 1][x.first + now], x.second % a[i]);
    }
  }
  cout << dp[n][0] << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(30000 * n, 0));
  vector<vector<bool>> ok(n + 1, vector<bool>(30000 * n, false));
  dp[0][0] = 1000;
  ok[0][0] = true;
  rep(i, n) {
    rep(j, n * 30000) {
      if (!ok[i][j])
        continue;
      chmax(dp[i + 1][0], dp[i][j] + a[i] * j);
      ok[i + 1][0] = true;
      ll now = dp[i][j] / a[i];
      chmax(dp[i + 1][j + now], dp[i][j] % a[i]);
      ok[i + 1][j + now] = true;
    }
  }
  ll ans = 0;
  cout << dp[n][0] << endl;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define mx 500005
#define ll long long

int ar[mx];
int n, m, k, ii;

ll tot = 1000;

void stockBuySell(int m) {
  // Prices must be given for at least two days
  if (m == 1)
    return;

  // Traverse through given price array
  int i = 1;
  while (i < m) {

    // Find Local Minima
    // Note that the limit is (n-2) as we are
    // comparing present element to the next element
    while ((i < m) && (ar[i + 1] <= ar[i]))
      i++;

    // If we reached the end, break
    // as no further solution possible
    if (i == m)
      break;

    // Store the index of minima
    int buy = i++;

    // Find Local Maxima
    // Note that the limit is (n-1) as we are
    // comparing to previous element
    while ((i <= m) && (ar[i] >= ar[i - 1]))
      i++;

    // Store the index of maxima
    int sell = i - 1;
    ll ex = tot / ar[buy];
    ll val = tot - ex * ar[buy];
    ll lav = ex * ar[sell];
    tot = lav + val;

    //        cout << "Buy on day: " << buy
    //             << "\\t Sell on day: " << sell << endl;
  }
}

void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &ar[i]);
  stockBuySell(n);
  printf("%lld\\n", tot);
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

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

#define mx 100005
#define ll long long

int ar[mx];
char ch[mx];

int n, m, k, ii;

ll re;

void func(int i, ll ex, ll koy) {
  if (i == n) {
    ll tot = ex + koy * ar[i];
    re = max(re, tot);
    return;
  }
  ll nibo = ex / ar[i];
  func(i + 1, ex - (nibo * ar[i]), koy + nibo);
  ll val = koy * ar[i];
  func(i + 1, ex + val, 0);
  func(i + 1, ex, koy);
}

void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &ar[i]);
  func(1, 1000, 0);
  printf("%lld\\n", re);
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    solve();
  return 0;
}
Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll ans = 1000;
  for (int i = 0; i < n - 1; i++) {
    if (arr[i] < arr[i + 1]) {
      ll num = ans / arr[i];
      ans = ans % arr[i];
      ans += num * arr[i + 1];
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int dp[100][10001];
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j <= 1000; j++)
      dp[i][j] = -1;
  }
  dp[0][0] = 1000;
  for (int i = 0; i < n; i++) {
    for (int j = 0; dp[i][j] != -1; j++) {
      for (int k = 0; k <= j; k++) {
        int money = dp[i][j] + k * arr[i];
        int num = money / arr[i];
        for (int x = 0; x <= num; x++) {
          dp[i + 1][j - k + x] = max(dp[i + 1][j - k + x], money - x * arr[i]);
        }
      }
    }
  }
  cout << dp[n][0];
  return 0;
}