Candy Picking Contest
Time limit1sMemory limit256 MB
Choose boxes in an M by N grid so no two chosen boxes touch vertically or horizontally, maximizing the total candies collected.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Array, Matrix
- Solved
- No attempts yet
Problem
Sangkeun is a boy who loves candy. He is an avid subscriber of Candy Magazine, and this year he represents Korea at the International Candy Picking Contest.
The contest takes place where boxes of candy are arranged in a grid of rows and columns, so there are boxes in total. The number of candies inside each box is written on its outside.
A contestant chooses one box and takes all of the candies inside it. Whenever a box is chosen, the candies in the boxes at the following positions disappear:
- every box in the row immediately above the chosen box,
- every box in the row immediately below the chosen box,
- the box immediately to the left and the box immediately to the right of the chosen box, in the same row.
The contestant may keep choosing boxes until no box with any candy remains.
Given , , and the number of candies in every box, write a program that finds the maximum number of candies Sangkeun can take.
Input
The input consists of several test cases. The first line of each test case contains two integers and (). Each of the next lines contains the candy counts of the boxes in that row, separated by spaces. Every box contains at least and at most candies.
The last line of the input contains two zeros and must not be processed.
Output
For each test case, print on its own line the maximum number of candies Sangkeun can take.