C. Chinese Remainder Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For her social studies project, Cindy is doing extensive research into the Three Kingdoms period of Chinese History. Her primary sources are works of media centered on legendary Shu tactician Zhuge Liang (courtesy name Kongming)—these range from speculative fiction of how a strategist of Kongming's caliber would fare in cutthroat industries of the modern day, as well as rigorous combat simulations that highlight Zhuge Liang's own personal combat prowess and ability to decimate hundreds of enemies en masse using magic.

Through her research, she encountered this quite interesting excerpt:

I asked my army to divide themselves into squadrons such that each squadron contained the same number of soldiers. I know that they will do this task to the best of their ability, forming as many groups of that size as possible, as much as possible. However, depending on the squadron size I choose, it may be possible that despite their efforts, there may be soldiers left over who are unable to form a complete squadron.

I asked them to divide themselves into squadrons of size $$$a_1$$$, but this unfortunately resulted in $$$k$$$ people left over.

I asked them to divide themselves into squadrons of size $$$a_2$$$, but this unfortunately resulted in $$$k$$$ people left over.

I asked them to divide themselves into squadrons of size $$$a_3$$$, but this unfortunately resulted in $$$k$$$ people left over.

...and so on. I did this $$$n$$$ times—asking them to form as many squadrons as possible of sizes $$$a_1$$$, then $$$a_2$$$, ..., then $$$a_n$$$—but every single time, there were exactly $$$k$$$ people left over and unable to form a complete squadron. What a coincidence!

Cindy found, through another source, that the number of soldiers in Zhuge Liang's army was between $$$\ell$$$ and $$$r$$$, inclusive. Given this information, how many soldiers were in Zhuge Liang's army?

If there are multiple possible answers, output any of them. If there are no possible answers (meaning the historical sources are inaccurate), you must say so as well.

Input

The first line of input contains the space-separated integers $$$n$$$, $$$\ell$$$, $$$r$$$, and $$$k$$$.

The second line of input contains the $$$n$$$ distinct space-separated integers $$$a_1, a_2, \dots, a_n$$$.

Output

If an answer exists, output any integer $$$x$$$ such that $$$\ell \leq x \leq r$$$, and $$$x$$$ soldiers would result in the described scenario. If there are multiple possible answers, output any of them.

If there are no possible answers, output $$$-1$$$ instead.

Scoring

$$$$$$\begin{align*}

&\begin{array}{|l|} \hline \text{Constraints For All Subtasks} \\ \hline 1 \leq n \leq 10^5 \\ 1 \leq \ell \leq r \leq 10^{10} \\ 1 \leq k \leq 10^9 \\ \text{$2 \leq a_i \leq 10^{10}$ for all $i$} \\ \text{$a_i \neq a_j$ if $i \neq j$} \\ \hline \end{array}\\

&\begin{array}{|c|c|l|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & \mathbf{40} & r - \ell \leq 10 \\ \hline 2 & \mathbf{20} & n = 1 \\ \hline 3 & \mathbf{20} & \text{$a_i$ is prime, for all $i$} \\ \hline 4 & \mathbf{20} & \text{No further constraints.} \\ \hline \end{array}\\

\end{align*}$$$$$$

Examples
Input
4 200 300 1
2 3 5 7
Output
211
Input
4 100 110 3
4 6 9 12
Output
-1