City
Time limit4sMemory limit1024 MB
Given an N by N grid of developed and undeveloped regions with tourism values, pick K undeveloped regions to develop so the total value of the resulting cities is maximized.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
A square country is a grid of square regions. A map marks each region as if it is developed and if it is not. A region is a city if it is developed and all four regions adjacent to it (up, down, left, and right) are also developed. A region on the edge cannot be a city.
Each region has a tourism value. If the cell in row , column becomes a city, its tourism value is . Otherwise, its tourism value is .
The country has capital to develop more regions. The country is not wealthy, so is always less than or equal to the number of undeveloped regions. To maximize tourism revenue, find the maximum possible sum of tourism values of the cities after developing new regions.
Input
The first line contains and , separated by a space.
From the second line onward, the next lines describe the map. Line contains , separated by spaces. If is , the cell in row , column is already developed. If is , it is not.
From line onward, the next lines contain the tourism values. Line contains , separated by spaces.
Output
Print the maximum sum of tourism values of the cities after developing regions.
Constraints
- All given numbers are integers.
- ()
- ()
- is always less than or equal to the number of undeveloped regions.