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 (Python):
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools
from math import *
from bisect import *
from heapq import *
from collections import *

def lcm(a, b):  
    return (a*b)/gcd(a, b)

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()



X, K, D = In.ints()

X = abs(X)

howManyTimesDinX = X//D

if howManyTimesDinX > K:
    Out.int(X-K*D)
else:
    X = X-howManyTimesDinX*D
    K -= howManyTimesDinX

    if K%2==0:
        Out.int(X)
    else:
        X-=D
        X=abs(X)
        Out.int(X)
    




if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

Solution (Python):
x, k, d = [int(x) for x in input().split()]
x = abs(x)
if x - k*d <= 0:
    t = x//d
    x -= (d*t)
    k -= t
else:
    x -= k*d
    print(x)
    exit()


if k % 2 == 0:
    print(x)
else:
    print(abs(x - d))

Solution (Python):
x, k, d = map(int, input().split())
count = abs(x)//d
if count > k:
    if x < 0:
        print(abs(x+k*d))
    else:
        print(abs(x-k*d))
else:
    if x < 0:
        ans1 = x+count*d
    else:
        ans1 = x-count*d
    if ans1 > 0:
        ans2 = ans1-d
    else:
        ans2 = ans1+d
    if (k-count)%2==0:
        print(abs(ans1))
    else:
        print(abs(ans2))

Solution (Python):
X,K,D=input().split()
X,K,D=abs(int(X)),int(K),int(D)
reach=int(X/D)
if reach>K: print(X-D*K)
else: print( X-D*reach if (K-reach)%2==0 else -(X-D*reach-D) )

Solution (Python):
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline


def main():
    X, K, D = map(int, input().split())
    divD = abs(X)//D
    if K <= divD:
        ans = abs(X) - D*K
    elif X%D != 0:
        if (K - divD)%2 == 0:
            ans = abs(X)%D
        else:
            ans = -abs(X)%D
    else:
        if (K - divD)%2 == 0:
            ans = abs(X)%D
        else:
            ans = D
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math
import pprint
import fractions
import collections
import itertools
from decimal import *
from collections import deque
from bisect import *
X,K,D=map(int,input().split())
num=abs(X)//D
subX=0
if X<0:
    X=X+min(K,num)*D
    subX=X+D
else:
    X=X-min(K,num)*D
    subX=X-D
K=max(K-num,0)

if K%2==0:
    print(abs(X))
else:
    print(abs(subX))

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
x,k,d  =ma()
if abs(x) > k*d:
    ans = abs(x)-k*d
else:
    if x<0:
        x*=-1
    t1 = x%d
    t2 = abs(x%d-d)
    pal = (x-t1)//d
    if pal%2==k%2:
        ans = t1
    else:
        ans=t2
print(ans)

Solution (Python):
def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    if abs(x) >= k * d:
        print(abs(x - k * d))
        exit()
    c = x // d
    if abs(x - c * d) < abs(x - (c + 1) * d):
        k -= c
        k %= 2
        print(abs(x - c * d)) if k == 0 else print(abs(x - (c + 1) * d))
    elif abs(x - c * d) > abs(x - (c + 1) * d):
        k -= c + 1
        k %= 2
        print(abs(x - c * d)) if k == 1 else print(abs(x - (c + 1) * d))
    else:
        print(abs(x - c * d))





if __name__ == '__main__':
    main()

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import bisect
import math
import fractions
import sys
import copy


def L(): return sys.stdin.readline().split()


def I(): return int(sys.stdin.readline().rstrip())


def SL(): return list(sys.stdin.readline().rstrip())


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def LS(): return [list(x) for x in sys.stdin.readline().split()]


def IR(n): return [I() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def LIR1(n): return [LI1() for _ in range(n)]


def SR(n): return [SL() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def LR(n): return [L() for _ in range(n)]


def perm(n, r): return math.factorial(n) // math.factorial(r)


def comb(n, r): return math.factorial(
    n) // (math.factorial(r) * math.factorial(n-r))


alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007
INF = float("inf")


def main():
    X, K, D = LI()

    if abs(X) >= K * D:
        if X < 0:
            print(abs(X + K * D))
        else:
            print(abs(X - K * D))
    else:
        t = abs(X) // D
        rest = K - t
        if rest % 2 == 0:
            print(abs(X) % D)
        else:
            print(abs(abs(X) % D - D))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    A = X // D
    if A >= K:
        return X - D * K
    if (K - A) % 2:
        return D - X % D
    else:
        return X % D

print(main())

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    X, K, D = LI()

    if abs(X) >= K * D:
        ans = abs(X) - K * D
    else:
        if (K - abs(X) // D) % 2 == 0:
            ans = abs(X) % D
        else:
            ans = D - abs(X) % D

    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
x, k, d = map(int, input().split())
if x < 0:
    x *= (-1)
if (x - (k * d)) > 0:
    ans = x - (k * d)
else:
    if k % 2 == 1:
        x -= d
        k -= 1
    if (x - (k * (d * 2))) > 0:
        ans = x - (k * (d * 2))
    else:
        ans = min(abs(2 * d - (x % (d * 2))), x % (d * 2))
print(ans)

Solution (Python):
X, K, D = map(int, input().split())
if X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
else:
    X = abs(X)
    if X > K * D:
        print(X - K * D)
    else:
        d, m = divmod(X, D)
        K -= d
        if K % 2 == 0:
            print(m)
        else:
            print(D - m)

Solution (Python):
x, k, d = map(int, input().split())


if x == 0:
    if k % 2 == 0:
        print(0)
    else:
        print(d)
    exit()

x = abs(x)

if x-k*d <= 0:
    
    a, b = divmod(x, d)
    if (k-a) % 2 == 0:
        print(b)
    else:
        print(min(b+d, d-b))
else:
    
    print(x-k*d)

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
if k <= x // d:
    print(abs(abs(x) - d * k))
else:
    m = x // d
    k -= m
    x -= d * m
    x = abs(x)
    if k % 2 == 1:
        print(abs(abs(x) - d))
    else:
        print(abs(x))

Solution (Python):


import math

X, K, D = map(int, input().split())

X = abs(X)
N = math.ceil(X/D)

if K < N:
    print(X-K*D)
    exit()
if N % 2 == K % 2 :
    print(abs(X-N*D))
else:
    print(abs(X-N*D+D))

Solution (Python):




x, k, d = [int(i) for i in input().split()]

kotae = 0
if(abs(x) >= abs(k*d)):
    kotae = min(abs(x+k*d),abs(x-k*d))
else:

    temp = abs(x) // abs(d)
    kotae = min(abs(x + temp * d), abs(x - temp * d))
    if((k-temp) % 2 == 1):
        kotae = abs(kotae-d)


print(kotae)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    X,K,D =map(int,input().split())
    ans =10**19
    if X >= K*D:
        print(X-K*D)
    elif -X>= K*D:
        print(-X - K*D)

    else:
        X =abs(X)
        c =X//D
        if (K-c)%2  ==0:
            print(X-c*D)
        else:
            print(D*(c+1)-X)








if __name__ == "__main__":
    main()

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)

mv = K * D
if mv <= X:
    ans = X - mv
    print(ans)
    exit()

need = (X + D - 1) // D

X -= need * D
K -= need

if K % 2 == 0:
    ans = abs(X)
    print(ans)
else:
    X += D
    print(X)

Solution (Python):
X,K,D = map(int,input().split())
X = abs(X)
T = X//D
if K < T:
    print(min(abs(X-K*D),abs(X+K*D)))
else:
    X -= D*T
    K -= T
    if K%2:
        print(min(abs(X+D),abs(X-D)))
    else:
        print(abs(X))

Solution (Python):
x, k, d = map( int, input().split() )

p = x // d
q = x % d

if k < abs( p ):
    print( abs( x ) - k * d )
else:
    t = k - p
    if t % 2 == 0:
        print( q )
    else:
        print( d - q )

Solution (Python):

import sys


def solve(X: int, K: int, D: int):
    st = abs(X) - K*D    
    if abs(X) >= K*D:
        print(st)
    elif abs(X) < K*D:
        st = K*D - abs(X)
        p = st // (2*D)
        pp = st - (2*D)*p
        print(min(pp, abs(pp-(2*D))))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    K = int(next(tokens))  
    D = int(next(tokens))  
    solve(X, K, D)

if __name__ == '__main__':
    main()

Solution (Python):
x, k, d = map(int, input().split())

mx = x + k * d
mn = x - k * d

if (mx > 0 and mn < 0):
    ans1 = x % d
    ans2 = d - x % d
    k -= abs(x % d - x) // d
    if k % 2 == 0:
        print(ans1)
    else:
        print(ans2)
else:
    print(min(abs(mx), abs(mn)))

Solution (Python):

import sys
sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1  
MOD = 10 ** 9 + 7


def debug(*x):
    print(*x, file=sys.stderr)


def solve(X, K, D):
    X = abs(X)
    a = X // D
    if a >= K:
        return X - K * D
    r = X % D
    K -= a
    if K % 2 == 0:
        return r
    else:
        return D - r


def main():
    
    X, K, D = map(int, input().split())
    print(solve(X, K, D))



T1 = """
6 2 4
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
2
"""

T2 = """
7 4 3
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
1
"""

T3 = """
10 1 2
"""
TEST_T3 = """
>>> as_input(T3)
>>> main()
8
"""

T4 = """
1000000000000000 1000000000000000 1000000000000000
"""
TEST_T4 = """
>>> as_input(T4)
>>> main()
1000000000000000
"""


def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g)


def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii")


input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit()

main()

Solution (Python):
X, K, D = map(int, input().split())
if X > 0:
  px = X % D
  cnt = X // D
  if cnt > K:
    print(abs(X - D * K))
    exit()
  nx = px - D
  K -= cnt
  if K % 2 == 0:
    print(abs(px))
  else:
    print(abs(nx))
elif X == 0:
  if K % 2 == 0:
    print(X)
  else:
    print(abs(D))
else:
  cnt = int(-X / D)
  nx = X + cnt * D
  if cnt > K:
    print(abs(X + D * K))
    exit()
  px = nx + D
  K -= cnt
  if K % 2 == 0:
    print(abs(nx))
  else:
    print(abs(px))

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)
if X>abs(K*D):
    print(X-abs(K*D))
else:
    A = X%D
    B = D-A
    kk = K - X // D
    if kk%2 == 0:
        print(A)
    else:
        print(B)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    x, k, d = in_nl()

    if x < 0:
        x = -x
    if d * k <= x:
        ans = abs(x - d * k)
    else:
        cnt = x // d
        if cnt % 2 == k % 2:
            ans = abs(x - d * cnt)
        else:
            ans = abs(x - d * (cnt + 1))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
x, k ,d = map(int, input().split())

if abs(x) > k*d:
    print(abs(x)-k*d)
else:
    t = abs(x)%d
    s = (abs(x)-t)//d
    if (k-s) % 2 == 1:
        print(abs(t-d))
    else:
        print(t)

Solution (Python):
x,k,d=map(int,input().split())
if x>0:
  if x-k*d>=0:
    print(x-k*d)
  else:
    t=x%d
    if (int((x-t)/d))%2==(k%2):
      print(t)
    else:
      print(d-t)
    
elif x<0:
  if x+k*d<=0:
    print(-(x+k*d))
  else:
    t=(-x)%d
    if (int((-x-t)/d))%2==(k%2):
      print(t)
    else:
      print(d-t)
else:
  if k%2==0:
    print(0)
  else:
    print(d)

Solution (Python):

def inputlist(): return [int(j) for j in input().split()]

X,K,D= inputlist()
count = abs(X)//D +1
if K < count:
    print(min(abs(X-D*K),abs(X+D*K)))
    exit()
d_odd = 0
d_even = 0
if X >= 0:
    d_odd = X - D*(count-1)
    d_even = X-D*count
if X < 0:
    d_odd = X + D*(count-1)
    d_even = X + D*count
K -=count
if K % 2 == 0:
    print(abs(d_even))
else:
    print(abs(d_odd))

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

X, K, D = mapint()
if abs(X)>=K*D:
    print(abs(abs(X)-K*D))
else:
    l, r = 0, K
    X = abs(X)
    while r-l>1:
        half = (r+l)//2
        if half*D>X:
            r = half
        else:
            l = half
    now = X-l*D
    if (K-l)%2==0:
        print(now)
    else:
        print(D-now)

Solution (Python):

x,k,d = map(int,input().split())
ans = 0
if x-k*d >=0:
    ans = x-k*d
elif x+k*d <= 0:
    ans = -(x+k*d)
else:
    a = -(x-k*d)//(2*d)
    ans = min(- (x+(2*a-k)*d),x+(2*(a+1)-k)*d)

print(ans)

Solution (Python):
X,K,D=map(int,input().split())

X=abs(X)

tmp=X//D

if tmp>=K:
  print(X-K*D)
  exit()

if (K-tmp)%2==0:
  print(X-tmp*D)
  
else:
  print(abs(X-(tmp+1)*D))

Solution (Python):
import sys
from math import pi, cos, sqrt
from collections import defaultdict
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    x, k, d = geta(int)

    ans = 0
    if abs(x) > k * d:
        ans = abs(x) - k * d
    else:
        q, r = divmod(abs(x), d)

        if (k - q) & 1 == 0:
            ans = r
        else:
            ans = d - r

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    x, k, d = map(int, input().split())

    if x < 0:
        step = abs(x) // d
        if step >= k:
            ans = abs(x + k*d)
        else:
            residue = k - step
            residue %= 2
            ans = abs(x + (step+residue)*d)

    elif x == 0:
        k %= 2
        ans = k * d
    else:
        step = abs(x) // d
        if step >= k:
            ans = abs(x - k*d)
        else:
            residue = k - step
            residue %= 2
            ans = abs(x - (step+residue)*d)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math
x,k,d=map(int,input().split())
x1=abs(x)
if k*d<x1:
    print(x1-k*d)
else:
    a=math.ceil(x1/d)
    if (k-a)%2==0:
        print(abs(x1-d*a))
    else:
        print(abs(x1-d*(a-1)))

Solution (Python):
x,k,d=map(int,input().split())
print(max(abs(x)-k*d,[x%d,d-x%d][(k-x//d)%2]))

Solution (Python):
X,K,D=map(int,input().split())
X=abs(X)

m=min(K,X//D)
X-=D*m
K-=m
K%=2
X-=D*K
print(abs(X))

Solution (Python):
X, K,D = map(int,input().split())
n = abs(X)
check = n//D
if check > K:
    print(n - D*K)
else:
    point = K - check
    if point%2 == 0:
        print(n-D*check)
    else:
        print(D*(check+1)-n)

Solution (Python):
X, K, D = map(int, input().split())

if K * D < abs(X):
    if X < 0:
        print(abs(X + K * D))
    else:
        print(X - K * D)
else:
    a = abs(X // D)
    if (K - a) % 2 == 0:
        print(X % D)
    else:
        print(abs(X % D - D))

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)

if K * D <= X:
    ans = X - K * D
else:
    r = X // D + 1
    l = X // D
    if r % 2 == K % 2:
        ans = abs(X - D * r)
    else:
        ans = abs(X - D * l)
print(ans)

Solution (Python):
x,k,d=map(int,input().split())
m=abs(x)//d
if k<=m:
    print(abs(x)-k*d)
else:
    if (m-k)%2==0:
        print(abs(x)%d)
    else:
        print(d-abs(x)%d)

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

x, k, d = MAP()
n = x//d

if k <= abs(n):
    if x > 0:
        ans = x - d*k
    else:
        ans = x + d*k
elif ( k - abs(n) ) % 2 == 0:
    ans = x - d*n
else:
    ans = x - d*(n+1)

print(abs(ans))

Solution (Python):
X, K, D = map(int, input().split())
if abs(X) > D*K:
    print(abs(X) - D*K)

else:
    k = abs(X) // D
    X = abs(X) % D
    K -= k
    if K % 2 == 0:
        print(X)
    else:
        print(D-X)

Solution (Python):
def resolve():
    X,K,D = map(int,input().split())
    X = abs(X)
    if K * D <= X:
        print(X-K*D)
        return
    K -= X//D
    X %= D
    if K %2 ==0:
        print(X)
    else:
        print(abs(X-D))

resolve()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))



def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  
from functools import reduce

X, K, D = ints()

if X < 0:
    X = -X


if X >= K * D:
    exit(X - K * D)


XX = X % D  
K -= X // D  


print(XX if K & 1 == 0 else -(XX - D))

Solution (Python):
X, K, D = map(int, input().split())
if X < 0:
  X = -X
if K*D <= X:
  print(abs(X-K*D))
else:
  if K%2 != (X//D)%2:
    print(abs(X%D - D))
  else:
    print(abs(X%D))

Solution (Python):
x, k, d = map(int, input().split())
print(abs(x) - d * k) if abs(x) - d * k > 0 else print(abs(x)%d) if k % 2 == (abs(x)//d)%2 else print(abs(abs(x)%d - d))

Solution (Python):
x,k,d = map(int,input().split())
r = abs(x)//d
if r >= k:
    print(abs(x)-abs(d)*k)
else:
    if (k-r)%2==0:
        print(abs(x)-abs(d)*r)
    else:
        print(abs(abs(x)-abs(d)*(r+1)))

Solution (Python):
x,k,d=map(int,input().split())

x=abs(x)

if x>0:
    
    if x>=d*k:
        print(x-d*k)
    
    else:
        cnt=((-1*x)//d)*(-1)
        if cnt==k:
            print(abs(x-d*cnt))
        else:
            if (k-cnt)%2==0:
                print(abs(x-d*cnt))
            else:
                print(abs(x-d*(cnt-1)))

else:
    if k%2==0:
        print(0)
    else:
        print(abs(d))

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    x, k, d = rli()
    x = abs(x)
    if x >= k*d:
        print(x-k*d)
        return
    
    n = x//d
    if n % 2 != k % 2:
        print(abs(x%d-d))
    else:
        print(x%d)


if __name__ == '__main__':
    main()

Solution (Python):
x, k, d = map(int, input().split())

x = abs(x)

if k * d < x:
    print(x - k*d)
else:
    k -= x // d
    x %= d
    if k % 2 == 0:
        print(x)
    else:
        print(d - x)

Solution (Python):
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 19 + 7
EPS = 10 ** -10

X, K, D = MAP()

if X < 0:
    X = -X

d = X // D
if d >= K:
    ans = X - D*K
else:
    ans = X - D*d
    K -= d
    if K % 2 == 1:
        ans = -(ans - D)
print(ans)

Solution (Python):
X,K,D=map(int,input().split())

if abs(X)>=K*D:
  print(abs(X)-K*D)
  exit()
  
q,r=divmod(X,D)

if K%2 == q%2:
  print(r)
else:
  print(abs(r-D))

Solution (Python):
x,k,d = map(int, input().split())
if (x>0 and x-k*d>0):
    print(x-k*d)
    exit()
elif (x<0 and x+k*d<0):
    print(abs(x+k*d))
    exit()
x = abs(x)
a = x//d
b = x%d
if (k-a)%2==0:
    print(b)
else:
    print(d-b)

Solution (Python):
X, K, D = map(int, input().split())

times = abs(X) // D

if times >= K:
    print(abs(X) - abs(D * K))
else:
    K -= times
    if K % 2 == 0:
        print(abs(X) - abs(D * times))
    else:
        print(D - (abs(X) - abs(D * times)))

Solution (Python):
X,K,D=map(int,input().split())
if abs(X)>=K*D:
    print(abs(X)-K*D)
else:
    if K%2==abs(X)//D%2:
        print(abs(X)%D)
    else:
        print(D-abs(X)%D)

Solution (Python):
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

x,k,d=map(int,input().split())
if x>0:
    tmp=x//d
    if tmp>k:
        print(x-k*d)
    elif tmp%2 == k%2:
        print(x-tmp*d)
    else:
        print(abs(x-(tmp+1)*d))

else:
    tmp=(-x)//d
    if tmp>k:
        print(-x-k*d)
    elif tmp%2 == k%2:
        print(-x-tmp*d)
    else:
        print(abs(-x-(tmp+1)*d))

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    x, k, d = map(int, input().split())
    if abs(x) > k * d:
        print(abs(x) - k * d)
    else:
        tmp = abs(x) - d * (abs(x) // d)
        if (k - abs(x) // d) % 2 == 0:
            print(tmp)
        else:
            print(abs(tmp - d))


if __name__ == '__main__':
    main()

Solution (Python):
X,K,D=map(int,input().split())
X=abs(X)
L=X//D
ans=0
if L>=K:
    ans=X-D*K
else:
    m=K-L
    if m%2==0:
        ans=X%D
    else:
        ans=abs(X%D-D)
print(ans)

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)

if X > K * D:
  print(X - K * D)
else:
  if K % 2 == 1:
    X -= D
  print(min(X % (2 * D) , (D * 2) - X % (D * 2)))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


X, K, D = inl()


def solve():
    if X >= K * D:
        return X - K * D
    if X <= -K * D:
        return abs(X + K * D)
    y = X + K * D
    assert y > 0
    r = y // (2 * D)

    ans = y
    for k in range(r - 10, r + 10):
        if k < 0 or k > K:
            continue
        z = y - k * 2 * D
        ans = min(ans, abs(z))

    return ans


print(solve())

Solution (Python):
def resolve():
    x, k, d = map(int, input().split())
    if x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(d)
    elif x > 0:
        minmove = x // d
        if minmove > k:
            print(x-k*d)
        else:
            if (k-minmove)%2 == 0:
                print(x-minmove*d)
            else:
                print(abs(x-(minmove+1)*d))
    else:
        minmove = abs(x)//d
        if minmove > k:
            print(abs(x+k*d))
        else:
            if (k-minmove)%2 == 0:
                print(abs(x+minmove*d))
            else:
                print(abs(x+(minmove+1)*d))
resolve()

Solution (Python):
x, k, d = list(map(int, input().split()))

s = 0

if x - k * d > 0:
    s = x - k * d
elif x + k * d < 0:
    s = abs(x + k * d)
else:
    e = x // d
    r = x % d
    if (k-e) % 2 == 1:
        s = abs(r-d)
    else:
        s = r

print(s)

Solution (Python):
X,K,D = map(int, input().split())
if X == 0:
  if K % 2 == 0:
    print(0)
  else:
    print(D)
  exit(0)

if X < 0:
  xminus = True
  X = (-1) * X
else:
  xminus = False
  
if abs(X) > K*D:
  ans = X - K*D
else:
  wk = X // D
  K -= wk
  now = X % D
  
  if K % 2 == 0:
    ans = now
  else:
    ans = abs(now-D)
    
print(ans)

Solution (Python):
x, k, d = map(int, input().split())

if abs(x) - k * d > 0:
    print(abs(x) - k * d)
else:
    kk = abs(x) // d
    l = (k - kk) % 2
    print(abs(abs(x) - (kk + l) * d))

Solution (Python):

x,k,d = list(map(int,input().split()))

mv_cnt_near_zero = abs(x)//d
point_near_zero = abs(x)%d

ret = 0
if mv_cnt_near_zero >=k:
    ret = abs(x) - k * d
else:
    rest_can_mv = k - mv_cnt_near_zero
    if rest_can_mv % 2 ==0:
        ret = point_near_zero
    else:
        ret = abs(point_near_zero - d)

print(ret)

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
if k<=x//d:
    print(abs(x-k*d))
elif (x//d+k)%2==0:
    print(abs(x-x//d*d))
else:
    print(abs(x-(x//d+1)*d))