Effcient Slabstones Rearrangement
시간 제한2초메모리 제한1024 MB
길이 x인 새 슬래브를 놓을 수 있도록 간격 d를 유지하며 기존 슬래브 n개를 옮길 때 필요한 인접 이동 횟수의 최솟값을 구한다.
문제
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 birthday present. Barbara then placed these slabstones in her garden, so she can enjoy stepping slabstones from one to another every day. The -th slabstone fully occupies the -th to -th region of the garden. The slabstones do not overlap, and any two slabstones have at least empty regions between them.
Below is a valid placement of the slabstones with , , , and the three slabstones occupy the regions , , respectively.

Barbara recently bought another slabstone that will occupy consecutive regions in her garden. 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 should remain non-overlapping during slabstone rearrangement.

Shifting a single slabstone to an adjacent region takes one minute. For example, the above rearrangement process takes minutes. Now Barbara wants to know the minimum possible total time required to rearrange the slabstones, so she can save time for “other purposes”.
입력
The first line contains four integers , , and . The -th of the following lines contains two integers and .
출력
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, just output -1.
제한
- for
- for . That is, the slabstones are given in order from left to right.