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

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int dp[2][1000010];
int fault[1000010], temp[1000010];

int main() {
  int n, p;
  cin >> n >> p;
  int x[n], y[n];
  rep(i, n) cin >> x[i] >> y[i];
  rep(i, 2) rep(j, p + 1) dp[i][j] = -1;
  dp[0][p] = 0;
  rep(i, p + 1) {
    fault[i] = 0;
    temp[i] = 0;
  }
  rep(i, n) {
    rep(j, p + 1) {
      if (dp[0][j] == -1)
        continue;
      dp[1][j] = dp[0][j];
      temp[j] = 0;
      if (fault[j] == 0) {
        if (x[i] >= y[i]) {
          if (dp[1][j] < dp[0][j] + 1) {
            dp[1][j] = dp[0][j] + 1;
            temp[j] = 0;
          }
        } else {
          if (j >= (y[i] - x[i])) {
            if (dp[1][j - (y[i] - x[i])] < dp[0][j] + 1) {
              dp[1][j - (y[i] - x[i])] = dp[0][j] + 1;
              temp[j - (y[i] - x[i])] = y[i] - x[i];
            }
          }
        }
      } else {
        if ((x[i] - fault[j] >= y[i])) {
          dp[1][j] = dp[0][j] + 1;
          temp[j] = 0;
        } else {
          int alt = max(x[i] - fault[j], 0);
          if (j >= (y[i] - alt)) {
            if (dp[1][j - (y[i] - alt)] < dp[0][j] + 1) {
              dp[1][j - (y[i] - alt)] = dp[0][j] + 1;
              temp[j - (y[i] - alt)] = (y[i] - alt);
            }
          }
        }
      }
    }
    rep(j, p + 1) dp[0][j] = dp[1][j];
    rep(j, p + 1) fault[j] = temp[j];
  }
  int ans = 0;
  rep(i, p + 1) { ans = max(ans, dp[1][i]); }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int dp[2][1000010];

int main() {
  int n, p;
  cin >> n >> p;
  int x[n], y[n];
  rep(i, n) cin >> x[i] >> y[i];
  int fault[p + 1], temp[p + 1];
  rep(i, 2) rep(j, p + 1) dp[i][j] = -1;
  dp[0][p] = 0;
  rep(i, p + 1) {
    fault[i] = 0;
    temp[i] = 0;
  }
  rep(i, n) {
    rep(j, p + 1) {
      if (dp[0][j] == -1)
        continue;
      dp[1][j] = dp[0][j];
      temp[j] = 0;
      if (fault[j] == 0) {
        if (x[0] >= y[i]) {
          if (dp[1][j] < dp[0][j] + 1) {
            dp[1][j] = dp[0][j] + 1;
            temp[j] = 0;
          }
        } else {
          if (j >= (y[i] - x[i])) {
            if (dp[1][j - (y[i] - x[i])] < dp[0][j] + 1) {
              dp[1][j - (y[i] - x[i])] = dp[0][j] + 1;
              temp[j - (y[i] - x[i])] = y[i] - x[i];
            }
          }
        }
      } else {
        if ((x[i] - fault[j] >= y[i])) {
          dp[1][j] = dp[0][j] + 1;
          temp[j] = 0;
        } else {
          int alt = max(x[i] - fault[j], 0);
          if (j >= (y[i] - alt)) {
            if (dp[1][j - (y[i] - alt)] < dp[0][j] + 1) {
              dp[1][j - (y[i] - alt)] = dp[0][j] + 1;
              temp[j - (y[i] - alt)] = j - (y[i] - alt);
            }
          }
        }
      }
    }
    rep(j, p + 1) dp[0][j] = dp[1][j];
    rep(j, p + 1) fault[j] = temp[j];
  }
  int ans = 0;
  rep(i, p + 1) { ans = max(ans, dp[1][i]); }
  cout << ans << endl;
  return 0;
}
Problem p02104 (C++) — buggy submission, verdict: Runtime Error

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

int n, p, ans[100005];
deque<int> q, q2[100005];

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    q2[p - 1].push_back(i + 1);
  }

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {

      while (!q2[j].empty()) {
        q.push_front(q2[j].back());
        q2[j].pop_back();
      }

      if (ans[j] == 0 && !q.empty()) {
        ans[j] = q.front();
        q.pop_front();
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int n, p, ans[100005];
deque<int> q, q2[100005];

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    q2[p - 1].push_back(i + 1);
  }

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {

      while (!q2[j].empty()) {
        q.push_front(q2[j].back());
        q2[j].pop_back();
      }

      if (ans[j] == 0) {
        ans[j] = q.front();
        q.pop_front();
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {
  int n;
  cin >> n;
  vector<int> m(n);
  vector<int> d(n);
  vector<int> v(n);
  vector<int> s(n);
  vector<int> c(12 * 30, 0);
  for (int i = 0; i < n; i++) {
    cin >> m[i] >> d[i] >> v[i] >> s[i];
    m[i]--;
    d[i]--;
    int tmp = m[i] * 30 + d[i];
    for (int j = tmp; j < tmp + v[i]; j++) {
      if (j < 0)
        c[(720 + j) % 360] = max(c[(720 + j) % 360], s[i]);
      else if (j >= 360)
        c[j % 360] = max(c[(j) % 360], s[i]);
      else
        c[j] = max(c[j], s[i]);
    }
    for (int j = 1; j <= s[i]; j++) {
      if (tmp - j < 0)
        c[(720 + tmp - j) % 360] = max({0, c[(720 + tmp - j) % 360], s[i] - j});
      else if (tmp - j >= 360)
        c[(tmp - j) % 360] = max({0, c[(tmp - j) % 360], s[i] - j});
      else
        c[tmp - j] = max({0, c[tmp - j], s[i] - j});
    }
    for (int j = 0; j < s[i]; j++) {
      if (tmp + v[i] + j < 0)
        c[(720 + tmp + v[i] + j) % 360] =
            max({0, c[(720 + tmp + v[i] + j) % 360], s[i] - j - 1});
      else if (tmp + v[i] + j >= 360)
        c[(tmp + v[i] + j) % 360] =
            max({0, c[(tmp + v[i] + j) % 360], s[i] - j - 1});
      else
        c[tmp + v[i] + j] = max({0, c[tmp + v[i] + j], s[i] - j - 1});
    }
  }

  cout << *min_element(c.begin(), c.end()) << endl;

  return 0;
}

// EOF

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {
  int n;
  cin >> n;
  vector<int> m(n);
  vector<int> d(n);
  vector<int> v(n);
  vector<int> s(n);
  vector<int> c(12 * 30, 0);
  for (int i = 0; i < n; i++) {
    cin >> m[i] >> d[i] >> v[i] >> s[i];
    m[i]--;
    d[i]--;
    int tmp = m[i] * 30 + d[i];
    for (int j = tmp; j < tmp + v[i]; j++) {
      if (j < 0)
        c[360 + j] = max(c[360 + j], s[i]);
      else if (j >= 360)
        c[j - 360] = max(c[j - 360], s[i]);
      else
        c[j] = max(c[j], s[i]);
    }
    for (int j = 1; j <= s[i]; j++) {
      if (tmp - j < 0)
        c[360 + tmp - j] = max({0, c[360 + tmp - j], s[i] - j});
      else if (tmp - j >= 360)
        c[tmp - j - 360] = max({0, c[tmp - j - 360], s[i] - j});
      else
        c[tmp - j] = max({0, c[tmp - j], s[i] - j});
    }
    for (int j = 0; j < s[i]; j++) {
      if (tmp + v[i] + j < 0)
        c[360 + tmp + v[i] + j] =
            max({0, c[360 + tmp + v[i] + j], s[i] - j - 1});
      else if (tmp + v[i] + j >= 360)
        c[tmp + v[i] + j - 360] =
            max({0, c[tmp + v[i] + j - 360], s[i] - j - 1});
      else
        c[tmp + v[i] + j] = max({0, c[tmp + v[i] + j], s[i] - j - 1});
    }
  }

  cout << *min_element(c.begin(), c.end()) << endl;

  return 0;
}

// EOF
Problem p02114 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int N = 64;
const int INF = 19191919;

int n;
string a[128];

int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dp[N][N][N][N];

bool is_candidate(int lx, int ly, int rx, int ry, int X, int Y) {
  if (a[Y][X] != 'x')
    return false;
  bool ret = true;
  for (int i = lx; i <= rx; ++i)
    ret &= (a[Y][i] == 'x');
  for (int i = ly; i <= ry; ++i)
    ret &= (a[i][X] == 'x');
  return ret;
}

int f(int lx, int ly, int rx, int ry) {
  if (dp[lx / 2][ly / 2][rx / 2][ry / 2] < INF)
    return dp[lx / 2][ly / 2][rx / 2][ry / 2];

  // printf("CALL %d %d %d %d\\n", lx,ly,rx,ry);

  bool is_empty = true;
  for (int i = ly; i <= ry; ++i)
    for (int j = lx; j <= rx; ++j)
      if (a[i][j] == 'x')
        is_empty = false;
  if (is_empty)
    return 0;

  int ret = INF;
  for (int i = ly + 1; i < ry; i += 2)
    for (int j = lx + 1; j < rx; j += 2) {
      // ???????????????????£?
      if (is_candidate(lx, ly, rx, ry, j, i)) {
        // printf("  !CANDIDATE %d %d\\n", i,j);
        ret = min(ret, 1 + f(lx, ly, j - 1, i - 1) + f(j + 1, ly, rx, i - 1) +
                           f(lx, i + 1, j - 1, ry) + f(j + 1, i + 1, rx, ry));
      }
    }

  return dp[lx / 2][ly / 2][rx / 2][ry / 2] = ret;
}

int main() {
  rep(i, N) rep(j, N) rep(k, N) rep(l, N) dp[i][j][k][l] = INF;

  cin >> n;
  rep(i, n) cin >> a[i];
  cout << f(0, 0, n - 1, n - 1) << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

const int N = 128;
const int INF = 19191919;

int n;
string a[N];

int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dp[N][N][N][N];

bool is_candidate(int lx, int ly, int rx, int ry, int X, int Y) {
  if (a[Y][X] != 'x')
    return false;
  bool ret = true;
  for (int i = lx; i <= rx; ++i)
    ret &= (a[Y][i] == 'x');
  for (int i = ly; i <= ry; ++i)
    ret &= (a[i][X] == 'x');
  return ret;
}

int f(int lx, int ly, int rx, int ry) {
  if (dp[lx][ly][rx][ry] < INF)
    return dp[lx][ly][rx][ry];

  // printf("CALL %d %d %d %d\\n", lx,ly,rx,ry);

  bool is_empty = true;
  for (int i = ly; i <= ry; ++i)
    for (int j = lx; j <= rx; ++j)
      if (a[i][j] == 'x')
        is_empty = false;
  if (is_empty)
    return 0;

  int ret = INF;
  for (int i = ly + 1; i < ry; i += 2)
    for (int j = lx + 1; j < rx; j += 2) {
      // ???????????????????£?
      if (is_candidate(lx, ly, rx, ry, j, i)) {
        // printf("  !CANDIDATE %d %d\\n", i,j);
        ret = min(ret, 1 + f(lx, ly, j - 1, i - 1) + f(j + 1, ly, rx, i - 1) +
                           f(lx, i + 1, j - 1, ry) + f(j + 1, i + 1, rx, ry));
      }
    }

  return dp[lx][ly][rx][ry] = ret;
}

int main() {
  rep(i, N) rep(j, N) rep(k, N) rep(l, N) dp[i][j][k][l] = INF;

  cin >> n;
  rep(i, n) cin >> a[i];
  cout << f(0, 0, n - 1, n - 1) << endl;
  return 0;
}
Problem p02115 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N, a[1000000], d = 0, ans1 = 0, ans2 = 0;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= N; i++) {
    if (a[i] > a[i - 1]) {
      d++;
      if (d >= ans1) {
        ans1 = d;
      }
    }
    if (a[i] < a[i - 1]) {
      d = 0;
    }
  }
  d = 0;
  for (int i = 2; i <= N; i++) {
    if (a[i] < a[i - 1]) {
      d++;
      if (d >= ans2) {
        ans2 = d;
      }
    }
    if (a[i] > a[i - 1]) {
      d = 0;
    }
  }
  cout << max(ans1, ans2) + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N, a[1100], d = 0, ans1 = 0, ans2 = 0;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= N; i++) {
    if (a[i] > a[i - 1]) {
      d++;
      if (d >= ans1) {
        ans1 = d;
      }
    }
    if (a[i] < a[i - 1]) {
      d = 0;
    }
  }
  d = 0;
  for (int i = 2; i <= N; i++) {
    if (a[i] < a[i - 1]) {
      d++;
      if (d >= ans2) {
        ans2 = d;
      }
    }
    if (a[i] > a[i - 1]) {
      d = 0;
    }
  }
  cout << max(ans1, ans2) + 1 << endl;
  return 0;
}
Problem p02115 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Int(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define int2(x, y)                                                             \\
  Int(x);                                                                      \\
  Int(y);
#define vi vector<int>
#define vvi vector<vi>

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

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

  int ans = 0, pos = -1, cnt = 0;
  bool state = data[0] < data[1];
  rep(i, n - 1) {
    if (data[i] != data[i + 1]) {
      state = data[i] < data[i + 1];
      cnt = 2;
      pos = i + 1;
      break;
    }
  }
  if (pos == -1) {
    cout << 1 << endl;
    return 0;
  }

  for (int i = pos + 1; i < n; i++) {
    if (data[i - 1] == data[i]) {
      ;
    } else if (state == (data[i - 1] < data[i])) {
      cnt++;
    } else {
      state = !state;
      ans = max(cnt, ans);
      cnt = 2;
    }
  }

  ans = max(ans, cnt);

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, 0, N) { cin >> a[i]; }
  set<int> s;
  s.insert(-1);
  s.insert(INF);
  int ans(1);
  pair<int, int> cnt;
  rep(i, 0, N - 1) {
    auto x = s.insert(a[i]);
    auto l = x.first, r = x.first;
    l--;
    r++;
    dump(*l, *x.first, *r);
    cnt.first = (distance(s.begin(), l));
    cnt.second = (distance(r, prev(s.end())));

    if (a[i] > a[i + 1] && !cnt.first || a[i] < a[i + 1] && !cnt.second)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll calc(ll x, ll y) {
  if (y == 0) {
    return x;
  } else {
    return calc(y, x % y);
  }
}

int main() {

  ll N, POW[70];

  POW[0] = 1;
  POW[1] = 2;
  for (int i = 2; i <= 69; i++) {
    POW[i] = POW[i - 1] * 2;
  }

  scanf("%lld", &N);

  for (int i = 0; i <= 69; i++) {
    if (POW[i] - 1 == N) {
      printf("%lld\\n", N + 1);
      return 0;
    }
  }

  if (N % 2 == 0) {
    printf("1\\n");
    return 0;
  }

  vector<ll> bunshi;

  bunshi.push_back(N);

  ll tmp, common;

  for (ll m = 2;; m *= 2) {

    ll tmp = N - (m - 1);

    if (tmp >= m) {
      common = calc(tmp, m);
    } else {
      common = calc(m, tmp);
    }

    tmp /= common;

    if (tmp % 2 == 0) {
      printf("%lld\\n", m);
      return 0;
    }
  }

  printf("%lld\\n", N + 1);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll calc(ll x, ll y) {
  if (y == 0) {
    return x;
  } else {
    return calc(y, x % y);
  }
}

int main() {

  ll N, POW[70];

  POW[0] = 1;
  POW[1] = 2;
  for (int i = 2; i <= 69; i++) {
    POW[i] = POW[i - 1] * 2;
  }

  scanf("%lld", &N);

  for (int i = 0; i <= 69; i++) {
    if (POW[i] - 1 == N) {
      printf("%lld\\n", N + 1);
      return 0;
    }
  }

  if (N % 2 == 0) {
    printf("1\\n");
    return 0;
  }

  vector<ll> bunshi;

  bunshi.push_back(N);

  ll tmp, common;

  for (ll m = 2;; m += 2) {

    ll tmp = N - (m - 1);

    if (tmp >= m) {
      common = calc(tmp, m);
    } else {
      common = calc(m, tmp);
    }

    tmp /= common;

    if (tmp % 2 == 0) {
      printf("%lld\\n", m);
      return 0;
    }
  }

  printf("%lld\\n", N + 1);

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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 58;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

ll f(ll n) {
  ll N = n;
  bool flag = 1;
  while (N) {
    if (N % 2 == 0)
      flag = 0;
    N /= 2;
  }
  if (flag)
    return n + 1;
  for (ll i = 60; i >= 0; i--)
    if (n & 1ll << i) {
      return f(n - (1ll << i));
    }
}

ll n;

int main() {
  cin >> n;
  cout << f(n) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 58;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

ll f(ll n) {
  ll N = n;
  bool flag = 1;
  while (N) {
    if (N % 2 == 0)
      flag = 0;
    N /= 2;
  }
  if (flag)
    return n + 1;
  for (ll i = 60; i >= 0; i--)
    if (n & 1 << i) {
      n -= 1 << i;
      break;
    }
  return f(n);
}

ll n;

int main() {
  cin >> n;
  cout << f(n) << endl;
}
Problem p02116 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define ll long long

int nof(int n) {
  int ans = 0;
  for (int i = 2; i <= n; i *= 2) {
    ans += n / i;
  }
  return ans;
}

signed main() {
  int N;
  cin >> N;

  int nn = nof(N);

  int sub;
  for (int i = 1; i <= N; i *= 2) {
    if (nn > nof(i) + nof(N - i)) {
      cout << i << endl;
      return 0;
    }
    sub = i;
  }

  cout << sub * 2 << endl;
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define ll long long

int nof(int n) {
  int ans = 0;
  for (int i = 2; i <= n; i *= 2) {
    ans += n / i;
  }
  return ans;
}

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

  int nn = nof(N);

  int sub;
  for (int i = 1; i <= N; i *= 2) {
    if (nn > nof(i) + nof(N - i)) {
      cout << i << endl;
      return 0;
    }
    sub = i;
  }

  cout << sub * 2 << endl;
  return 0;
}
Problem p02116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Int(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define int2(x, y)                                                             \\
  Int(x);                                                                      \\
  Int(y);
#define vi vector<int>
#define vvi vector<vi>

string funami_yui(int n) {
  string ret;
  while (n) {
    ret.push_back((n % 2) + '0');
    n /= 2;
  }
  // reverse(ret.begin(), ret.end());
  return ret;
}

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

  string st = funami_yui(n);
  //    cout << st << endl;

  rep(i, st.size()) {
    if (st[i] == '0') {
      std::cout << (int)pow(2LL, i) << std::endl;
      // printf("%lld\\n", (int)pow(2LL, i));
      return 0;
    }
  }
  std::cout << (int)pow(2LL, st.size()) << std::endl;
  // printf("%lld\\n", (int)pow(2LL, st.size()));

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Int(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define int2(x, y)                                                             \\
  Int(x);                                                                      \\
  Int(y);
#define vi vector<int>
#define vvi vector<vi>

signed main() {
  int n;
  cin >> n;
  /*
  if (n == 1) {
      cout << 0 << endl;
      return 0;
  }
  */

  int cnt1 = 0, cnt2 = 0;
  for (int i = n, j = 1;; i--, j++) {
    int tmpi = i;
    if (i) {
      while (tmpi % 2 == 0) {
        cnt1++;
        tmpi /= 2;
      }
    }
    int tmpj = j;
    while (tmpj % 2 == 0) {
      cnt2++;
      tmpj /= 2;
    }
    // cout << i << " " << j << " : ";
    // cout << cnt1 << " " << cnt2 << endl;
    if (cnt1 > cnt2 || !i) {
      cout << j << endl;
      return 0;
    }
  }
  // cout << 0 << endl;

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

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  int N, ans, m = 0;
  cin >> N;
  for (int i = 0;; ++i) {
    if (N < m) {
      ans = i - 1;
      break;
    }
    m += pow(2, i);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int f(int x) {
  int ans = 0;
  while (x) {
    if (x % 2)
      ++ans;
    x /= 2;
  }
  return ans;
}

int main() {
  int N;
  cin >> N;
  int max = -1;
  for (int i = 0; i < N; ++i) {
    if (f(i) > max)
      max = f(i);
  }
  cout << max << endl;

  return 0;
}