This page is still under construction.

Parts of this page are still being built. What you see may change.

Wizard Shark and Firestorm

Interview

Time limit1sMemory limit512 MB

Summary
Repeat Q times: rotate every 2^L x 2^L block 90 degrees clockwise, then subtract 1 from each ice cell that has fewer than 3 ice neighbors. Output the total ice and the largest connected ice chunk.
Level

Medium6 of 10

Topics
Implementation, Simulation, Matrix, BFS
Solved
No attempts yet

Problem

Wizard Shark can cast Firestorm by combining Fireball and Tornado. Today he wants to practice Firestorm on an ice sheet divided into a grid of size 2N×2N2^N \times 2^N. Position (r,c)(r, c) means row rr and column cc of the grid, and A[r][c]A[r][c] means the amount of ice at (r,c)(r, c). If A[r][c]A[r][c] is 0, there is no ice there.

To cast Firestorm, he must decide on a level LL for each cast. Firestorm first divides the grid into subgrids of size 2L×2L2^L \times 2^L. Then it rotates every subgrid 90 degrees clockwise. After that, any cell that is not adjacent to at least 3 cells containing ice loses 1 from its amount of ice. The cells adjacent to (r,c)(r, c) are (r−1,c)(r-1, c), (r+1,c)(r+1, c), (r,c−1)(r, c-1), and (r,c+1)(r, c+1). The integers written in the cells of the figures below are just labels to distinguish the cells.

Before casting the spellL=1L = 1L=2L = 2

Wizard Shark wants to cast Firestorm QQ times in total. After casting all the Firestorms, find the following two values.

  1. The sum of the remaining ice A[r][c]A[r][c]
  2. The number of cells occupied by the largest chunk of remaining ice

If a cell containing ice is adjacent to another cell containing ice, the two cells are said to be connected. A chunk is a set of connected cells.

Input

The first line gives NN and QQ. From the second line, 2N2^N lines give the amount of ice in each cell of the grid. The integer given at position cc in the rr-th line is A[r][c]A[r][c].

The last line gives the levels L1,L2,…,LQL_1, L_2, \dots, L_Q that Wizard Shark cast, in order.

Output

Print the sum of the remaining ice A[r][c]A[r][c] on the first line, and the number of cells occupied by the largest chunk on the second line. If there is no chunk, print 0.

Constraints

  • 2≤N≤62 \le N \le 6
  • 1≤Q≤1,0001 \le Q \le 1,000
  • 0≤A[r][c]≤1000 \le A[r][c] \le 100
  • 0≤Li≤N0 \le L_i \le N

Examples5

  1. Example 1

    Input
    3 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1
    
    Expected output
    284
    64
    
  2. Example 2

    Input
    3 2
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2
    
    Expected output
    280
    64
    
  3. Example 3

    Input
    3 5
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 0 3 2
    
    Expected output
    268
    64
    
  4. Example 4

    Input
    3 10
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 0 3 2 1 2 3 2 3
    
    Expected output
    248
    62
    
  5. Example 5

    Input
    3 10
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8
    8 7 6 5 4 3 2 1
    1 2 3 1 2 3 1 2 3 1
    
    Expected output
    246
    60