C - Walking Takahashi 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300

問題文

数直線上で暮らす高橋君は、今座標 X にいます。これから高橋君はちょうど K 回、座標の正または負の方向に D 移動する行為を繰り返そうと考えています。

より正確には、1 回の移動では 座標 x から x + D または x - D に移動できます。

高橋君は、ちょうど K 回移動した後にいる座標の絶対値が最小となるように移動したいです。

K 回の移動後の座標の絶対値としてあり得る値の最小値を求めてください。

制約

  • -10^{15} \leq X \leq 10^{15}
  • 1 \leq K \leq 10^{15}
  • 1 \leq D \leq 10^{15}
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

X K D

出力

K 回の移動後の座標の絶対値としてあり得る値の最小値を出力せよ。


入力例 1

6 2 4

出力例 1

2

高橋君は、今座標 6 にいます。次のように移動するのが最適です。

  • 6 から (6 - 4 =) 2 に移動する。
  • 2 から (2 - 4 =) -2 に移動する。

移動後の座標の絶対値は 2 で、これより小さくすることはできません。


入力例 2

7 4 3

出力例 2

1

高橋君は、今座標 7 にいます。例えば次のように移動するのが最適です。

  • 7 から 4 に移動する。
  • 4 から 7 に移動する。
  • 7 から 4 に移動する。
  • 4 から 1 に移動する。

移動後の座標の絶対値は 1 で、これより小さくすることはできません。


入力例 3

10 1 2

出力例 3

8

入力例 4

1000000000000000 1000000000000000 1000000000000000

出力例 4

1000000000000000

答えは非常に大きな値になる場合もあります。

Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

  • -10^{15} \leq X \leq 10^{15}
  • 1 \leq K \leq 10^{15}
  • 1 \leq D \leq 10^{15}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.


Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

  • Move from coordinate 6 to (6 - 4 =) 2.
  • Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.


Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

  • Move from coordinate 7 to 4.
  • Move from coordinate 4 to 7.
  • Move from coordinate 7 to 4.
  • Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.


Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.