Narrow Art Gallery
Time limit2sMemory limit256 MB
Close exactly k rooms with no row fully closed and no diagonal closures to keep the maximum total value of open rooms.
- Level
Medium5 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
A long art gallery has rooms. The gallery is a grid of rows and 2 columns, and rooms that touch vertically or horizontally are connected. Today the curator is told to close rooms to cut running costs.
Visitors have to enter through at least one of the two rooms in the row at one end and leave through one of the two rooms in the row at the other end. To keep that possible, the curator must not close both rooms of the same row, and must not close two rooms that touch diagonally. Closing two vertically adjacent rooms in the same column is allowed.
The curator already knows how much value each room produces when it is open to the public. Close exactly rooms under the rules above so that the total value of the open rooms is as large as possible.
Input
The input holds several galleries. The first line of each gallery has two integers and (, ), the number of rows of the gallery and the number of rooms to close. Each of the next lines has two integers, the value of the left room and the value of the right room in that row. Each room value satisfies .
A line holding 0 0 follows the last gallery and ends the input.
Output
For each gallery, print the largest total value that can stay open to the public, one per line, in input order.