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 Rust (1.42.0) (judge verdict: Wrong Answer)

use std::io::{stderr, stdin, stdout, BufReader, BufWriter, Cursor, Read, Write};
use std::iter::Iterator;
use std::str::FromStr;
use std::cmp::min;
// use std::slice::Iter;
// use std::vec::IntoIter;

#[allow(dead_code)]
fn main() {
  let stdin = stdin();
  let r = &mut BufReader::new(stdin.lock());
  let stdout = stdout();
  let w = &mut BufWriter::new(stdout.lock());
  let stderr = stderr();
  let err = &mut BufWriter::new(stderr.lock());
  run(r, w, err);
  w.flush().unwrap();
  err.flush().unwrap();
}

macro_rules! _ft { () => (impl FnOnce(&mut Cursor<Vec<u8>>, &mut Vec<u8>, &mut Vec<u8>)); }

#[allow(dead_code)]
fn test(input: &str, output: &str, f: _ft!()) {
  let r = &mut Cursor::new(input.as_bytes().to_vec());
  let w: &mut Vec<u8> = &mut Vec::new();
  let err: &mut Vec<u8> = &mut Vec::new();
  f(r, w, err);
  let mut stderr = stderr();
  writeln!(stderr, "{}", String::from_utf8(err.to_vec()).unwrap()).unwrap();
  stderr.flush().ok();
  assert_eq!(String::from_utf8(w.to_vec()).unwrap(), output)
}

#[allow(dead_code)]
macro_rules! t {
  ($f: ident => $input: expr, $output: expr) => (
    #[test]
    fn $f() { test($input, $output, run); }
  );
}

#[allow(unused_macros)]
macro_rules! args {
  ($($arg: expr),*) => ({
    let mut s = String::new();
    $(s += &format!("{} ", $arg);)*
    s.pop().unwrap();
    s
  });
}

#[allow(unused_macros)]
macro_rules! st {
  ($name: ident => $($p: ident : $t: ty),+) => (
    struct $name { $($p : $t),+ }
    impl FromStr for $name {
      type Err = ();
      fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split_whitespace();
        Ok(Self {
          $($p: iter.next().unwrap().parse::<$t>().unwrap(),)+
        })
      }
    }
  );
  ($name: ident => $($($p: ident),+: $t: ty);+) => (st!($name => $($($p : $t),+),+););
  ($name: ident => $($($p: ident),+: $($t: ty),+);+) => (st!($name => $($($p : $t),+),+););
  ($name: ident => $($p: ident),+: $t: ty) => (st!($name => $($p : $t),+););
  ($name: ident => $($p: ident),+: $($t: ty),+) => (st!($name => $($p : $t),+););
}

fn _read_iter<R: Read>(reader: &mut R) -> impl Iterator<Item=char> + '_ {
  reader.by_ref()
    .bytes()
    .map(|c| c.unwrap() as char)
    .skip_while(|c| c.is_whitespace())
}

#[allow(dead_code)]
fn read<F: FromStr, R: Read>(reader: &mut R) -> F {
  let str = _read_iter(reader).take_while(|c| !c.is_whitespace()).collect::<String>();
  str.parse::<F>().ok().unwrap()
}

#[allow(dead_code)]
fn read_line<F: FromStr, R: Read>(reader: &mut R) -> F {
  let str = _read_iter(reader).take_while(|c| c != &'\n').collect::<String>();
  let str = if str.ends_with('\r') { &str[0..str.len() - 1] } else { &str };
  str.parse::<F>().ok().unwrap()
}

#[allow(dead_code)]
fn read1<R: Read, T>(reader: &mut R, mut f: impl FnMut(char) -> T) -> T {
  f(_read_iter(reader).next().unwrap())
}

#[allow(dead_code)]
fn read1_line<R: Read, T>(reader: &mut R, f: impl FnMut(char) -> T) -> Vec<T> {
  _read_iter(reader).take_while(|c| c != &'\n').map(f).collect::<Vec<_>>()
}

/// 1要素読む
#[allow(unused_macros)]
macro_rules! r {
  ($stream: expr) => (read::<String, _>($stream));
  ($stream: expr, ) => (r!($stream));
  ($stream: expr; ) => (r!($stream));

  ($stream: expr; $t: ty) => (read::<$t,  _>($stream));
  ($stream: expr; $($t: ty),+) => (($(r!($stream; $t)),*));

  ($stream: expr; $t: ty; 1~) => (read::<$t,  _>($stream) - 1);
  ($stream: expr; $($t: ty),+; 1~) => (($(r!($stream; $t) - 1),*));

  ($stream: expr; $t: ty; $n: expr) => ((0..$n).map(|_| r!($stream; $t)).collect::<Vec<_>>());
  ($stream: expr; $t: ty; $h: expr, $w: expr) => (
    (0..$h).map(|_| (0..$w).map(|_| r!($stream; $t)).collect::<Vec<_>>()).collect::<Vec<_>>()
  );
}

#[allow(unused_macros)]
macro_rules! r1 {
  ($stream: expr) => (read1($stream, |c| c));
  ($stream: expr, ) => (r!($stream));
  ($stream: expr; ) => (r!($stream));
  ($stream: expr; $f: expr) => (read1($stream, $f));
  ($stream: expr; $f: expr; $n: expr) => ((0..$n).map(|_| r1!($stream, $f)).collect::<Vec<_>>());
  ($stream: expr; $f: expr; $h: expr, $w: expr) => (
    (0..$h).map(|_| (0..$w).map(|_| r1!($stream; $f)).collect::<Vec<_>>()).collect::<Vec<_>>()
  );
}

/// 1行読む
#[allow(unused_macros)]
macro_rules! rl {
  ($stream: expr) => (read_line::<String, _>($stream));
  ($stream: expr, ) => (r!($stream));
  ($stream: expr; ) => (r!($stream));
  ($stream: expr; $t: ty) => (read_line::<$t,  _>($stream));
  ($stream: expr; $t: ty; $n: expr) => ((0..$n).map(|_| rl!($stream; $t)).collect::<Vec<_>>());
}

macro_rules! _writes {
  ($stream: expr; $arg0: expr, $($arg: expr),*) => ({
    write!($stream, "{}", $arg0).ok();
    $(write!($stream, " {}", $arg).ok();)*
  });
}

/// write
#[allow(unused_macros)]
macro_rules! w {
  ($stream: expr) => (write!($stream, " ").ok());
  ($stream: expr; ) => (w!($stream));
  ($stream: expr, ) => (w!($stream));

  ($stream: expr; $arg: expr) => (write!($stream, "{}", $arg).ok());
  ($stream: expr; $arg: expr, ) => (w!($stream; $arg));

  ($stream: expr; $arg0: expr, $($arg: expr),+) => (_writes!($stream; $arg0, $($arg),+));
  ($stream: expr; $arg0: expr, $($arg: expr),+, ) => (w!($stream; $arg0, $($arg),*));

  ($stream: expr, $($arg: tt)*) => (write!($stream, $($arg)*).ok());
}

/// write_line
#[allow(unused_macros)]
macro_rules! wl {
  ($stream: expr) => (writeln!($stream).ok());
  ($stream: expr, ) => (wl!($stream));
  ($stream: expr; ) => (wl!($stream));

  ($stream: expr; $arg: expr) => (writeln!($stream, "{}", $arg).ok());
  ($stream: expr; $arg: expr, ) => (wl!($stream; $arg));

  ($stream: expr; $arg0: expr, $($arg: expr),+) => ({
    _writes!($stream; $arg0, $($arg),+);
    wl!($stream)
  });
  ($stream: expr; $arg0: expr, $($arg: expr),+, ) => (wl!($stream; $arg0, $($arg),*));

  ($stream: expr, $($arg: tt)*) => (writeln!($stream, $($arg)*).ok());
}

/// write Vec
#[allow(unused_macros)]
macro_rules! w_vec {
  ($stream: expr; $vec: expr) => ({
    let mut iter = $vec.iter();
    match iter.next() { Some(&i) => { w!($stream, "{}", i); }, None => {}, }
    for &i in iter { w!($stream, " {}", i); }
  });
}

/// write_line Vec
#[allow(unused_macros)]
macro_rules! wl_vec {
  ($stream: expr; $vec: expr) => ({
    w_vec!($stream; $vec);
    wl!($stream)
  });
}

trait VecAlias<T> {
  fn select<U>(&self, f: impl FnMut(&T) -> U)  -> Vec<U>;
}

impl<T> VecAlias<T> for Vec<T> {
  fn select<U>(&self, mut f: impl FnMut(&T) -> U)  -> Vec<U> {
    self.iter().map(|i| f(i)).collect::<Vec<_>>()
  }
}

trait VecOrdAlias<T> {
  fn asc(&mut self);
  fn desc(&mut self);
}

impl<T: Ord> VecOrdAlias<T> for Vec<T> {
  fn asc(&mut self) { self.sort_unstable(); }
  fn desc(&mut self) { self.sort_unstable_by(|i, j| j.cmp(i)); }
}

#[allow(unused_macros)]
macro_rules! v {
  ($elem: expr; $n: expr) => (vec![$elem; $n as usize]);
  ($elem: expr; $h: expr, $w: expr) => (vec![vec![$elem; $w as usize]; $h as usize]);
}

#[allow(unused_macros)]
macro_rules! check_v {
  ($h: expr, $w: expr => $i: expr, $j: expr) => (
    $i >= 0 && $i < $h && $j >= 0 && $j < $w
  );
}

#[allow(unused_macros)]
macro_rules! check_get_v {
  ($($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    if (check_v!($h, $w => $i, $j)) { Some(($($vec[$i as usize][$j as usize],)+)) }
    else { None }
  );
  (up; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    check_get_v!($($vec, )+; $h, $w => $i - 1, $j)
  );
  (down; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    check_get_v!($($vec, )+; $h, $w => $i + 1, $j)
  );
  (left; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    check_get_v!($($vec, )+; $h, $w => $i, $j - 1)
  );
  (right; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    check_get_v!($($vec, )+; $h, $w => $i, $j + 1)
  );
}

#[allow(unused_macros)]
macro_rules! get_v {
  ($($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    ($($vec[$i as usize][$j as usize], )+)
  );

  (up; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    if $i > 0 { Some(($($vec[($i - 1) as usize][$j as usize],)+)) } else { None }
  );
  (down; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    if $i < $h - 1 { Some(($($vec[($i + 1) as usize][$j as usize],)+)) } else { None }
  );
  (left; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    if $j > 0 { Some(($($vec[$i as usize][($j - 1)  as usize],)+)) } else { None }
  );
  (right; $($vec: expr),+; $h: expr, $w: expr => $i: expr, $j: expr) => (
    if $j < $w - 1 { Some(($($vec[$i as usize][($j + 1) as usize],)+)) } else { None }
  );
}

#[allow(dead_code)]
struct Point<T> { i: T, j: T }

#[allow(unused_macros)]
macro_rules! pos {
  ($i: expr, $j: expr) => (Point { i: $i, j: $j });
  (up; $i: expr, $j: expr) => (pos!($i - 1, $j));
  (down; $i: expr, $j: expr) => (pos!($i + 1, $j));
  (left; $i: expr, $j: expr) => (pos!($i, $j - 1));
  (right; $i: expr, $j: expr) => (pos!($i, $j + 1));
}

#[allow(unused_variables)]
fn run<R: Read, W: Write, E: Write>(reader: &mut R, writer: &mut W, err: &mut E) {
  let (x, k, d) = r!(reader; i64, i64, i64);
  let x = x.abs();
  if k == 1 { w!(writer; min(x, (x - d).abs())); return; }
  if x / k >= d { w!(writer; x - k * d); return; }
  let ans =
    if k % 2 == 0 {
      let a = x % (2 * d);
      if a > d { 2 * d - a } else { a }
    }
    else {
      let a = (x % (2 * d) - d);
      a.abs()
    };
  // let ans = if k % 2 == 0 { x } else { x + d };
  // let ans = ans % (2 * d);
  // let ans = if ans < d { ans } else { 2 * d - ans };
  w!(writer; ans);
}

t!(tc1 => r"6 2 4", r"2");
t!(tc2 => r"7 4 3", r"1");
t!(tc3 => r"10 1 2", r"8");
t!(tc4 => r"1000000000000000 1000000000000000 1000000000000000", r"1000000000000000");
t!(tc5 => r"8 4 3", r"2");
t!(tc6 => r"9 4 3", r"3");
t!(tc7 => r"6 4 3", r"0");
t!(tc8 => r"5 4 3", r"1");