prithivMLmods/Coder-Stat huggingface dataset:
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int k;
double p, e, t;

double ans = 0;

void dfs(double l, double r, int cnt, double x) {
  double h = (l + r) / 2;

  if (cnt == k) {
    if (t - e <= h && h <= t + e)
      ans += x;
    return;
  }

  if (h >= t) {
    dfs(l, h, cnt + 1, (1.0 - p) * x);
    dfs(h, r, cnt + 1, p * x);
  } else {
    dfs(h, r, cnt + 1, (1.0 - p) * x);
    dfs(l, h, cnt + 1, p * x);
  }
}

int main() {
  double l, r;
  cin >> k >> l >> r;
  cin >> p >> e >> t;

  dfs(l, r, 0, 1.0);

  cout << fixed;
  cout.precision(20);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int k;
double p, e, t;

double ans = 0;

void dfs(double l, double r, int cnt, double x) {
  if (r < t - e)
    return;
  if (l > t + e)
    return;

  double h = (l + r) / 2;

  if (cnt == k) {
    if (t - e <= h && h <= t + e)
      ans += x;
    return;
  }

  if (h >= t) {
    dfs(l, h, cnt + 1, (1.0 - p) * x);
    dfs(h, r, cnt + 1, p * x);
  } else {
    dfs(h, r, cnt + 1, (1.0 - p) * x);
    dfs(l, h, cnt + 1, p * x);
  }
}

int main() {
  double l, r;
  cin >> k >> l >> r;
  cin >> p >> e >> t;

  dfs(l, r, 0, 1.0);

  cout << fixed;
  cout.precision(20);
  cout << ans << endl;

  return 0;
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;

int K, R, L;
double P, E, T;

double dfs(int depth, double r, double l) {
  // cout<<string(depth,'\t');printf("%.8f %.8f\n",l,r);
  if (depth == K) {
    // string(depth,'\t');printf("%.8f %d\n",(l+r)/2,abs(T-(l+r)/2)<=E);
    return 1.0 * (abs(T - (l + r) / 2) <= E);
  }
  double h = (l + r) / 2;

  if (l < T - E)
    return 0;
  if (r > T + E)
    return 0;

  double ret = 0.0;
  if (h >= T) {
    ret += dfs(depth + 1, r, h) * (1 - P);
    ret += dfs(depth + 1, h, l) * (P);
  } else {
    ret += dfs(depth + 1, r, h) * (P);
    ret += dfs(depth + 1, h, l) * (1 - P);
  }

  return ret;
}

int main() {
  cin >> K >> R >> L;
  cin >> P >> E >> T;

  printf("%.20f\n", dfs(0, R, L));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;

int K, R, L;
double P, E, T;

inline double dfs(int depth, double r, double l) {
  // cout<<string(depth,'\t');printf("%.8f %.8f\n",l,r);
  if (depth == K) {
    // string(depth,'\t');printf("%.8f %d\n",(l+r)/2,abs(T-(l+r)/2)<=E);
    return 1.0 * (abs(T - (l + r) / 2) <= E);
  }
  double h = (l + r) / 2;

  if (l < T - E)
    return 0;
  if (r > T + E)
    return 0;

  double ret = 0.0;
  if (h >= T) {
    ret += dfs(depth + 1, r, h) * (1 - P);
    ret += dfs(depth + 1, h, l) * (P);
  } else {
    ret += dfs(depth + 1, r, h) * (P);
    ret += dfs(depth + 1, h, l) * (1 - P);
  }

  return ret;
}

int main() {
  cin >> K >> R >> L;
  cin >> P >> E >> T;

  printf("%.20f\n", dfs(0, R, L));
  return 0;
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;

int k, r, l;
double p, e, t;

double rec(int depth, double l, double r) {
  //    printf("%d %lf %lf\n", depth, l, r);
  double mid = (l + r) / 2, ret = 0;
  if (depth == k) {
    if (mid >= t - e && mid <= t + e)
      return 1;
    return 0;
  }

  if (r + e < t || l - e > t)
    return 0;

  if (mid > t) {
    ret += (1 - p) * rec(depth + 1, l, mid);
    ret += p * rec(depth + 1, mid, r);
  } else {
    ret += p * rec(depth + 1, l, mid);
    ret += (1 - p) * rec(depth + 1, mid, r);
  }
  return ret;
}

int main() {
  scanf("%d %d %d %lf %lf %lf", &k, &l, &r, &p, &e, &t);
  printf("%.12f\n", rec(0, l, r));
}

Fixed submission:
#include <cstdio>
using namespace std;

int k, r, l;
double p, e, t;

double rec(int depth, double l, double r) {
  //    printf("%d %lf %lf\n", depth, l, r);
  double mid = (l + r) / 2, ret = 0;
  if (depth == k) {
    if (mid >= t - e && mid <= t + e)
      return 1;
    return 0;
  }

  if (r + e < t || l - e > t)
    return 0;
  if (r < t + e && l > t - e)
    return 1;

  if (mid > t) {
    ret += (1 - p) * rec(depth + 1, l, mid);
    ret += p * rec(depth + 1, mid, r);
  } else {
    ret += p * rec(depth + 1, l, mid);
    ret += (1 - p) * rec(depth + 1, mid, r);
  }
  return ret;
}

int main() {
  scanf("%d %d %d %lf %lf %lf", &k, &l, &r, &p, &e, &t);
  printf("%.12f\n", rec(0, l, r));
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

const double eps = 1e-11;

int K, R, L;
double P, E, T;

bool equals(double a, double b) { return abs(a - b) < eps; }

double rec(double r, double l, int k, int flag) {
  if (flag) {
    double dist = min(abs(T - r), abs(T - l));
    if (!equals(dist, E) && dist > E)
      return 0.0;
  }

  if (k == 0) {
    double t = (r + l) / 2.0;
    double diff = abs(T - t);
    return (equals(diff, E) || diff < E) ? 1.0 : 0.0;
  } else {
    double h = (r + l) / 2.0;
    if (equals(h, T) || h > T) {
      return (1 - P) * rec(r, h, k - 1, 0) + P * rec(h, l, k - 1, 1);
    } else {
      return (1 - P) * rec(h, l, k - 1, 0) + P * rec(r, h, k - 1, 1);
    }
  }
}

int main() {
  cin >> K >> R >> L;
  cin >> P;
  cin >> E;
  cin >> T;
  printf("%.10f\n", rec(R, L, K, 0));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

const double eps = 1e-11;

int K, R, L;
double P, E, T;

bool equals(double a, double b) { return abs(a - b) < eps; }

double rec(double r, double l, int k, int flag) {
  if (flag) {
    double dist = min(abs(T - r), abs(T - l));
    if (!equals(dist, E) && dist > E)
      return 0.0;
  }

  {
    double tr = T - E;
    double tl = T + E;
    if ((equals(tr, r) || tr < r) && (equals(l, tl) || l < tl)) {
      return 1.0;
    }
  }

  if (k == 0) {
    double t = (r + l) / 2.0;
    double diff = abs(T - t);
    return (equals(diff, E) || diff < E) ? 1.0 : 0.0;
  } else {
    double h = (r + l) / 2.0;
    if (equals(h, T) || h > T) {
      return (1 - P) * rec(r, h, k - 1, 0) + P * rec(h, l, k - 1, 1);
    } else {
      return (1 - P) * rec(h, l, k - 1, 0) + P * rec(r, h, k - 1, 1);
    }
  }
}

int main() {
  cin >> K >> R >> L;
  cin >> P;
  cin >> E;
  cin >> T;
  printf("%.10f\n", rec(R, L, K, 0));
  return 0;
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

double P, E, T;

double solve(double L, double R, int K) {
  double H = (L + R) / 2.0;
  if (K == 0) {
    if (T - E <= H && H <= T + E) {
      return 1;
    } else {
      return 0;
    }
  }

  if (T - E > L || T + E < R) {
    return 0;
  }

  double res = 0;
  if (H < T) {
    res += solve(L, H, K - 1) * (1 - P);
    res += solve(H, R, K - 1) * P;
  } else {
    res += solve(H, R, K - 1) * (1 - P);
    res += solve(L, H, K - 1) * P;
  }
  return res;
}

int main() {
  int K;
  double R, L;
  cin >> K >> R >> L >> P >> E >> T;
  printf("%.8f\n", solve(L, R, K));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

double P, E, T;

double solve(double L, double R, int K) {
  double H = (L + R) / 2.0;
  if (K == 0) {
    if (T - E <= H && H <= T + E) {
      return 1;
    } else {
      return 0;
    }
  }

  if (T - E <= L && L <= T + E && T - E <= R && R <= T + E) {
    return 1;
  }

  if (T - E > L || T + E < R) {
    return 0;
  }

  double res = 0;
  if (H < T) {
    res += solve(L, H, K - 1) * (1 - P);
    res += solve(H, R, K - 1) * P;
  } else {
    res += solve(H, R, K - 1) * (1 - P);
    res += solve(L, H, K - 1) * P;
  }
  return res;
}

int main() {
  int K;
  double R, L;
  cin >> K >> R >> L >> P >> E >> T;
  printf("%.8f\n", solve(L, R, K));
  return 0;
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

double R, L, H;
double P, E, T;
int k;

double sleep(double L, double R, double k) {
  double H = (L + R) / 2;
  if (!k) {
    if (abs(T - H) > E)
      return 0.0;
    return 1.0;
  }

  if (T + E < R)
    return 0.0;
  if (T - E > L)
    return 0.0;

  if (H < T)
    return P * sleep(H, R, k - 1) + (1.0 - P) * sleep(L, H, k - 1);
  else
    return (1.0 - P) * sleep(H, R, k - 1) + P * sleep(L, H, k - 1);
}

int main() {
  cin >> k >> R >> L >> P >> E >> T;

  printf("%.8lf\n", sleep(L, R, k));
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

double R, L, H;
double P, E, T;
int k;

double sleep(double L, double R, double k) {
  double H = (L + R) / 2;
  if (!k) {
    if (abs(T - H) > E)
      return 0.0;
    return 1.0;
  }

  if (T - E < R && L < T + E)
    return 1.0;
  if (T + E < R)
    return 0.0;
  if (T - E > L)
    return 0.0;

  if (H < T)
    return P * sleep(H, R, k - 1) + (1.0 - P) * sleep(L, H, k - 1);
  else
    return (1.0 - P) * sleep(H, R, k - 1) + P * sleep(L, H, k - 1);
}

int main() {
  cin >> k >> R >> L >> P >> E >> T;

  printf("%.8lf\n", sleep(L, R, k));
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
using namespace std;

int R, L, K;
double P, E, T, ans = 0;

void b_search(int k, double r, double l, double p) {
  if (l <= (T - E) || (T + E) <= r)
    return;

  if (k == K) {
    double H = (r + l) / 2;
    if (T - E < H && H < T + E)
      ans += p;
  } else {
    double H = (r + l) / 2;
    if (H <= T) {
      b_search(k + 1, H, l, (1 - P) * p);
      b_search(k + 1, r, H, P * p);
    }
    if (H > T) {
      b_search(k + 1, r, H, (1 - P) * p);
      b_search(k + 1, H, l, P * p);
    }
  }
}

int main() {
  cin >> K >> R >> L >> P >> E >> T;

  b_search(0, R, L, 1);
  printf("%.8f\n", ans);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

int R, L, K;
double P, E, T, ans = 0;

void b_search(int k, double r, double l, double p) {
  if (l <= (T - E) || (T + E) <= r)
    return;
  if ((T - E) <= r && l <= (T + E)) {
    ans += p;
    return;
  }
  if (k == K) {
    double H = (r + l) / 2;
    if (T - E < H && H < T + E)
      ans += p;
  } else {
    double H = (r + l) / 2;
    if (H <= T) {
      b_search(k + 1, H, l, (1 - P) * p);
      b_search(k + 1, r, H, P * p);
    }
    if (H > T) {
      b_search(k + 1, r, H, (1 - P) * p);
      b_search(k + 1, H, l, P * p);
    }
  }
}

int main() {
  cin >> K >> R >> L >> P >> E >> T;

  b_search(0, R, L, 1);
  printf("%.8f\n", ans);

  return 0;
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;

int day[33];

double low, high;
LL sum = 0;

double P, E, T;
double Tp;
int K;
double res;
void gao(int u, int ntp, int tp, double R, double L) {
  double H = (R + L) / 2;
  // printf("H=%lf\n",H);

  if (R >= low && L <= high) {
    res += pow(1 - P, ntp) * pow(P, tp);
    return;
  }
  if (R > high || L < low)
    return;

  if (u == K) {
    Tp = H;
    if (low <= Tp && Tp <= high) {
      // printf("%d  %d\n", K-tp,tp);
      res += pow(1 - P, ntp) * pow(P, tp);
    }
    return;
  }
  if (H >= T) {
    gao(u + 1, ntp + 1, tp, R, H);

    gao(u + 1, ntp, tp + 1, H, L);
  } else {
    gao(u + 1, ntp, tp + 1, R, H);

    gao(u + 1, ntp + 1, tp, H, L);
  }
}
double R, L;
int main() {

  while (scanf("%d%lf%lf", &K, &R, &L)) {
    scanf("%lf%lf%lf", &P, &E, &T);

    low = max(0.0, T - E);
    high = T + E;
    res = 0;
    gao(0, 0, 0, R, L);

    printf("%lf\n", res);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;

int day[33];

double low, high;
LL sum = 0;

double P, E, T;
double Tp;
int K;
double res;
void gao(int u, int ntp, int tp, double R, double L) {
  double H = (R + L) / 2;
  // printf("H=%lf\n",H);

  if (R >= low && L <= high) {
    res += pow(1 - P, ntp) * pow(P, tp);
    return;
  }
  if (R > high || L < low)
    return;

  if (u == K) {
    Tp = H;
    if (low <= Tp && Tp <= high) {
      // printf("%d  %d\n", K-tp,tp);
      res += pow(1 - P, ntp) * pow(P, tp);
    }
    return;
  }
  if (H >= T) {
    gao(u + 1, ntp + 1, tp, R, H);

    gao(u + 1, ntp, tp + 1, H, L);
  } else {
    gao(u + 1, ntp, tp + 1, R, H);

    gao(u + 1, ntp + 1, tp, H, L);
  }
}
double R, L;
int main() {

  while (~scanf("%d%lf%lf", &K, &R, &L)) {
    scanf("%lf%lf%lf", &P, &E, &T);

    low = max(0.0, T - E);
    high = T + E;
    res = 0;
    gao(0, 0, 0, R, L);

    printf("%lf\n", res);
  }

  return 0;
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iomanip>
#include <iostream>
using namespace std;
int K, L, R;
double P, E, T;
double solve(double l, double r, int d) {
  double v = (l + r) / 2;
  if (d == K)
    return ((T - E <= v && v < T + E) ? 1.0 : 0.0);
  if (v < T)
    return solve(l, (l + r) / 2, d + 1) * P +
           solve((l + r) / 2, r, d + 1) * (1.0 - P);
  return solve(l, (l + r) / 2, d + 1) * (1.0 - P) +
         solve((l + r) / 2, r, d + 1) * P;
}
int main() {
  cin >> K >> L >> R >> P >> E >> T;
  cout << fixed << setprecision(15) << solve(L, R, 0) << endl;
  return 0;
}

Fixed submission:
#include <iomanip>
#include <iostream>
using namespace std;
int K, L, R;
double P, E, T;
inline double solve(double l, double r, int d) {
  double v = (l + r) / 2;
  if (d == K)
    return ((T - E <= v && v < T + E) ? 1.0 : 0.0);
  if (v < T)
    return solve(l, (l + r) / 2, d + 1) * P +
           solve((l + r) / 2, r, d + 1) * (1.0 - P);
  return solve(l, (l + r) / 2, d + 1) * (1.0 - P) +
         solve((l + r) / 2, r, d + 1) * P;
}
int main() {
  cin >> K >> L >> R >> P >> E >> T;
  cout << fixed << setprecision(15) << solve(L, R, 0) << endl;
  return 0;
}
Problem p01422 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 25
#define MAX_C 300000
#define INF 9999999999.99

vector<int> yakusuu[MAX_C];
double dp[MAX_N][MAX_C];
int n;
int x[MAX_N];

void yaku() {
  for (int i = 1; i < MAX_C; i++) {
    for (int j = i; j < MAX_C; j += i) {
      yakusuu[j].push_back(i);
    }
  }
}

int main() {
  yaku();
  cin >> n;
  x[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
  }
  dp[0][0] = INF;
  for (int i = 1; i < MAX_C; i++) {
    dp[1][i] = 1.0 * abs(x[1] - i) / x[1];
  }
  for (int i = 2; i < MAX_N; i++) {
    for (int j = 0; j < MAX_C; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j < MAX_C; j++) {
      for (int k = 0; k < yakusuu[j].size(); k++) {
        double pos1 = dp[i - 1][yakusuu[j][k]];
        double pos2 = 1.0 * abs(x[i] - j) / x[i];
        double pos3 = max(pos1, pos2);
        dp[i][j] = min(dp[i][j], pos3);
      }
    }
  }
  double minx = INF;
  for (int i = 0; i < MAX_C; i++) {
    minx = min(minx, dp[n][i]);
  }
  cout << fixed << setprecision(15) << minx << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 22
#define MAX_C 222222
#define INF 9999999999.99

vector<int> yakusuu[MAX_C];
double dp[MAX_N][MAX_C];
int n;
int x[MAX_N];

void yaku() {
  for (int i = 1; i < MAX_C; i++) {
    for (int j = i; j < MAX_C; j += i) {
      yakusuu[j].push_back(i);
    }
  }
}

int main() {
  yaku();
  cin >> n;
  x[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
  }
  dp[0][0] = INF;
  for (int i = 1; i < MAX_C; i++) {
    dp[1][i] = 1.0 * abs(x[1] - i) / x[1];
  }
  for (int i = 2; i < MAX_N; i++) {
    for (int j = 0; j < MAX_C; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j < MAX_C; j++) {
      for (int k = 0; k < yakusuu[j].size(); k++) {
        double pos1 = dp[i - 1][yakusuu[j][k]];
        double pos2 = 1.0 * abs(x[i] - j) / x[i];
        double pos3 = max(pos1, pos2);
        dp[i][j] = min(dp[i][j], pos3);
      }
    }
  }
  double minx = INF;
  for (int i = 0; i < MAX_C; i++) {
    minx = min(minx, dp[n][i]);
  }
  cout << fixed << setprecision(15) << minx << endl;
  return 0;
}
Problem p01422 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int maxn = 22;
const int maxm = 222222;
double dp[maxn][maxm];
int a[maxn];

double rat(int x, int y) { return 1.0 * abs(x - y) / x; }

int main() {
  int n, i, j, k;
  double ans, Min;
  while (~scanf("%d", &n)) {
    for (i = 1; i <= n; ++i)
      scanf("%d", &a[i]);
    memset(dp, 0x7f, sizeof(dp));
    for (i = 1; i < maxm; ++i) {
      dp[1][i] = rat(a[1], i);
    }
    for (i = 2; i <= n; ++i) {
      // Min = dp[0][0];
      for (j = 1; j < maxm; ++j) {
        for (k = 1; k * j < maxm; ++k) {
          dp[i][k * j] = min(dp[i][k * j], max(dp[i - 1][j], rat(a[i], k * j)));
          // printf ("%lf %lf %lf\n", dp[i][k * j], dp[i-1][j], rat (a[i], k *
          // j)); Min = min (Min, dp[i][k * j]);
        }
      }
      // printf ("Min: %lf\n", Min);
    }
    ans = dp[n][1];
    for (j = 2; j < maxm; ++j) {
      ans = min(ans, dp[n][j]);
    }
    printf("%.12lf\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int maxn = 22;
const int maxm = 150000;
double dp[maxn][maxm];
int a[maxn];

double rat(int x, int y) { return 1.0 * abs(x - y) / x; }

int main() {
  int n, i, j, k;
  double ans, Min;
  while (~scanf("%d", &n)) {
    for (i = 1; i <= n; ++i)
      scanf("%d", &a[i]);
    memset(dp, 0x7f, sizeof(dp));
    for (i = 1; i < maxm; ++i) {
      dp[1][i] = rat(a[1], i);
    }
    for (i = 2; i <= n; ++i) {
      // Min = dp[0][0];
      for (j = 1; j < maxm; ++j) {
        for (k = 1; k * j < maxm; ++k) {
          dp[i][k * j] = min(dp[i][k * j], max(dp[i - 1][j], rat(a[i], k * j)));
          // printf ("%lf %lf %lf\n", dp[i][k * j], dp[i-1][j], rat (a[i], k *
          // j)); Min = min (Min, dp[i][k * j]);
        }
      }
      // printf ("Min: %lf\n", Min);
    }
    ans = dp[n][1];
    for (j = 2; j < maxm; ++j) {
      ans = min(ans, dp[n][j]);
    }
    printf("%.12lf\n", ans);
  }
  return 0;
}
Problem p01422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const double eps = 1e-9;
int N;
int a[20];
vector<double> v;

bool equals(double s, double t) { return fabs(s - t) < eps; }

double cost(int s, int t) { return (double)abs(s - t) / s; }

bool rec(int bx, int k, double lim) {
  if (k == N)
    return true;
  for (int b = bx;; ++b) {
    if (b % bx)
      continue;
    double newV = cost(a[k], b);
    if (newV < lim || equals(newV, lim)) {
      if (rec(b, k + 1, lim))
        return true;
    } else {
      if (a[k] < b)
        break;
      else
        continue;
    }
  }
  return false;
}

double solve() {
  v.push_back(0.0);
  for (int m = 1; m <= 100000; ++m) {
    for (int i = 0; i < N; ++i) {
      v.push_back((double)m / a[i]);
    }
  }
  sort(v.begin(), v.end());
  int l = 0;
  int r = v.size();
  int ansi = 0;
  while (l < r) {
    int m = (l + r) / 2;
    if (rec(1, 0, v[m])) {
      ansi = r = m;
    } else {
      l = m + 1;
    }
  }
  return v[ansi];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  printf("%.12f\n", solve());
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const double eps = 1e-9;
int N;
int a[20];
vector<double> v;

bool equals(double s, double t) { return fabs(s - t) < eps; }

double cost(int s, int t) { return (double)abs(s - t) / s; }

bool rec(int bx, int k, double lim) {
  if (k == N)
    return true;
  for (int b = bx;; b += bx) {
    double newV = cost(a[k], b);
    if (newV < lim || equals(newV, lim)) {
      if (rec(b, k + 1, lim))
        return true;
    } else {
      if (a[k] < b)
        break;
      else
        continue;
    }
  }
  return false;
}

double solve() {
  v.push_back(0.0);
  for (int m = 1; m <= 100000; ++m) {
    for (int i = 0; i < N; ++i) {
      v.push_back((double)m / a[i]);
    }
  }
  sort(v.begin(), v.end());
  int l = 0;
  int r = v.size();
  int ansi = 0;
  while (l < r) {
    int m = (l + r) / 2;
    if (rec(1, 0, v[m])) {
      ansi = r = m;
    } else {
      l = m + 1;
    }
  }
  return v[ansi];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  printf("%.12f\n", solve());
  return 0;
}
Problem p01423 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int n, m;
int ans;
vector<int> neighbors[100];
set<set<int>> used;
int weight[100][100];

void dfs(set<int> vs) {
  if (used.count(vs))
    return;

  int score = 0;
  if (vs.size() > 1) {
    for (auto &&i : vs) {
      int c = 1e9;
      for (auto &&j : vs) {
        if (i != j) {
          c = min(weight[i][j], c);
        }
      }
      score += c;
    }
  }
  ans = max(ans, score);

  const int representative = *vs.begin();

  int cost[100];
  for (auto &&i : neighbors[representative])
    cost[i] = 1e9;

  for (auto &&i : vs) {
    for (auto &&j : neighbors[representative]) {
      cost[j] = min(weight[i][j], cost[j]);
    }
  }
  for (auto &&i : neighbors[representative])
    if (!vs.count(i) and cost[i] > 0) {
      set<int> nvs = vs;
      nvs.insert(i);
      dfs(nvs);
    }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    weight[a][b] = c;
    weight[b][a] = c;
    neighbors[a].push_back(b);
    neighbors[b].push_back(a);
  }
  ans = 0;
  for (int i = 0; i < n; i++) {
    dfs({i});
  }
  cout << ans << endl;
}

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

int n, m;
int ans;
vector<int> neighbors[100];
set<set<int>> used;
int weight[100][100];

void dfs(set<int> vs) {
  if (used.count(vs))
    return;
  used.insert(vs);

  int score = 0;
  if (vs.size() > 1) {
    for (auto &&i : vs) {
      int c = 1e9;
      for (auto &&j : vs) {
        if (i != j) {
          c = min(weight[i][j], c);
        }
      }
      score += c;
    }
  }
  ans = max(ans, score);

  const int representative = *vs.begin();

  int cost[100];
  for (auto &&i : neighbors[representative])
    cost[i] = 1e9;

  for (auto &&i : vs) {
    for (auto &&j : neighbors[representative]) {
      cost[j] = min(weight[i][j], cost[j]);
    }
  }
  for (auto &&i : neighbors[representative])
    if (!vs.count(i) and cost[i] > 0) {
      set<int> nvs = vs;
      nvs.insert(i);
      dfs(nvs);
    }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    weight[a][b] = c;
    weight[b][a] = c;
    neighbors[a].push_back(b);
    neighbors[b].push_back(a);
  }
  ans = 0;
  for (int i = 0; i < n; i++) {
    dfs({i});
  }
  cout << ans << endl;
}
Problem p01423 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
const int INF = 1e8;
using namespace std;

#define int long long

int n, m;
int g[105][105];
int ans;
set<vector<bool>> memo;

void dfs(vector<bool> used) {
  if (not(memo.emplace(used)).second)
    return;
  // for(auto i : used){ cout << i << ' ' ; } cout << endl;

  vector<int> to;
  rep(i, n) {
    if (used[i])
      continue;
    bool f = true;
    rep(j, n) {
      if (not used[i])
        continue;
      if (i == j || g[j][i] == 0) {
        f = false;
        break;
      }
    }
    if (f)
      to.emplace_back(i);
  }

  for (auto i : to) {
    used[i] = true;
    dfs(used);
    used[i] = false;
  }

  int sum = 0;
  rep(i, n) {
    if (not used[i])
      continue;
    int mini = INF;
    rep(j, n) {
      if (not used[j])
        continue;
      if (g[i][j] == 0)
        continue;
      mini = min(mini, g[i][j]);
    }
    if (mini == INF)
      continue;
    sum += mini;
  }
  ans = max(ans, sum);
}

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

  vector<int> v;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a][b] = g[b][a] = c;
  }

  rep(i, n) {
    vector<bool> tmp(n, 0);
    tmp[i] = true;
    dfs(tmp);
  }
  cout << ans << endl;

  // for(auto i : memo){ for(auto j : i){ cout << j << ' '; } cout << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
const int INF = 1e8;
using namespace std;

#define int long long

int n, m;
int g[105][105];
int ans;
set<vector<bool>> memo;

void dfs(vector<bool> used) {
  if (not(memo.emplace(used)).second)
    return;
  // for(auto i : used){ cout << i << ' ' ; } cout << endl;

  vector<int> to;
  rep(i, n) {
    if (used[i])
      continue;
    bool f = true;
    rep(j, n) {
      if (not used[j])
        continue;
      if (i == j || g[j][i] == 0) {
        f = false;
        break;
      }
    }
    if (f)
      to.emplace_back(i);
  }

  for (auto i : to) {
    used[i] = true;
    dfs(used);
    used[i] = false;
  }

  int sum = 0;
  rep(i, n) {
    if (not used[i])
      continue;
    int mini = INF;
    rep(j, n) {
      if (not used[j])
        continue;
      if (g[i][j] == 0)
        continue;
      mini = min(mini, g[i][j]);
    }
    if (mini == INF)
      continue;
    sum += mini;
  }
  ans = max(ans, sum);
}

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

  vector<int> v;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a][b] = g[b][a] = c;
  }

  rep(i, n) {
    vector<bool> tmp(n, 0);
    tmp[i] = true;
    dfs(tmp);
  }
  cout << ans << endl;

  // for(auto i : memo){ for(auto j : i){ cout << j << ' '; } cout << endl; }
}
Problem p01429 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;
constexpr int MAX = 1011;

int N, M, L;
ll memo[MAX][MAX];
vector<ll> K, sum;

ll calc(int l, int r) { return (sum[K[r] + 1] - sum[K[l]]) / L; }

ll solve(int l, int r) {
  ll &res = memo[l][r];
  if (res != -1)
    return res;

  int n = max(l, r) + 1;
  if (n == N - 1) {
    return calc(l, n) + calc(r, n);
  }
  res = min(solve(n, r) + calc(l, n), solve(l, n) + calc(r, n));
  return res;
}

int main() {
  cin >> N >> M >> L;
  K.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  sort(K.begin(), K.end());

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

  sum.resize(M + 1);
  for (int i = 0; i < M; i++) {
    sum[i + 1] = sum[i] + S[i];
  }

  memset(memo, -1, sizeof(memo));
  cout << solve(0, 0) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;
constexpr int MAX = 2010;

int N, M, L;
ll memo[MAX][MAX];
vector<ll> K, sum;

ll calc(int l, int r) { return (sum[K[r] + 1] - sum[K[l]]) / L; }

ll solve(int l, int r) {
  ll &res = memo[l][r];
  if (res != -1)
    return res;

  int n = max(l, r) + 1;
  if (n == N - 1) {
    return calc(l, n) + calc(r, n);
  }
  res = min(solve(n, r) + calc(l, n), solve(l, n) + calc(r, n));
  return res;
}

int main() {
  cin >> N >> M >> L;
  K.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  sort(K.begin(), K.end());

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

  sum.resize(M + 1);
  for (int i = 0; i < M; i++) {
    sum[i + 1] = sum[i] + S[i];
  }

  memset(memo, -1, sizeof(memo));
  cout << solve(0, 0) << endl;
  return 0;
}
Problem p01434 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 10005
using namespace std;
long long n, m, s[N], t[N], p[N], d[N], ans;

int bynary_search(int x) {
  int l = 0, r = m;
  while (l < r) {
    int mid = (l + r) / 2;
    if (x <= p[mid])
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i] >> t[i];
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  d[0] = p[0], d[1] = p[1] - p[0];
  ans = 0;
  for (int i = 2; i < m; i++)
    d[i] = d[i - 2] + p[i] - p[i - 1];
  for (int i = 0; i < n; i++) {
    int r1 = bynary_search(s[i]);
    int r2 = bynary_search(t[i]);
    if (r1 == r2)
      ans += max(s[i], t[i]) - min(s[i], t[i]);
    else if (abs(r1 - r2) == 1) {
      if (s[i] < t[i])
        ans += p[r1] - s[i];
      else
        ans += s[i] - p[r1 - 1];
    } else if (s[i] < t[i]) {
      if (r2 % 2 == r1 % 2)
        ans += p[r1] - s[i] + d[r2 - 2] - d[r1] + t[i] - p[r2 - 1];
      else
        ans += p[r1] - s[i] + d[r2 - 1] - d[r1];
    } else {
      if (r2 % 2 == r1 % 2)
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2] + p[r2] - t[i];
      else
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2 - 1];
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 100005
using namespace std;
long long n, m, s[N], t[N], p[N], d[N], ans;

int bynary_search(int x) {
  int l = 0, r = m;
  while (l < r) {
    int mid = (l + r) / 2;
    if (x <= p[mid])
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i] >> t[i];
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  d[0] = p[0], d[1] = p[1] - p[0];
  ans = 0;
  for (int i = 2; i < m; i++)
    d[i] = d[i - 2] + p[i] - p[i - 1];
  for (int i = 0; i < n; i++) {
    int r1 = bynary_search(s[i]);
    int r2 = bynary_search(t[i]);
    if (r1 == r2)
      ans += max(s[i], t[i]) - min(s[i], t[i]);
    else if (abs(r1 - r2) == 1) {
      if (s[i] < t[i])
        ans += p[r1] - s[i];
      else
        ans += s[i] - p[r1 - 1];
    } else if (s[i] < t[i]) {
      if (r2 % 2 == r1 % 2)
        ans += p[r1] - s[i] + d[r2 - 2] - d[r1] + t[i] - p[r2 - 1];
      else
        ans += p[r1] - s[i] + d[r2 - 1] - d[r1];
    } else {
      if (r2 % 2 == r1 % 2)
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2] + p[r2] - t[i];
      else
        ans += s[i] - p[r1 - 1] + d[r1 - 2] - d[r2 - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01434 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, m;
int s[10010], t[10010];
int p[10010];
int a[10010];

int main() {
  cin >> n >> m, m++;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 1; i < m; i++) {
    cin >> p[i];
  }
  sort(p, p + m);

  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (lower_bound(p, p + m, s[i]) == lower_bound(p, p + m, t[i])) {
      res += abs(s[i] - t[i]);
    } else if (s[i] < t[i]) {
      int l = lower_bound(p, p + m, s[i]) - p - 1;
      int r = lower_bound(p, p + m, t[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= s[i] - p[l];
        res -= p[r] - t[i];
      } else {
        a[l]++;
        a[r]--;
        res -= s[i] - p[l];
      }
    } else {
      int l = lower_bound(p, p + m, t[i]) - p - 1;
      int r = lower_bound(p, p + m, s[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= t[i] - p[l];
        res -= p[r] - s[i];
      } else {
        a[l + 1]++;
        a[r + 1]--;
        res -= p[r] - s[i];
      }
    }
  }

  for (int i = 0; i < m; i++) {
    a[i + 2] += a[i];
    res += (long long)(p[i + 1] - p[i]) * a[i];
  }

  cout << res << endl;
}

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

int n, m;
int s[100010], t[100010];
int p[100010];
int a[100010];

int main() {
  cin >> n >> m, m++;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 1; i < m; i++) {
    cin >> p[i];
  }
  sort(p, p + m);

  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (lower_bound(p, p + m, s[i]) == lower_bound(p, p + m, t[i])) {
      res += abs(s[i] - t[i]);
    } else if (s[i] < t[i]) {
      int l = lower_bound(p, p + m, s[i]) - p - 1;
      int r = lower_bound(p, p + m, t[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= s[i] - p[l];
        res -= p[r] - t[i];
      } else {
        a[l]++;
        a[r]--;
        res -= s[i] - p[l];
      }
    } else {
      int l = lower_bound(p, p + m, t[i]) - p - 1;
      int r = lower_bound(p, p + m, s[i]) - p;
      if ((l ^ r) & 1) {
        a[l]++;
        a[r + 1]--;
        res -= t[i] - p[l];
        res -= p[r] - s[i];
      } else {
        a[l + 1]++;
        a[r + 1]--;
        res -= p[r] - s[i];
      }
    }
  }

  for (int i = 0; i < m; i++) {
    a[i + 2] += a[i];
    res += (long long)(p[i + 1] - p[i]) * a[i];
  }

  cout << res << endl;
}
Problem p01437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int todir[256];
char tochr[4];

int h, w;
long long l;
string c[110];
int vis[110][110][4];

int main() {
  for (int i = 0; i < 4; i++) {
    todir["NESW"[i]] = i;
    tochr[i] = "NESW"[i];
  }

  while (cin >> h >> w >> l, h) {
    memset(vis, -1, sizeof(vis));
    c[0] = c[h + 1] = string(w + 2, '#');
    for (int i = 1; i <= h; i++) {
      cin >> c[i];
      c[i] = "#" + c[i] + "#";
    }
    int x, y, dir;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (c[i][j] != '.' && c[i][j] != '#') {
          x = i;
          y = j;
          dir = todir[c[i][j]];
          c[i][j] = '.';
        }
      }
    }
    for (long long t = 0; t < l; t++) {
      while (true) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (c[nx][ny] == '.')
          break;
        dir = (dir + 1) % 4;
      }
      if (~vis[x][y][dir]) {
        t += (l - t - 1) / (t - vis[x][y][dir]) * (t - vis[x][y][dir]);
      }
      x += dx[dir];
      y += dy[dir];
    }
    cout << x << " " << y << " " << tochr[dir] << endl;
  }
}

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int todir[256];
char tochr[4];

int h, w;
long long l;
string c[110];
int vis[110][110][4];

int main() {
  for (int i = 0; i < 4; i++) {
    todir["NESW"[i]] = i;
    tochr[i] = "NESW"[i];
  }

  while (cin >> h >> w >> l, h) {
    memset(vis, -1, sizeof(vis));
    c[0] = c[h + 1] = string(w + 2, '#');
    for (int i = 1; i <= h; i++) {
      cin >> c[i];
      c[i] = "#" + c[i] + "#";
    }
    int x, y, dir;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (c[i][j] != '.' && c[i][j] != '#') {
          x = i;
          y = j;
          dir = todir[c[i][j]];
          c[i][j] = '.';
        }
      }
    }
    for (long long t = 0; t < l; t++) {
      while (true) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (c[nx][ny] == '.')
          break;
        dir = (dir + 1) % 4;
      }
      if (~vis[x][y][dir]) {
        t += (l - t - 1) / (t - vis[x][y][dir]) * (t - vis[x][y][dir]);
      } else
        vis[x][y][dir] = t;
      x += dx[dir];
      y += dy[dir];
    }
    cout << x << " " << y << " " << tochr[dir] << endl;
  }
}
Problem p01437 (C++)

Buggy submission (status: Time Limit Exceeded):
// 11
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  for (long long h, w, l; cin >> h >> w >> l, h | w | l;) {
    bool s[102][102] = {};
    int x, y, d;
    string ds = "NESW";
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        char c;
        cin >> c;
        s[i][j] = c != '#';
        if (c != '#' && c != '.') {
          x = j;
          y = i;
          d = ds.find(c);
        }
      }
    }
    long long p[102][102][4];
    fill(p[0][0], p[102][0], -1);
    while (l) {
      int dx[] = {0, 1, 0, -1};
      int dy[] = {-1, 0, 1, 0};
      for (;;) {
        if (p[y][x][d] != -1) {
          int m = p[y][x][d] - l;
          if (l > m) {
            l = l % m + m;
          }
        }
        p[y][x][d] = l;
        if (s[y + dy[d]][x + dx[d]])
          break;
        d = (d + 1) % 4;
      }
      y += dy[d];
      x += dx[d];
      l--;
    }
  end:
    cout << y << ' ' << x << ' ' << ds[d] << endl;
  }
  return 0;
}

Fixed submission:
// 11
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  for (long long h, w, l; cin >> h >> w >> l, h | w | l;) {
    bool s[102][102] = {};
    int x, y, d;
    string ds = "NESW";
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        char c;
        cin >> c;
        s[i][j] = c != '#';
        if (c != '#' && c != '.') {
          x = j;
          y = i;
          d = ds.find(c);
        }
      }
    }
    long long p[102][102][4];
    fill(p[0][0], p[102][0], -1);
    while (l) {
      int dx[] = {0, 1, 0, -1};
      int dy[] = {-1, 0, 1, 0};
      for (;;) {
        if (p[y][x][d] != -1) {
          long long m = p[y][x][d] - l;
          if (l > m) {
            l = l % m + m;
          }
        }
        p[y][x][d] = l;
        if (s[y + dy[d]][x + dx[d]])
          break;
        d = (d + 1) % 4;
      }
      y += dy[d];
      x += dx[d];
      l--;
    }
  end:
    cout << y << ' ' << x << ' ' << ds[d] << endl;
  }
  return 0;
}
Problem p01438 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

  int N;
  while (cin >> N, N) {
    V L(N);
    VV want(N, V(16, 0));
    rep(i, N) {
      int M;
      cin >> M >> L[i];
      rep(j, M) {
        int s, t;
        cin >> s >> t;
        rep2(k, s, t) want[i][k] = 1;
      }
    }

    V dp(1 << 16, 0);
    rep(s, 1 << 16) {
      rep(i, N) {
        bool ok = true;
        rep(j, 16) {
          if (((s >> j) & 1) && want[i][j]) {
            ok = false;
            break;
          }
        }
        if (ok) {
          int ns = s;
          rep(j, 16) { ns |= want[i][j] * (1 << j); }
          dp[ns] = max(dp[ns], dp[s] + L[i]);
        }
      }
    }

    cout << *max_element(all(dp)) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

  int N;
  while (cin >> N, N) {
    V L(N);
    VV want(N, V(16, 0));
    rep(i, N) {
      int M;
      cin >> M >> L[i];
      rep(j, M) {
        int s, t;
        cin >> s >> t;
        rep2(k, s, t) want[i][k - 6] = 1;
      }
    }

    V dp(1 << 16, 0);
    rep(s, 1 << 16) {
      rep(i, N) {
        bool ok = true;
        rep(j, 16) {
          if (((s >> j) & 1) && want[i][j]) {
            ok = false;
            break;
          }
        }
        if (ok) {
          int ns = s;
          rep(j, 16) { ns |= want[i][j] * (1 << j); }
          dp[ns] = max(dp[ns], dp[s] + L[i]);
        }
      }
    }

    cout << *max_element(all(dp)) << endl;
  }
}
Problem p01438 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

ll dp[101][65536];
int main() {
  while (1) {
    fill(dp[0], dp[100] + 65536, 0);

    int n;
    cin >> n;
    vector<ll> L(n);
    vector<vector<int>> S, E;
    loop(n, i) {
      int m;
      cin >> m >> L[i];
      vector<int> s(m), e(m);
      loop(m, j) cin >> s[j] >> e[j];
      S.push_back(s);
      E.push_back(e);
    }

    loop(n, i) {
      int mask = 0;
      loop(S[i].size(), k) {
        for (int l = S[i][k]; l < E[i][k]; l++) {
          mask ^= (1 << (l - 6));
        }
      }
      loop(1 << 16, j) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if ((j & mask) == 0) {
          dp[i + 1][j ^ mask] = max(dp[i + 1][j ^ mask], dp[i][j] + L[i]);
        }
      }
    }

    ll ans = 0;
    loop(1 << 16, i) ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

ll dp[101][65536];
int main() {
  while (1) {
    fill(dp[0], dp[100] + 65536, 0);

    int n;
    cin >> n;
    if (!n)
      break;
    vector<ll> L(n);
    vector<vector<int>> S, E;
    loop(n, i) {
      int m;
      cin >> m >> L[i];
      vector<int> s(m), e(m);
      loop(m, j) cin >> s[j] >> e[j];
      S.push_back(s);
      E.push_back(e);
    }

    loop(n, i) {
      int mask = 0;
      loop(S[i].size(), k) {
        for (int l = S[i][k]; l < E[i][k]; l++) {
          mask ^= (1 << (l - 6));
        }
      }
      loop(1 << 16, j) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if ((j & mask) == 0) {
          dp[i + 1][j ^ mask] = max(dp[i + 1][j ^ mask], dp[i][j] + L[i]);
        }
      }
    }

    ll ans = 0;
    loop(1 << 16, i) ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p01447 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ret = 0;
    while (n != 1) {
      if (n % 3 == 0) {
        n /= 3;
      } else {
        n /= 3;
        n++;
      }
      ret++;
    }
    cout << ret << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int ret = 0;
    while (n != 1) {
      if (n % 3 == 0) {
        n /= 3;
      } else {
        n /= 3;
        n++;
      }
      ret++;
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p01447 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int calc(int n) {
  return n <= 1 ? 0 : (n % 3 ? calc(n / 3 + 1) : calc(n / 3)) + 1;
}

int main() {
  for (int N; cin >> N, N; cout << calc(N) << '\n')
    ;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int calc(int n) {
  return n <= 1 ? 0 : (n % 3 ? calc(n / 3 + 1) : calc(n / 3)) + 1;
}

int main() {
  for (int N; cin >> N; cout << calc(N) << '\n')
    ;
  return 0;
}
Problem p01447 (C++)

Buggy submission (status: Time Limit Exceeded):
// Earth Invasion Diary of Miyabi-sensei
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  ll N;
  while (cin >> N, N != 0)
    cout << ceil(log((double)N) / log(3.0)) << endl;
  return 0;
}

Fixed submission:
// Earth Invasion Diary of Miyabi-sensei
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  ll N;
  cin >> N;
  cout << ceil(log((double)N) / log(3.0)) << endl;
  return 0;
}
Problem p01447 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  while (n > 1)
    n = n / 3 + min(1, n % (n / 3)), ans++;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  while (n > 1)
    n = n / 3 + min(1, n % 3), ans++;
  cout << ans << endl;
  return 0;
}
Problem p01448 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define PI 4 * atan(1)
#define INF 1e8

typedef long long ll;

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

int main() {
  int N;
  cin >> N;
  vector<int> c(N + 1, 0);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    c[a - 1]++;
    c[b]--;
  }
  int sum = 0, ans = 0;
  for (int i = 1; i <= N; i++) {
    sum += c[i];
    if (i <= sum)
      ans = i;
  }
  cout << ans << endl;
}

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

#define PI 4 * atan(1)
#define INF 1e8

typedef long long ll;

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

int main() {
  int N;
  cin >> N;
  vector<int> c(100000 + 2, 0);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    c[a - 1]++;
    c[b]--;
  }
  int sum = 0, ans = 0;
  for (int i = 1; i <= N; i++) {
    sum += c[i];
    if (i <= sum)
      ans = i;
  }
  cout << ans << endl;
}