#GYM104789A. Fence Painting
Fence Painting
Description
Tom and Huck are painting a fence. The fence consists of $a + b$ segments, with the first $a$ segments being wooden and the remaining $b$ being metal.
Tom and Huck have been promised a reward for painting the fence. Since different amounts of effort are required for wooden and metal segments, painting a wooden segment is valued at $x$ coins, and a metal segment at $y$ coins.
Tom starts painting the fence from the first (far left) segment and paints one segment after another in succession. Similarly, Huck starts painting from the right side and moves towards Tom. Naturally, the boys want the reward for painting the fence to be divided fairly: the amounts that Tom and Huck receive for the fence should differ by the smallest possible value.
Help them determine their meeting point $k$, so that the reward for painting is as evenly distributed as possible. The meeting point is considered to be the number of the last segment painted by Tom. In other words, if the meeting point is $k$, Tom has painted the $k$ left segments, and Huck has painted the remaining $a + b - k$ right segments.
The first line of input contains two integers $a$ and $b$ — the number of wooden and metal fence segments, respectively ($1 \le a, b \le 10^9$).
The second line of input contains two integers $x$ and $y$ — the number of coins paid for painting one wooden or metal segment, respectively ($1 \le x, y \le 10^9$).
Output a single integer $k$ — the meeting point where Tom and Huck receive the most closely valued rewards. If there are several optimal answers, output any of them.
Scoring
Points for each subtask are awarded only if all tests of that subtask and the required subtasks are successfully passed.
Subtask | Points | Constraints | Required subtasks |
0 | – | examples | |
1 | 16 | $a, b \le 1000$ | 0 |
2 | 19 | $x = y$ | |
3 | 21 | $a = b$ | |
4 | 24 | $a, b \le 10^6$ | 0, 1 |
5 | 20 | none | 0 – 4 |
Input
The first line of input contains two integers $a$ and $b$ — the number of wooden and metal fence segments, respectively ($1 \le a, b \le 10^9$).
The second line of input contains two integers $x$ and $y$ — the number of coins paid for painting one wooden or metal segment, respectively ($1 \le x, y \le 10^9$).
Output
Output a single integer $k$ — the meeting point where Tom and Huck receive the most closely valued rewards. If there are several optimal answers, output any of them.
1 1
1 1
5 10
2 1
1
5