Coin-collecting robot vacuum
Time limit4sMemory limit128 MB
A vacuum walks 4N steps on an (N+1)x(N+1) grid of coins, must collect all four corners, and return to start; maximize coins.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, Implementation
- Solved
- No attempts yet
Problem
Mirko bought a smart robot vacuum at a fair. To try it out, he built a cardboard box and divided its floor into rows and columns, numbered to . He put some coins on every cell of the floor. The four corner cells , , and hold gold coins, and every other cell holds silver coins.
The vacuum starts on cell . Every second it moves to one of the eight neighbouring cells, and it cannot leave the box. It picks up every coin on a cell it passes through, and a cell it enters again has no coins left. The coins on the starting cell are picked up at the start.
Mirko ordered the vacuum to pick up all the gold coins and as many silver coins as possible, then come back to the starting cell after exactly seconds. Write a program that computes the largest number of coins the vacuum can pick up.
Input
The first line contains an integer ().
Each of the next lines contains numbers of coins, the cells of one row of the floor given in column order. Every value is between and .
Output
Print the largest number of coins the vacuum can pick up.