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

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long int H[N], A[N], D[N], S[N], h, a, d, s, t, ans = 0LL;
  cin >> h >> a >> d >> s;
  for (int i = 0; i < N; ++i) {
    cin >> H[i] >> A[i] >> D[i] >> S[i];
    A[i] -= d;
    A[i] = max(A[i], 0LL);
    if (S[i] < s)
      ans -= A[i];
  }
  vector<pair<double, int>> V;
  for (int i = 0; i < N; ++i) {
    if (a <= D[i] && A[i] > 0) {
      cout << -1 << endl;
      return 0;
    }
    if (a > D[i]) {
      t = (H[i] + a - D[i] - 1) / (a - D[i]);
      V.push_back(make_pair(-(double)A[i] / t, i));
    } else { // A[i] == 0 && a == D[i]
      V.push_back(make_pair(0, i));
    }
  }
  sort(V.begin(), V.end());
  t = 0LL;
  for (int i = 0; i < N; ++i) {
    // cout << V[i].first << " " << V[i].second << endl;
    int j = V[i].second;
    if (a != D[j]) {
      t += (H[j] + a - D[j] - 1) / (a - D[j]);
    }
    ans += t * A[j];
    // cout << t << "  " << ans << endl;
    if (ans >= h) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (ans >= h)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long int H[N], A[N], D[N], S[N], h, a, d, s, t, ans = 0LL;
  cin >> h >> a >> d >> s;
  for (int i = 0; i < N; ++i) {
    cin >> H[i] >> A[i] >> D[i] >> S[i];
    A[i] -= d;
    A[i] = max(A[i], 0LL);
    if (S[i] < s)
      ans -= A[i];
  }
  vector<pair<double, int>> V;
  for (int i = 0; i < N; ++i) {
    if (a <= D[i] && A[i] > 0) {
      cout << -1 << endl;
      return 0;
    }
    if (a != D[i]) {
      t = (H[i] + a - D[i] - 1) / (a - D[i]);
      V.push_back(make_pair(-(double)A[i] / t, i));
    } else {
      V.push_back(make_pair(0, i));
    }
  }
  sort(V.begin(), V.end());
  t = 0LL;
  for (int i = 0; i < N; ++i) {
    // cout << V[i].first << " " << V[i].second << endl;
    int j = V[i].second;
    t += (H[j] + a - D[j] - 1) / (a - D[j]);
    ans += t * A[j];
    // cout << t << "  " << ans << endl;
    if (ans >= h) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (ans >= h)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
Problem p01353 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 40001
using namespace std;
typedef long long ll;
struct po {
  ll h, a, d, s;
};

ll n;
po A[N];
ll hp, a, d, s, sum;
bool compare(po x, po y) {
  if (a - x.d <= 0 || a - y.d <= 0)
    return 0;

  ll cnt1 = 1 + (x.h - 1) / (a - x.d), cnt2 = 1 + (y.h - 1) / (a - y.d);
  ll X = cnt1 * (x.a - d) + (cnt1 + cnt2) * (y.a - d);
  ll Y = cnt2 * (y.a - d) + (cnt1 + cnt2) * (x.a - d);
  return X < Y;
}

ll solve() {
  sort(A, A + n, compare);
  for (int i = 0; i < n; i++) {
    po t = A[i];
    if (hp <= 0)
      return -1;
    if (a - t.d <= 0)
      return -1;
    ll cost = ((t.h - 1) / (a - t.d)) * sum;
    sum -= t.a - d;
    hp -= (cost + sum);
  }
  if (hp <= 0)
    return -1;
  return hp;
}

int main() {
  cin >> n;
  cin >> hp >> a >> d >> s;

  ll ofs = 0;
  for (int i = 0; i < n; i++) {
    po &t = A[i];
    cin >> t.h >> t.a >> t.d >> t.s;
    if (t.a - d <= 0) {
      n--, i--;
      continue;
    };
    if (t.s > s)
      ofs += (t.a - d);
    sum += (t.a - d);
  }

  ll H = hp;
  hp -= ofs;
  ll ans = H - solve();
  if (H < ans)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 40001
using namespace std;
typedef long long ll;
struct po {
  ll h, a, d, s;
};

ll n;
po A[N];
ll hp, a, d, s, sum;
bool compare(po x, po y) {
  ll cnt1 = 1 + (x.h - 1) / (a - x.d), cnt2 = 1 + (y.h - 1) / (a - y.d);
  ll X = cnt1 * (x.a - d) + (cnt1 + cnt2) * (y.a - d);
  ll Y = cnt2 * (y.a - d) + (cnt1 + cnt2) * (x.a - d);
  return X < Y;
}

ll solve() {

  for (int i = 0; i < n; i++)
    if (a - A[i].d <= 0)
      return -1;

  sort(A, A + n, compare);
  for (int i = 0; i < n; i++) {
    po t = A[i];

    ll cost = ((t.h - 1) / (a - t.d)) * sum;
    sum -= t.a - d;
    hp -= (cost + sum);
    if (hp <= 0)
      return -1;
  }
  if (hp <= 0)
    return -1;
  return hp;
}

int main() {
  cin >> n;
  cin >> hp >> a >> d >> s;

  ll ofs = 0;
  for (int i = 0; i < n; i++) {
    po &t = A[i];
    cin >> t.h >> t.a >> t.d >> t.s;
    if (t.a - d < 0) {
      i--;
      continue;
    };
    if (t.s > s)
      ofs += (t.a - d);
    sum += (t.a - d);
  }

  ll H = hp;
  hp -= ofs;
  ll ans = H - solve();
  if (H < ans)
    cout << -1 << endl;
  else
    cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;

struct info {
  int h, a;
  double rate;
  info(int h, int a, double r) : h(h), a(a), rate(r) {}
  info() {}
};
bool comp(const info &a, const info &b) { return a.rate > b.rate; }

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

  lli h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<info> e(n);
  lli sum = 0, damage = 0;
  for (int i = 0; i < n; i++) {
    int eh, ea, ed, es;
    cin >> eh >> ea >> ed >> es;
    ea -= d;
    if (ea <= 0) {
      e[i] = info(1, 0, 0);
      continue;
    }
    if (es > s) {
      damage += ea;
    }
    if (a - ed <= 0) {
      h = 0;
      continue;
    }
    eh = (eh + (a - ed) - 1) / (a - ed);
    e[i] = info(eh, ea, (double)ea / eh);
    sum += ea;
  }
  if (h == 0) {
    cout << -1 << endl;
    return 0;
  }

  sort(e.begin(), e.end(), comp);
  for (int i = 0; sum > 0; i++) {
    int limit = (h - damage) / sum;
    if (e[i].h > limit) {
      h = 0;
      break;
    }
    damage += sum * (e[i].h - 1);
    sum -= e[i].a;
    damage += sum;
  }
  if (h <= damage) {
    cout << -1 << endl;
  } else {
    cout << damage << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;

struct info {
  int h, a;
  double rate;
  info(int h, int a, double r) : h(h), a(a), rate(r) {}
  info() {}
};
bool comp(const info &a, const info &b) { return a.rate > b.rate; }

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

  lli h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<info> e(n);
  lli sum = 0, damage = 0;
  for (int i = 0; i < n; i++) {
    int eh, ea, ed, es;
    cin >> eh >> ea >> ed >> es;
    ea -= d;
    if (ea <= 0) {
      e[i] = info(1, 0, 0);
      continue;
    }
    if (es > s) {
      damage += ea;
    }
    if (a - ed <= 0) {
      h = 0;
      continue;
    }
    eh = (eh + (a - ed) - 1) / (a - ed);
    e[i] = info(eh, ea, (double)ea / eh);
    sum += ea;
  }
  if (h == 0) {
    cout << -1 << endl;
    return 0;
  }

  sort(e.begin(), e.end(), comp);
  for (int i = 0; i < (int)e.size(); i++) {
    int limit = (h - damage) / sum;
    if (e[i].h > limit) {
      h = 0;
      break;
    }
    damage += sum * (e[i].h - 1);
    sum -= e[i].a;
    damage += sum;
  }
  if (h <= damage) {
    cout << -1 << endl;
  } else {
    cout << damage << endl;
  }
  return 0;
}
Problem p01353 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

struct at {
  ll a, t;
};

bool comp(const at &x, const at &y) {
  return (x.a * (x.t - 1) + y.a * (x.t + y.t - 1)) <
         (y.a * (y.t - 1) + x.a * (x.t + y.t - 1));
}

vector<at> e;

int main() {
  int n;
  cin >> n;
  ll hp, atk, def, sp;
  ll ans = 0;
  cin >> hp >> atk >> def >> sp;
  for (int i = 0; i < n; i++) {
    ll ehp, eatk, edef, esp;
    cin >> ehp >> eatk >> edef >> esp;
    ll dm = max((ll)0, atk - edef);
    ll dmk = max(0LL, eatk - def);
    if (esp > sp)
      ans += dmk;

    if (dm == 0 && dmk > 0) {
      cout << -1 << endl;
      exit(0);
    }

    if (dm == 0)
      continue;
    e.push_back((at){dmk, (ehp + dm - 1) / dm});
  }

  sort(e.begin(), e.end(), comp);
  ll cnt = 0;
  for (int i = 0; i < (int)e.size(); i++) {
    ll x = e[i].a, y = e[i].t;
    cnt += y;
    ans += x * (cnt - 1);
    if (hp - ans <= 0)
      break;
  }
  if (hp - ans <= 0)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

struct at {
  ll a, t;
};

bool comp(const at &x, const at &y) {
  return (x.a * (x.t - 1) + y.a * (x.t + y.t - 1)) <
         (y.a * (y.t - 1) + x.a * (x.t + y.t - 1));
}

vector<at> e;

int main() {
  int n;
  cin >> n;
  ll hp, atk, def, sp;
  ll ans = 0;
  cin >> hp >> atk >> def >> sp;
  for (int i = 0; i < n; i++) {
    ll ehp, eatk, edef, esp;
    cin >> ehp >> eatk >> edef >> esp;
    ll dm = max((ll)0, atk - edef);
    ll dmk = max(0LL, eatk - def);
    if (esp > sp)
      ans += dmk;

    if (dm == 0 && dmk > 0) {
      cout << -1 << endl;
      exit(0);
    }
    e.push_back((at){dmk, (ehp + dm - 1) / dm});
  }
  sort(e.begin(), e.end(), comp);
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    ll x = e[i].a, y = e[i].t;
    cnt += y;
    ans += x * (cnt - 1);
    if (hp - ans <= 0)
      break;
  }
  if (hp - ans <= 0)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
Problem p01353 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

using pl = pair<ll, ll>;

const ll INF = 100000000000LL;
ll mul(ll x, ll y) {
  if (x == 0 || y == 0)
    return 0;

  // x*y>INF
  if (x > INF / y)
    return INF;
  return x * y;
}

ll solve() {
  int n;
  cin >> n;

  ll H, A, D, S;
  cin >> H >> A >> D >> S;

  vector<ll> h(n), a(n), d(n), s(n);
  rep(i, n) cin >> h[i] >> a[i] >> d[i] >> s[i];

  ll ret = 0;

  vector<pl> e;
  rep(i, n) {
    ll damage = max(0LL, a[i] - D);
    ll dd = max(0LL, A - d[i]);

    if (damage > 0 && dd == 0)
      return -1;

    if (dd > 0) {
      ll turn = (h[i] + dd - 1) / dd;
      if (S > s[i])
        ret -= damage;

      e.pb({damage, turn});
    }
  }

  sort(all(e),
       [](const pl &l, const pl &r) { return l.fi * r.se > l.se * r.fi; });

  ll T = 0;
  rep(i, e.size()) {
    T += e[i].se;
    ret += mul(e[i].fi, T);

    if (ret >= H)
      return -1;
  }

  return ret;
}

int main() {
  cout << solve() << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

using pl = pair<ll, ll>;

bool comp(const pl &l, const pl &r) { return l.fi * r.se >= l.se * r.fi; }

const ll INF = 100000000000LL;
ll mul(ll x, ll y) {
  if (x == 0 || y == 0)
    return 0;

  // x*y>INF
  if (x > INF / y)
    return INF;
  return x * y;
}

ll solve() {
  int n;
  cin >> n;

  ll H, A, D, S;
  cin >> H >> A >> D >> S;

  ll ret = 0;

  vector<pl> e(n);
  rep(i, n) {
    ll h, a, d, s;
    cin >> h >> a >> d >> s;

    ll damage = max(0LL, a - D);

    ll dd = max(0LL, A - d);
    if (dd == 0)
      return -1;
    ll turn = (h + dd - 1) / dd;

    if (S > s)
      ret -= damage;

    e[i] = {damage, turn};
  }

  sort(all(e), comp);

  ll T = 0;
  rep(i, n) {
    T += e[i].se;
    ret += mul(e[i].fi, T);

    if (ret >= H)
      return -1;
  }

  return ret;
}

int main() {
  cout << solve() << endl;
  return 0;
}
Problem p01353 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#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;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;

i64 h[40404];
i64 a[40404];
i64 d[40404];
i64 s[40404];
i64 A[40404];
i64 B[40404];

i64 ceil(i64 A, i64 B) { return A / B + min(1LL, A % B); }

int main() {
  cin >> n;
  vector<int> vec;
  rep(i, 0, n) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i == 0)
      continue;

    // cant
    if (a[0] <= d[i] && a[i] > d[0]) {
      cout << -1 << endl;
      return 0;
    }
    if (a[0] > d[i]) {
      vec.push_back(i);
      A[i] = (ceil(h[i], a[0] - d[i]));
      B[i] = (max(0LL, a[i] - d[0]));
    }
  }

  auto ope = [&](int i, int j) { return A[i] * B[j] < A[j] * B[i]; };

  sort(vec.begin(), vec.end(), ope);

  i64 HP = 0;
  i64 cou = 0;

  rep(i, 0, (int)vec.size() - 1) {
    int index = vec[i];
    cou += A[index];
    HP += cou * B[index];
    if (s[index] < s[0])
      HP -= B[index];
    if (h[0] <= HP) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << HP << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#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;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;

i64 h[40404];
i64 a[40404];
i64 d[40404];
i64 s[40404];
i64 A[40404];
i64 B[40404];

i64 ceil(i64 A, i64 B) { return A / B + min(1LL, A % B); }

int main() {
  cin >> n;
  vector<int> vec;
  rep(i, 0, n) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i == 0)
      continue;

    // cant
    if (a[0] <= d[i] && a[i] > d[0]) {
      cout << -1 << endl;
      return 0;
    }
    if (a[0] > d[i]) {
      vec.push_back(i);
      A[i] = (ceil(h[i], a[0] - d[i]));
      B[i] = (max(0LL, a[i] - d[0]));
    }
  }

  auto ope = [&](int i, int j) { return A[i] * B[j] < A[j] * B[i]; };

  sort(vec.begin(), vec.end(), ope);

  i64 HP = 0;
  i64 cou = 0;

  rep(i, 0, vec.size() - 1) {
    int index = vec[i];
    cou += A[index];
    HP += cou * B[index];
    if (s[index] < s[0])
      HP -= B[index];
    if (h[0] <= HP) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << HP << endl;
  return 0;
}
Problem p01353 (C++) — buggy submission, verdict: Runtime Error

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  int n;
  cin >> n;
  LL h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<LL> eh(n), ea(n), ed(n), es(n);
  vector<pair<LD, LL>> ss(n, {0, -1});
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> eh[i] >> ea[i] >> ed[i] >> es[i];
    ea[i] = max(ea[i] - d, 0);
    ed[i] = max(a - ed[i], 0);
    if (es[i] > s)
      ans += ea[i];
    if (ed[i] == 0) {
      if (ea[i] > 0) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      if (ea[i] == 0)
        ss[i] = {INF, i};
      else
        ss[i] = {((eh[i] + ed[i] - 1) / ed[i]) / (LD)ea[i], i};
    }
  }
  sort(all(ss));
  LL turn = 0;
  int id;
  // cout << ans << endl;
  for (int i = 0; i < n; i++) {
    if (ss[i].second != -1) {
      id = ss[i].second;
      turn += (eh[id] + ed[id] - 1) / ed[id];
      ans += (turn - 1) * ea[id];
      // cout << id SP turn SP ans << endl;
      if (ans >= h) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  int n;
  cin >> n;
  LL h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<LL> eh(n), ea(n), ed(n), es(n);
  vector<pair<LD, LL>> ss(n, {0, -1});
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> eh[i] >> ea[i] >> ed[i] >> es[i];
    ea[i] = max(ea[i] - d, 0);
    ed[i] = max(a - ed[i], 0);
    if (es[i] > s)
      ans += ea[i];
    if (ed[i] == 0) {
      if (ea[i] > 0) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      ss[i] = {((eh[i] + ed[i] - 1) / ed[i]) / (LD)ea[i], i};
    }
    if (ea[i] == 0)
      ss[i] = {INF, i};
  }
  sort(all(ss));
  LL turn = 0;
  int id;
  // cout << ans << endl;
  for (int i = 0; i < n; i++) {
    if (ss[i].second != -1) {
      id = ss[i].second;
      turn += (eh[id] + ed[id] - 1) / ed[id];
      ans += (turn - 1) * ea[id];
      // cout << id SP turn SP ans << endl;
      if (ans >= h) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int m, n;
double t[16][16];
double mem[16][(1 << 16)];
bool visited[16][(1 << 16)];

double rec(int x, int S) {
  if (visited[x][S])
    return mem[x][S];
  visited[x][S] = true;
  double res = 0;
  for (int i = 0; i < m; i++) {
    if (S >> i & 1)
      continue;
    double p = 1.0, k = 0;
    for (int j = x; j < n; j++) {
      k += rec(j, S | (1 << i)) * p * (1.0 - t[i][j]);
      p *= t[i][j];
    }
    res = max(res, k + p);
  }
  return mem[x][S] = res;
}

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> t[i][j];
  printf("%.12f\\n", rec(0, 0));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int m, n;
long double t[16][16];
long double mem[16][(1 << 16)];
bool visited[16][(1 << 16)];

long double rec(int x, int S) {
  if (visited[x][S])
    return mem[x][S];
  visited[x][S] = true;
  long double res = 0;
  for (int i = 0; i < m; i++) {
    if (S >> i & 1)
      continue;
    long double p = 1.0, k = 0;
    for (int j = x; j < n; j++) {
      k += rec(j, S | (1 << i)) * p * (1.0 - t[i][j]);
      p *= t[i][j];
    }
    res = max(res, k + p);
  }
  return mem[x][S] = res;
}

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> t[i][j];
  printf("%.12f\\n", (double)rec(0, 0));
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int M, N;
  cin >> M >> N;
  // M - cat N - room
  double pos[16][16];
  REP(i, M) REP(j, N) cin >> pos[i][j];

  static double dp[1 << 16][17];
  REP(S, 1 << M) REP(u, N + 1) dp[S][N] = (S > 0 && u == N) ? 1.0 : 0.0;

  for (int u = N - 1; u >= 0; u--) {
    for (int S = 0; S < 1 << M; S++) {
      for (int i = 0; i < M; i++)
        if (S >> i & 1) {
          double p = 1.0;
          double npos = 0.0;
          for (int v = u; v < N; v++) {
            npos += dp[S & ~(1 << i)][v] * p * (1.0 - pos[i][v]);
            p *= pos[i][v];
          }
          npos += dp[S][N] * p;
          dp[S][u] = max(dp[S][u], npos);
        }
    }
  }

  printf("%.12f\\n", dp[(1 << M) - 1][0]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N, M;
double p[16][16];
double prob(int S, int k) {
  double res = 0;
  REP(i, N) if (!(S >> i & 1)) {
    double r = 1;
    double sum = 0;
    FOR(j, k, M) {
      sum += r * (1 - p[i][j]) * prob(S | (1 << i), j);
      r *= p[i][j];
    }
    sum += r;
    res = max(res, sum);
  }
  return res;
}

int main() {
  while (cin >> N >> M && N) {
    REP(i, N) REP(j, M) cin >> p[i][j]; // cat-i wins ops-j.
    printf("%.12lf\\n", prob(0, 0));
  }
  return 0;
}