农夫约翰收到了一批 N 个干草捆,并将它们放到了连接他家和牛棚的道路沿线的不同位置。
道路可视为一个一维数轴。
每个干草捆 j 的尺寸为 Sj,位置为 Pj。
奶牛贝茜目前位于位置 B,该位置没有干草捆。
贝茜可以沿着道路自由移动,甚至可以到达干草捆所在的位置,但是她不能越过该位置。
但有一种例外,如果她沿一个方向奔跑了 D 个单位的距离,她就会积攒足够的速度突破并破坏掉任何尺寸严格小于 D 的干草捆。
当然,这样做之后,她就有了更大的空间用来奔跑、突破并消除更多的干草捆。
约翰正在粉刷他的牛棚和房屋,他不希望贝茜来捣乱。
因此,约翰希望确保贝茜永远不会突破最左边或最右边的干草捆,这样就可以把她困在干草捆中。
约翰可以选择其中一个干草捆,并增大其尺寸,用以困住贝茜。
请确定为了困住贝茜,约翰需要添加到某个干草捆中的最小额外尺寸。
第一行包含整数 N 和 B。
接下来 N 行,每行描述一个干草捆,包含两个整数(范围 1∼109),描述其尺寸和位置。
输出为了困住贝茜,约翰需要添加的最少干草量。
如果无法困住贝茜,则输出 −1。
1≤N≤105,
1≤B≤109
5 7 8 1 1 4 3 8 12 15 20 20
4
Bessie the cow can move around freely along the road, even up to the position at which a bale is located, but she cannot cross through this position. As an exception, if she runs in the same direction for D units of distance, she builds up enough speed to break through and permanently eliminate any hay bale of size strictly less than D. Of course, after doing this, she might open up more space to allow her to make a run at other hay bales, eliminating them as well.
FJ is currently re-painting his house and his barn, and wants to make sure Bessie cannot reach either one (cows and fresh paint do not make a good combination!) Accordingly, FJ wants to make sure Bessie never breaks through the leftmost or rightmost hay bale, so she stays effectively trapped within the hay bales. FJ has the ability to add hay to a single bale of his choosing to help keep Bessie trapped. Please help him determine the minimum amount of extra size he needs to add to some bale to ensure Bessie stays trapped.
5 7 8 1 1 4 3 8 12 15 20 20
4
[Problem credits: Brian Dean, 2015]