Scanf.scanf "%d %d %d" (fun x k d ->
    let x = abs x in
    let c = x / d in
    Printf.printf "%d\n" @@
        if c >= k then x - d * k else
            abs (if (c - k) mod 2 = 0 then x - d * c else x - d * (c + 1))
)