Problem 659
Minimum Falling Path Sum II
Hard · 30 pts · Dynamic Programming, Matrix
Problem
Given an N × N grid of integers, choose exactly one element from each row so that no two elements chosen from neighbouring rows are in the same column. Find the minimum possible sum. For each row you only need the smallest and the second smallest totals of the previous row (and the column of the smallest): every cell adds its value to the smallest previous total, unless it shares that column, in which case it uses the second smallest.
Input
The first line contains N. Each of the next N lines contains N integers.
Output
Print the minimum falling path sum.
Constraints
1 <= N <= 200; -99 <= value <= 99
Example input
3 1 2 3 4 5 6 7 8 9
Example output
13
Explanation
In the grid 1 2 3 / 4 5 6 / 7 8 9, choose 1 from the first row, 5 from the second and 7 from the third. Neighbouring choices are in different columns and the sum is 13, the smallest possible.