Base Defense
Time limit1sMemory limit1024 MB
Given F formations and each day's fixed last formation, find the minimum total squared-difference fatigue over L days of T changes each.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Matrix, Shortest path
- Solved
- No attempts yet
Problem
Starting today, the training period lasts days. Every day, the unit members must demonstrate formations selected from the base defense formation catalog.
For the training, formations are selected from the catalog and numbered 1 to . Formation is written (). Each formation is an grid matching the training site. is the cell in row and column of , equal to 1 if a soldier must stand in that cell and 0 otherwise. satisfies for all , (, ).
On the first day, the routine starts from . From the second day on, each day starts from the last formation of the previous day. Until the day ends, more formations are demonstrated.
Each change of formation adds fatigue. If the formation being demonstrated is and the next one is , the fatigue added is the square of the number of pairs where and differ (, ).
The last formation of each day is already fixed. The chief sergeant ordered the lead soldier to fill the remaining demonstrations with the selected formations so that the total fatigue is minimized. Find the total fatigue of that routine.
Input
The first line contains , , , , and , separated by spaces.
From the second line on, the information for is given in order, using lines in total. Each formation takes lines with values separated by spaces. The -th value on the -th line is , and each value is 0 or 1.
Finally, the numbers of the formation to be demonstrated last on day 1 through day are given, separated by spaces.
Output
Print the total fatigue of the routine that minimizes fatigue on the first line.
Constraints
- ()
- For all , (, ),