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):
X, K, D = map(int, input().split())


def sign(x):
    return 1 if x >= 0 else -1


if abs(X) >= D * K:
    ans = X - sign(X) * D * K
else:
    l = abs(X) // D
    mod = abs(X) % D

    residual = K - l

    if residual % 2 == 0:
        ans = mod
    else:
        ans = mod - sign(mod) * D

print(abs(ans))

Solution (Python):
def A():
    s = input()
    if s == 'SSS': print(0)
    elif s=='RRR': print(3)
    elif s=='RRS' or s=='SRR': print(2)
    else: print(1)

def B():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    for i in range(n):
        for j in range(i+1, n):
            if a[i] == a[j]: continue
            for k in range(j+1, n):
                if a[i]==a[k] or a[j] == a[k]: continue
                s = a[i] + a[j] + a[k]
                if s > 2 * max(a[i], a[j], a[k]):
                    cnt += 1
    print(cnt)

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

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

Solution (Python):
x,k,d = map(int, input().split())
lists = []
x = abs(x)
 
step = int(x/d) 
 
if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = min(abs(x % d - d), abs(x % d + d))
 
print(answer)

Solution (Python):
x,k,d = map(int, input().split())
lists = []
x = abs(x)

step = int(x/d) 

if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = abs(x - step * d - d)

print(answer)

Solution (Python):
X,K,D=list(map(int,input().split()))
if X<0:
    X=-X
if X-K*D>=0:
    print(X-K*D)
else:
    N=X//D
    if (K-N)%2==0:
        print(X-N*D)
    else:
        print((N+1)*D-X)

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

if K > step:
  extra = - D * ((K - step) % 2)
else:
  extra = 0

print(abs(absX - step * D + extra))

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

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

N = abs(X) // D
O = abs(X) - N * D

if O <= (D - O):
    pass
else:
    N += 1
    O = D - O
    
if N >= K:
    print(abs(X) - K * D)
else:
    if (K - N) % 2 == 0:
        print (O)
    else:
        print(D - O)

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

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

Solution (Python):
def main():
    x, k, d = map(int,input().split())
    abs_x = abs(x)
    times = abs_x // d
    if(times > k):
        print(abs_x - k * d)
    else:
        if((k - times) % 2 == 0):
            print(abs_x - times * d)
        else:
            print(abs(abs_x - (times + 1) * d))
if __name__ == '__main__':
    main()

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

    if abs(x) % d == 0:
        exceed_num = (abs(x) // d)
    else:
        exceed_num = (abs(x) // d) + 1


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



if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    x, k, d = map(int, input().split())
    cycle = abs(x) // d     
    if cycle >= k:          
        print(abs(abs(x) - k * d))
    else:                   
        k -= cycle          
        if k&1:             
            print(abs(abs(x) - (cycle + 1) * d))
        else:
            print(abs(abs(x) - (cycle) * d))

if __name__ == '__main__':
    main()

Solution (Python):

[X, K, D] = [int(i) for i in input().split()]

X = abs(X)
if X - K*D > 0:

    print(X-K*D)

else:
    
    time = int(X/D)
    start = X - D * time
    
    time_re = K - time
    
    if time_re % 2 == 0:
        print(start)
    else:
        print(abs(start-D))

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

t = X // D
if abs(t) >= K:
  if X > 0:
  	print(abs(X-(D*K)))
  elif X < 0:
    print(abs(X+(D*K)))
  exit(0)

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

Solution (Python):
import math
import collections
import itertools

def YesNo(Bool):
    if(Bool):
        print("Yes")
    else:
        print("No")
    return

def resolve():

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

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

    print(ans)
resolve()

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

cur  = abs(x)
rem = k

cnt = min(cur // d, k)
cur = cur - d * cnt
rem = rem - cnt

if rem  > 0:
        if rem % 2 == 1:
                cur = cur - d
                
ans = abs(cur)
print(ans)

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

x = abs(x)
straight = min(k, x//d)
k -= straight
x -= straight*d

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

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

N = X // D
if K < abs(N):
    ans = abs(abs(X) - K * D)
else:
    if (K - abs(N)) % 2 == 0:
        ans = abs(X - N * D)
    else:
        ans = abs(X - (N+1) * D)

print(ans)

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

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

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

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

Solution (Python):

X, K, D = list(map(int, input().split()))
div = abs(X)//D
mod = abs(X)%D
if abs(X) > K*D:
    print(abs(X) - K*D)
elif (K-div)%2 == 0:
    print(mod)
else:
    print(D-mod)

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

if X - K * D >= 0:
    ans = abs(X - K * D)
elif X + K * D <= 0:
    ans = abs(X + K * D)
else:
    n = int(abs(X - K * D) / (2 * D))
    ans = min(abs(X - K * D + n * 2 * D), abs(X - K * D + (n + 1) * 2 * D))
    
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


X, K, D = MI()

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

Solution (Python):
import sys





array = list(map(int, input().strip().split()))
x = abs(array[0])
k = array[1]
d = array[2]
if (d == 0):
    amari = abs(x)

else:
    move_cnt = int(x / d)
    if (move_cnt <= k):
        k -= move_cnt
        amari = x - move_cnt * d

    else:
        amari = x - k * d
        k = 0

    if (k > 0):
        if (k % 2 == 1):
            if (amari > 0):
                amari -= d
            else:
                amari += d

print(abs(amari))

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

if k <= l:
    print(abs(x - k * d))
else:
    print(abs(abs(x - d * l) - d * ((k - l) % 2)))

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

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

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

print(ans)

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

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

    if a <= k :
        if (k - a) % 2 == 0 :
            print(abs(x+a*d))
        else :
            print(abs(x+(a+1)*d))
    else :
            print(abs(x+k*d))

Solution (Python):

[X, K, D] = [int(i) for i in input().split()]

X = abs(X)
if X - K*D > 0:

    print(X-K*D)

else:
    
    time = int(X/D)
    start = X - D * time
    
    time_re = K - time
    
    if time_re % 2 == 0:
        print(start)
    else:
        print(abs(start-D))

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

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

if X<0:
    X=-X

if K*D<X:
    print(X-K*D)
else:
    k = K - int(X/D)
    x = X % D
    if k%2:
        print(abs(x-D))
    else:
        print(x)

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

x = abs(x)
mincount = x // d


if mincount < k:
  k = (k-mincount)%2
  result = x - d*mincount -d*k
else:
  result = x - k*d
print(abs(result))

Solution (Python):


from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys

INF = float('inf')


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


def main():

    sys.setrecursionlimit(10 ** 6)

    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))  
    print(f'{solve(X, K, D)}')


if __name__ == '__main__':
    main()

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

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

ax = abs(x)

kd = k*d

if(ax>kd):
    print(ax-kd)
else:
    num = ax//d
    if((num%2)==(k%2)):
        print(ax-(d*num))
    else:
        if(num==k):
            print(ax-d*num)
        else:
            print(abs(ax-d*num-d))

Solution (Python):
input_line = input().split()
x = int(input_line[0])
k = int(input_line[1])
d = int(input_line[2])

x = abs(x)
y = int(x/d)

if y < k:
  x -= y * d
  if (k-y) % 2 == 1:
    x -= d

else:
  x -= k*d
    
print(abs(x))

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):
x, k, d = map(int, input().split())
maxi = k * d
res = x

if x < 0:
    x = abs(x)

if x - maxi >= 0:
    res = x - maxi
else:
    p = x % d
    m = p - d
    rest = k - (x // d)
    if rest % 2 == 1:
        res = m
    else:
        res = p

print(abs(res))

Solution (Python):



import sys

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

X = abs(X)

if X >= K * D:
    print(X - (K * D))
else:
    k = K - X // D
    x = X - D * (X // D)

    if k % 2:
        print(abs(x - D))
    else:
        print(x)

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

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

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

Solution (Python):

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

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

if x < 0:
    x = -x

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

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

if x < 0:
    x *= -1
    
m = x // d

if k <= m:
    print(x - k*d)
    
else:
    x %= d
    y = abs(x-d)
    
    print([x,y][(k-m)%2])

Solution (Python):
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,k,d = mi()
x = abs(x)

count = min(k,x//d)
k -= count
x -= d*count

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

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


X = abs(X)
a = K*D
if X >= a:
    result = X-a
else:
    b = X//D
    c = (X-b*D)
    d = abs(X-(b+1)*D)
    if c < d:
        if b%2==K%2:
            result = c
        else:
            result = d
    else:
        if (b+1)%2==K%2:
            result=d
        else:
            result=c

print(result)

Solution (Python):

import numpy as np
X,K,D = map(int, input().split())
ans = 0
if X >= 0 :
    if int(X/D)<=K:
        if int(X/D)%2==K%2:
            ans = X%D
        else:
            ans = D-X%D
    else:
        ans = abs(X-K*D)       
if X < 0 :
    if abs(int(X/D))<=K:
        if abs(int(X/D))%2==K%2:
            ans = abs(X)%D
        else:
            ans = D-abs(X)%D
    else:
        ans = abs(X+K*D)          
print(ans)

Solution (Python):
X, K, D = map(int,input().split())
ans = abs(X)
rem = K
k = min(ans//D,K)
ans -= k*D
rem -= k
if rem > 0:
    if rem % 2 == 1:
        ans = ans - D
        
print(abs(ans))

Solution (Python):
x, k, d = [int(s) for s in input().split()]
x = abs(x)
q, m = divmod(x, d)
if q >= k:
    x -= k * d
else:
    x = m
    if (k - q) & 1:
        x = d - x
print(x)

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

cur = abs(x)  
rem = k  

cnt = min(cur // d, k)  
cur -= d * cnt
rem -= cnt


if rem > 0:
    if rem % 2 == 1:
        cur = cur - d

ans = abs(cur)  
print(ans)

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

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

XX = abs(X)

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

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

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

Solution (Python):
a=input().split(" ")
b=[x,k,d]= [int(a[0]),int(a[1]),int(a[2])]  
ans = 0
if x - k*d > 0:
  ans = x - k*d
elif x + k*d < 0:
  ans = - k*d -x
else:
  if k%2==0:
    ans= x%(2*d)
    if d<x%(2*d):
      ans= 2*d-x%(2*d)
  elif k%2==1:
    ans= (x+d)%(2*d)
    if d<(x+d)%(2*d):
      ans= 2*d-(x+d)%(2*d)
print(ans)

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

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

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

x = -x if x <= 0 else x

if x - d * k >= 0:
    print(x - d * k)
else:
    a = x // d
    b = a + 1
    rest_cnt = k - a
    if rest_cnt % 2 == 0:
        print(abs(x - d * a))
    else:
        print(abs(x - d * b))

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

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

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

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

def sign(x):
    return 1 if x >= 0 else -1


ans = abs(X) - K * D

if abs(X) - K * D < 0:
    divs = abs(X) // D
    mods = abs(X) % D
    nums = K - divs

    ans = abs(mods - (nums%2) * D)

print(ans)

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

print(ans)

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

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

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

if X < 0:
    X = -X

if X > K * D:
    print(X - K * D)
else:
    count = X // D
    coodinate = X - count * D
    if (K - count) % 2 == 0:
        print(coodinate)
    else:
        print(D - coodinate)

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

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

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




if __name__ == '__main__':
    main()

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

if k*d<=x:
  print(x-k*d)
else:
  e = math.floor(x/d)
  k -= e
  x = x-e*d
  if k%2 == 0:
    print(x)
  else:
    print(abs(x-d))

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

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

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

if x+d >=k*d:
    print(abs(x-k*d))
    exit()

if k%2==1:
    x=x-d

kaisu=int(x/(2*d))
ans=abs(x-kaisu*d*2)
if ans >=d:
    ans=2*d-ans

print(ans)

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


if __name__ == '__main__':
    main()

Solution (Python):
X,K,D = list(map(int,input().split()))
max_number = X + K*D
min_number = X - K*D
if min_number >= 0:
    ans = min_number
elif max_number <= 0:
    ans = -max_number
else:
    q = max_number // (2*D)
    ans1 = max_number - 2*q*D
    ans2 = max_number - 2*(q+1)*D
    if ans1 > -ans2:
        ans = -ans2
    else:
        ans = ans1
print(ans)

Solution (Python):

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

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

x = abs(x)
div = min(x//d, k)
if (k - div) % 2 == 0:
    ans = abs(x - d * div)
else:
    ans = abs(x - d * div - d)
print(ans)

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

    
    X = X if X >= 0 else -X

    if X - (K * D) >= 0:
        print(X - K * D)
        return

    
    K -= X // D
    X %= D

    if K % 2 == 0:
        print(X)
    else:
        print(min(abs(X+D), abs(X-D)))

main()

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

if x >= k*d:
    ans = x - k*d
elif k&1:
    ans = min((x-d)%(2*d), 2*d - (x-d)%(2*d) )
else:
    ans = min(x%(2*d), 2*d - x%(2*d))

print(ans)

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

x = abs(x)
if x ==0:
    ans = d * (k % 2)
elif x - d * k  >= 0:
    ans = x - d * k
else:
    if abs(x - x // d * d) <= abs(x - (x // d + 1) * d):
        div = x // d
    else:
        div = x // d + 1
    if (k - div) % 2 == 0:
        ans = abs(x - d * div)
    else:
        ans = min(abs(x - d * div + d),abs(x - d * div - d))
print(ans)

Solution (Python):
x,k,d = map(int,input().split(' '))
a = abs(x) % d 
b = -(a - d)
if abs(x) // d > k:
    ans = abs(x) - k * d
else:
    if a == b:
        ans = a
    else:
        if a < b:
            n1 = a
            n2 = b
        if b < a:
            n1 = b
            n2 = a
        if k % 2 == (abs(x) // d) % 2:
            ans = a
        else:
            ans = b
print(ans)

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

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

X = abs(X)
count = X // D

if count >= K:
    answer = X - K*D
else:
    if (K - count) % 2 == 0:
        answer = X - count*D
    else:
        answer = abs(X - (count+1)*D)

print(answer)

Solution (Python):
X, K, D = [int(x) for x in input().split()]

X = abs(X)

straight = min(K, X // D)

K -= straight;
X -= straight * D;

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

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

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
if x > k*d:
    print(x-k*d)
else:
    p = x//d
    c = k-p
    if p == 0 or p==1:
          for i in range(1,k+1):
            p2 = x-i*d
            c = k-i
            if p2 <= 0:
                if c%2 != 0:
                    print(abs(d+p2))
                    break
                elif c%2 == 0:
                    print(abs(p2))
                    break
    elif (c%2 == 0):
        print(abs(x-p*d))
    else:
        print(abs(d-abs(x-p*d)))

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

m = x // d
p = k - m if m <= k else 0

if p == 0:
  print(x - (k*d))
else:
  x -= m * d
  if p & 1:
    print(abs(x - d))
  else:
    print(x)

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

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

count = abs(x)//d

if x<0:
    before_border = x+d*count
    after_border = x+d*(count+1)
else:
    before_border = x-d*count
    after_border = x-d*(count+1)
    
if count >= k:
    if x<0:
        print(abs(x+d*k))
    else:
        print(abs(x-d*k))
else:
    if (count-k)%2 == 0:
        print(abs(before_border))
    else:
        print(abs(after_border))

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

Solution (Python):
X,K,D=input().split(' ')
x,k,d=abs(int(X)),int(K),int(D)

x_par=x%d
x_num=int((x-x_par)/d)

s=0
if x_num<=k:
  x_div=x-x_num*d
  k-=x_num
  if k%2==0:
    s=abs(x_div)
  else:
    s=x_div-d
else:
  s=x-d*k
print(abs(s))

Solution (Python):
import math
X, K, D = map(int, input().split())
ans = 0
if K * D < abs(X):
    ans = abs(X) - K * D
elif (K - math.floor(abs(X) / D)) % 2 == 0:
    ans = abs(X) - (math.floor(abs(X) / D)) * D
else:
    ans = abs(X) - (math.floor(abs(X) / D)) * D - D

print(abs(int(ans)))

Solution (Python):
from sys import stdin
input = stdin.readline
X, K, D = map(int, input().split())
A=int((K-X/D)/2)
if 0<=A and A<=K:print(min(abs(-K*D+2*A*D+X),abs(-K*D+2*(A+1)*D+X)))
else:print(min(abs(X+K*D),abs(X-K*D)))

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

if x == 0:
    if k % 2 == 1:
        x = d

elif x > 0:
    if x >= k*d:
        x -= k*d
    else:
        n = x//d
        x -= n*d
        k -= n

        if k%2 ==1:
            x -= d
else:
    if x <= -(k*d):
        x += k*d

    else:
        n = abs(x)//d
        x += n*d
        k -= n
        if k%2 ==1:
            x += d

print(abs(x))

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

if x - k*d > 0:
    print(x-k*d)
else:
    v = x // d
    k -= v
    x = x - d*v
    if k % 2 == 0:
        print(x)
    else:
        print(min(abs(x-d), abs(x+d)))

Solution (Python):
X, K, D = [int(v) for v in input().split()]

X = abs(X)
m = X // D
p = K - m if m <= K else 0

if p == 0:
    print(X - (K*D))
else:
    X -= m * D
    if p & 1:
        print(abs(X-D))
    else:
        print(X)

Solution (Python):
import math
X,K,D=map(int,input().split())
count=0





Y = 0

if X / D > K and X / D > 0:
    Y = abs(X - D * K)
elif abs(X / D) > K and X / D < 0:
    Y = abs(X + D * K)
else:
    
    L = abs(X) % D
    M = math.floor(abs(X) / D)
    if (K - M) % 2 == 0:
        Y = L
    else:
        Y=abs(L-D)

print(Y)

Solution (Python):
def execute(X, K, D):
    for i in range(K):
        if X > 0:
            X -= D
        else:
            X += D

    return abs(X)

def execute2(X, K, D):
    T = int(abs(X) / D)
    if X > 0:
        if K < T:
            return X - K * D
        else:
            Y = X - T * D
            if (K - T) % 2 == 0:
                return Y
            else:
                return D - Y
    else:
        if K < T:
            return (X + K * D) * -1
        else:
            Y = X + T * D
            if (K - T) % 2 == 0:
                return -1 * Y
            else:
                return D + Y


if __name__ == '__main__':
    X, K, D = map(int, input().split())
    print(execute2(X, K, D))

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

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

Solution (Python):
import numpy as np

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

x = np.abs(x)

if x - k * d > 0:
	print(x - k * d)
elif x == d:
	if k % 2 == 0: print(x)
	else:print(0)
else:
	times = k - x // d
	num1 = x % d
	num2 = num1 - d
	if times % 2 == 0:
		print(np.abs(num1))
	else:
		print(np.abs(num2))

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

Solution (Python):
def main():
    in_list = list(map(int, input().split()))
    X = abs(in_list[0])
    K = in_list[1]
    D = in_list[2]

    max_move = K*D
    if max_move < X:
        print(X-max_move)
    else:
        iter1 = int(X/D)+1
        iter2 = K - iter1
        ans = X - iter1*D
        if iter2%2 == 0:
            print(abs(ans))
        else:
            print(abs(ans+D))

if __name__ == "__main__":
    main()

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

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

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

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



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

print(abs(x))

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