No tags yet

No tag edit access

time limit per test: 0.25 sec.

memory limit per test: 65536 KB

memory limit per test: 65536 KB

input: standard

output: standard

output: standard

When entering some analog data into a computer, this information must be quantized. Quantization transforms each measured value x to some other value l(x) selected from the predefined set L of levels. Sometimes to reduce the influence of the levels set to the information, the group of levels sets L_{i} is used. The number of levels sets is usually chosen to be the power of 2.

When using the number of levels sets, some additional information should be used to specify which set was used for each quantization. However, providing this information may be too expensive — the better solution would be to choose more levels and use one set. To avoid the specification of the quantization set, the following technique is used. Suppose that n values x_{1}, x_{2}, ..., x_{n} are to be quantized and the group of m=2^{p} levels sets L_{i}, i=0, ..., m-1; each of size s=2^{q} is used to quantize it. After quantization x_{j} is replaced with some number l_{j} = in L_{f(j)}. Instead of sending l_{j}, its ordinal number in L_{f(j)} is usually sent, let k_{j} be the ordinal number of l_{j} in L_{f(j)} (levels are numbered starting with 0). Take p least significant bits of k_{j} and say that the number k_{j} & (2^{p}-1) is the number of the levels set that will be used for next quantization, that is f(j+1) = k_{j} & (2^{p}-1).

Since the least significant bits of k_{j} are usually distributed quite randomly, the sets used for quantization change often and weakly depend on values of quantized data, thus this technique provides the good way to perform the quantization.

Usually to perform the quantization the closest to the value level of the levels set is chosen. However, using the technique described, sometimes it pays off to choose not the optimal level, but some other one, the ordinal number of which has other least significant bits, thus choosing another levels set for next measure and providing better approximation of quantized values in the future. Let us call the*deviation* of quantization the value of sum(j=1.. n, |x_{j} - l_{j}|). Your task is given measures and levels sets to choose quantized value for each measure in such a way, that the deviation of quantization is minimal possible.

The first value is always quantized using set L_{0}.

When using the number of levels sets, some additional information should be used to specify which set was used for each quantization. However, providing this information may be too expensive — the better solution would be to choose more levels and use one set. To avoid the specification of the quantization set, the following technique is used. Suppose that n values x

Since the least significant bits of k

Usually to perform the quantization the closest to the value level of the levels set is chosen. However, using the technique described, sometimes it pays off to choose not the optimal level, but some other one, the ordinal number of which has other least significant bits, thus choosing another levels set for next measure and providing better approximation of quantized values in the future. Let us call the

The first value is always quantized using set L

The first line of the input file contains n (1 ≤ n ≤ 1000). The second line contains n integer numbers x_{i} ranging from 1 to 10^{6}. The next line contains m and s (1 ≤ m ≤ 128, m ≤ s ≤ 128). Next m lines contain s integer numbers each — levels of the quantization sets given in increasing order for each set, all levels satisfy 1 ≤ level ≤ 10^{6}.

First output the minimal possible deviation of the quantization. Then output n integer numbers in range from 0 to s - 1. For each input value output the number of the level in the corresponding levels set (k_{j}) used for this number to achieve the quantization required.

Input

3

8 8 19

2 4

5 10 15 20

3 7 13 17

8 8 19

2 4

5 10 15 20

3 7 13 17

Output

5

1 1 3

1 1 3

Author: | Andrew Stankevich |

Resource: | Petrozavodsk Summer Trainings 2003 |

Date: | 2003-08-23 |

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/17/2019 04:54:26 (g1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|