100 atcoder#ABC175C. [ABC175C] Walking Takahashi

[ABC175C] Walking Takahashi

Score : 300300 points

Problem Statement

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

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

He wants to make KK 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

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

Input

Input is given from Standard Input in the following format:

XX KK DD

Output

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

6 2 4
2

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

  • Move from coordinate 66 to (64=6 - 4 =) 22.
  • Move from coordinate 22 to (24=2 - 4 =) 2-2.

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

7 4 3
1

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

  • Move from coordinate 77 to 44.
  • Move from coordinate 44 to 77.
  • Move from coordinate 77 to 44.
  • Move from coordinate 44 to 11.

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

10 1 2
8
1000000000000000 1000000000000000 1000000000000000
1000000000000000

The answer can be enormous.