| ZCO 2025 |
|---|
| Закончено |
In a distant city, there is a river. The river runs directly through the city and splits the city into two sides. The north side of the river has $$$K$$$ locations numbered from $$$0$$$ to $$$K-1$$$, and the south side of the river also has $$$K$$$ locations numbered from $$$0$$$ to $$$K-1$$$, both inclusive.
There are currently two ways to travel throughout the city: walking and taking a boat. For walking:
Note that you cannot walk from a location on one side of the river to a location on the other side of the river. To travel across the river, you must currently take a boat:
There are $$$N$$$ people living in the city. Person $$$i$$$ lives at location $$$H_i$$$ on the north side and works at location $$$W_i$$$ on the south side. Each person plans their trip as a combination of walking and taking the boat as to take the minimum possible amount of time to travel between their home and workplace.
It turns out that the boats on the rivers are hugely unsustainable and contribute to global warming! As a result, you would like to build a bridge at some location $$$L$$$, where $$$0 \leq L \leq K - 1$$$. This bridge will make it possible to walk from location $$$L$$$ on one side to location $$$L$$$ on the other side in exactly one unit of time.
However, people still want to save time, so simply building a bridge does not mean everyone will use it. The citizens have a tolerance level called $$$T$$$, which is initially $$$0$$$. Fortunately, you can run a social awareness campaign which will increase the value of $$$T$$$ for everyone in the city. A person will use the bridge if and only if their overall time taken for their travel is at most $$$T$$$ units of time more than the minimum time it would take them to travel using a boat.
You've decided that you want at least $$$M$$$ of the $$$N$$$ people in the city to start using the bridge. What is the minimum tolerance level you need everyone to have to make this happen?
The first line of input contains four space-separated integers: $$$N$$$, $$$M$$$, $$$K$$$ and $$$B$$$. $$$N$$$ represents the total number of people in the city, $$$M$$$ represents the minimum number of people you want to use the bridge instead of the boat, $$$K$$$ represents the number of locations on each side of the river, and $$$B$$$ represents the time it takes to use the boat to cross the river.
The next $$$N$$$ lines of input each contain two space-separated integers $$$H_i$$$ and $$$W_i$$$. $$$H_i$$$ represents the home location of the $$$i$$$th person (on the north side) and $$$W_i$$$ represents the work location of the $$$i$$$th person (on the south side).
Output a single integer $$$T$$$, the minimum possible tolerance level such that at least $$$M$$$ of the $$$N$$$ people in the city will choose to use the bridge instead of the boat.
For all subtasks, it is guaranteed that:
Subtasks
5 3 10 11 27 44 39 90 0
2
5 4 10 21 27 44 39 90 0
3
5 5 10 41 27 44 39 90 0
7
2 2 8 11 16 6
6
10 8 100 560 02 821 920 2036 5840 4212 5668 6982 7598 99
52
Sample 0 Explanation
Consider building a bridge at $$$L = 3$$$. Then, with tolerance level $$$T = 2$$$, persons numbered $$$1$$$, $$$2$$$, and $$$3$$$ will use the bridge.
For example, for person $$$1$$$:
Sample 1 Explanation
Consider building a bridge at $$$L = 2$$$. Then with tolerance level $$$T = 3$$$, we can prove that persons numbered $$$1$$$, $$$2$$$, $$$3$$$, and $$$5$$$ will use the bridge.
For example, for person $$$1$$$:
| Название |
|---|


