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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define p pair<int, int>
#define P pair<p, int>
using namespace std;

int parse(string a) {
  int b = 0;
  for (char c : a)
    b = b * 10 + c - 48;
  return b;
}
int main() {
  int a, b;
  while (cin >> b >> a, a | b) {
    p G;
    vector<P> d[102];
    int MAX = 0;
    for (int e = 0; e < a; e++) {
      for (int g = 0; g < b; g++) {
        string o;
        cin >> o;
        if (o == "S")
          d[0].push_back(P(p(e, g), 0));
        else if (o == "G")
          G = p(e, g);
        else if (o != ".") {
          d[parse(o)].push_back(P(p(e, g), 0));
          MAX = max(MAX, parse(o));
        }
      }
    }
    for (int i = 1; i <= MAX; i++) {
      for (int j = 0; j < d[i].size(); j++) {
        int MIN = 1 << 29;
        for (int k = 0; k < d[i - 1].size(); k++) {
          MIN = min(MIN,
                    d[i - 1][k].second +
                        abs(d[i][j].first.first - d[i - 1][k].first.first) +
                        abs(d[i][j].first.second - d[i - 1][k].first.second));
        }
        d[i][j].second = MIN;
      }
    }
    int MINCOST = 1 << 29;
    for (P s : d[MAX]) {
      MINCOST = min(MINCOST, s.second + abs(G.first - s.first.first) +
                                 abs(G.second - s.first.second));
    }
    cout << MINCOST << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define p pair<int, int>
#define P pair<p, int>
using namespace std;

int parse(string a) {
  int b = 0;
  for (char c : a)
    b = b * 10 + c - 48;
  return b;
}
int main() {
  int a, b;
  while (cin >> b >> a, a | b) {
    p G;
    vector<P> d[20000];
    int MAX = 0;
    for (int e = 0; e < a; e++) {
      for (int g = 0; g < b; g++) {
        string o;
        cin >> o;
        if (o == "S")
          d[0].push_back(P(p(e, g), 0));
        else if (o == "G")
          G = p(e, g);
        else if (o != ".") {
          d[parse(o)].push_back(P(p(e, g), 0));
          MAX = max(MAX, parse(o));
        }
      }
    }
    for (int i = 1; i <= MAX; i++) {
      for (int j = 0; j < d[i].size(); j++) {
        int MIN = 1 << 29;
        for (int k = 0; k < d[i - 1].size(); k++) {
          MIN = min(MIN,
                    d[i - 1][k].second +
                        abs(d[i][j].first.first - d[i - 1][k].first.first) +
                        abs(d[i][j].first.second - d[i - 1][k].first.second));
        }
        d[i][j].second = MIN;
      }
    }
    int MINCOST = 1 << 29;
    for (P s : d[MAX]) {
      MINCOST = min(MINCOST, s.second + abs(G.first - s.first.first) +
                                 abs(G.second - s.first.second));
    }
    cout << MINCOST << endl;
  }
}
Problem p01401 (C++)

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second
#define pb push_back
#define mp nake_pair

typedef long long ll;
typedef vector<int> vi;

string s;
int perseInt(const string &s) {
  int ret = 0;
  rep(i, s.size()) ret = ret * 10 + (s[i] - '0');
  return ret;
}

struct P {
  int x, y;
  P(){};
  P(int x, int y) : x(x), y(y){};
  int dist(const P &p) { return abs(x - p.x) + abs(y - p.y); }
};
vector<P> ps[2600];

int main() {
  int w, h;
  while (cin >> w >> h && w && h) {
    int m = 0;
    P g;
    rep(i, 10010) ps[i].clear();
    rep(i, h) rep(j, w) {
      cin >> s;
      if (s == ".")
        continue;
      else if (s == "S")
        ps[0].pb({i, j});
      else if (s == "G")
        g = {i, j};
      else {
        int k = perseInt(s);
        ps[k].pb({i, j});
        m = max(m, k);
      }
    }
    ps[++m].pb(g);
    vector<vi> d(2600, vi(2600, 1e9));
    d[0][0] = 0;
    rep(i, m) {
      rep(j, ps[i].size()) {
        rep(k, ps[i + 1].size()) {
          d[i + 1][k] = min(d[i + 1][k], d[i][j] + ps[i][j].dist(ps[i + 1][k]));
        }
      }
    }
    cout << d[m][0] << endl;
  }
}

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second
#define pb push_back
#define mp nake_pair

typedef long long ll;
typedef vector<int> vi;

string s;
int perseInt(const string &s) {
  int ret = 0;
  rep(i, s.size()) ret = ret * 10 + (s[i] - '0');
  return ret;
}

struct P {
  int x, y;
  P(){};
  P(int x, int y) : x(x), y(y){};
  int dist(const P &p) { return abs(x - p.x) + abs(y - p.y); }
};
vector<P> ps[2600];

int main() {
  int w, h;
  while (cin >> w >> h && w && h) {
    int m = 0;
    P g;
    rep(i, 2600) ps[i].clear();
    rep(i, h) rep(j, w) {
      cin >> s;
      if (s == ".")
        continue;
      else if (s == "S")
        ps[0].pb({i, j});
      else if (s == "G")
        g = {i, j};
      else {
        int k = perseInt(s);
        ps[k].pb({i, j});
        m = max(m, k);
      }
    }
    ps[++m].pb(g);
    vector<vi> d(2600, vi(2600, 1e9));
    d[0][0] = 0;
    rep(i, m) {
      rep(j, ps[i].size()) {
        rep(k, ps[i + 1].size()) {
          d[i + 1][k] = min(d[i + 1][k], d[i][j] + ps[i][j].dist(ps[i + 1][k]));
        }
      }
    }
    cout << d[m][0] << endl;
  }
}
Problem p01403 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int out[10000100] = {0};
int dp[10000100] = {0};
#define MAX 10000100
int main() {
  out[1] = 2;
  rep(i, MAX) dp[i] = i;
  for (int i = 2; i < MAX; i++) {
    if (dp[i] == i) {
      for (int j = i; j < MAX; j += i)
        dp[j] = dp[j] / i * (i - 1);
    }
  }
  for (int i = 2; i < MAX; i++)
    out[i] = out[i - 1] + dp[i];
  int n;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    cout << out[a] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
ll out[1000100] = {0};
ll dp[1000100] = {0};
#define MAX 1000100
int main() {
  out[1] = 2;
  rep(i, MAX) dp[i] = i;
  for (int i = 2; i < MAX; i++) {
    if (dp[i] == i) {
      for (int j = i; j < MAX; j += i)
        dp[j] = dp[j] / i * (i - 1);
    }
  }
  for (int i = 2; i < MAX; i++)
    out[i] = out[i - 1] + dp[i];
  int n;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    cout << out[a] << endl;
  }
}
Problem p01403 (C++)

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

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_N 1000100

using namespace std;

typedef unsigned long long ll;

ll euler_phi(ll n) {
  ll res = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      res = res / i * (i - 1);
      for (; n % i == 0; n /= i)
        ;
    }
  }
  if (n != 1)
    res = res / n * (n - 1);
  return res;
}

ll euler[MAX_N];
ll answer[MAX_N];

void euler_phi2() {
  for (ll i = 0; i < MAX_N; i++)
    euler[i] = i;
  for (ll i = 2; i < MAX_N; i++) {
    if (euler[i] == i) {
      for (ll j = i; j < MAX_N; j += i)
        euler[j] = euler[j] / i * (i - 1);
    }
  }
}

int main() {
  euler_phi2();
  answer[1] = 2;
  REP(i, 2, MAX_N) answer[i] = answer[i - 1] + euler[i];
  int t, n;
  while (scanf("%d", &t), t)
    rep(_, t) {
      scanf("%d", &n);
      printf("%lld\n", answer[n]);
    }
  return 0;
}

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

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_N 1000100

using namespace std;

typedef unsigned long long ll;

ll euler_phi(ll n) {
  ll res = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      res = res / i * (i - 1);
      for (; n % i == 0; n /= i)
        ;
    }
  }
  if (n != 1)
    res = res / n * (n - 1);
  return res;
}

ll euler[MAX_N];
ll answer[MAX_N];

void euler_phi2() {
  for (ll i = 0; i < MAX_N; i++)
    euler[i] = i;
  for (ll i = 2; i < MAX_N; i++) {
    if (euler[i] == i) {
      for (ll j = i; j < MAX_N; j += i)
        euler[j] = euler[j] / i * (i - 1);
    }
  }
}

int main() {
  euler_phi2();
  answer[1] = 2;
  REP(i, 2, MAX_N) answer[i] = answer[i - 1] + euler[i];
  int t, n;
  scanf("%d", &t);
  rep(_, t) {
    scanf("%d", &n);
    printf("%lld\n", answer[n]);
  }
  return 0;
}
Problem p01411 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int H, N, P, M, K;

vector<int> stick(505, -1);

vector<vector<ld>> dp(505, vector<ld>(101, 0));

int main() {
  cin >> H >> N >> P >> M >> K;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    stick[a] = b;
  }

  dp[K][P] = 1;

  for (int i = H; i >= 1; i--) {
    if (stick[i] != -1) {
      vector<vector<ld>> next(505, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) {
        int st = stick[i];
        if (st == j)
          next[k][j + 1] += dp[k][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k][j];
        else
          next[k][j] += dp[k][j];
      }
      swap(dp, next);
    } else {
      vector<vector<ld>> next(101, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) { next[k][j] += dp[k][j]; }
      rep(j, 1, N) rep(k, 0, K) rep(st, 1, N - 1) {
        if (st == j)
          next[k][j + 1] += dp[k + 1][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k + 1][j];
        else
          next[k][j] += dp[k + 1][j];
      }
      swap(dp, next);
    }
  }

  ld result = 0;
  ld sum = 0;
  for (int i = 1; i <= N; i++) {
    sum += dp[0][i];
    result = max(result, dp[0][i]);
  }

  cout << fixed << setprecision(10) << result / sum << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int H, N, P, M, K;

vector<int> stick(505, -1);

vector<vector<ld>> dp(505, vector<ld>(101, 0));

int main() {
  cin >> H >> N >> P >> M >> K;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    stick[a] = b;
  }

  dp[K][P] = 1;

  for (int i = H; i >= 1; i--) {
    if (stick[i] != -1) {
      vector<vector<ld>> next(505, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) {
        int st = stick[i];
        if (st == j)
          next[k][j + 1] += dp[k][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k][j];
        else
          next[k][j] += dp[k][j];
      }
      swap(dp, next);
    } else {
      vector<vector<ld>> next(505, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) { next[k][j] += dp[k][j]; }
      rep(j, 1, N) rep(k, 0, K) rep(st, 1, N - 1) {
        if (st == j)
          next[k][j + 1] += dp[k + 1][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k + 1][j];
        else
          next[k][j] += dp[k + 1][j];
      }
      swap(dp, next);
    }
  }

  ld result = 0;
  ld sum = 0;
  for (int i = 1; i <= N; i++) {
    sum += dp[0][i];
    result = max(result, dp[0][i]);
  }

  cout << fixed << setprecision(10) << result / sum << endl;

  return 0;
}
Problem p01412 (C++)

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

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

  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  sort(A.begin(), A.end());

  vector<int> last;
  while (A.size() && A[0] == 0)
    last.push_back(0), A.erase(A.begin());
  if (A.size() && last.size() % 2)
    last.insert(last.end() - 2, A[0]), A.erase(A.begin());
  while (A.size() && A[0] == 1)
    last.push_back(1), A.erase(A.begin());
  if (A.size() >= 2 && A[A.size() - 1] == 3 && A[A.size() - 2] == 2)
    swap(A[A.size() - 2], A[A.size() - 1]);

  for (int i = 0; i < A.size(); i++)
    cout << A[i] << endl;
  for (int i = 0; i < last.size(); i++)
    cout << last[i] << endl;

  return 0;
}

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

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

  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  sort(A.begin(), A.end());

  vector<int> last;
  while (A.size() && A[0] == 0)
    last.push_back(0), A.erase(A.begin());
  if (A.size() && last.size() % 2)
    last.insert(last.end() - 1, A[0]), A.erase(A.begin());
  while (A.size() && A[0] == 1)
    last.push_back(1), A.erase(A.begin());
  if (A.size() >= 2 && A[A.size() - 1] == 3 && A[A.size() - 2] == 2)
    swap(A[A.size() - 2], A[A.size() - 1]);

  for (int i = 0; i < A.size(); i++)
    cout << A[i] << endl;
  for (int i = 0; i < last.size(); i++)
    cout << last[i] << endl;

  return 0;
}
Problem p01412 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

int n;
int a[101];
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  reverse(a, a + n);
  int all = n;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == 0)
      all--;
    else
      break;
  }
  int zero = all;
  for (int i = all - 1; i >= 0; i--) {
    if (a[i] == 1)
      all--;
    else
      break;
  }
  int sw = (n - all) - (zero - all);
  sort(a + all, a + n);
  if ((n - zero) % 2 == 1) {
    if (zero - all >= 1)
      swap(a[all + sw - 1], a[all + sw]);
    else {
      for (int i = all - 1; i + 2 < n; i += 2) {
        swap(a[i], a[i + 2]);
      }
      all--;
    }
  }
  sort(a, a + all);
  if (all >= 2 && a[all - 2] == 2 && a[all - 1] == 3)
    swap(a[all - 2], a[all - 1]);
  for (int i = 0; i < n; i++) {
    printf("%d\n", a[i]);
  }
  return 0;
}

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

int n;
int a[101];
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  reverse(a, a + n);
  int all = n;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == 0)
      all--;
    else
      break;
  }
  int zero = all;
  for (int i = all - 1; i >= 0; i--) {
    if (a[i] == 1)
      all--;
    else
      break;
  }
  int sw = (n - all) - (zero - all);
  sort(a + all, a + n);
  if ((n - zero) % 2 == 1) {
    if (zero - all >= 1)
      swap(a[all + sw - 1], a[all + sw]);
    else {
      for (int i = all - 1; i + 2 < n; i += 2) {
        swap(a[i], a[i + 2]);
      }
      all--;
    }
  }
  if (all >= 0)
    sort(a, a + all);
  if (all >= 2 && a[all - 2] == 2 && a[all - 1] == 3)
    swap(a[all - 2], a[all - 1]);
  for (int i = 0; i < n; i++) {
    printf("%d\n", a[i]);
  }
  return 0;
}
Problem p01414 (C++)

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  const string cols = "RGB";

  int N;
  cin >> N;
  vector<pair<int, int>> vs(N);
  rep(i, N) { cin >> vs[i].first >> vs[i].second; }

  vector<string> G(4);
  rep(i, 4) cin >> G[i];

  static int dist[1 << 16];
  minus(dist);
  dist[0] = 0;

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    if (~dist[(1 << 16) - 1])
      break;

    int curr = q.front();
    q.pop();

    for (auto hw : vs) {
      int h = hw.first, w = hw.second;

      for (auto col : cols)
        REP(i, -h + 1, 4) REP(j, -w + 1, 4) {
          int next = curr;
          REP(y, i, min(i + h, 4)) REP(x, j, min(j + w, 4)) {
            if (G[y][x] == col)
              next |= 1 << (y * 4 + x);
            else
              next &= ~(1 << (y * 4 + x));
          }
          if (dist[next] < 0) {
            dist[next] = dist[curr] + 1;
            q.push(next);
          }
        }
    }
  }

  cout << dist[(1 << 16) - 1] << endl;

  return 0;
}

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  const string cols = "RGB";

  int N;
  cin >> N;
  vector<pair<int, int>> vs(N);
  rep(i, N) { cin >> vs[i].first >> vs[i].second; }

  vector<string> G(4);
  rep(i, 4) cin >> G[i];

  static int dist[1 << 16];
  minus(dist);
  dist[0] = 0;

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    if (~dist[(1 << 16) - 1])
      break;

    int curr = q.front();
    q.pop();

    for (auto hw : vs) {
      int h = hw.first, w = hw.second;

      for (auto col : cols)
        REP(i, -h + 1, 4) REP(j, -w + 1, 4) {
          int next = curr;
          REP(y, max(0, i), min(i + h, 4)) REP(x, max(0, j), min(j + w, 4)) {
            if (G[y][x] == col)
              next |= 1 << (y * 4 + x);
            else
              next &= ~(1 << (y * 4 + x));
          }
          if (dist[next] < 0) {
            dist[next] = dist[curr] + 1;
            q.push(next);
          }
        }
    }
  }

  cout << dist[(1 << 16) - 1] << endl;

  return 0;
}
Problem p01414 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
vector<int> h, w;

vector<vector<int>> fie(4, vector<int>(4));

vector<int> dist(1 << 16, -1);

int main() {
  cin >> n;
  h.resize(n);
  w.resize(n);
  rep(i, 0, n - 1) cin >> h[i] >> w[i];
  rep(i, 0, 3) rep(j, 0, 3) {
    char c;
    cin >> c;
    if (c == 'R')
      fie[i][j] = 0;
    if (c == 'G')
      fie[i][j] = 1;
    if (c == 'B')
      fie[i][j] = 2;
  }
  queue<int> que;
  que.push(0);
  dist[0] = 0;

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (v == (1 << 16) - 1) {
      cout << dist[v] << endl;
      return 0;
    }
    rep(c, 0, 2) rep(s, 0, n - 1) {
      rep(i, -3, 3) rep(j, -3, 3) {
        int u = v;
        rep(x, max(i, 0), min(i + h[s] - 1, 3))
            rep(y, max(j, 0), min(j + w[s] - 1, 3)) {
          int index = (x * 4 + y);
          if (u & (1 << index)) {
            if (fie[x][y] != c)
              u ^= (1 << index);
          } else {
            if (fie[x][y] == c)
              u ^= (1 << index);
          }
        }
        if (dist[u] == -1) {
          dist[u] = dist[v] + 1;
          que.push(u);
        }
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
vector<int> h, w;

vector<vector<int>> fie(4, vector<int>(4));

vector<int> dist(1 << 16, -1);

int main() {
  cin >> n;
  h.resize(n);
  w.resize(n);
  rep(i, 0, n - 1) cin >> h[i] >> w[i];
  rep(i, 0, 3) rep(j, 0, 3) {
    char c;
    cin >> c;
    if (c == 'R')
      fie[i][j] = 0;
    if (c == 'G')
      fie[i][j] = 1;
    if (c == 'B')
      fie[i][j] = 2;
  }
  queue<int> que;
  que.push(0);
  dist[0] = 0;

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (v == (1 << 16) - 1) {
      cout << dist[v] << endl;
      return 0;
    }
    rep(c, 0, 2) rep(s, 0, n - 1) {
      rep(i, -h[s] + 1, 3) rep(j, -w[s] + 1, 3) {
        int u = v;
        rep(x, max(i, 0), min(i + h[s] - 1, 3))
            rep(y, max(j, 0), min(j + w[s] - 1, 3)) {
          int index = (x * 4 + y);
          if (u & (1 << index)) {
            if (fie[x][y] != c)
              u ^= (1 << index);
          } else {
            if (fie[x][y] == c)
              u ^= (1 << index);
          }
        }
        if (dist[u] == -1) {
          dist[u] = dist[v] + 1;
          que.push(u);
        }
      }
    }
  }
}
Problem p01414 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
int p[20];
int q[20];
char str[5][5];
int bfs[1 << 16];
char col[5] = "RGB";
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d%d", p + i, q + i);
  }
  for (int i = 0; i < 4; i++)
    scanf("%s", str[i]);
  for (int i = 0; i < (1 << 16); i++)
    bfs[i] = -1;
  bfs[0] = 0;
  queue<int> Q;
  Q.push(0);
  while (Q.size()) {
    if (~bfs[(1 << 16) - 1])
      break;
    int at = Q.front();
    Q.pop();
    for (int i = 0; i < a; i++) {
      for (int j = -p[i] + 1; j < 4; j++) {
        for (int k = -q[i] + 1; k < 4; k++) {
          for (int c = 0; c < 3; c++) {
            int to = at;
            for (int l = 0; l < p[i]; l++)
              for (int m = 0; m < q[i]; m++) {
                if (j + l < 0 || k + m < 0 || j + l > 3 || k + m > 3)
                  continue;
                if (str[j + l][k + m] == col[c])
                  to |= (1 << ((j + l) * 4 + k + m));
                else
                  to &= (~(1 << ((j + l) * 4 + k + m)));
              }
            if (!~bfs[to]) {
              bfs[to] = bfs[at] + 1;
              Q.push(to);
            }
          }
        }
      }
    }
  }
  printf("%d\n", bfs[(1 << 16) - 1]);
}

Fixed submission:
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
int p[20];
int q[20];
char str[5][5];
int bfs[1 << 16];
char col[5] = "RGB";
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d%d", p + i, q + i);
  }
  for (int i = 0; i < 4; i++)
    scanf("%s", str[i]);
  for (int i = 0; i < (1 << 16); i++)
    bfs[i] = -1;
  bfs[0] = 0;
  queue<int> Q;
  Q.push(0);
  while (Q.size()) {
    if (~bfs[(1 << 16) - 1])
      break;
    int at = Q.front();
    Q.pop();
    for (int i = 0; i < a; i++) {
      for (int j = -p[i] + 1; j < 4; j++) {
        for (int k = -q[i] + 1; k < 4; k++) {
          for (int c = 0; c < 3; c++) {
            int to = at;
            for (int l = max(0, -j); l < min(p[i], 4 - j); l++)
              for (int m = max(0, -k); m < min(q[i], 4 - k); m++) {
                // if(j+l<0||k+m<0||j+l>3||k+m>3)continue;
                if (str[j + l][k + m] == col[c])
                  to |= (1 << ((j + l) * 4 + k + m));
                else
                  to &= (~(1 << ((j + l) * 4 + k + m)));
              }
            if (!~bfs[to]) {
              bfs[to] = bfs[at] + 1;
              Q.push(to);
            }
          }
        }
      }
    }
  }
  printf("%d\n", bfs[(1 << 16) - 1]);
}
Problem p01415 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int main(void) {
  int n, k, t, u, v, l;
  char d[1000] = {};
  scanf("%d%d%d%d%d%d", &n, &k, &t, &u, &v, &l);
  for (int i = 0; i < n; i++) {
    int t;
    scanf("%d", &t);
    d[t] = 1;
  }

  double res = 0.0, rem = 0.0;
  int carrot = 0;
  for (int i = 0; i < l; i++) {
    if (d[i])
      carrot++;
    if (carrot > k)
      carrot = k, rem = 1.0 * t;

    double now = 0.0;
    while (now < 1.0) {
      if (rem == 0.0) {
        if (carrot) {
          carrot--;
          rem = 1.0 * t;
        } else {
          res += (1.0 - now) / u;
          now = 1.0;
        }
      }

      if (rem > (1.0 - now) / v) {
        res += (1.0 - now) / v;
        rem -= (1.0 - now) / v;
        now = 1.0;
      } else {
        res += rem;
        now += rem * v;
        rem = 0.0;
      }
    }
  }
  printf("%f\n", res);

  return 0;
}

Fixed submission:
#include <cstdio>

int main(void) {
  int n, k, t, u, v, l;
  char d[100000] = {};
  scanf("%d%d%d%d%d%d", &n, &k, &t, &u, &v, &l);
  for (int i = 0; i < n; i++) {
    int t;
    scanf("%d", &t);
    d[t] = 1;
  }

  double res = 0.0, rem = 0.0;
  int carrot = 0;
  for (int i = 0; i < l; i++) {
    if (d[i])
      carrot++;
    if (carrot > k)
      carrot = k, rem = 1.0 * t;

    double now = 0.0;
    while (now < 1.0) {
      if (rem == 0.0) {
        if (carrot) {
          carrot--;
          rem = 1.0 * t;
        } else {
          res += (1.0 - now) / u;
          now = 1.0;
        }
      }

      if (rem > (1.0 - now) / v) {
        res += (1.0 - now) / v;
        rem -= (1.0 - now) / v;
        now = 1.0;
      } else {
        res += rem;
        now += rem * v;
        rem = 0.0;
      }
    }
  }
  printf("%f\n", res);

  return 0;
}
Problem p01415 (C++)

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

enum State { Carrot, End };

typedef struct Event {
  int x;
  State state;

  bool operator<(const Event &right) const { return x > right.x; }
} Event;

int N, K, T, U, V, L;
vector<int> D;

int main() {
  cout.precision(16);
  cout.setf(ios::fixed);

  cin >> N >> K >> T >> U >> V >> L;
  D.resize(N);
  for (auto &v : D)
    cin >> v;

  priority_queue<Event> que;
  for (auto v : D)
    que.push(Event({v, Carrot}));

  vector<bool> zone(L);
  int count = 0;
  while (!que.empty()) {
    auto event = que.top();
    que.pop();
    if (event.state == Carrot) {
      if (zone[event.x]) {
        if (count < K) {
          count++;
        } else {
          que.push({event.x + T * V, End});
          for (int x = min(L, event.x + T * V) - 1; event.x <= x && !zone[x];
               x--)
            zone[x] = true;
        }
      } else {
        que.push({event.x + T * V, End});
        for (int x = min(L, event.x + T * V) - 1; event.x <= x && !zone[x]; x--)
          zone[x] = true;
      }
    } else {
      if (count && !zone[event.x]) {
        count--;
        que.push({event.x + T * V, End});
        for (int x = min(L, event.x + T * V) - 1; event.x <= x && !zone[x]; x--)
          zone[x] = true;
      }
    }
  }

  double span = 0;
  for (int i = 0; i < L; i++)
    span += zone[i];

  cout << (L - span) / U + span / V << endl;

  return 0;
}

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

enum State { Carrot, End };

typedef struct Event {
  int x;
  State state;

  bool operator<(const Event &right) const { return x > right.x; }
} Event;

int N, K, T, U, V, L;
vector<int> D;

int main() {
  cout.precision(16);
  cout.setf(ios::fixed);

  cin >> N >> K >> T >> U >> V >> L;
  D.resize(N);
  for (auto &v : D)
    cin >> v;

  priority_queue<Event> que;
  for (auto v : D)
    que.push(Event({v, Carrot}));

  vector<bool> zone(L);
  int count = 0;
  while (!que.empty()) {
    auto event = que.top();
    que.pop();
    if (event.state == Carrot) {
      if (zone[event.x]) {
        if (count < K) {
          count++;
        } else {
          que.push({event.x + T * V, End});
          for (int x = min(L, event.x + T * V) - 1; event.x <= x && !zone[x];
               x--)
            zone[x] = true;
        }
      } else {
        que.push({event.x + T * V, End});
        for (int x = min(L, event.x + T * V) - 1; event.x <= x && !zone[x]; x--)
          zone[x] = true;
      }
    } else {
      if (count && event.x < L && !zone[event.x]) {
        count--;
        que.push({event.x + T * V, End});
        for (int x = min(L, event.x + T * V) - 1; event.x <= x && !zone[x]; x--)
          zone[x] = true;
      }
    }
  }

  double span = 0;
  for (int i = 0; i < L; i++)
    span += zone[i];

  cout << (L - span) / U + span / V << endl;

  return 0;
}
Problem p01417 (C++)

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

using namespace std;

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

template <typename T> inline T sqr(T x) { return x * x; }
template <typename T> inline void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

int N, M;

int next_combination(int x) {
  int l = x & -x;
  int m = x + l;
  int ls = x & ~m;
  return m | ((ls / l) >> 1);
}

double solve(vector<double> &L, vector<double> &a, vector<double> &b) {
  double res = 0.0;
  for (int S = (1 << M) - 1; S < (1 << N); S = next_combination(S)) {
    double total = 0.0;
    rep(i, N) if (S >> i & 1) {
      rep(j, i) if (S >> j & 1) {
        total += sqr(L[i] - L[j]) + sqr(a[i] - a[j]) + sqr(b[i] - b[j]);
      }
    }
    chmax(res, total);
  }
  return res;
}

int main() {
  while (cin >> N >> M) {
    vector<double> L(N), a(N), b(N);
    rep(i, N) cin >> L[i] >> a[i] >> b[i];
    cout << fixed << setprecision(16) << solve(L, a, b) << '\n';
  }
  return 0;
}

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

using namespace std;

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

template <typename T> inline T sqr(T x) { return x * x; }
template <typename T> inline void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

int N, M;

int next_combination(int x) {
  int l = x & -x;
  int m = x + l;
  int ls = x & ~m;
  return m | ((ls / l) >> 1);
}

double solve(vector<double> &L, vector<double> &a, vector<double> &b) {
  if (M == 0)
    return 0.0;
  double res = 0.0;
  for (int S = (1 << M) - 1; S < (1 << N); S = next_combination(S)) {
    double total = 0.0;
    rep(i, N) if (S >> i & 1) {
      rep(j, i) if (S >> j & 1) {
        total += sqr(L[i] - L[j]) + sqr(a[i] - a[j]) + sqr(b[i] - b[j]);
      }
    }
    chmax(res, total);
  }
  return res;
}

int main() {
  while (cin >> N >> M) {
    vector<double> L(N), a(N), b(N);
    rep(i, N) cin >> L[i] >> a[i] >> b[i];
    cout << fixed << setprecision(16) << solve(L, a, b) << '\n';
  }
  return 0;
}
Problem p01417 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  int N, M;
  cin >> N >> M;
  vector<double> L(N), a(N), b(N);
  for (int i = 0; i < N; i++)
    cin >> L[i] >> a[i] >> b[i];
  vector<vector<double>> d(N, vector<double>(N));
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++) {
      double dL = L[i] - L[j], da = a[i] - a[j], db = b[i] - b[j];
      d[i][j] = dL * dL + da * da + db * db;
    }
  double maxi = 0;
  for (int S = (1 << M) - 1; S < (1 << N);) {
    double sum = 0;
    for (int i = 0; i < N; i++)
      for (int j = i + 1; j < N; j++)
        if ((S & (1 << i)) && (S & (1 << j)))
          sum += d[i][j];
    maxi = max(maxi, sum);
    int x = S & -S, y = S + x;
    S = ((S & ~y) / x >> 1) | y;
  }
  printf("%.10f\n", maxi);
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  int N, M;
  cin >> N >> M;
  if (M == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<double> L(N), a(N), b(N);
  for (int i = 0; i < N; i++)
    cin >> L[i] >> a[i] >> b[i];
  vector<vector<double>> d(N, vector<double>(N));
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++) {
      double dL = L[i] - L[j], da = a[i] - a[j], db = b[i] - b[j];
      d[i][j] = dL * dL + da * da + db * db;
    }
  double maxi = 0;
  for (int S = (1 << M) - 1; S < (1 << N);) {
    double sum = 0;
    for (int i = 0; i < N; i++)
      for (int j = i + 1; j < N; j++)
        if ((S & (1 << i)) && (S & (1 << j)))
          sum += d[i][j];
    maxi = max(maxi, sum);
    int x = S & -S, y = S + x;
    S = ((S & ~y) / x >> 1) | y;
  }
  printf("%.10f\n", maxi);
}
Problem p01417 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;

int n, m;
double ans = 0;
double x[21], y[21], z[21];

double f(int i, int j) {
  return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) +
         (z[i] - z[j]) * (z[i] - z[j]);
}

void dfs(int p, vector<int> &v) {
  if (p == n) {
    if (v.size() == m) {
      double sum = 0.0;
      for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
          sum += f(v[i], v[j]);
        }
      }
      ans = max(ans, sum);
      return;
    }
  } else {
    dfs(p + 1, v);
    v.push_back(p);
    dfs(p + 1, v);
    v.pop_back();
  }
}

bool solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> z[i];
  }

  ans = 0.0;
  vector<int> v;
  dfs(0, v);
  cout << ans << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  solve();

  return 0;
}


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;

int n, m;
double ans = 0;
double x[21], y[21], z[21];

double f(int i, int j) {
  return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) +
         (z[i] - z[j]) * (z[i] - z[j]);
}

void dfs(int p, vector<int> &v) {
  if (p == n) {
    if (v.size() == m) {
      double sum = 0.0;
      for (int i = 0; i < v.size(); i++) { //
        for (int j = 0; j < i; j++) {      //
          sum += f(v[i], v[j]);
        }
      }
      ans = max(ans, sum);
      return;
    }
  } else {
    dfs(p + 1, v);
    v.push_back(p);
    dfs(p + 1, v);
    v.pop_back();
  }
}

bool solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> z[i];
  }

  ans = 0.0;
  vector<int> v;
  dfs(0, v);
  cout << ans << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  solve();

  return 0;
}
Problem p01417 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;

double a[20], b[20], c[20];
double dist[20][32];
double squ(double x) { return x * x; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lf %lf %lf", a + i, b + i, c + i);
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      dist[i][j] = squ(a[i] - a[j]) + squ(b[i] - b[j]) + squ(c[i] - c[j]);
  double ans = 0;
  int comb = (1 << m) - 1;
  while (comb < 1 << n) {
    double sum = 0;
    for (int i = 0; i < n; i++) {
      if ((comb >> i & 1) ^ 1)
        continue;
      for (int j = i + 1; j < n; j++) {
        if (comb >> j & 1)
          sum += dist[i][j];
      }
    }
    ans = max(ans, sum);
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  printf("%.5f\n", ans);
  return 0;
}

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

double a[20], b[20], c[20];
double dist[20][32];
double squ(double x) { return x * x; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lf %lf %lf", a + i, b + i, c + i);
  }
  if (m == 0) {
    printf("%.5f\n", 0.);
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      dist[i][j] = squ(a[i] - a[j]) + squ(b[i] - b[j]) + squ(c[i] - c[j]);
  double ans = 0;
  int comb = (1 << m) - 1;
  while (comb < 1 << n) {
    double sum = 0;
    for (int i = 0; i < n; i++) {
      if ((comb >> i & 1) ^ 1)
        continue;
      for (int j = i + 1; j < n; j++) {
        if (comb >> j & 1)
          sum += dist[i][j];
      }
    }
    ans = max(ans, sum);
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  printf("%.5f\n", ans);
  return 0;
}
Problem p01418 (C++)

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

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

double solve(int k, double r, double l) {
  if (t + e < r || l < t - e) {
    return 0.0;
  }

  double h = (r + l) / 2;

  if (k == 0) {
    if (t - e <= h && h <= t + e) {
      return 1.0;
    } else
      return 0.0;
  }

  double k1 = 1.0 - p, k2 = p;
  if (t > h) {
    swap(k1, k2);
  }

  return k1 * solve(k - 1, r, h) + k2 * solve(k - 1, h, l);
}

int main() {
  cin >> k >> r >> l >> p >> e >> t;
  printf("%.20f\n", solve(k, r, l));
}

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

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

double solve(int k, double r, double l) {
  if (t + e < r || l < t - e) {
    return 0.0;
  }
  if (t - e <= r && l <= t + e) {
    return 1.0;
  }

  double h = (r + l) / 2;

  if (k == 0) {
    if (t - e <= h && h <= t + e) {
      return 1.0;
    } else
      return 0.0;
  }

  double k1 = 1.0 - p, k2 = p;
  if (t > h) {
    swap(k1, k2);
  }

  return k1 * solve(k - 1, r, h) + k2 * solve(k - 1, h, l);
}

int main() {
  cin >> k >> r >> l >> p >> e >> t;
  printf("%.20f\n", solve(k, r, l));
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> pii;

double p, e, t;

double solve(int k, double l, double r) {
  double c = (l + r) / 2.0;
  if (k == 0) {
    return (t - e <= c && c <= t + e) ? 1.0 : 0.0;
  }
  if (c >= t) {
    return (1.0 - p) * solve(k - 1, c, r) + p * solve(k - 1, l, c);
  } else {
    return (1.0 - p) * solve(k - 1, l, c) + p * solve(k - 1, c, r);
  }
}

int main() {
  int k, r, l;
  cin >> k >> r >> l;
  cin >> p >> e >> t;
  double res = solve(k, l, r);
  cout << fixed << setprecision(10) << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> pii;

double p, e, t;

double solve(int k, double l, double r) {
  double c = (l + r) / 2.0;
  if (k == 0) {
    return (t - e <= c && c <= t + e) ? 1.0 : 0.0;
  }
  if (t - e <= r && l <= t + e)
    return 1.0;
  if (l < t - e || t + e < r)
    return 0.0;

  if (c >= t) {
    return (1.0 - p) * solve(k - 1, c, r) + p * solve(k - 1, l, c);
  } else {
    return (1.0 - p) * solve(k - 1, l, c) + p * solve(k - 1, c, r);
  }
}

int main() {
  int k, r, l;
  cin >> k >> r >> l;
  cin >> p >> e >> t;
  double res = solve(k, l, r);
  cout << fixed << setprecision(10) << res << endl;
  return 0;
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

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

double rec(int num, double r, double l) {
  if (t < r - e || l + e < t)
    return 0;
  double tt = (r + l) / 2;
  if (num == k) {
    if (abs(t - tt) < e)
      return 1;
    else
      return 0;
  }
  if (tt < t) {
    return p * rec(num + 1, r, tt) + (1.0 - p) * rec(num + 1, tt, l);
  } else {
    return (1.0 - p) * rec(num + 1, r, tt) + p * rec(num + 1, tt, l);
  }
}

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

  printf("%.12f\n", rec(0, r, l));
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

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

double rec(int num, double r, double l) {
  if (t < r - e || l + e < t)
    return 0;
  if (t - e < r && l < t + e)
    return 1;
  double tt = (r + l) / 2;
  if (num == k) {
    if (abs(t - tt) < e)
      return 1;
    else
      return 0;
  }
  if (tt < t) {
    return p * rec(num + 1, r, tt) + (1.0 - p) * rec(num + 1, tt, l);
  } else {
    return (1.0 - p) * rec(num + 1, r, tt) + p * rec(num + 1, tt, l);
  }
}

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

  printf("%.12f\n", rec(0, r, l));
}
Problem p01418 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;
using ll = long long;

int K;
long double P;
long double rec(const int n, const long double l, const long double r,
                const long double tl, const long double tr,
                const long double t) {
  const long double mid = (l + r) / 2;
  if (n == K) {
    return (tl <= mid and mid <= tr) ? 1 : 0;
  }
  if (tr <= mid) {
    return (1 - P) * rec(n + 1, l, mid, tl, tr, t);
  } else if (mid <= tl) {
    return (1 - P) * rec(n + 1, mid, r, tl, tr, t);
  } else {
    return ((t <= mid) ? 1 - P : P) * rec(n + 1, l, mid, tl, mid, t) +
           ((t > mid) ? 1 - P : P) * rec(n + 1, mid, r, mid, tr, t);
  }
}

int main() {
  cin >> K;
  long double R, L, E, T;
  cin >> R >> L >> P >> E >> T;
  const long double TL = T - E;
  const long double TR = T + E;
  cout << fixed << setprecision(15) << rec(0, R, L, TL, TR, T) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;
using ll = long long;

int K;
long double P;
long double rec(const int n, const long double l, const long double r,
                const long double tl, const long double tr,
                const long double t) {
  const long double mid = (l + r) / 2;
  if (n == K) {
    return (tl <= mid and mid <= tr) ? 1 : 0;
  }
  if (tl <= l and r <= tr) {
    return 1;
  } else if (tr < l) {
    return 0;
  } else if (tl > r) {
    return 0;
  }
  if (tr <= mid) {
    return (1 - P) * rec(n + 1, l, mid, tl, tr, t);
  } else if (mid <= tl) {
    return (1 - P) * rec(n + 1, mid, r, tl, tr, t);
  } else {
    return ((t <= mid) ? 1 - P : P) * rec(n + 1, l, mid, tl, mid, t) +
           ((t > mid) ? 1 - P : P) * rec(n + 1, mid, r, mid, tr, t);
  }
}

int main() {
  cin >> K;
  long double R, L, E, T;
  cin >> R >> L >> P >> E >> T;
  const long double TL = T - E;
  const long double TR = T + E;
  cout << fixed << setprecision(15) << rec(0, R, L, TL, TR, T) << endl;

  return 0;
}
Problem p01418 (C++)

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

double dfs(int k, double L, double R) {
  double H = (R + L) / 2, PP = H >= T ? 1 - P : P;
  if (k == 0 || (abs(L - T) <= E && abs(R - T) <= E))
    return abs(H - T) <= E;
  return PP * dfs(k - 1, H, R) + (1 - PP) * dfs(k - 1, L, H);
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
double P, E, T;

double dfs(int k, double L, double R) {
  double H = (R + L) / 2, PP = H >= T ? 1 - P : P;
  if (k == 0 || (abs(L - T) <= E && abs(R - T) <= E))
    return abs(H - T) <= E;
  if (T < R - E || L + E < T)
    return 0;
  return PP * dfs(k - 1, H, R) + (1 - PP) * dfs(k - 1, L, H);
}

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