Official algorithmic problem description and constraints.
Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem. The river is a grid of π rows and π columns. The intersection of the π -th row and the π -th column contains the number ππ,π β the depth in the corresponding cell. All cells in the first and last columns correspond to the river banks, so the depth for them is 0 . The river may look like this. Rudolf can choose the row (π,1),(π,2),β¦,(π,π) and build a bridge over it. In each cell of the row, he can install a support for the bridge. The cost of installing a support in the cell (π,π) is ππ,π+1 . Supports must be installed so that the following conditions are met: A support must be installed in cell (π,1) ; A support must be installed in cell (π,π) ; The distance between any pair of adjacent supports must be no more than π . The distance between supports (π,π1) and (π,π2) is |π1βπ2|β1 . Building just one bridge is boring. Therefore, Rudolf decided to build π bridges on consecutive rows of the river, that is, to choose some π (1β€πβ€πβπ+1 ) and independently build a bridge on each of the rows π,π+1,β¦,π+πβ1 . Help Rudolf minimize the total cost of installing supports.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.