Problem:
ビートパネル
遊太君は、近所のゲームセンターで人気のゲーム「ビートパネル」にはまっています。このゲームは図のようなグリッド状に並べられた 4×4 の計16個のパネル型のボタンからなります。
図のように、左上から右下にボタン1, ボタン2, …, ボタン16の順にボタンが並んでいます。ゲームでは一定間隔でビート音が鳴り最後に終了音が鳴ります。ビート音が鳴ると同時に複数のボタンが光ります。１個も光らない場合もあります。プレイヤーは、ビート音が鳴った直後から次の音が鳴るまでの間に両手の指を使って複数のボタンを１度だけ同時に押すことができます。何も押さないということも可能です。終了音が鳴ったと同時にゲームは終了します。
遊太君は c 通りの押し方を習得しており、ビート音が鳴る度に、それらの押し方の中から１つ決めてボタンを押します。押したボタンが光っていれば、それらのボタンの光は消え、消えたボタンの数がプレイヤーのスコアに加算されます。また、一度光ったボタンはそのボタンが押されるまで光は消えることはありません。
ビート音が n 回鳴るときのボタンの光り方と遊太君が習得している c 通りのボタンの押し方を入力とし、遊太君が獲得することのできる得点の最大値を出力するプログラムを作成してください。
入力
入力は複数のデータセットからなる。入力の終わりはゼロ２つの行で示される。各データセットは以下の形式で与えられる。
n c
a
1,1
a
1,2
... a
1,16
a
2,1
a
2,2
... a
2,16
...
a
n,1
a
n,2
... a
n,16
b
1,1
b
1,2
... b
1,16
b
2,1
b
2,2
... b
2,16
...
b
c,1
b
c,2
... b
c,16
１行目は１つの空白で区切られた２つの整数からなる。n (1 ≤ n ≤ 30) はビート音の数、c (1 ≤ c ≤ 30) は習得しているボタンの押し方の数を示す。続く n＋c 行にボタンの光り方とボタンの押し方が与えられる。行中の入力項目の区切りは１つの空白である。a
k,i
は k 回目のビート音が鳴ったときのボタン i の光り方を、b
j,i
は c 通り中 j 番目の押し方でボタン i が押されるかどうかを表す。a
k,i
の値は 0 が「光らない」、1 が「光る」を表す。b
j,i
の値は 0 が「押さない」、1 が「押す」を表す。
データセットの数は 20 を超えない。
出力
各データセットごとに、得点の最大値を１行に出力する。
入力例
2 2
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
2 2
0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 0
0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0
0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0
5 3
0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0
1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
0 0
出力例
8
7
16

Submitted solutions:

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/07/25  Problem: AOJ 0263 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0263  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

template<class T> using V = vector <T>; template <class T> using VV = V<V<T>>;


int dp[32][1 << 16];
// U I - N U I - N beet beet ...
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int N, C;
	while (cin >> N >> C, N) {
		V<int>quiz(N);
		FOR(i, 0, N) {
			int ret = 0;
			FOR(j, 0, 16) {
				int a; cin >> a;
				ret *= 2;
				ret += a;
			}
			quiz[i] = ret;
		}
		V<int>pat(C);
		FOR(i, 0, C) {
			int ret = 0;
			FOR(j, 0, 16) {
				int a; cin >> a;
				ret *= 2;
				ret += a;
			}
			pat[i] = ret;
		}
		int ans = 0;
		// NC2^16で max add dp
		fill(*dp, *dp +  32 * (1 << 16), -1);
		dp[0][0] = 0;
		FOR(i, 0, N) {
			FOR(state, 0, 1 << 16) {
				if (dp[i][state] == -1)continue;
				int nstate = state | quiz[i];
				FOR(j, 0, C) {
					int add = nstate & pat[j];
					int nnstate = nstate  - add ;
					dp[i + 1][nnstate] = max(dp[i + 1][nnstate], dp[i][state] + __builtin_popcount(add));
				}
				dp[i + 1][nstate] = max(dp[i + 1][nstate], dp[i][nstate]);
			}
		}
		FOR(i, 0, 1 << 16) {
			ans = max(ans, dp[N][i]);
		}
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 11660
final_rank: 4

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/07/25  Problem: AOJ 0263 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0263  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

template<class T> using V = vector <T>; template <class T> using VV = V<V<T>>;


int dp[32][1 << 16];
// ういーんういーん
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int N, C;
	while (cin >> N >> C, N) {
		V<int>quiz(N);
		FOR(i, 0, N) {
			int ret = 0;
			FOR(j, 0, 16) {
				int a; cin >> a;
				ret *= 2;
				ret += a;
			}
			quiz[i] = ret;
		}
		V<int>pat(C);
		FOR(i, 0, C) {
			int ret = 0;
			FOR(j, 0, 16) {
				int a; cin >> a;
				ret *= 2;
				ret += a;
			}
			pat[i] = ret;
		}
		int ans = 0;
		// NC2^16で max add dp
		fill(*dp, *dp +  32 * (1 << 16), -1);
		dp[0][0] = 0;
		FOR(i, 0, N) {
			FOR(state, 0, 1 << 16) {
				if (dp[i][state] == -1)continue;
				int nstate = state | quiz[i];
				FOR(j, 0, C) {
					int add = nstate & pat[j];
					int nnstate = nstate  - add ;
					dp[i + 1][nnstate] = max(dp[i + 1][nnstate], dp[i][state] + __builtin_popcount(add));
				}
				dp[i + 1][nstate] = max(dp[i + 1][nstate], dp[i][nstate]);
			}
		}
		FOR(i, 0, 1 << 16) {
			ans = max(ans, dp[N][i]);
		}
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 11584
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 10000;
int main(){
  while (true){
    int n, c;
    cin >> n >> c;
    if (n == 0 && c == 0){
      break;
    }
    vector<vector<int>> a(n, vector<int>(16));
    for (int i = 0; i < n; i++){
      for (int j = 0; j < 16; j++){
        cin >> a[i][j];
      }
    }
    vector<vector<int>> b(c, vector<int>(16));
    for (int i = 0; i < c; i++){
      for (int j = 0; j < 16; j++){
        cin >> b[i][j];
      }
    }
    vector<int> A(n, 0);
    for (int i = 0; i < n; i++){
      for (int j = 0; j < 16; j++){
        if (a[i][j] == 1){
          A[i] |= 1 << j;
        }
      }
    }
    vector<int> B(c, 0);
    for (int i = 0; i < c; i++){
      for (int j = 0; j < 16; j++){
        if (b[i][j] == 1){
          B[i] |= 1 << j;
        }
      }
    }
    vector<vector<int>> dp(n + 1, vector<int>(1 << 16, -INF));
    dp[0][0] = 0;
    for (int i = 0; i < n; i++){
      for (int j = 0; j < (1 << 16); j++){
        if (dp[i][j] != -INF){
          int p = j | A[i];
          for (int k = 0; k < c; k++){
            int s = __builtin_popcount(p & B[k]);
            int p2 = p & ~B[k];
            dp[i + 1][p2] = max(dp[i + 1][p2], dp[i][j] + s);
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++){
      ans = max(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 140
memory_kb: 11372
final_rank: 5

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int dp[33][(1<<16)];
int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n, c; 
  while(cin >> n >> c) {
    if(n == 0 &&c == 0) break;

    vector<int> mask1(n);
    REP(i,n) {
      REP(j,16) {
        int a; cin >> a;
        mask1[i] = mask1[i]*2 + a;
      }
    }
    vector<int> mask2(c);
    REP(i,c) {
      REP(j,16) {
        int a; cin >> a;
        mask2[i] = mask2[i]*2 + a;
      }
    }
    REP(i,n+1) REP(j,(1<<16)) dp[i][j] = -1;
    dp[0][0] = 0;
    REP(i,n) {
      REP(bit,(1<<16)) {
        if(dp[i][bit] == -1) continue;
        int mask = bit | mask1[i];
        REP(j,c) {
          int score = __builtin_popcount(mask & mask2[j]);
          int nxt = mask & ~(mask & mask2[j]);
          dp[i+1][nxt] = max(dp[i+1][nxt], dp[i][bit] + score);
        }
        dp[i+1][bit] = max(dp[i+1][bit], dp[i][bit]);
      }
    }
    int ans = 0;
    REP(i,(1<<16)) ans = max(ans, dp[n][i]);
    cout << ans << '\n';
  }
  
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 11532
final_rank: 1

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<climits>
#include<iomanip>
#include<cctype>
#include<sstream>
#include<regex>
#include<bitset>
#include<random>
#include<complex>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
constexpr int mod=1e9+7;
constexpr int Mod=998244353;
constexpr int inf=mod;
constexpr ll linf=(ll)mod*mod;
struct fastio{
	fastio(){
		cin.tie(0);
		ios::sync_with_stdio(0);
		cout<<fixed<<setprecision(20);
	};
}fio;
template<class T>
bool chmax(T&a,const T&b){
	if(a<b){
		a=b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a,const T&b){
	if(a>b){
		a=b;
		return 1;
	}
	return 0;
}
int dp[31][65536];
int main(){
	int n,c;
	while(cin>>n>>c,n){
		int a[30]={},b[30]={};
		bool f;
		for(int i=0;i<n;i++){
			for(int j=0;j<16;j++){
				cin>>f;
				if(f)
					a[i]|=1<<j;
			}
		}
		for(int i=0;i<c;i++){
			for(int j=0;j<16;j++){
				cin>>f;
				if(f)
					b[i]|=1<<j;
			}
		}
		for(int i=0;i<=n;i++)
			for(int j=0;j<1<<16;j++)
				dp[i][j]=-inf;
		dp[0][0]=0;
		for(int i=0;i<n;i++)
			for(int j=0;j<1<<16;j++)
				for(int k=0;k<c;k++)
					chmax(dp[i+1][(j|a[i])&~b[k]],dp[i][j]+__builtin_popcount((j|a[i])&b[k]));
		cout<<*max_element(dp[n],dp[n]+(1<<16))<<endl;
	}
}
accuracy: 1
time_ms: 1360
memory_kb: 11324
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int pcount(int a){
    int ans = 0;
    for(int i = 0; i < 16; i++){
        if(a & (1 << i)) ans++;
    }
    return ans;
}

void chmax(int &a, int x){
    if(a < x) a = x;
}

int main(){
    while(true){
        int n, c;
        cin >> n >> c;
        if(n == 0 && c == 0) break;
        vector<int> a(n), b(c);
        for(auto &p: a){
            for(int i = 0; i < 16; i++){
                int aa;
                cin >> aa;
                if(aa){
                    p |= 1 << i;
                }
            }
        }
        a.push_back(0);
        for(auto &p: b){
            for(int i = 0; i < 16; i++){
                int aa;
                cin >> aa;
                if(aa){
                    p |= 1 << i;
                }
            }
        }
        b.push_back(0);

        vector<vector<int>> dp(n+1, vector<int>(1 << 16, -1001001001));
        dp[0][a[0]] = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < (1 << 16); j++){
                if(dp[i][j] < 0) continue;
                for(auto &p: b){
                    chmax(dp[i+1][(j&(~p))|a[i+1]], dp[i][j]+pcount(j&p));
                }
            }
        }

        int ans = -1001001001;
        for(int i = 0; i < (1 << 16); i++) chmax(ans, dp[n][i]);
        cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 11416
final_rank: 9

code_snippet: #define _overload(_1, _2, _3, _4, name, ...) name
#define _rep1(Itr, N) _rep3(Itr, 0, N, 1)
#define _rep2(Itr, A, B) _rep3(Itr, A, B, 1)
#define _rep3(Itr, A, B, S) for(i64 (Itr) = A; (Itr) < B; (Itr) += S)
#define repeat(...) _overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define rep(...) _overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)

#include <bits/stdc++.h>
using i64 = long long;
using u32 = unsigned int;
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	i64 n, c;
	while (cin >> n >> c, n > 0) {
		vector<u32> a(n);
		for (auto &vs: a) {
			i64 bit;
			rep(i, 16) {
				cin >> bit;
				vs |= bit;
				vs <<= 1;
			}
		}

		vector<u32> b(c);
		for (auto &vs: b) {
			i64 bit;
			rep(i, 16) {
				cin >> bit;
				vs |= bit;
				vs <<= 1;
			}
		}

		auto popcnt = [](u32 b) {
			u32 ret = 0;
			while (b) {
				if (b & 1) ret++;
				b >>= 1;
			}
			return ret;
		};

		vector<vector<i64>> dp(n, vector<i64>(1 << 17, -1));
		auto f = [&](auto &&df, int t, u32 board) -> i64 {
			if (t >= n) return 0;
			if (dp[t][board] != -1) return dp[t][board];

			i64 nb = board | a[t];
			i64 ret = 0;
			rep(i, c) {
				ret = max(ret,
						df(df, t + 1, nb ^ (nb & b[i])) + popcnt(nb & b[i]));
			}
			return dp[t][board] = ret;
		};

		cout << f(f, 0, 0) << endl;
	}

	return 0;
};
accuracy: 1
time_ms: 490
memory_kb: 35660
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(long long i = (a)*(strlen(#__VA_ARGS__)!=0);i<(long long)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(long long i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(long long)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

template <class T>
vector<T> make_vector(int n,T init){
    return vector<T>(n,init);
}

template <class... tail>
auto make_vector(int n, tail... args){
    return vector<decltype(make_vector(args...))>(n,make_vector(args...));
}

int input(){
    int res=0;
    rep(i,16){
        res = res * 2 + in();
    }
    return res;
}

int func(int n,int c){
    vector<int> A(n);
    vector<int> B(c);
    foreach(i,A)i=input();
    foreach(i,B)i=input();
    vvector<int> dp(n+1,vector<int>(bit(16),-INF));
    dp[0][0] = 0;
    rep(i,n){
        rep(c,bit(16)){
            foreach(j,B){
                chmax(dp[i+1][(c|A[i])&~j],dp[i][c]+__builtin_popcount((c|A[i])&j));
            }
        }
    }
    int res = 0;
    foreach(i,dp[n])chmax(res,i);
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(true){
        int n = in();
        int c = in();
        if(n==0)break;
        println(func(n,c));
    }
    return 0;
}
accuracy: 1
time_ms: 1200
memory_kb: 11500
final_rank: 15

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

int popcount(int n){
    n = (n & 0x55555555) + (n >> 1 & 0x55555555);
    n = (n & 0x33333333) + (n >> 2 & 0x33333333);
    n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
    n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
    return (n & 0x0000ffff) + (n >>16 & 0x0000ffff);
}

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

        vector<int> a(n);
        for(int i=0; i<n; i++){
            for(int j=0; j<16; j++){
                int c;
                cin >> c;
                a[i] <<= 1;
                a[i] |= c;
            }
        }
        vector<int> b(m);
        for(int i=0; i<m; i++){
            for(int j=0; j<16; j++){
                int c;
                cin >> c;
                b[i] <<= 1;
                b[i] |= c;
            }
        }
        vector<vector<int>> dp(n+1, vector<int>(1<<16, -1));
        dp[0][0] = 0;
        for(int i=0; i<n; i++){
            for(int j=0; j<(1<<16); j++){
                if(dp[i][j] == -1) continue;
                for(int k=0; k<m; k++){
                    int l = j|a[i];
                    int p = popcount(l&b[k]);
                    int r = l&(-b[k]-1);
                    dp[i+1][r] = max(dp[i+1][r], dp[i][j]+p);
                }
            }
        }
        cout << *max_element(dp[n].begin(), dp[n].end()) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 11260
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define each(i, a) for(auto &i : a)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#define line cout << "-----------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }

int main() {
    init();

    VI popcnt(1 << 16);
    FOR(bit, 0, 1 << 16)FOR(i, 0, 16) {
        if (~bit & (1 << i))
            popcnt[bit | (1 << i)] = popcnt[bit] + 1;
    }

    for (int n, c; cin >> n >> c && n;) {
        VI a(n + 1), b(c);
        FOR(i, 0, n)FOR(j, 0, 16) {
            a[i] <<= 1;
            int x; cin >> x;
            a[i] += x;
        }
        FOR(i, 0, c)FOR(j, 0, 16) {
            b[i] <<= 1;
            int x; cin >> x;
            b[i] += x;
        }
        auto dp = defvec<int>(-INF, n + 1, 1 << 16);
        dp[0][a[0]] = 0;
        FOR(i, 0, n)FOR(bit, 0, 1 << 16){
            chmax(dp[i + 1][bit | a[i + 1]], dp[i][bit]);
            FOR(j, 0, c)
                chmax(dp[i + 1][(bit ^ (bit & b[j])) | a[i + 1]],
                    dp[i][bit] + popcnt[bit & b[j]]);
        }
        print(*max_element(all(dp[n])));
    }

    return 0;
}
accuracy: 1
time_ms: 560
memory_kb: 11336
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define each(i, a) for(auto &i : a)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#define line cout << "-----------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }

int main() {
    init();

    VI popcnt(1 << 16);
    FOR(bit, 0, 1 << 16)FOR(i, 0, 16) {
        if (~bit & (1 << i))
            popcnt[bit | (1 << i)] = popcnt[bit] + 1;
    }

    for (int n, c; cin >> n >> c && n;) {
        VI a(n + 1), b(n);
        FOR(i, 0, n)FOR(j, 0, 16) {
            a[i] <<= 1;
            int x; cin >> x;
            a[i] += x;
        }
        FOR(i, 0, c)FOR(j, 0, 16) {
            b[i] <<= 1;
            int x; cin >> x;
            b[i] += x;
        }
        auto dp = defvec<int>(-INF, n + 1, 1 << 16);
        dp[0][a[0]] = 0;
        FOR(i, 0, n)FOR(bit, 0, 1 << 16){
            chmax(dp[i + 1][bit | a[i + 1]], dp[i][bit]);
            FOR(j, 0, c)
                chmax(dp[i + 1][(bit ^ (bit & b[j])) | a[i + 1]],
                    dp[i][bit] + popcnt[bit & b[j]]);
        }
        print(*max_element(all(dp[n])));
    }

    return 0;
}
accuracy: 0.8333333333333334
time_ms: 90
memory_kb: 11460
final_rank: 20

code_snippet: #include "bits/stdc++.h"

// Begin Header {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

#define all(x) x.begin(), x.end()
#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)
#define reps(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

constexpr size_t    operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t  operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };

constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;

template <class T, class Compare = less<>>
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template <class T, class Compare = greater<>>
using MinHeap = priority_queue<T, vector<T>, Compare>;

inline void input() {}
template <class Head, class... Tail>
inline void input(Head&& head, Tail&&... tail) {
    cin >> head;
    input(forward<Tail>(tail)...);
}

template <class T>
inline istream& operator>>(istream &is, vector<T> &vec) {
    for (auto &e: vec) {
        is >> e;
    }
    return is;
}

inline void output() { cout << "\n"; }
template <class Head, class... Tail>
inline void output(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) {
        cout << " ";
    }
    output(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<<(ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = {" ", ""};
    for (const auto &e: vec) {
        os << e << delim[&e == &vec.back()];
    }
    return os;
}

template <class T>
inline vector<T> makeVector(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeVector(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeVector<T>(initValue, args...))>(sz, makeVector<T>(initValue, args...));
}

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class Container>
struct reverse_t {
    Container &c;
    reverse_t(Container &c) : c(c) {}
    auto begin() { return c.rbegin(); }
    auto end() { return c.rend(); }
};

template <class Container>
auto reversed(Container &c) {
    return reverse_t<Container>(c);
}

template <class T>
inline bool chmax(T &a, const T &b) noexcept {
    return b > a && (a = b, true);
}

template <class T>
inline bool chmin(T &a, const T &b) noexcept {
    return b < a && (a = b, true);
}

template <class T>
inline T diff(const T &a, const T &b) noexcept {
    return a < b ? b - a : a - b;
}
// }}} End Header

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios_base::fixed);
    cout.precision(10);

    int N, C;
    while (cin >> N >> C) {
        if (N + C == 0) {
            break;
        }

        auto a = makeVector<int>(0, N, 16);
        auto b = makeVector<int>(0, C, 16);
        input(a, b);

        vector<int> as(N), bs(C);
        rep(i, 0, N) {
            rep(j, 0, 16) {
                if (a[i][j]) as[i] |= (1 << j);
            }
        }
        // rep(i, 0, C) {
        //     rep(j, 0, 16) {
        //         bs[i] |= (1 << j);
        //     }
        // }

        auto dp = makeVector<int>(-1, N, 1 << 17);
        const auto res = makeFixPoint([&](auto rec, int i, int mask) -> int {
            if (i == N)
                return 0;
            auto &ret = dp[i][mask];
            if (~ret)
                return ret;
            ret = 0;
            const int cur_mask = as[i] | mask;
            chmax(ret, rec(i + 1, cur_mask));
            rep(j, 0, C)
            {
                int cnt = 0;
                int nxt_mask = cur_mask;
                rep(k, 0, 16)
                {
                    if (nxt_mask >> k & b[j][k])
                    {
                        cnt++;
                        nxt_mask ^= (1 << k);
                    }
                }
                chmax(ret, rec(i + 1, nxt_mask) + cnt);
            }
            return ret;
        })(0, 0);

        output(res);
    }

    return 0;
}
accuracy: 1
time_ms: 950
memory_kb: 18828
final_rank: 14

code_snippet: #include<bits/stdc++.h>

using namespace std;

int N, C;
int LightInfo[30];
int ButtonInfo[30];
int DpScore[31][(1 << 16)];

int popcnt(int u) {
	int ret;

	ret = 0;
	while (u) {
		u = (u & (u - 1));
		++ret;
	}

	return ret;
}

void solve() {
	int bits, total;

	fill_n(*DpScore, 31 * ((1 << 16)), -1);
	DpScore[0][0] = 0;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < ((1 << 16)); ++j) {
			if (DpScore[i][j] == -1) {
				continue;
			}
			for (int k = 0; k < C; ++k) {
				bits = (LightInfo[i] | j) & (~ButtonInfo[k]);
				DpScore[i + 1][bits] = max(DpScore[i + 1][bits], DpScore[i][j] + popcnt((LightInfo[i] | j)&ButtonInfo[k]));
			}
		}
	}

	total = *max_element(DpScore[0], DpScore[0] + 31 * (1 << 16));
	cout << total << endl;
}

int main() {
	int num;

	while (cin >> N >> C, N) {
		memset(LightInfo, 0, sizeof(LightInfo));
		memset(ButtonInfo, 0, sizeof(ButtonInfo));
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < 16; ++j) {
				cin >> num;
				if (num == 1) {
					LightInfo[i] |= (1 << j);
				}
			}
		}
		for (int i = 0; i < C; ++i) {
			for (int j = 0; j < 16; ++j) {
				cin >> num;
				if (num == 1) {
					ButtonInfo[i] |= (1 << j);
				}
			}
		}

		solve();
	}

	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 11020
final_rank: 8

code_snippet: #include<bits/stdc++.h>

using namespace std;

int N, C;
int LightInfo[30];
int ButtonInfo[30];
int DpScore[31][1 << 16];

int popcnt(int u) {
	int ret;

	ret = 0;
	while (u) {
		u = (u & (u - 1));
		++ret;
	}

	return ret;
}

void solve() {
	int bits, total;

	fill_n(*DpScore, 31 * (1 << 16), -1);
	DpScore[0][0] = 0;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < (1 << 16); ++j) {
			if (DpScore[i][j] == -1) {
				continue;
			}
			for (int k = 0; k < C; ++k) {
				bits = (LightInfo[i] | j) & (~ButtonInfo[k]);
				DpScore[i + 1][bits] = max(DpScore[i + 1][bits], DpScore[i][j] + popcnt((LightInfo[i] | j)&ButtonInfo[k]));
			}
		}
	}

	total = *max_element(DpScore[0], DpScore[0] + 31 * (1 << 16));
	cout << total << endl;
}

int main() {
	int num;

	while (cin >> N >> C, N) {
		memset(LightInfo, 0, sizeof(LightInfo));
		memset(ButtonInfo, 0, sizeof(ButtonInfo));
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < 16; ++j) {
				cin >> num;
				if (num == 1) {
					LightInfo[i] |= (1 << j);
				}
			}
		}
		for (int i = 0; i < C; ++i) {
			for (int j = 0; j < 16; ++j) {
				cin >> num;
				if (num == 1) {
					ButtonInfo[i] |= (1 << j);
				}
			}
		}

		solve();
	}

	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 11016
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

using A = array<int, 1<<16>;
int main() {
    int n, c;
    while(cin >> n >> c, n){
        vector<int> a(n), b(c);
        for (int i = 0; i < n; ++i) {
            int val = 0;
            for (int j = 0; j < 16; ++j) {
                int x;
                cin >> x;
                val |= x << j;
            }
            a[i] = val;
        }
        for (int i = 0; i < c; ++i) {
            int val = 0;
            for (int j = 0; j < 16; ++j) {
                int x;
                cin >> x;
                val |= x << j;
            }
            b[i] = val;
        }
        vector<int> cnt(1 << 16);
        for (int k = 0; k < 1<<16; ++k) {
            for (int i = 0; i < 16; ++i) {
                if(k & (1 << i)) cnt[k] = cnt[k^(1 << i)]+1;
            }
        }
        vector<A> dp(n+1);
        dp[0][0] = 1;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < 1 << 16; ++j) {
                if(!dp[i][j]) continue;
                int jj = j|a[i];
                for (int k = 0; k < c; ++k) {
                    chmax(dp[i+1][jj^(jj&b[k])], dp[i][j]+cnt[jj&b[k]]);
                }
            }
        }
        cout << *max_element(dp.back().begin(),dp.back().end())-1 << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 18088
final_rank: 10

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

using ll = long long int;
using P = pair<int, int>;
using P3 = pair<int, P>;
using PP = pair<P, P>;
constexpr int INF = 1 << 30;
constexpr ll MOD = ll(1e9) + 7;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};

void chmax(int &a, int b){
    a = max(a,b);
}

bool solve(){
    int n, c;
    cin >> n >> c;
    if(n==0) return true;
    vector<int> a(n), b(c);
    for(int i=0;i<n;i++){
        for(int j=0;j<16;j++){
            int x;
            cin >> x;
            a[i] |= x<<j;
        }
    }
    for(int i=0;i<c;i++){
        for(int j=0;j<16;j++){
            int x;
            cin >> x;
            b[i] |= x<<j;
        }
    }
    vector<vector<int> > dp(n+1, vector<int>(1<<16,-1));
    dp[0][0] = 0;
    for(int i=0;i<n;i++){
        for(int S=0;S<1<<16;S++){
            if(dp[i][S] < 0) continue;
            int T = S|a[i];
            chmax(dp[i+1][T], dp[i][S]);
            for(int j=0;j<c;j++){
                int X = T&b[j], cnt = 0;
                for(int k=0;k<16;k++){
                    if((X>>k)&1) cnt++;
                }
                chmax(dp[i+1][T^X], dp[i][S]+cnt);
            }
        }
    }
    int ans = 0;
    for(int S=0;S<1<<16;S++){
        chmax(ans, dp[n][S]);
    }
    cout << ans << endl;
    return false;
}

int main() {
    while(!solve());
    return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 11024
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define INF 1000000

int n, c;
int a[40];
int b[40];

int bitcount(uint bits) {
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >> 16);
}

int main() {
    while (true) {
        cin >> n >> c;
        
        if (n==0 && c==0) {
            break;
        }
        
        for (int i=0; i<n; i++) {
            a[i] = 0, b[i] = 0;
        }
        
        for (int i=0; i<n; i++) {
            for (int j=0; j<16; j++) {
                int aij; cin >> aij;
                a[i] += aij<<j;
            }
        }
        
        for (int i=0; i<c; i++) {
            for (int j=0; j<16; j++) {
                int bij; cin >> bij;
                b[i] += bij<<j;
            }
        }
        
        int dp[n+1][1<<16];
        
        for (int i=0; i<=n; i++) {
            for (int j=0; j<(1<<16); j++) {
                dp[i][j] = -INF;
            }
        }
        
        dp[0][0] = 0;
        
        for (int i=0; i<n; i++) {
            for (int j=0; j<(1<<16); j++) {
                int now = j|a[i];
                
                for (int k=0; k<c; k++) {
                    int score = bitcount(now&b[k]);
                    int next = now-(now&b[k]);
                    dp[i+1][next] = max(dp[i+1][next], dp[i][j]+score);
                }
            }
        }
        
        int ans = 0;
        
        for (int i=0; i<(1<<16); i++) {
            ans = max(ans, dp[n][i]);
        }
        
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 1260
memory_kb: 10944
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define INF 1000000

int n, c;
int a[40];
int b[40];

int bitcount(uint bits) {
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >> 16);
}

int main() {
    while (true) {
        cin >> n >> c;
        
        if (n==0 && c==0) {
            break;
        }
        
        for (int i=0; i<n; i++) {
            a[i] = 0, b[i] = 0;
        }
        
        for (int i=0; i<n; i++) {
            int p = 1;
            
            for (int j=0; j<16; j++) {
                int aij; cin >> aij;
                a[i] += aij*p;
                p *= 2;
            }
        }
        
        for (int i=0; i<c; i++) {
            int p = 1;
            
            for (int j=0; j<16; j++) {
                int bij; cin >> bij;
                b[i] += bij*p;
                p *= 2;
            }
        }
        
        int dp[n+1][1<<16];
        
        for (int i=0; i<=n; i++) {
            for (int j=0; j<(1<<16); j++) {
                dp[i][j] = -INF;
            }
        }
        
        dp[0][0] = 0;
        
        for (int i=0; i<n; i++) {
            for (int j=0; j<(1<<16); j++) {
                int now = j|a[i];
                
                for (int k=0; k<c; k++) {
                    int score = bitcount(now&b[k]);
                    int next = now^(now&b[k]);
                    dp[i+1][next] = max(dp[i+1][next], dp[i][j]+score);
                }
            }
        }
        
        int ans = 0;
        
        for (int i=0; i<(1<<16); i++) {
            ans = max(ans, dp[n][i]);
        }
        
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 1260
memory_kb: 11032
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
#include<iostream>
#include<cmath>
int count_bit(int value) {
	int result = 0;
	while (value != 0) {
		result += value % 2;
		value >>= 1;
	}
	return result;
}
int max_score(const int current, const int state, const std::vector<int>& beats, const std::vector<int>& patterns, std::vector<std::unordered_map<int, int>>& memo) {
	if (current == beats.size()) return 0;
	if (memo[current].find(state) != memo[current].end()) return memo[current][state];
	const auto next_state = state | beats[current];
	int max = 0;
	for (const auto pattern : patterns) {
		max = std::max(max, max_score(current + 1, next_state & (~pattern), beats, patterns, memo) + count_bit(pattern & next_state));
	}
	return memo[current][state] = max;
}
int main() {
	while (true) {
		int n, c; std::cin >> n >> c; if (n == 0 && c == 0) break;
		std::vector<int> beats(n, 0), patterns(c, 0);
		for (auto& beat : beats) {
			int d;
			for (auto i = 0; i < 16; ++i) {
				std::cin >> d;
				beat <<= 1;
				beat += d;
			}
		}
		for (auto& pattern : patterns) {
			int d;
			for (auto i = 0; i < 16; ++i) {
				std::cin >> d;
				pattern <<= 1;
				pattern += d;
			}
		}
		std::vector<std::unordered_map<int, int>> memo(n);
		std::cout << max_score(0, 0, beats, patterns, memo) << '\n';
	}
}
accuracy: 1
time_ms: 1560
memory_kb: 76712
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int popcnt[1<<16] = {};
int dp[31][1<<16], a[31], b[31];
bool valid[31][1<<16];

int main(){
    vector<int> popcnt(1<<16, 0);
    for(int i = 1; i < 1<<16; i++){
        for(int j = 0; j < 16; j++) popcnt[i] += (i>>j)&1;
    }
    int n, c;
    while(cin >> n >> c, n+c){
        memset(dp, 0, sizeof(dp));
        memset(valid, 0, sizeof(valid));
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        valid[0][0] = true;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < 16; j++){
                int x;  cin >> x;
                a[i] += (1<<j)*x;
            }
        }
        for(int i = 0; i < c; i++){
            for(int j = 0; j < 16; j++){
                int x;  cin >> x;
                b[i] += (1<<j)*x;
            }
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < 1<<16; j++){
                if(!valid[i][j])    continue;
                int nj = j|a[i];
                for(int k = 0; k < c; k++){
                    valid[i+1][~b[k]&nj] = true;
                    dp[i+1][~b[k]&nj] = max(dp[i+1][~b[k]&nj], dp[i][j]+popcnt[nj&b[k]]);
                }
            }
        }
        int ans = 0;
        for(int j = 0; j < 1<<16; j++){
            if(valid[n][j]) ans = max(ans, dp[n][j]);
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 13064
final_rank: 2