Under construction

Still being built. Things may change

Generate Optimal Key

Interview

Time limit2sMemory limit2048 MB

Summary
Given n binary patterns and m forbidden keys of length L, find the non-forbidden binary key with the smallest total Hamming distance to all patterns.
Level

Medium6 of 10

Topics
Greedy, Heap, Bit manipulation
Solved
No attempts yet

Problem

A pattern is a binary string of length LL. Given nn patterns, we want to find a key, which is also a binary string of length LL.

For a given pattern and a given key, the error value is the number of positions where the key differs from the pattern. For example, if the pattern is 101 and the key is 000, the first and third positions differ, so the error value is 22.

We want to choose a key that minimizes the sum of the error values between the key and the nn given patterns. There are also mm distinct forbidden keys, and the chosen key cannot be one of them.

Input

The first line contains three integers nn, mm, and LL (1≤n≤10001 \le n \le 1000; 1≤m≤min⁡(1000,2L−1)1 \le m \le \min(1000, 2^L - 1); 1≤L≤10001 \le L \le 1000).

Each of the next nn lines contains a pattern, a binary string of length LL.

Each of the next mm lines contains a forbidden key, a binary string of length LL. All forbidden keys are distinct.

Output

Print a single integer: the minimum sum of error values over all non-forbidden keys and the nn given patterns.

Examples2

  1. Example 1

    Input
    4 1 4
    0000
    1000
    1100
    1010
    1000
    
    Expected output
    5
    
  2. Example 2

    Input
    2 4 4
    0000
    0000
    0000
    1000
    0100
    0010
    
    Expected output
    2