Time limit
2s
Memory limit
128 MB
Only black stones are placed on an N x N Go board. Among the empty cells, cells completely surrounded by black stones are called territory.
Empty cells belong to the same empty region when they are adjacent vertically or horizontally. If an empty region touches the edge of the board, none of its cells are territory. Every cell in an empty region that does not touch the edge is territory.
You are given the number of black stones on every row, every column, and both diagonal directions. These counts always determine exactly one board. Reconstruct the board and compute the total size of its territory.
The first line contains an integer N. (1 <= N <= 15)
The second line contains N integers: the number of black stones in each row from top to bottom.
The third line contains N integers: the number of black stones in each column from left to right.
The fourth line contains 2N-1 integers for the / diagonals. They are ordered from the upper-left corner toward the lower-right corner, equivalently from the smallest value of r+c to the largest.
The fifth line contains 2N-1 integers for the \ diagonals. They are ordered from the lower-left corner toward the upper-right corner, equivalently from the smallest value of c-r to the largest.
No invalid input is given, and exactly one board satisfies all counts.
Print the total size of the territory on the first line.