Efficient Slabstones Rearrangement
Time limit2sMemory limit1024 MB
Shift the n slabstones with the least total movement so a new slabstone of length x fits while every pair keeps at least d empty regions between them.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Implementation
- Solved
- No attempts yet
Problem
Barbara has a garden. The garden is long and narrow, divided into equal-sized regions arranged in a row. Her friend, Babara, gave her slabstones as a birthday present. Barbara placed these slabstones in her garden so she can enjoy stepping from one slabstone to another every day. The -th slabstone fully occupies regions to . The slabstones do not overlap, and any two slabstones have at least empty regions between them.

Barbara recently bought another slabstone that occupies consecutive regions. She will shift the original slabstones within the garden, then place the new slabstone somewhere in the garden. After shifting the original slabstones and placing the new slabstone, the slabstones cannot overlap, and any two slabstones must have at least empty regions between them. The slabstones must also stay non-overlapping during the rearrangement.

Shifting a single slabstone to an adjacent region takes one minute. For example, the rearrangement above takes 4 minutes. Barbara wants to know the minimum possible total time required to rearrange the slabstones, so she can save time for other purposes.
Input
The first line contains four integers , , and . The -th of the following lines contains two integers and .
Output
Print the minimum possible total time (in minutes) to rearrange the slabstones so the new slabstone can be placed in the garden. If the new slabstone cannot be placed in the garden no matter how the slabstones are rearranged, print .
Constraints
- for
- for . The slabstones are given in order from left to right.