alexjercan/bugnet huggingface dataset:
Problem p01701 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
void func(string str, int *au, int *al, int i);
int yakubun(int a, int b);

int main() {
  string str;
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    int l = str.length(), i;
    int ans_upper = 0, ans_lower = 1;
    if (str[l - 1] == 'h') {
      ans_upper = 0;
      i = l - 6;
    } else if (str[l - 1] == 't') {
      ans_upper = 90;
      i = l - 5;
    }
    func(str, &ans_upper, &ans_lower, i);
    int gcd = yakubun(ans_upper, ans_lower);
    ans_upper = ans_upper / gcd;
    ans_lower = ans_lower / gcd;
    if (ans_lower == 1)
      cout << ans_upper << endl;
    else
      cout << ans_upper << '/' << ans_lower << endl;
  }
  return 0;
}

void func(string str, int *au, int *al, int i) {
  if (i < 0)
    return;
  else if (str[i] == 'h') {
    *al = *al * 2;
    *au = *au * 2 - 90;
    func(str, au, al, i - 5);
  } else if (str[i] == 't') {
    *al = *al * 2;
    *au = *au * 2 + 90;
    func(str, au, al, i - 4);
  }
  return;
}

int yakubun(int a, int b) {
  if (b == 0)
    return a;
  else {
    int r = a % b;
    return yakubun(b, r);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
void func(string str, int *au, int *al, int i);

int main() {
  string str;
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    int l = str.length(), i;
    int ans_upper = 0, ans_lower = 1;
    if (str[l - 1] == 'h') {
      ans_upper = 0;
      i = l - 6;
    } else if (str[l - 1] == 't') {
      ans_upper = 90;
      i = l - 5;
    }
    func(str, &ans_upper, &ans_lower, i);
    while (1) {
      if (ans_upper % 2 == 0) {
        ans_upper = ans_upper / 2;
        ans_lower = ans_lower / 2;
      } else
        break;
    }
    if (ans_lower == 1)
      cout << ans_upper << endl;
    else
      cout << ans_upper << '/' << ans_lower << endl;
  }
  return 0;
}

void func(string str, int *au, int *al, int i) {
  if (i < 0)
    return;
  else if (str[i] == 'h') {
    *al = *al * 2;
    *au = *au * 2 - 90;
    func(str, au, al, i - 5);
  } else if (str[i] == 't') {
    *al = *al * 2;
    *au = *au * 2 + 90;
    func(str, au, al, i - 4);
  }
  return;
}
Problem p01702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M, Q;
  while (cin >> N >> M >> Q, N || M || Q) {

    vector<string> S(2 * Q + 1), B(2 * Q + 1);
    vector<vector<int>> D(2 * Q + 1, vector<int>(M));

    string str;

    for (int i = 0; i < M; i++) {
      B[0] += '0';
      str += '1';
    }

    for (int i = 1; i <= Q; i++) {
      cin >> S[i - 1] >> B[i];

      for (int j = 0; j < N; j++) {
        S[i + Q - 1] += (S[i - 1][j] == '0' ? '1' : '0');
      }

      for (int j = 0; j < M; j++) {
        if (B[i - 1][j] != B[i][j]) {
          D[i - 1][j] = 1;
        } else {
          D[i + Q - 1][j] = 1;
        }
      }
    }

    vector<set<int>> ans(M);
    if (Q == 0) {
      if (N == 1) {
        for (auto &i : ans)
          i.insert(0);
      }
    } else {
      for (int i = 0; i < N; i++) {
        auto tmp = str;

        for (int j = 0; j < 2 * Q; j++) {
          if (S[j][i] == '0')
            continue;
          for (int k = 0; k < M; k++) {
            if (D[j][k] == 0)
              tmp[k] = '0';
          }
        }

        for (int j = 0; j < M; j++) {
          if (tmp[j] == '1')
            ans[j].insert(i);
        }
      }
    }

    for (int i = 0; i < M; i++) {
      if (ans[i].size() != 1)
        cout << '?';
      else if (*(ans[i].begin()) < 10)
        cout << *(ans[i].begin());
      else
        cout << (char)('A' + *(ans[i].begin()) - 10);
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N, M, Q;
  while (cin >> N >> M >> Q, N || M || Q) {

    vector<string> S(2 * Q + 1), B(2 * Q + 1);
    vector<vector<int>> D(2 * Q + 1, vector<int>(M));

    B[0] = "000000000000000000000000000000000000";
    for (int i = 1; i <= Q; i++) {
      cin >> S[i - 1] >> B[i];

      for (int j = 0; j < N; j++) {
        S[i + Q - 1] += (S[i - 1][j] == '0' ? '1' : '0');
      }

      for (int j = 0; j < M; j++) {
        if (B[i - 1][j] != B[i][j]) {
          D[i - 1][j] = 1;
        } else {
          D[i + Q - 1][j] = 1;
        }
      }
    }
    vector<int> ans(M, -1);
    if (Q == 0) {
      if (N == 1) {
        for (auto &i : ans)
          i = 0;
      }
    } else {
      for (int i = 0; i < N; i++) {
        string tmp = "111111111111111111111111111111111111";
        for (int j = 0; j < 2 * Q; j++) {
          if (S[j][i] == '0')
            continue;
          for (int k = 0; k < M; k++) {
            if (D[j][k] == 0)
              tmp[k] = '0';
          }
        }
        for (int j = 0; j < M; j++) {
          if (tmp[j] == '1')
            ans[j] = i;
        }
      }
    }

    for (int i = 0; i < M; i++) {
      if (ans[i] < 0)
        cout << '?';
      else if (ans[i] < 10)
        cout << ans[i];
      else
        cout << 'A';
    }
    cout << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <iostream>
using namespace std;
typedef long long ll;
int N, M, Q;
ll s[4000];
string t[4000];

ll func(string str) {
  ll res = 0;
  for (int i = 0; i < (int)str.size(); i++)
    if (str[i] == '1')
      res += (1LL << i);
  return res;
}

int main() {
  char change[36];
  change[0] = '0';
  for (int i = 1; i < 10; i++)
    change[i] = change[i - 1] + 1;
  change[10] = 'A';
  for (int i = 11; i < 36; i++)
    change[i] = change[i - 1] + 1;

  while (1) {
    cin >> N >> M >> Q;
    if (N == 0 && M == 0 && Q == 0)
      break;
    for (int i = 0; i < Q; i++) {
      string str;
      cin >> str >> t[i];
      s[i] = func(str);
      if (i)
        s[i] = s[i] ^ s[i - 1];
    }

    for (int i = 0; i < M; i++) {
      ll f = (1LL << N) - 1;
      for (int j = 0; j < Q; j++) {
        if (t[j][i] == '1') {
          f = f & s[j];
        } else {
          f = f & (~s[j]);
        }
      }
      assert(__builtin_popcountll(f) != 0);
      if (__builtin_popcountll(f) == 1) {
        int ans = 0;
        for (ans = 0; ans < N; ans++) {
          if (f == (1LL << ans))
            break;
        }
        cout << change[ans];
      } else {
        cout << '?';
      }
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
using namespace std;
typedef long long ll;
int N, M, Q;
ll s[4000], t[4000];

ll func(string str) {
  ll res = 0;
  for (int i = 0; i < (int)str.size(); i++)
    if (str[i] == '1')
      res += (1LL << i);
  return res;
}

int main() {
  char change[36];
  change[0] = '0';
  for (int i = 1; i < 10; i++)
    change[i] = change[i - 1] + 1;
  change[10] = 'A';
  for (int i = 11; i < 36; i++)
    change[i] = change[i - 1] + 1;

  while (1) {
    cin >> N >> M >> Q;
    if (N == 0 && M == 0 && Q == 0)
      break;
    for (int i = 0; i < Q; i++) {
      string str, str2;
      cin >> str >> str2;
      s[i] = func(str);
      t[i] = func(str2);
      if (i)
        s[i] = s[i] ^ s[i - 1];
    }

    for (int i = 0; i < M; i++) {
      ll f = (1LL << N) - 1;
      for (int j = 0; j < Q; j++) {
        if ((t[j] >> i) & 1) {
          f = f & s[j];
        } else {
          f = f & (~s[j]);
        }
      }
      assert(__builtin_popcountll(f) != 0);
      if (__builtin_popcountll(f) == 1) {
        int ans = 0;
        for (ans = 0; ans < N; ans++) {
          if (f == (1LL << ans))
            break;
        }
        cout << change[ans];
      } else {
        cout << '?';
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p01702 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;
using lint = long long;

inline char decode(int i) { return i + (i < 10 ? '0' : 'A' - 10); }

bool solve() {
  int N, M, Q;
  cin >> N >> M >> Q;
  if (N == 0)
    return false;

  vector<lint> pos(M, (1LL << N) - 1);
  lint light = 0;

  for (int q = 0; q < Q; ++q) {
    string S;
    cin >> S;
    for (int i = 0; i < N; ++i) {
      if (S[i] == '1')
        light ^= (1LL << i);
    }

    string B;
    cin >> B;
    for (int i = 0; i < M; ++i) {
      if (B[i] == '0') {
        pos[i] &= (~light & ((1LL << N) - 1));
      } else {
        pos[i] &= light;
      }
    }
  }

  for (auto pat : pos) {
    vector<int> ans;
    for (int i = 0; i < N; ++i) {
      if ((pat >> i) & 1)
        ans.push_back(i);
    }
    if (ans.size() > 1) {
      cout << '?';
    } else {
      cout << decode(ans[0]);
    }
  }
  cout << endl;
  return true;
}

int main() {
  while (solve()) {
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
using lint = long long;

inline char decode(int i) { return i + (i < 10 ? '0' : 'A' - 10); }

bool solve() {
  int N, M, Q;
  cin >> N >> M >> Q;
  if (N == 0)
    return false;

  vector<lint> pos(M, (1 << N) - 1);
  lint light = 0;

  for (int q = 0; q < Q; ++q) {
    string S;
    cin >> S;
    for (int i = 0; i < N; ++i) {
      if (S[i] == '1')
        light ^= (1 << i);
    }

    string B;
    cin >> B;
    for (int i = 0; i < M; ++i) {
      if (B[i] == '0') {
        pos[i] &= (~light & ((1 << N) - 1));
      } else {
        pos[i] &= light;
      }
    }
  }

  for (auto pat : pos) {
    vector<int> ans;
    for (int i = 0; i < N; ++i) {
      if ((pat >> i) & 1)
        ans.push_back(i);
    }
    if (ans.size() > 1) {
      cout << '?';
    } else {
      cout << decode(ans[0]);
    }
  }
  cout << endl;
  return true;
}

int main() {
  while (solve()) {
  }
  return 0;
}
Problem p01702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (1) {
    char c, ans[1001];
    int n, m, q, cnt = 0, f[37][1001], s[37] = {}, b[1001];
    cin >> n >> m >> q;
    if (n + m + q == 0)
      break;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        f[i][j] = 1;

    while (cnt < q) {
      cnt++;
      for (int i = 0; i < n; i++)
        cin >> c, s[i] = (s[i] + (c - '0')) % 2;
      for (int i = 0; i < m; i++)
        cin >> c, b[i] = c - '0';

      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
          if (f[i][j] == 2)
            continue;
          else if (s[i] == b[j])
            f[i][j] = 1;
          else
            f[i][j] = 2;
        }
    }

    for (int j = 0; j < m; j++) {
      int flag = 0;
      for (int i = 0; i < n; i++) {
        if (f[i][j] == 1 && flag == 0) {
          flag = 1;
          if (i >= 0 && i <= 9)
            ans[j] = i + '0';
          else
            ans[j] = i - 10 + 'A';
        } else if (flag == 1 && f[i][j] == 1)
          ans[j] = '?';
      }
    }

    for (int i = 0; i < m; i++)
      cout << ans[i];
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  while (1) {
    char c, ans[1001];
    int n, m, q, cnt = 0, f[37][1001], s[37] = {}, b[37] = {};
    cin >> n >> m >> q;
    if (n + m + q == 0)
      break;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        f[i][j] = 1;

    while (cnt < q) {
      cnt++;
      for (int i = 0; i < n; i++)
        cin >> c, s[i] = (s[i] + (c - '0')) % 2;
      for (int i = 0; i < m; i++)
        cin >> c, b[i] = c - '0';

      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
          if (f[i][j] == 2)
            continue;
          else if (s[i] == b[j])
            f[i][j] = 1;
          else
            f[i][j] = 2;
        }
    }

    for (int j = 0; j < m; j++) {
      int flag = 0;
      for (int i = 0; i < n; i++) {
        if (f[i][j] == 1 && flag == 0) {
          flag = 1;
          if (i >= 0 && i <= 9)
            ans[j] = i + '0';
          else
            ans[j] = i - 10 + 'A';
        } else if (flag == 1 && f[i][j] == 1)
          ans[j] = '?';
      }
    }

    for (int i = 0; i < m; i++)
      cout << ans[i];
    cout << endl;
  }
  return 0;
}
Problem p01703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

double ans[100010];

void MAIN(int n) {
  std::vector<double> prob;
  for (int i = 0; i < n; ++i) {
    double p;
    scanf("%lf", &p);
    if (p < 0.995) {
      prob.push_back(p);
    } else if (prob.empty() || prob.back() < 0.995) {
      prob.push_back(p);
    } else {
      prob[prob.size() - 1] += 1;
    }
    ans[i] = 1e100;
  }
  n = prob.size();
  ans[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    double now = 0;
    int upper = std::min(n, i + 700);
    for (int j = i + 1; j <= upper; ++j) {
      if (prob[j - 1] >= 0.995) {
        now += prob[j - 1];
      } else {
        now = (now + 1) / prob[j - 1];
      }
      ans[i] = std::min(ans[i], now + ans[j] + 1);
    }
  }
  printf("%.11f\\n", ans[0] - 1);
}

int main() {
  int n;
  while (scanf("%d", &n) == 1 && n > 0) {
    MAIN(n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

double ans[100010];

void MAIN(int n) {
  std::vector<double> prob;
  for (int i = 0; i < n; ++i) {
    double p;
    scanf("%lf", &p);
    if (p < 0.995) {
      prob.push_back(p);
    } else if (prob.empty() || prob.back() < 0.995) {
      prob.push_back(p);
    } else {
      prob[prob.size() - 1] += 1;
    }
    ans[i] = 1e100;
  }
  n = prob.size();
  ans[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    double now = 0;
    int upper = std::min(n, i + 1000);
    for (int j = i + 1; j <= upper; ++j) {
      if (prob[j - 1] >= 0.995) {
        now += prob[j - 1];
      } else {
        now = (now + 1) / prob[j - 1];
      }
      ans[i] = std::min(ans[i], now + ans[j] + 1);
    }
  }
  printf("%.11f\\n", ans[0] - 1);
}

int main() {
  int n;
  while (scanf("%d", &n) == 1 && n > 0) {
    MAIN(n);
  }
  return 0;
}
Problem p01703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int main() {
  while (true) {
    int N;
    double p[100000], dp[100001] = {}, inf = 1e20;
    vector<int> vc;
    cin >> N;
    if (N == 0)
      break;
    rep(i, N) cin >> p[i];
    rep(i, N) if (p[i] != 1) vc.pb(i);
    vc.pb(N);
    rep(i, N + 1) dp[i] = inf;
    dp[N] = 0;
    for (int i = N - 1; i >= 0; i--) {
      int id = lower_bound(all(vc), i + 1) - vc.begin();
      double ex = 0;
      int pre = i;
      for (int j = id, cnt = 0; j < vc.size() && cnt < 30; j++, cnt++) {
        int v = vc[j];
        ex = (ex + 1) / p[pre] + (v - pre - 1);
        chmin(dp[i], ex + (v == N ? 0 : 1) + dp[v]);
        pre = v;
      }
    }
    printf("%.12f\\n", dp[0]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
int N;
vector<double> p;
double dp[100001][100], eps = 1e-10;
int main() {
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    p.clear();
    double ans = 0;
    rep(i, N) {
      double o;
      cin >> o;
      if (o == 1)
        ans++;
      else
        p.pb(o);
    }
    N = p.size();
    rep(j, 100) dp[N][j] = 0;
    for (int s = N - 1; s >= 0; s--) {
      double ub = dp[s + 1][0] + 1 + 1 / p[s], lb = dp[s + 1][0];
      while (ub - lb > eps) {
        double m = (ub + lb) / 2;
        dp[s][0] = m;
        for (int j = min(100 - 1, N - s - 1); j >= 0; j--) {
          int i = s + j;
          if (j == 0) {
            double mm = 1 / p[i] + dp[i + 1][1];
            if (m > mm)
              ub = m;
            else
              lb = m;
          } else {
            dp[i][j] = 1 + min(dp[i][0], p[i] * dp[i + 1][j + 1] +
                                             (1 - p[i]) * dp[i - j][0]);
          }
        }
      }
    }
    printf("%.12f\\n", dp[0][0] + ans);
  }
}
Problem p01704 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
const double INF = 1e18;

int main() {
  cout << fixed;
  cout << setprecision(15);
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    double pw;
    cin >> pw;
    vector<double> vw(n), pf(n), vf(n), th(n);
    vector<double> w(1, 0);
    w.reserve(n + 1);
    for (int i = 0; i < n; i++) {
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
      if (th[i] * vw[i] > 0)
        w.push_back(th[i] / vw[i]);
    }
    sort(w.begin(), w.end());

    int lb = 0, ub = w.size() - 1;
    while (lb + 2 < ub) {
      int mid[2] = {lb + (ub - lb) / 3, lb + (ub - lb) * 2 / 3};
      double cost[2];
      for (int i = 0; i < 2; i++) {
        cost[i] = w[mid[i]] * pw;
        for (int j = 0; j < n; j++) {
          if (w[mid[i]] * vw[j] < th[j]) {
            cost[i] += (th[j] - w[mid[i]] * vw[j]) / vf[j] * pf[j];
          }
        }
      }
      if (cost[0] < cost[1]) {
        ub = mid[1];
      } else {
        lb = mid[0];
      }
    }

    double ans = INF;
    for (int i = lb; i <= ub; i++) {
      double cost = w[i] * pw;
      for (int j = 0; j < n; j++) {
        if (w[i] * vw[j] < th[j]) {
          cost += (th[j] - w[i] * vw[j]) / vf[j] * pf[j];
        }
      }
      ans = min(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
const double INF = 1e18;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    double pw;
    cin >> pw;
    vector<double> vw(n), pf(n), vf(n), th(n);
    vector<double> cand(n + 1, 0);
    for (int i = 0; i < n; i++) {
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
      cand[i] = th[i] / vw[i];
    }

    double ans = INF;
    for (int i = 0; i < n + 1; i++) {
      if (cand[i] < 0)
        continue;
      double cost = cand[i] * pw;
      for (int j = 0; j < n; j++) {
        if (cand[i] * vw[j] < th[j]) {
          cost += (th[j] - cand[i] * vw[j]) / vf[j] * pf[j];
        }
      }
      ans = min(ans, cost);
    }
    cout << fixed;
    cout << setprecision(10);
    cout << ans << endl;
  }
  return 0;
}
Problem p01704 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    double pw;
    cin >> pw;
    vector<double> vw(n);
    vector<double> pf(n);
    vector<double> vf(n);
    vector<double> th(n);
    for (int i = 0; i < n; i++) {
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
    }

    double ans;
    double l = 0.0, r = 1000.0;

    while (r - l >= 0.00000000001) {

      double p1 = (r + 2 * l) / 3;
      double p2 = (2 * r + l) / 3;

      // cout << l << " " << r << " " << p1 << " " << p2 << endl;

      double cost1 = (double)(p1 * pw);
      double cost2 = (double)(p2 * pw);

      for (int i = 0; i < n; i++) {
        double F1 = (double)((th[i] - p1 * vw[i]) / vf[i]);
        double F2 = (double)((th[i] - p2 * vw[i]) / vf[i]);
        F1 = max(F1, 0.0);
        F2 = max(F2, 0.0);

        cost1 += (double)(F1 * pf[i]);
        cost2 += (double)(F2 * pf[i]);
      }

      if (cost1 < cost2)
        r = p2;
      else
        l = p1;

      ans = cost1;
    }

    printf("%.4f\\n", ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    double pw;
    cin >> pw;
    vector<double> vw(n);
    vector<double> pf(n);
    vector<double> vf(n);
    vector<double> th(n);
    for (int i = 0; i < n; i++) {
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
    }

    double ans;
    double l = 0.0, r = 1000.0;

    while (r - l >= 0.000000000000001) {

      double p1 = (r + 2 * l) / 3;
      double p2 = (2 * r + l) / 3;

      // cout << l << " " << r << " " << p1 << " " << p2 << endl;

      double cost1 = (double)(p1 * pw);
      double cost2 = (double)(p2 * pw);

      for (int i = 0; i < n; i++) {
        double F1 = (double)((th[i] - p1 * vw[i]) / vf[i]);
        double F2 = (double)((th[i] - p2 * vw[i]) / vf[i]);
        F1 = max(F1, 0.0);
        F2 = max(F2, 0.0);

        cost1 += (double)(F1 * pf[i]);
        cost2 += (double)(F2 * pf[i]);
      }

      if (cost1 < cost2)
        r = p2;
      else
        l = p1;

      ans = cost1;
    }

    printf("%.4f\\n", ans);
  }

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

#include <bits/stdc++.h>

using namespace std;

int N;
double pw, vw[100010], pf[100010], vf[100010], th[100010];

double f(double x) {
  double ret = pw * x;
  for (int i = 0; i < N; i++) {
    double v = vw[i] * x;
    if (v >= th[i])
      continue;
    ret += pf[i] * (th[i] - v) / vf[i];
  }
  return ret;
}

double ternary_search() {
  double l = 0.0, r = 1000.0;
  for (int i = 0; i < 200; i++) {
    double c = (r - l) / 3.0;
    double a = l + c, b = r - c;
    if (f(a) >= f(b))
      l = a;
    else
      r = b;
  }
  return f(l);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  while (cin >> N, N) {
    cin >> pw;
    for (int i = 0; i < N; i++)
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
    cout << ternary_search() << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define FR first
#define SC second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for (int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for (auto &a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;

int N;
double pw, vw[10010], pf[10010], vf[10010], th[10010];

double f(double x) {
  double ret = pw * x;
  for (int i = 0; i < N; i++) {
    int v = vw[i] * x;
    if (v >= th[i])
      continue;
    ret += pf[i] * (th[i] - v) / vf[i];
  }
  return ret;
}

double ternary_search() {
  double l = 0.0, r = 1000.0;
  for (int i = 0; i < 200; i++) {
    double a = (l + l + r) / 3.0;
    double b = (l + r + r) / 3.0;
    if (f(a) + 1e-10 >= f(b))
      l = a;
    else
      r = b;
  }
  return f(l);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  while (cin >> N, N) {
    cin >> pw;
    for (int i = 0; i < N; i++)
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
    cout << ternary_search() << endl;
  }

  return 0;
}