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

// 2017-07-13
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n, m, s[15], d[100], sum[1 << 15], dp[1 << 15];
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i];
    }
    sort(d, d + m);
    for (int i = 0; i < 1 << n; i++) {
      sum[i] = 0;
      for (int j = 0; j < n; j++) {
        if (i & 1 << j) {
          sum[i] += s[j];
        }
      }
    }
    fill(dp, dp + (1 << n), 0);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < 1 << n; j++) {
        dp[j] += abs(sum[j] - d[i]);
      }
      for (int j = 0; j < 1 << n; j++) {
        for (int k = 0; k < n; k++) {
          dp[j] = min(dp[j], dp[j & ~(1 << k)]);
        }
      }
    }
    cout << dp[(1 << n) - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 2017-07-13
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int sum[1 << 15], dp[100][1 << 15];

int main() {
  int n, m, s[15], d[100];
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i];
    }
    sort(d, d + m);
    for (int i = 0; i < 1 << n; i++) {
      sum[i] = 0;
      for (int j = 0; j < n; j++) {
        if (i & 1 << j) {
          sum[i] += s[j];
        }
      }
    }
    fill(dp[0], dp[m], 1000000000);
    for (int i = 0; i < 1 << n; i++) {
      dp[0][i] = abs(sum[i] - d[0]);
    }
    for (int i = 1; i < m; i++) {
      for (int j = 0; j < 1 << n; j++) {
        for (int k = 0; k <= j; k++) {
          dp[i][j] = min(dp[i][j], dp[i - 1][~(~j | k)]);
        }
        dp[i][j] += abs(sum[j] - d[i]);
      }
    }
    int r = dp[m - 1][0];
    for (int i = 1; i < 1 << n; i++) {
      r = min(r, dp[m - 1][i]);
    }
    cout << r << endl;
  }
  return 0;
}
Problem p01849 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m, A[15], B[101];

int mem[101][1 << 15], used[101][1 << 15], sum[1 << 15];

int dfs(int idx, int sta) {
  if (idx == m)
    return 0;
  if (used[idx][sta]++)
    return mem[idx][sta];
  int &res = mem[idx][sta] = abs(sum[sta] - B[idx]) + dfs(idx + 1, sta);

  for (int i = 0; i < n; i++) {
    if (!((sta >> i) & 1))
      continue;
    res = min(res, dfs(idx, ~((~sta) | (1 << i))));
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++)
      cin >> A[i];
    for (int i = 0; i < m; i++)
      cin >> B[i];
    sort(A, A + n);
    sort(B, B + m, greater<int>());
    memset(used, 0, sizeof(used));
    memset(sum, 0, sizeof(sum));

    for (int i = 0; i < (1 << n); i++)
      for (int j = 0; j < n; j++)
        if ((i >> j) & 1)
          sum[i] += A[j];

    cout << dfs(0, (1 << n) - 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, A[15], B[101];

int mem[101][1 << 15], used[101][1 << 15], sum[1 << 15];
int dfs(int idx, int sta) {
  if (idx == m)
    return 0;
  if (used[idx][sta]++)
    return mem[idx][sta];
  int &res = mem[idx][sta] = 1e9 + 1;

  for (int i = 0; i < (1 << n); i++) {
    if ((sta | i) != sta)
      continue;
    int flg = 0;
    for (int j = 0; j < n; j++)
      if (!((i >> j) & 1) && ((sta >> j) & 1) &&
          abs(sum[i] + A[j] - B[idx]) <= abs(sum[i] - B[idx]))
        flg = 1;
    if (flg)
      continue;
    res = min(res, abs(B[idx] - sum[i]) + dfs(idx + 1, i));
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++)
      cin >> A[i];
    for (int i = 0; i < m; i++)
      cin >> B[i];
    sort(A, A + n);
    sort(B, B + m, greater<int>());
    memset(used, 0, sizeof(used));
    memset(sum, 0, sizeof(sum));

    for (int i = 0; i < (1 << n); i++)
      for (int j = 0; j < n; j++)
        if ((i >> j) & 1)
          sum[i] += A[j];

    cout << dfs(0, (1 << n) - 1) << endl;
  }
  return 0;
}
Problem p01849 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define INF 1e9

int main(void) {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    vector<int> s(n);
    vector<int> d(m);
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i];
    }
    sort(d.begin(), d.end());

    vector<int> sum_s(1 << n, 0);
    for (int i = 0; i < (1 << n); i++)
      for (int j = 0; j < n; j++)
        if ((i & (1 << j)) != 0)
          sum_s[i] += s[j];

    vector<vector<int>> dp(m, vector<int>(1 << n, INF));
    for (int i = 0; i < (1 << n); i++)
      dp[0][i] = abs(d[0] - sum_s[i]);

    for (int i = 1; i < m; i++) {
      for (int j = 0; j < (1 << n); j++) {
        dp[i][j] = dp[i - 1][j] + abs(d[i] - sum_s[j]);
        for (int k = 0; k < n; k++) {
          if ((j & (1 << k)) == 0) {
            continue;
          }
          int before = j & (~(1 << k));
          dp[i][j] = min(dp[i][j], dp[i][before] - abs(d[i] - sum_s[before]) +
                                       abs(d[i] - sum_s[j]));
        }
      }
    }

    int ans = INF;
    for (int i = 0; i < (1 << n); i++)
      ans = min(ans, dp[m - 1][i]);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define INF 1e9

int main(void) {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    vector<int> s(n);
    vector<int> d(m);
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i];
    }
    sort(d.begin(), d.end());

    vector<int> sum_s(1 << n, 0);
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if ((i & (1 << j)) == 0) {
          continue;
        }
        sum_s[i] += s[j];
      }
    }

    vector<vector<int>> dp(m + 1, vector<int>(1 << n, INF));
    for (int i = 0; i < (1 << n); i++)
      dp[0][i] = abs(d[0] - sum_s[i]);

    for (int i = 1; i < m; i++) {
      for (int j = 0; j < (1 << n); j++) {
        for (int k = 0; k <= j; k++) {
          if ((k | j) != j) {
            continue;
          }
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(d[i] - sum_s[j]));
        }
      }
    }

    int ans = INF;
    for (int i = 0; i < (1 << n); i++)
      ans = min(ans, dp[m - 1][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p01849 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

const int INF = 1e8;

int dp[20][1 << 17];
int sum[1 << 17];
int main() {
  int N, M;
  while (cin >> N >> M, N | M) {
    vector<int> s(N), d(M);
    rep(i, N) cin >> s[i];
    rep(i, M) cin >> d[i];

    N++;
    s.push_back(INF);

    sort(d.begin(), d.end());

    fill(dp[0], dp[0] + 20 * (1 << 17), INF);
    fill(sum, sum + (1 << 17), 0);
    dp[0][0] = 0;
    rep(i, N) rep(k, 1 << 16) rep(j, N) {
      if (k & (1 << j))
        continue;
      int nx = k | (1 << j);
      sum[nx] = sum[k] + s[j];
      auto bg = lower_bound(d.begin(), d.end(), sum[k]);
      auto en = lower_bound(d.begin(), d.end(), sum[nx]);
      int df = 0;
      for (auto a = bg; a != en; a++) {
        df += min(*a - sum[k], sum[nx] - *a);
      }
      dp[i + 1][nx] = min(dp[i + 1][nx], dp[i][k] + df);
    }

    int ans = dp[N][(1 << N) - 1];
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

const int INF = 1e8;

int dp[16][1 << 16];
int sum[1 << 16];
int main() {
  int N, M;
  while (cin >> N >> M, N | M) {
    vector<int> s(N), d(M);
    rep(i, N) cin >> s[i];
    rep(i, M) cin >> d[i];

    N++;
    s.push_back(INF);

    sort(d.begin(), d.end());

    fill(dp[0], dp[0] + 16 * (1 << 16), INF);
    fill(sum, sum + (1 << 16), 0);
    dp[0][0] = 0;
    rep(i, N) rep(k, 1 << 16) rep(j, N) {
      if (k & (1 << j))
        continue;
      int nx = k | (1 << j);
      sum[nx] = sum[k] + s[j];
      auto bg = lower_bound(d.begin(), d.end(), sum[k]);
      auto en = lower_bound(d.begin(), d.end(), sum[nx]);
      int df = 0;
      for (auto a = bg; a != en; a++) {
        df += min(*a - sum[k], sum[nx] - *a);
      }
      dp[i + 1][nx] = min(dp[i + 1][nx], dp[i][k] + df);
    }

    int ans = dp[N][(1 << N) - 1];
    cout << ans << endl;
  }
  return 0;
}
Problem p01849 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N, M;

inline string showbit(int n) {
  string bits;
  for (int i = 0; i < N; i++) {
    bits.push_back('0' + (n % 2));
    n /= 2;
  }
  reverse(bits.begin(), bits.end());
  return bits;
}

int main() {
  while (true) {
    cin >> N >> M;
    if (N == 0 and M == 0) {
      break;
    }
    vector<int> bed(N);
    for (int i = 0; i < N; i++) {
      cin >> bed[i];
    }
    vector<int> d(M);
    for (int i = 0; i < M; i++) {
      cin >> d[i];
    }
    sort(d.begin(), d.end());
    const int maximum = (1 << N);
    constexpr int INF = (1 << 28);
    vector<int> sum(maximum, 0);
    for (int i = 1; i < maximum; i++) {
      int num = i;
      int pop = 0;
      while (num % 2 == 0) {
        num /= 2;
        pop++;
      }
      sum[i] = sum[i - (1 << pop)] + bed[pop];
    }

    vector<vector<int>> dp(M, vector<int>(maximum, INF));
    vector<vector<int>> dpmin(M, vector<int>(maximum, INF));
    for (int j = 0; j < maximum; j++) {
      dp[0][j] = abs(d[0] - sum[j]);
    }
    for (int i = 1; i < M; i++) {
      for (int j = 0; j < maximum; j++) {
        dpmin[i][j] = dp[i - 1][j];
      }
      for (int j = 0; j < maximum; j++) {
        for (int k = 0; k < N; k++) {
          if ((j >> k) & 1) {
            dpmin[i][j] = min(dpmin[i][j], dpmin[i][j - (1 << k)]);
          }
        }
      }
      for (int j = 0; j < maximum; j++) {
        const int score = abs(d[i] - sum[j]);
        dp[i][j] = min(dp[i][j], dpmin[i][j] + score);
      }
    }

    int mini = INF;
    for (int i = 0; i < maximum; i++) {
      mini = min(mini, dp[M - 1][i]);
    }
    cout << mini << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 and M == 0) {
      break;
    }
    vector<int> bed(N);
    for (int i = 0; i < N; i++) {
      cin >> bed[i];
    }
    vector<int> d(M);
    for (int i = 0; i < M; i++) {
      cin >> d[i];
    }
    sort(d.begin(), d.end());
    const int maximum = (1 << N);
    constexpr int INF = (1 << 28);
    vector<int> sum(maximum, 0);
    for (int i = 1; i < maximum; i++) {
      int num = i;
      int pop = 0;
      while (num % 2 == 0) {
        num /= 2;
        pop++;
      }
      sum[i] = sum[i - (1 << pop)] + bed[pop];
    }

    vector<vector<int>> dp(M, vector<int>(maximum, INF));
    for (int j = 0; j < maximum; j++) {
      dp[0][j] = abs(d[0] - sum[j]);
    }
    for (int i = 1; i < M; i++) {
      for (int j = 0; j < maximum; j++) {
        int num = j;
        while (true) {
          dp[i][j] = min(dp[i][j], dp[i - 1][num] + abs(sum[j] - d[i]));
          if (num == 0) {
            break;
          }
          num = (num - 1) & j;
        }
      }
    }
    int mini = INF;
    for (int i = 0; i < maximum; i++) {
      mini = min(mini, dp[M - 1][i]);
    }
    cout << mini << endl;
  }
  return 0;
}
Problem p01849 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF (1e9)
#define M 105
#define N 15
using namespace std;
int n, m, s[N], d[M], dp[(1 << N)][M], ans[(1 << N)];

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < m; i++)
      cin >> d[i];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < (1 << n); i++)
      ans[i] = INF;
    for (int i = 0; i < (1 << n); i++) {
      int sum = 0;
      for (int j = 0; j < n; j++)
        if (i & (1 << j))
          sum += s[j];
      int temp[M];
      for (int j = 0; j < n; j++) {
        if (i & (1 << j))
          continue;
        int x = 0;
        for (int k = 0; k < m; k++) {
          temp[k] = dp[i][k];
          if (dp[i][k] == sum &&
              (abs(d[k] - dp[i][k]) > abs(d[k] - (dp[i][k] + s[j]))))
            temp[k] += s[j];
          x += abs(d[k] - temp[k]);
        }
        if (ans[i | (1 << j)] > x) {
          ans[i | (1 << j)] = x;
          for (int k = 0; k < m; k++)
            dp[i | (1 << j)][k] = temp[k];
        }
      }
    }
    cout << ans[(1 << n) - 1] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF (1e9)
#define N 16
#define M 105
using namespace std;
int n, m, s[N], d[N], dp[(1 << N)][M], ans[(1 << N)];

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < m; i++)
      cin >> d[i];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < (1 << n); i++)
      ans[i] = INF;
    for (int i = 0; i < (1 << n); i++) {
      int sum = 0;
      for (int j = 0; j < n; j++)
        if (i & (1 << j))
          sum += s[j];
      int temp[M];
      for (int j = 0; j < n; j++) {
        if (i & (1 << j))
          continue;
        int x = 0;
        for (int k = 0; k < m; k++) {
          temp[k] = dp[i][k];
          if (dp[i][k] == sum &&
              (abs(d[k] - dp[i][k]) > abs(d[k] - (dp[i][k] + s[j]))))
            temp[k] += s[j];
          x += abs(d[k] - temp[k]);
        }
        if (ans[i | (1 << j)] > x) {
          ans[i | (1 << j)] = x;
          for (int k = 0; k < m; k++)
            dp[i | (1 << j)][k] = temp[k];
        }
      }
    }
    cout << ans[(1 << n) - 1] << endl;
  }
  return 0;
}
Problem p01849 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>

using namespace std;

int n, m;
long long s[16], d[101], sigma[1 << 15], dp[101][1 << 15];

int main() {
  while (cin >> n >> m && n) {
    for (int i = 0; i < n; i++)
      cin >> s[i];
    d[0] = 0LL;
    for (int i = 1; i <= m; i++)
      cin >> d[i];
    sort(d, d + m + 1);

    sigma[0] = 0LL;
    for (int j = 0; j < (1 << n); j++) {
      for (int k = 0; k < n; k++) {
        if (j & (1 << k))
          continue;
        sigma[j | (1 << k)] = sigma[j] + s[k];
      }
    }

    fill(dp[0], dp[m + 1], LLONG_MAX);
    for (int j = 0; j < (1 << n); j++)
      dp[0][j] = 0LL;
    for (int i = 1; i <= m; i++)
      dp[i][0] = dp[i - 1][0] + d[i];
    for (int i = 1; i <= m; i++) {
      for (int j = 0; j < (1 << n); j++) {
        for (int k = 0; k < n; k++) {
          if (j & (1 << k))
            continue;
          dp[i][j | (1 << k)] =
              min({dp[i][j | (1 << k)], dp[i][j],
                   dp[i - 1][j] + abs(sigma[j] - d[i]),
                   dp[i - 1][j | (1 << k)] + abs(sigma[j | (1 << k)] - d[i])});
        }
      }
    }

    cout << dp[m][(1 << n) - 1] << endl;
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <iostream>

using namespace std;

int n, m;
long long s[16], d[101];

int main() {
  while (cin >> n >> m && n) {
    for (int i = 0; i < n; i++)
      cin >> s[i];
    d[0] = 0LL;
    for (int i = 1; i <= m; i++)
      cin >> d[i];
    sort(d, d + m + 1);

    long long sigma[1 << n];
    sigma[0] = 0LL;
    for (int j = 0; j < (1 << n); j++) {
      for (int k = 0; k < n; k++) {
        if (j & (1 << k))
          continue;
        sigma[j | (1 << k)] = sigma[j] + s[k];
      }
    }

    long long dp[m + 1][1 << n];
    fill(dp[0], dp[m + 1], 1e8);
    for (int j = 0; j < (1 << n); j++)
      dp[0][j] = 0LL;
    for (int i = 1; i <= m; i++)
      dp[i][0] = dp[i - 1][0] + d[i];
    for (int i = 1; i <= m; i++)
      cerr << i << " " << bitset<2>(0) << ": " << dp[i][0] << endl;
    for (int i = 1; i <= m; i++) {
      for (int j = 0; j < (1 << n); j++) {
        for (int k = 0; k < n; k++) {
          if (j & (1 << k))
            continue;
          dp[i][j | (1 << k)] =
              min(dp[i][j],
                  dp[i - 1][j | (1 << k)] + abs(sigma[j | (1 << k)] - d[i]));
        }
        cerr << i << " " << bitset<2>(j) << ": " << dp[i][j] << endl;
      }
    }

    cout << dp[m][(1 << n) - 1] << endl;
  }
}
Problem p01854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, char> P;
typedef long long ll;
const int INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};

vector<vi> s, h;
int n, m;
double dp[222][222];

double solve(int i, int w) {
  if (i == n)
    return 0;
  if (dp[i][w] >= 0)
    return dp[i][w];
  double res = -1;
  int tmp = 0, full = s[i][0] * h[i][0];
  double tmph = 0;
  res = max(res, solve(i + 1, w));
  FOR(j, 1, w + 1) {
    if (j > full) {
      tmp++;
      if (tmp == s[i].size())
        break;
      full += s[i][tmp] * h[i][tmp];
    }
    tmph += 1.0 / s[i][tmp];
    res = max(res, solve(i + 1, w - j) + tmph);
  }
  return dp[i][w] = res;
}

int main() {
  MS(dp, -1);
  cin >> n >> m;
  s.resize(n), h.resize(n);
  REP(i, n) {
    int k;
    cin >> k;
    s[i].resize(k), h[i].resize(k);
    REP(j, k) cin >> s[i][j] >> h[i][j];
  }
  cout << D10 << solve(0, m) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, char> P;
typedef long long ll;
const int INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};

vector<vi> s, h;
int n, m;
double dp[222][222];

double solve(int i, int w) {
  if (i == n)
    return 0;
  double res = -1;
  int tmp = 0, full = s[i][0] * h[i][0];
  double tmph = 0;
  res = max(res, solve(i + 1, w));
  FOR(j, 1, w + 1) {
    if (j > full) {
      tmp++;
      if (tmp == s[i].size())
        break;
      full += s[i][tmp] * h[i][tmp];
    }
    tmph += 1.0 / s[i][tmp];
    res = max(res, solve(i + 1, w - j) + tmph);
  }
  return dp[i][w] = res;
}

int main() {
  MS(dp, -1);
  cin >> n >> m;
  s.resize(n), h.resize(n);
  REP(i, n) {
    int k;
    cin >> k;
    s[i].resize(k), h[i].resize(k);
    REP(j, k) cin >> s[i][j] >> h[i][j];
  }
  cout << D10 << solve(0, m) << endl;
  return 0;
}
Problem p01854 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  double dp[223][223];
  fill(dp[0], dp[223], -1e9);
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    int K;
    cin >> K;
    int S[22], H[22];
    for (int j = 0; j < K; j++) {
      cin >> S[j] >> H[j];
    }
    for (int j = 0; j <= M; j++) {
      int x = 0;
      int t = 0;
      int h = 0;
      for (int k = 0; k + j <= M; k++) {
        if (x == K) {
          dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] + h);
          break;
        }
        dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] + h + t * 1. / S[x]);
        t++;
        if (t == H[x] * S[x]) {
          t = 0;
          h += H[x];
          x++;
        }
      }
    }
  }
  cout << fixed << *max_element(dp[0], dp[223]) << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  double dp[22][223];
  fill(dp[0], dp[22], -1e9);
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    int K;
    cin >> K;
    int S[22], H[22];
    for (int j = 0; j < K; j++) {
      cin >> S[j] >> H[j];
    }
    for (int j = 0; j <= M; j++) {
      int x = 0;
      int t = 0;
      int h = 0;
      for (int k = 0; k + j <= M; k++) {
        if (x == K) {
          dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] + h);
          break;
        }
        dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] + h + t * 1. / S[x]);
        t++;
        if (t == H[x] * S[x]) {
          t = 0;
          h += H[x];
          x++;
        }
      }
    }
  }
  cout << fixed << *max_element(dp[0], dp[22]) << endl;
}