Splitting a Histogram into Rectangles
Time limit2sMemory limit1024 MB
Given N adjacent rectangles with widths and heights, find the minimum total length of cuts that split the histogram into rectangles.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Jaewoo received a histogram as a gift from Jaehyeon. A histogram is a figure made of several rectangles aligned along the bottom. The width and height of each rectangle may differ. For example, the following figure is a histogram with rectangles of size , , and placed side by side.

Jaewoo found the uneven histogram hard to store, so he wants to cut it into pieces that are rectangles. Each cut costs effort equal to the length of the cut. Jaewoo is not strong, so he wants to split the histogram with the least effort.
For example, if the histogram above is cut as in the left figure below, the cost is . If it is cut as in the right figure, the cost is , which is the minimum.

Given the histogram Jaewoo received from Jaehyeon, find the minimum effort needed to cut it into rectangles.
Input
The first line contains the number of rectangles that make up the histogram, .
From the second line through the th line, each line contains two integers , the width and height of each rectangle. The rectangles are given separated by a space, in order from left to right.
Output
Print the minimum effort needed to cut the given histogram into rectangles on the first line.