Ride the dominoes on a chessboard
Time limit3sMemory limit128 MB
Place exactly K non-overlapping dominoes on an N by 3 board of integers to maximize the sum of covered cells.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Sanggeun owns one chessboard with rows and 3 columns.
While Sanggeun was out for a moment, Changyoung wrote an integer in every square of the board, left dominoes lying on the floor, and ran away.
Sanggeun came home, saw the integers on the board he treasured, and was heartbroken.
Changyoung could not bear to watch Sanggeun grieve, so he decided to cover the board using all dominoes. One domino has size and may be rotated. Dominoes cannot overlap, and one domino always covers two squares of the board. The board does not have to be covered without gaps, but every one of the dominoes has to be placed.
There are many ways to place the dominoes. Write a program that finds the largest sum you can get by adding up the numbers written in the squares the dominoes cover.
Input
The first line contains and . (, )
Each of the next lines contains the three numbers written in one row of the board, given in order from the first row. Every number is an integer whose absolute value is smaller than .
The input satisfies , so the dominoes can always be placed.
Output
Print on the first line the largest sum of the numbers written in the squares covered by the dominoes.