Problem:
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.

Solution in Java (OpenJDK 1.8.0) (judge verdict: Accepted)


import java.io.InputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
  public static void main(String[] args) throws Exception {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    // Your code here!
    Scanner scan = new Scanner(is);
    long X = scan.nextLong();
    long K = scan.nextLong();
    long D = scan.nextLong();

    long ans = 0;
    long prevprev = 0;
    long prev = 0;
    long times = 0;
    if(X > 0){
      if(X/D < K) {
        K -= X/D;
        X = X - D * (X/D);
      } else {
        X = X - D*K;
        K = 0;
      }
    } else {
      if(-1*X/D < K) {
        K -= -1*X/D;
        X = X + D*(X/D);
      } else {
        X = X + D*K;
        K = 0;
      }
    }

    while(K > 0) {
      if(X > 0) {
        X -= D;
      } else {
        X += D;
      }
      K--;
      times++;
      if(times == 1)
        prevprev = X;
      else if(times == 2)
        prev = X;

      if(times >= 3) {
        if((X > 0 && prev <= 0 && prevprev >0) || (X <=0 && prev > 0 && prevprev <= 0)) {
          if(K % 2 == 1){
            if(X > 0) {
              X -= D;
            } else {
              X += D;
            }
          }
          break;
        }


        prevprev = prev;
        prev = X;
      }
    }

    os.println(Math.abs(X));
  }
}