問題一覧 > 通常問題

No.2099 [Cherry Alpha B] Time Machine

レベル : / AI推定難易度 : 1600 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 92
作問者 : 👑 Kazun / テスター : butsurizuki keisuke6
お気に入りにしたユーザー ProblemId : 8103 / 自分の提出
問題文最終更新日: 2022-10-14 11:11:00
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 364 (Do you know Cherry Contest?) (順位表) の他の問題:

Problem Statement

Cherry-chan has recently developed a time machine. Using this time machine, her goal is to travel to the world exactly $T$ days from now.

Here, if $T \lt 0$, "$T$ days from now" means $|T|$ days ago.

Cherry-chan can perform the following any number of times, $0$ or more: choose $1$ of the $3$ types of actions below and execute it.

  • Without using the time machine, wait until the next day (taking $1$ day).
  • Using the time machine, spend $X$ days to travel to the world $A$ days after the moment before the move.
  • Using the time machine, spend $Y$ days to travel to the world $B$ days before the moment before the move.

Find the minimum number of days experienced by Cherry-chan $(= 3$ types of actions' total days spent $)$ required to travel to the world $T$ days from now.

In this problem, assume the following.

  • The time between actions is negligible.
  • Cherry-chan can travel to any world, no matter how far in the past or in the future.
  • The length of $1$ day is the same, no matter how far in the past or in the future.
  • Once she boards the time machine, she can get off only in the world $A$ days later or $B$ days earlier. Therefore, a move that crosses over the day $T$ days from now does not count as having traveled to the world $T$ days from now.

It can be proven that, under the constraints of this problem, Cherry-chan can travel to the world $T$ days from now.

Constraints

  • $-10^7 \leq T \leq 10^7,~T \neq 0$
  • $1 \leq X \lt A \leq 10^7$
  • $1 \leq Y \leq 10^7$
  • $1 \leq B \leq 10^7$
  • All inputs are integers.

Input

The input is given from standard input in the following format.

$T$
$X$ $A$
$Y$ $B$

Output

Output, as an integer on $1$ line, the minimum number of days experienced by Cherry-chan that is required.

Sample

Sample 1
Input
8
2 9
3 2
Output
6

The goal can also be achieved by doing nothing and waiting $8$ days, but by acting as follows, Cherry-chan can travel to the world $8$ days from now in $2+1+3=6$ experienced days.

  • Spend $2$ days to go $9$ days later $\to$ do nothing and wait until the next day $\to$ spend $3$ days to go $2$ days earlier.

Also, it is impossible to travel to the world $8$ days from now in $5$ or fewer experienced days, so the answer is $6$ experienced days.

Sample 2
Input
1
50 100
100 100
Output
1

It is best to do nothing and wait $1$ day. Note that if she waits $1$ day, Cherry-chan naturally experiences $1$ day.

Sample 3
Input
-2611
21 46
26 11
Output
6195

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。