The Queue
Contest: Codeforces Round 398 (Div. 2)
Difficulty rating: 2100
Tags: brute force, greedy

Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.

He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves).

Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.

"Reception 1"

For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.

Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!

Input format:
The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office.

All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

Output format:
Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.

In the second example, Vasya has to come before anyone else to be served.

Example input:
10 15 2
2
10 13

Example output:
12

Example input:
8 17 3
4
3 4 5 8

Example output:
2

Editorial:
Let's calculate the point of time when each visitor would be served. Let array a contain the points of time when visitors arrive. The receptionist would begin to serve the first visitor at the point of time when the receptionist begins to work ts, if the first visitor came to the passport office before it, or when he comes to the office, if he didn't. We can calculate the point of time when the receptionist would begin to serve the i-th visitor in the same way if we know when the receptionist began to serve the i - 1-th visitor. Let's suppose that the receptionist began to serve the i - 1-th visitor at bi - 1 minute. It means that receptionist would begin to serve the i-th visitor no sooner than t minutes later, that is bi - 1 + t minute. The receptionist would begin to serve him at this point of time if i-th visitor came before (ai ≤ bi - 1 + t). If he came later, his serving would begin when he comes. This way we will find the time bi when the receptionist would begin to serve him.
If any visitor came later than the previous visitor was served (ai > bi - 1 + t), the receptionist was free. It means that Vasya can be served immediately if he arrives at the proper time, for example, at bi - 1 + t minute. If there are no such visitors, to be the i-th person served this day, Vasya has to arrive at the passport office no later than (ai - 1) and he would have to wait a minimum of bi - (ai - 1) minutes. From all of these options we have to find a point of time with the minimal waiting time.
There are several special cases we have to consider:
1. If i-ый and (i - 1)-ый visitors arrived at the same time, Vasya can't arrive between them.
2. Some visitors wouldn't be served this day if the time the receptionist would begin to serve them is more or equal to Tf - t and that means we shouldn't attive to the passport office after them.
3. Vasya can be the last person served this day if there are at least t minutes between the point of time receptionist serves the last customer and the point of time he stops working.
4. Vasya can be the first person served only if he comes before every other visitor.
Considering the 1012 bound, all calculations should use the 64-bit data.
To conclude, this task is reduced to careful calculation of the visitors' serving times (O(n) time), considering the aforementioned edge cases.