Problem p00922 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <utility>

using namespace std;

const int MAXN = 1000;
int n;
int a[MAXN], b[MAXN], c[MAXN];
bool v[MAXN];
int f[2][1 << 20];
int que[2][1 << 20], head[2], tail[2];
int mark[1 << 20];
int ans;

void init() {
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    b[i] = 0;
    while (~a[i] & 1) {
      ++b[i];
      a[i] >>= 1;
    }
  }
}

int dp(int a[], int n) {
  static int stamp = 0;
  f[0][0] = 0;
  head[0] = tail[0] = 0;
  f[0][tail[0]++] = 0;
  for (int i = 0; i < n; ++i) {
    ++stamp;
    head[~i & 1] = tail[~i & 1] = 0;
    while (head[i & 1] < tail[i & 1]) {
      int mask = que[i & 1][head[i & 1]++];
      if (mark[mask] != stamp) {
        f[~i & 1][mask] = f[i & 1][mask];
        que[~i & 1][tail[~i & 1]++] = mask;
        mark[mask] = stamp;
      } else {
        f[~i & 1][mask] = max(f[~i & 1][mask], f[i & 1][mask]);
      }
      if (mask & ((1 << a[i]) - 1))
        continue;
      int nmask = mask + (1 << a[i]);
      if (mark[nmask] != stamp) {
        f[~i & 1][nmask] = f[i & 1][mask] + 1;
        que[~i & 1][tail[~i & 1]++] = nmask;
        mark[nmask] = stamp;
      } else {
        f[~i & 1][nmask] = max(f[~i & 1][nmask], f[i & 1][mask] + 1);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < 20; ++i) {
    if (mark[1 << i] == stamp)
      ans = max(ans, f[n & 1][1 << i]);
  }
  return ans;
}

void solve() {
  ans = 0;
  memset(v, 0, sizeof(v));
  for (int i = 0; i < n; ++i) {
    if (v[i])
      continue;
    int m = 0;
    for (int j = i; j < n; ++j) {
      if (a[i] == a[j])
        c[m++] = b[j], v[j] = true;
    }
    ans = max(ans, dp(c, m));
  }
}

int main() {
  while (scanf("%d", &n) != EOF && n) {
    init();
    solve();
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
vector<int> V[506];
int dp[2][(1 << 18) + 6];
int find() {
  int i, j, k, x, mask;
  int prev, now, n, mv;
  int ans = 0;
  for (i = 1; i <= 500; ++i) {
    memset(dp, 0, sizeof(dp));
    prev = 0, now = 1, mv = 0;
    for (j = 0; j < V[i].size(); ++j) {
      x = 1 << V[i][j];
      mask = (x - 1);
      dp[now][x] = 1;
      for (k = 0; k <= mv + 1; ++k) {
        if (!dp[prev][k])
          continue;
        if (mask & k)
          continue;
        dp[now][k + x] = max(dp[now][k + x], dp[prev][k] + 1);
        dp[now][k] = max(dp[now][k], dp[prev][k]);
        dp[prev][k] = 0;
      }
      mv += x;
      mv = max(mv, 1 << 18);
      prev = now;
      now = 1 - prev;
    }
    for (j = 0; j <= 18; ++j)
      ans = max(ans, dp[prev][1 << j]);
  }
  return ans;
}
int main() {
  int N, i, c, x;
  while (scanf("%d", &N) != EOF && N) {
    for (i = 1; i <= 500; ++i)
      V[i].clear();
    for (i = 1; i <= N; ++i) {
      scanf("%d", &x);
      c = 0;
      while (x % 2 == 0) {
        ++c;
        x /= 2;
      }
      V[x].push_back(c);
    }
    printf("%d\\n", find());
  }
  return 0;
}