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

#include <iostream>
#include <vector>
using namespace std;
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
long long a, b, c, d;
int main() {
  while (cin >> a >> b >> c >> d, a) {
    long long r = 119 * a - 1;
    for (int i = 0;; i++) {
      long long cp = (d * r + i) * 60 * a, cq = 3600 * a * r;
      long long bp = (c * 60 * r + cp / (60 * a)) * a, bq = 3600 * a * r;
      long long ap = b * 3600 * r + bp / a, aq = 3600 * a * r;
      ap %= aq;
      bp %= bq;
      cp %= cq;
      while (cp < ap)
        cp += cq;
      while (bp < cp)
        bp += bq;
      if (ap == bp)
        continue;
      if (cp - ap == bp - cp) {
        long long cr = d * r + i, br = c, ar = b;
        br += cr / (60 * r);
        cr %= 60 * r;
        ar += br / 60;
        br %= 60;
        ar %= a;
        long long f = gcd(cr, r);
        cr /= f;
        r /= f;
        cout << ar << ' ' << br << ' ' << cr << ' ' << r << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
long long a, b, c, d;
int main() {
  while (cin >> a >> b >> c >> d, a) {
    long long r = 119 * a - 1;
    for (int i = 1;; i++) {
      long long cp = (d * r + i) * 60 * a, cq = 3600 * a * r;
      long long bp = (c * 60 * r + cp / (60 * a)) * a, bq = 3600 * a * r;
      long long ap = b * 3600 * r + bp / a, aq = 3600 * a * r;
      while (cp < ap)
        cp += cq;
      while (bp < cp)
        bp += bq;
      if (ap == bp)
        continue;
      if (cp - ap == bp - cp) {
        long long cr = d * r + i, br = c, ar = b;
        br += cr / (60 * r);
        cr %= 60 * r;
        ar += br / 60;
        br %= 60;
        ar %= a;
        long long f = gcd(cr, r);
        cr /= f;
        r /= f;
        cout << ar << ' ' << br << ' ' << cr << ' ' << r << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00918 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;

const int dx[] = {-1, 1, -3, 3};
int costs[4];

map<int, vector<pair<int, int>>> m;

int main() {
  string s = "012345678";

  do {
    int val = atoi(s.c_str());
    int zpos = 0;
    rep(i, 9) if (s[i] == '0') zpos = i;
    rep(i, 4) {
      string next = s;
      swap(next[zpos], next[(9 + zpos + dx[i]) % 9]);
      m[val].push_back(MP(atoi(next.c_str()), i));
    }
  } while (next_permutation(s.begin(), s.end()));

  int ch, cv;
  while (cin >> ch >> cv, ch | cv) {
    costs[0] = costs[1] = ch;
    costs[2] = costs[3] = cv;

    int s = 0, g = 0;
    rep(i, 9) {
      int x;
      cin >> x;
      s *= 10;
      s += x;
    }
    rep(i, 9) {
      int x;
      cin >> x;
      g *= 10;
      g += x;
    }

    map<int, int> dist;
    dist[s] = 0;

    priority_queue<P, vector<P>, greater<P>> q;
    q.push(MP(0, s));

    while (!q.empty()) {
      int d = q.top().first;
      int pos = q.top().second;
      q.pop();

      if (dist[pos] < d)
        continue;

      if (pos == g)
        break;

      rep(i, 4) {
        P &p = m[pos][i];
        int next = p.first;
        int nd = d + costs[p.second];
        if (dist.count(next) && dist[next] <= nd)
          continue;
        dist[next] = nd;
        q.push(MP(nd, next));
      }
    }

    cout << dist[g] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;

const int dx[] = {-1, 1, -3, 3};
int costs[4];

struct state {
  int cost;
  vector<int> v;
  bool operator<(const state &r) const { return cost > r.cost; }
};

int main() {
  int ch, cv;
  while (cin >> ch >> cv, ch | cv) {
    costs[0] = costs[1] = ch;
    costs[2] = costs[3] = cv;

    state s;
    s.v.resize(9);
    vector<int> g(9);
    rep(i, 9) cin >> s.v[i];
    rep(i, 9) cin >> g[i];

    map<vector<int>, int> dist;
    s.cost = 0;
    dist[s.v] = 0;
    priority_queue<state> q;
    q.push(s);
    while (!q.empty()) {
      state st = q.top();
      q.pop();

      if (dist[st.v] < st.cost)
        continue;

      if (st.v == g)
        break;

      int zpos = 0;
      rep(i, 9) if (!st.v[i]) zpos = i;

      rep(i, 4) {
        state next = st;
        swap(next.v[zpos], next.v[(9 + zpos + dx[i]) % 9]);
        next.cost += costs[i];
        if (dist.find(next.v) != dist.end())
          continue;
        dist[next.v] = next.cost;
        q.push(next);
      }
    }

    cout << dist[g] << endl;
  }

  return 0;
}
Problem p00918 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define sz size()
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> P;

const int adj[4] = {6, 1, 3, 8};

ll encode(vi v) {
  ll res = 0;
  rep(i, 9) res |= v[i] << (4 * i);
  return res;
}

vi decode(ll l) {
  vi res(9);
  rep(i, 9) res[i] = (l >> (4 * i)) & 15LL;
  return res;
}

int main() {
  int ch, cv;
  vi s(9), t(9);

  while (cin >> ch >> cv, ch) {
    rep(i, 9) cin >> s[i];
    rep(i, 9) cin >> t[i];

    map<ll, ll> d;
    ll ds = encode(s), dt = encode(t);
    d[ds] = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(P(0, ds));

    while (q.sz) {
      P p = q.top();
      q.pop();
      ll dis = p.fs;
      ds = p.sc;
      if (d[ds] < dis)
        continue;
      if (ds == dt)
        break;
      s = decode(ds);

      int pos = 0;
      rep(i, 9) {
        if (s[i] == 0)
          pos = i;
      }

      rep(i, 4) {
        int nxt = (adj[i] + pos) % 9;
        ll cost = (i & 1 ? ch : cv) + dis;
        swap(s[pos], s[nxt]);
        ds = encode(s);

        if (d.find(ds) == d.end() || d[ds] > cost) {
          d[ds] = cost;
          q.push(P(cost, ds));
        }
        swap(s[pos], s[nxt]);
      }
    }
    cout << d[dt] << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define sz size()
#define fs first
#define sc second
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll, vi> P;

const int adj[4] = {6, 1, 3, 8};

int main() {
  int ch, cv;
  vi s(9), t(9);

  while (cin >> ch >> cv, ch) {
    rep(i, 9) cin >> s[i];
    rep(i, 9) cin >> t[i];

    map<vi, ll> d;
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(P(0, s));

    while (q.sz) {
      P p = q.top();
      q.pop();
      ll dis = p.fs;
      s = p.sc;
      if (d[s] < dis)
        continue;
      if (s == t)
        break;

      int pos = 0;
      rep(i, 9) {
        if (s[i] == 0)
          pos = i;
      }
      rep(i, 4) {
        int nxt = (adj[i] + pos) % 9;
        ll cost = (i & 1 ? ch : cv) + dis;
        swap(s[pos], s[nxt]);
        if (d.find(s) == d.end() || d[s] > cost) {
          d[s] = cost;
          q.push(P(cost, s));
        }
        swap(s[pos], s[nxt]);
      }
    }
    cout << d[t] << endl;
  }
}
Problem p00918 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define mp(a, b) make_pair(a, b)

const int adj[] = {1, 3, -1, -3};
int ch, cv;

inline void bitswap(ll &b, int i, int j, int n) {
  ll x = ((b >> i) ^ (b >> j)) & ((1UL << n) - 1);
  b ^= ((x << i) | (x << j));
}

inline int zero(ll b) {
  for (int k = 8;; --k, b >>= 4)
    if (!(b & 0x0F))
      return k;
}

int solve(ll s, ll g) {
  map<ll, int> mem;
  typedef pair<int, ll> state;
  priority_queue<state, vector<state>, greater<state>> q;
  mem[s] = 0;
  for (q.push(mp(0, s)); !q.empty();) {
    state pzl = q.top();
    q.pop();
    if (pzl.second == g)
      break;
    int cur = zero(pzl.second);
    rep(k, 4) {
      int nxt = (cur + adj[k] + 9) % 9;
      int cost = pzl.first + (k & 1 ? cv : ch);
      bitswap(pzl.second, (8 - cur) * 4, (8 - nxt) * 4, 4);
      if (!mem.count(pzl.second) || cost < mem[pzl.second]) {
        q.push(mp(cost, pzl.second));
        mem[pzl.second] = cost;
      }
      bitswap(pzl.second, (8 - cur) * 4, (8 - nxt) * 4, 4);
    }
  }
  return mem[g];
}

ll in() {
  ll k, res = 0;
  for (int i = 32; i >= 0; res |= k << i, i -= 4)
    cin >> k;
  return res;
}

int main() {
  while (cin >> ch >> cv, ch | cv)
    cout << solve(in(), in()) << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(c) (c).begin(), (c).end()

const int adj[] = {1, 3, -1, -3};
int ch, cv;

inline void bitswap(ll &b, int i, int j, int n) {
  ll x = ((b >> i) ^ (b >> j)) & ((1UL << n) - 1);
  b ^= ((x << i) | (x << j));
}

inline int zero(ll b) {
  for (int k = 8;; --k, b >>= 4)
    if (!(b & 0x0F))
      return k;
}

int solve(ll s, ll g) {
  map<ll, int> mem;
  auto comp = [&](ll &a, ll &b) { return mem[b] < mem[a]; };
  priority_queue<ll, vector<ll>, decltype(comp)> q(comp);
  mem[g] = 362880 * 100;
  mem[s] = 0;
  for (q.push(s); !q.empty();) {
    ll pzl = q.top();
    q.pop();
    if (mem[g] <= mem[pzl])
      continue;
    int cur = zero(pzl);
    rep(k, 4) {
      int nxt = (cur + adj[k] + 9) % 9;
      int cost = mem[pzl] + (k & 1 ? cv : ch);
      bitswap(pzl, (8 - cur) * 4, (8 - nxt) * 4, 4);
      if (!mem.count(pzl) || cost < mem[pzl]) {
        q.push(pzl);
        mem[pzl] = cost;
      }
      bitswap(pzl, (8 - cur) * 4, (8 - nxt) * 4, 4);
    }
  }
  return mem[g];
}

ll in() {
  ll k, res = 0;
  for (int i = 32; i >= 0; res |= k << i, i -= 4)
    cin >> k;
  return res;
}

int main() {
  while (cin >> ch >> cv, ch | cv) {
    ll sd = in(), gd = in();
    cout << solve(sd, gd) << '\\n';
  }
  return 0;
}
Problem p00918 (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;
typedef pair<int, int> P;
int ds[4] = {1, 8, 3, 6};
int main() {
  while (true) {
    int h, w;
    cin >> w >> h;
    if (h == 0)
      break;
    int s = 0, g = 0;
    map<int, int> d;
    rep(i, 9) {
      int a;
      cin >> a;
      s = s * 9 + a;
    }
    rep(i, 9) {
      int a;
      cin >> a;
      g = g * 9 + a;
    }
    priority_queue<P, vector<P>, greater<P>> que;
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int a = p.sc;
      int c = p.fs;
      if (c != d[a])
        continue;
      if (a == g)
        break;
      int v[9];
      int ac = a;
      rep(i, 9) {
        v[8 - i] = ac % 9;
        ac /= 9;
      }
      int z = 0;
      rep(i, 9) if (v[i] == 0) z = i;
      rep(i, 4) {
        swap(v[z], v[(z + ds[i]) % 9]);
        int na = 0;
        rep(j, 9) na = na * 9 + v[j];
        swap(v[z], v[(z + ds[i]) % 9]);
        int cost = (i < 2 ? w : h);
        if (!d.count(na) || c + cost < d[na]) {
          d[na] = c + cost;
          que.push(P(c + cost, na));
        }
      }
    }
    cout << d[g] << endl;
  }
}

Error: TLE

Fixed submission:

#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;
typedef vector<int> vi;
typedef pair<int, vi> P;
int ds[4] = {1, 8, 3, 6};
int main() {
  while (true) {
    int h, w;
    cin >> w >> h;
    if (h == 0)
      break;
    vector<int> s, g;
    map<vi, int> d;
    rep(i, 9) {
      int a;
      cin >> a;
      s.pb(a);
    }
    rep(i, 9) {
      int a;
      cin >> a;
      g.pb(a);
    }
    priority_queue<P, vector<P>, greater<P>> que;
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      vi v = p.sc;
      int c = p.fs;
      if (c != d[v])
        continue;
      if (v == g)
        break;
      int z = 0;
      rep(i, 9) if (v[i] == 0) z = i;
      rep(i, 4) {
        vi nv = v;
        swap(nv[z], nv[(z + ds[i]) % 9]);
        int cost = (i < 2 ? w : h);
        if (!d.count(nv) || c + cost < d[nv]) {
          d[nv] = c + cost;
          que.push(P(c + cost, nv));
        }
      }
    }
    cout << d[g] << endl;
  }
}
Problem p00922 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> nums[500];
int dp[134001];

int go(vector<int> &vec) {
  int n = vec.size();
  if (n == 0)
    return 0;
  fill(dp, dp + 134001, -(1 << 30));
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 134000 / vec[i] * vec[i]; j - vec[i] >= 0; j -= vec[i]) {
      dp[j] = max(dp[j], dp[j - vec[i]] + 1);
    }
  }
  int res = 0;
  for (int i = 1; i < 134001; i *= 2) {
    res = max(res, dp[i]);
  }
  return res;
}

void solve(int n) {
  int a;
  int res = 0;
  for (int i = 0; i < 500; i++) {
    nums[i].clear();
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    int odd = a / (a & -a);
    int even = a / odd;
    nums[odd].push_back(even);
  }

  for (int i = 0; i < 500; i++) {
    res = max(res, go(nums[i]));
  }

  cout << res << endl;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    solve(n);
  }
  return 0;
}

Error: 0

Fixed submission:

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

vector<int> nums[500];
int dp[129000];

int go(vector<int> &vec) {
  fill(dp, dp + 129000, 0);
  int n = vec.size();
  for (int i = 0; i < n; i++) {
    for (int j = 129000 / vec[i] * vec[i]; j - vec[i] >= 0; j -= vec[i]) {
      dp[j] = max(dp[j], dp[j - vec[i]] + 1);
    }
  }
  int res = 0;
  for (int i = 1; i < 129000; i *= 2) {
    res = max(res, dp[i]);
  }
  return res;
}

void solve(int n) {
  int a;
  int res = 0;
  for (int i = 0; i < 500; i++) {
    nums[i].clear();
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    int odd = a / (a & -a);
    int even = a / odd;
    nums[odd].push_back(even);
  }

  for (int i = 0; i < 500; i++) {
    res = max(res, go(nums[i]));
  }

  cout << res << endl;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    solve(n);
  }
  return 0;
}
Problem p00922 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n;
  while (cin >> n, n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int ans = 0;

    vector<int> p[555];
    for (int i = 0; i < n; i++) {
      int x = a[i];
      while (x % 2 == 0)
        x /= 2;
      p[x].push_back(a[i] / x);
    }
    auto calc = [](vector<int> &a) {
      int sum = accumulate(a.begin(), a.end(), 0);
      vector<vector<int>> dp(2, vector<int>(sum + 1, -1));
      dp[0][0] = 0;
      int idx = 0;
      for (int x : a) {
        fill(dp[!idx].begin(), dp[!idx].end(), -1);
        for (int i = 0; i <= sum; i++) {
          dp[!idx][i] = max(dp[!idx][i], dp[idx][i]);
          if (~dp[idx][i] && i % x == 0 && i + x <= sum)
            dp[!idx][i + x] = max(dp[!idx][i + x], dp[idx][i] + 1);
        }
        idx = !idx;
      }
      int res = 0;
      for (int i = 1; i <= sum; i <<= 1)
        res = max(res, dp[idx][i]);
      return res;
    };
    for (int i = 0; i < 555; i++)
      if (!p[i].empty())
        ans = max(ans, calc(p[i]));
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n;
  while (cin >> n, n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int ans = 0;

    vector<int> p[555];
    for (int i = 0; i < n; i++) {
      int x = a[i];
      while (x % 2 == 0)
        x /= 2;
      p[x].push_back(a[i] / x);
    }
    auto calc = [](vector<int> &a) {
      int sum = accumulate(a.begin(), a.end(), 0);
      vector<vector<int>> dp(a.size() + 1, vector<int>(sum + 1, -1));
      dp[0][0] = 0;
      int idx = 0;
      for (int x : a) {
        for (int i = 0; i <= sum; i++) {
          dp[idx + 1][i] = max(dp[idx + 1][i], dp[idx][i]);
          if (~dp[idx][i] && i % x == 0 && i + x <= sum)
            dp[idx + 1][i + x] = max(dp[idx + 1][i + x], dp[idx][i] + 1);
        }
        idx++;
      }
      int res = 0;
      for (int i = 1; i <= sum; i <<= 1)
        res = max(res, dp[idx][i]);
      return res;
    };
    for (int i = 0; i < 555; i++)
      if (!p[i].empty())
        ans = max(ans, calc(p[i]));
    cout << ans << endl;
  }
  return 0;
}
Problem p00922 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

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

const int MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;

int main() {
  for (;;) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<int> a(N);
    rep(i, N) cin >> a[i];
    reverse(a.begin(), a.end());
    vector<vector<int>> as(501);
    for (int x : a) {
      int y;
      for (y = x; y % 2 == 0; y /= 2)
        ;
      as[y].pb(x / y);
    }
    int ans = 0;
    for (vector<int> a : as) {
      int M = 0;
      for (int x : a)
        M += x;
      vector<int> dp(M + 1, -INF);
      dp[0] = 0;
      for (int x : a) {
        vector<int> _dp = dp;
        for (int S = 0; S + x <= M; S += x)
          _dp[S + x] = max(_dp[S + x], dp[S] + 1);
        dp = _dp;
      }
      for (int S = 1; S <= M; S *= 2)
        ans = max(ans, dp[S]);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

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

const int MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;

const int M = 20;

int main() {
  for (;;) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<int> a(N);
    rep(i, N) cin >> a[i];
    reverse(a.begin(), a.end());
    vector<vector<int>> as(501);
    for (int x : a) {
      int y;
      for (y = x; y % 2 == 0; y /= 2)
        ;
      as[y].pb(x / y);
    }
    int ans = 0;
    for (vector<int> a : as) {
      vector<int> dp(1 << M, -INF);
      dp[0] = 0;
      for (int x : a) {
        vector<int> _dp = dp;
        for (int S = 0; S < 1 << M; S += x)
          _dp[S + x] = max(_dp[S + x], dp[S] + 1);
        dp = _dp;
      }
      rep(k, M) ans = max(ans, dp[1 << k]);
    }
    cout << ans << endl;
  }
}
Problem p00924 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
// typedef pair<int,int>P;

int N, M, P, r1, r2;
vector<int> B, a1, a2;

int ch(vector<int> o, vector<int> s) {
  int c = 0, r = 0;
  for (int i = 0; i < o.size(); i++)
    if (o[i] != s[i])
      c++;
  if (c % 2)
    return 1000000000;
  for (int i = 0; i < o.size(); i++) {
    if (o[i] != s[i]) {
      for (int j = i + 1; j < o.size(); j++) {
        if (o[j] != s[j] && o[i] != o[j]) {
          r += j - i;
          swap(o[i], o[j]);
          break;
        }
      }
    }
    if (o[i] != s[i])
      return 1000000000;
  }
  return r;
}

int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++) {
    int t;
    scanf("%d", &t);
    B.push_back(t);
  }
  for (int i = 0; i < M; i++) {
    scanf("%d", &P);
    for (int j = 0; j < P; j++) {
      a1.push_back(i % 2);
      a2.push_back((i + 1) % 2);
    }
  }
  printf("%d\\n", min(ch(B, a1), ch(B, a2)));
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
// typedef pair<int,int>P;

typedef struct D {
  int s;
  vector<int> b;
  bool operator<(const D &a) const { return s < a.s; };
} D;

int N, M, P, r1, r2;
vector<int> B, a1, a2;
map<int, int> flg;
int b2i(vector<int> s) {
  int r = 0;
  for (int i = 0; i < s.size(); i++, r *= 2)
    r += s[i];
  return r;
}

int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++) {
    int t;
    scanf("%d", &t);
    B.push_back(t);
  }
  for (int i = 0; i < M; i++) {
    scanf("%d", &P);
    for (int j = 0; j < P; j++) {
      a1.push_back(i % 2);
      a2.push_back((i + 1) % 2);
    }
  }
  r1 = b2i(a1);
  r2 = b2i(a2);
  queue<D> Q;
  D tmp = {0, B};
  Q.push(tmp);
  while (!Q.empty()) {
    tmp = Q.front();
    Q.pop();
    int f = b2i(tmp.b);
    if (f == r1 || f == r2)
      break;
    if (flg.find(f) != flg.end() && flg[f] < tmp.s)
      continue;
    flg[f] = tmp.s;
    for (int i = 0; i < N - 1; i++) {
      vector<int> t = tmp.b;
      swap(t[i], t[i + 1]);
      if (flg.find(b2i(t)) == flg.end() || flg[b2i(t)] < tmp.s + 1) {
        D ins = {tmp.s + 1, t};
        Q.push(ins);
      }
    }
  }
  printf("%d\\n", tmp.s);
}