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())

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):
x,k,d = map(int,input().split())
check = []
if abs(x) // d > k :
    print(abs(abs(x)- k*d))
else:
    if (k - x//d) % 2 == 0:
        print(abs(x - d*(x//d)))
    else:
        print(min(abs(x - d*(x//d - 1)), abs(x - d*(x//d + 1))))

Solution (Python):
position,moves,distance=[int(x) for x in input().split()]
position=abs(position)
if position>=distance*moves:
	print(abs(position-distance*moves))
else:
	variable=moves-position//distance
	if variable%2==0:
		print(abs((position-distance*(position//distance))))
	else:
		print(abs(position-distance*((position//distance) + 1)))

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

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

print(ans)

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

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

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

    div = abs(X) // D
    tmp = X + D*div

    if div >= K:
        print(abs(X + D*K))
    elif div < K:
        K -= div
        print(abs(tmp) if K % 2 == 0 else abs(tmp+D))    

else:

    div = abs(X) // D
    tmp = X - D*div

    if div >= K:
        print(abs(X - D*K))
    elif div < K:
        K -= div
        print(abs(tmp) if K % 2 == 0 else abs(tmp-D))

Solution (Python):


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

move=min(abs(X)//D,K)
ans=abs(X)-move*D

if move==K:
    print(ans)
else:
    if (K-move)%2==0:
        print(ans)
    else:
        print(abs(ans-D))

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

t = min(abs(x)//d, k)
u = abs(x)-d*t
print(abs(u-d*((k-t)%2)))

Solution (Python):
import numpy as np

inpt = input()
inpt_list = inpt.split(" ")
X, K, D = [int(x) for x in inpt_list]

X_abs = np.abs(X)
num = X_abs // D
rem = K - num
if rem < 0:
    out = X_abs - D*K
else:
    if rem%2==0:
        out = X_abs - num*D
    else:
        out = np.abs(X_abs - num*D - D)
print(out)

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

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

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

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

Solution (Python):
import math

def solve(x, k, d):
    x = math.fabs(x)
    if x - k * d >= 0:
        return int(x - k * d)
    else:
        cur = x - x // d * d
        k = k - x // d
        if k % 2 == 0:
            return int(cur)
        else:
            return int(abs(cur - d))

if __name__ == "__main__":
    x, k, d = map(int, input().split(' '))
    print(solve(x, k, d))

Solution (Python):
x, k, d = map(int, input().split())
ans = 0
if abs(x) <= k * d:
  if (abs(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())
if X < 0: X *= -1
n = X // D
if K <= n: print(X - K * D)
else:
  X -= n * D
  K -= n
  if K % 2 == 0: print(X)
  else: print(-X + D)

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

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

Solution (Python):
def solve(string):
    x, k, d = map(int, string.split())
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    x, k, d = abs(x), k // 2, d * 2
    return str(min(x % d, abs((x % d) - d))) if x < k * d else str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    x, k, d = map(int, string.split())
    x = abs(x)
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    k, d = k // 2, d * 2
    return str(min(x % d, abs((x % d) - d))) if x < k * d else str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    x, k, d = map(int, string.split())
    x = abs(x)
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    k, d = k // 2, d * 2
    if x < k * d:
        return str(min(x % d, abs((x % d) - d)))
    else:
        return str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    x, k, d = map(int, string.split())
    x = abs(x)
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    if x < k * d:
        return str(min(x % (2 * d), abs((x % (2 * d)) - 2 * d)))
    else:
        return str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

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

ans_min = X % D

if K <= X_div_D:
  print(X - K * D)
else:
  if (K % 2) ^ (X_div_D % 2) == 0:
    print(ans_min)
  else:
    print(abs(ans_min - D))

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())
x =abs(x)
s = x//d
if x-k*d>0:
  print(x-k*d)
else:
    if (k-s)%2==0:
        print(x-s*d)
    else:
        print(-x+(s+1)*d)

Solution (Python):

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

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

Solution (Python):
import math
X, K, D = map(int,input().split())
X = math.fabs(X)
ans = 0
E = X//D
if E > K:
    ans = X - K * D
elif K%2 == E%2:
    ans = X-E*D
else:
    ans = -X+(E+1)*D
print(int(ans))

Solution (Python):

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

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

Solution (Python):

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

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

Solution (Python):















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

if x < 0:
    x = -x

l = int(x/d)

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

Solution (Python):
def solve():
    X,K,D = [int(i) for i in input().split()]
    abs_X = abs(X)
    if D*K <= abs_X:
        print(abs_X - D*K)
    else:
        steps2pivot = abs_X // D
        abs_X -= steps2pivot * D
        rest = K - steps2pivot
        if rest % 2 == 0:
            print(abs_X)
        else:
            print(abs(abs_X-D))

if __name__ == "__main__":
    solve()

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

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


X = abs(X)
ans = 0


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

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

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

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

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

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())
x = abs(x)
count = 0

if x - k*d > 0:
    print(x - k*d)

elif x - k*d == 0:
    print(0)

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

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

if kd<=x:
    print(x-kd)
else:
    count = x // d
    k_dash = k - count
    x -= count * d
    if k_dash % 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:
  y=X//D
  z=K-y
  j=y*D
  if z%2==0:
    print(X-j)
  else:
    print(abs(X-j-D))

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

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


if x//D <= K:
  if (K-x//D)%2 == 0:
    print(min(x, x%D))
  else:
    print(abs(D-x%D))
else:
  print(x-K*D)

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

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

print(g)

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

X = abs(X)

cuts = X//D

if cuts>=K:
    print(X-(K*D))
else:
    remainingcuts = K-cuts
    r = X-cuts*D

    if remainingcuts%2 == 1:
        print(abs(r-D))
    else:
        print(r)

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

if x + k * d <= 0:
    print(-(x + k * d))
elif x - k * d >= 0:
    print(x - k * d)
elif x >= 0:
    print(min([abs(x - (k - 2 * i) * d) for i in range((k * d - x) // (2 * d) - 100, (k * d - x) // (2 * d) + 100)]))
else:
    print(min([abs(x + (k - 2 * i) * d) for i in range((k * d + x) // (2 * d) - 100, (k * d + x) // (2 * d) + 100)]))

Solution (Python):



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

if __name__=='__main__':
  main()

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

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

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

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

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

X = abs(X)

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

Solution (Python):
a, b, c = map(int, input().split())
d = abs(a)
count = min(d//c, b)
d -= c*count
b -= count
if b % 2 == 1:
    d -= c
print(abs(d))

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

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

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

if abs(X) > K*D:
    print(abs(X)-K*D)
    sys.exit()
    
for t in range(K):
    
    if abs(temp - D) < abs(temp + D):
        temp = temp-D
    else:
        temp = temp+D

    
    if abs(temp) < D:
        if (K-t-1) % 2 == 0:
            print(abs(temp))
            sys.exit()
        else:
            if abs(temp - D) < abs(temp + D):
                temp = temp-D
                print(abs(temp))
                sys.exit()
            else:
                temp = temp+D
                print(abs(temp))
                sys.exit()

print(abs(temp))

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

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

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

Solution (Python):
x, k, d = [int(i) for i in input().split()]
if x < 0:
  x = -x
l = min(k, x // d)
k -= l
x -= l * d
if k % 2 == 0:
  print(x)
else:
  print(d - x)

Solution (Python):
import math

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

X = abs(X)

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

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

straight = min(k, math.floor(x / d))

k -= straight
x -= straight * d

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

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

result=abs(x)-k*d
swithn=0

if result<0:
    if k%2==1:
        x-=d
    result=min(x%(2*d),-x%(2*d))
print(result)

Solution (Python):

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

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

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

kyori = abs(X)
kaisu = math.ceil(kyori / D)

if(K < kaisu):
    ans = kyori - (K * D)
    
    
    
    
else:
    if((K - kaisu) % 2 == 0):
        ans = kyori - (kaisu * D)
    else:
        ans = kyori - (kaisu * D) + D

    
    
    
    

print(abs(ans))

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

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

X = abs(X)

if K * D < X:
    print(X - K * D)

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

Solution (Python):
x,k,d = list(map(int, input().split()))
x = abs(x)
a,b = divmod(x, d)
a = min(a, k)
x -= a * d



x -= d * ((k-a) % 2)
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:
  if K%2==0:
    s=X-2*D*int(X/(2*D))
    t=-(s-2*D)
    print(min(abs(s),t))
    
  else:
    s=X-2*D*int((X-D)/(2*D))-D
    t=-(s-2*D)
    
    print(min(abs(s),t))

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

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

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

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):
import sys

input = sys.stdin.readline

def main():
    ans = 0
    x, k, d = map(int, input().split())
    ans = abs(x) - (d * min(k, abs(x)//d))
    t = k - abs(x)//d
    if t%2 != 0 and t > 0:
        ans = min(abs(ans-d), abs(ans+d))
    print(ans)

main()

Solution (Python):
"""
AtCoder :: Beginner Contest 175 :: C - Walking Takahashi 
https://atcoder.jp/contests/abc175/tasks/abc175_c
"""
import sys


def solve(X, K, D):
    """Solve puzzle."""
    
    if D == 0:
        return X

    X = abs(X)
    soln = abs(X - (K * D))
    steps_to_zero = abs(X) // D
    
    
    
    if steps_to_zero <= K and ((K - steps_to_zero) % 2 == 0):
        soln = min(soln, abs(X - (steps_to_zero * D)))

    
    steps_past_zero = steps_to_zero + 1
    
    
    if steps_past_zero <= K and ((K - steps_past_zero) % 2 == 0):
        soln = min(soln, abs(X - (steps_past_zero * D)))

    
    steps_back_zero = steps_past_zero + 1
    
    
    if steps_back_zero <= K and ((K - steps_back_zero) % 2 == 0):
        soln = min(soln, abs(X - (steps_back_zero * D)))

    return soln


def main():
    """Main program."""
    X, K, D = (int(i) for i in sys.stdin.readline().split())
    print(solve(X, K, D))


if __name__ == '__main__':
    main()

Solution (Python):

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

x = abs(x)
syou = x // d
amari = x % d

if k <= syou:
    ans = x - (d * k)

else:
    if (k - syou) % 2 == 0: 
        ans = amari

    else:
        ans = abs(amari - d)

print(ans)

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

if a < 0:
    x = -a
else:
    x = a

y = x % d
l = x // d
m = k - l

if m < 0:
    ans = x - (k * d)
elif m % 2 ==0:
    ans = y
else :
    ans = y - d

print(abs(ans))

Solution (Python):


X, K, D = map(int, input().split())
X = abs(X)
d = X // D
m = X % D

if K <= d:
    ans = X - (D * K)
elif (K - d) % 2 == 0:
    ans = X - (D * d)
else:
    ans = X - (D * (d+1))
print(abs(ans))

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

zan = X%D


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

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

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

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




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

else:
    
    r_count = abs(x)//d
 
    
    rest = k - r_count

    
    
    if(rest %2 == 0):
        print(abs(abs(x) - d*r_count))
    
    else:
        print(abs(abs(abs(x) - d*r_count)-d))

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

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

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

Solution (Python):


startX, countK, distanceD = map(int, input().split())
n = int(abs(startX) / distanceD)
ans = abs(startX) % distanceD
if n > countK:
    ans += distanceD * (n - countK)
else:
    if (countK - n) % 2 == 1:
        ans= abs(ans - distanceD)
print(ans)

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

ax = abs(x)
n = ax // d
if n >= k:
    ans = ax - k*d
else:
    a = ax - n*d
    if (k - n) % 2:
        ans = abs(a - d)
    else:
        ans = abs(a)

print(ans)

Solution (Python):
from math import floor, ceil

def solve(X, D, lb, ub):
    
    ts = [
        lb,
        ub,
        max(min(floor(-X / D), ub), lb),
        max(min(ceil(-X / D), ub), lb),
    ]
    return min([abs(X + t * D) for t in ts])

X, K, D = map(int, input().split())
if K % 2 == 0:
    ans = solve(X, D * 2, -(K // 2), +(K // 2))
else:
    ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
    ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
    ans = min(ans1, ans2)
print(ans)

Solution (Python):
from math import floor

def solve(X, D, lb, ub):
    
    ts = [
        lb,
        ub,
        max(min(floor(-X / D), ub), lb),
        max(min(floor(-X / D) + 1, ub), lb),
    ]
    return min([abs(X + t * D) for t in ts])

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

if K % 2 == 0:
    ans = solve(X, D * 2, -(K // 2), +(K // 2))
else:
    ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
    ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
    ans = min(ans1, ans2)
print(ans)

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

X = abs(X)
reach_or_before = X // D


if K <= reach_or_before:
    print(X - D * K)
else:
    
    remain = K - reach_or_before
    before_dist = X % D 
    after_dist = D - before_dist
    if remain % 2 == 0:
        print(before_dist)
    else:
        print(after_dist)

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

X = abs(X)
reach_or_before = X // D


if K <= reach_or_before:
    print(X - D * K)
else:
    
    remain = K - reach_or_before
    before_dist = X - D * reach_or_before 
    after_dist = D * (reach_or_before + 1) - X
    if remain % 2 == 0:
        print(before_dist)
    else:
        print(after_dist)

Solution (Python):
from math import floor

def solve(X, D, lb, ub):
    
    ts = [
        lb,
        ub,
        max(min(floor(-X / D), ub), lb),
        max(min(floor(-X / D) + 1, ub), lb),
    ]   
    return min([abs(X + t * D) for t in ts])

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

y1 = X - K * D
y2 = X + K * D
if (y1 > 0 and y2 > 0) or (y1 < 0 and y2 < 0): 
    print(min(abs(y1), abs(y2)))
else:
    if K % 2 == 0:
        ans = solve(X, D * 2, -(K // 2), +(K // 2))
    else:
        ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
        ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
        ans = min(ans1, ans2)
    print(ans)

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

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

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

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

Solution (Python):



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

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

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

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

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

if x%d==0:
    minNear  = x%d + d
    minFar = x%d
    minTransitionToNear = (x-minNear)/d
    
    
    
    
else:
    minNear  = x%d
    minFar = x%d - d
    minTransitionToNear = (x-minNear)/d
    
    
    
    
if k<minTransitionToNear:
    print(x-k*d)
else:
    if (k-minTransitionToNear)%2==0:
        print(abs(minNear))
    else:
        print(abs(minFar))

Solution (Python):


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

    ABS_X = abs(X)

    if K * D <= ABS_X:
        ans = ABS_X - (K * D)

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

    print(ans)

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

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



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

print(ans)

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

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

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

Solution (Python):
x,k,d=map(int,input().split());xx=abs(x)
tmp=xx;tmp=(xx//d);cc=min(tmp,k)
xx-=d*cc;k-=cc;k&=1;xx-=d*k
print(abs(xx))

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

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

a = abs(X)//D
b = abs(X)%D
if (K - a)%2 == 1 and a < K:
    ans = D - b
elif (K - a)%2 == 0 and a < K:
    ans = b

print(ans)

Solution (Python):
X,K,D=(int(x) for x in input().split())
x=abs(X)
a=x//D
 
b=x-a*D
B=abs(x-(a+1)*D)
 
if b>B:
  a=a+1
 
if a>=K:
  print(abs(x-K*D))
 
else:
  c=K-a
  if c%2 == 0:
    print(abs(x-a*D))
  else:
    d=abs(x-(a-1)*D)
    e=abs(x-(a+1)*D)
    print(min(d,e))

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

Solution (Python):
X,K,D=(int(x) for x in input().split())
x=abs(X)
a=x//D

b=x-a*D
B=abs(x-(a+1)*D)

if b>B:
  a=a+1

if a>=K:
  print(abs(x-K*D))

else:
  c=K-a
  if c%2 == 0:
    print(abs(x-a*D))
  else:
    d=abs(x-(a-1)*D)
    e=abs(x-(a+1)*D)
    print(min(d,e))

Solution (Python):


X, K, D = map(int, input().split())
DIV = (X + K * D) // (2 * D)

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

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

x = abs(x)

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

bai = x//d

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

Solution (Python):
xkd = input().split()
X = int(xkd[0])
K = int(xkd[1])
D = int(xkd[2])

X = abs(X)
ans = 0
times = int(X/D)
oneMore = False
if X%D > abs(D-X%D):
    times+=1

if times > K:
    ans = X - (D*K)
else:
    if (times%2) == (K%2):
        ans = min(X%D, (D-X%D))
    else:
        ans = max(X%D, (D-X%D))

print(ans)

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

    else:
        print(x-k*d)
    return

if __name__ == '__main__':
    main()

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

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

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

import math

X=abs(X)
U=min(math.floor(X/D),K)
K=K-U
A=X-U*D

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

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

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

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

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

if x < 0 :
  x = -x

l = x // D

if K > l:
  K = K - l
  if (K % 2) == 0:
    print(abs(x-D*l))
  else:
    print(abs(x-D*(l+1)))
else:
  print(abs(x - D*K))

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

if (X // D) >= K:
    ans = X - (K * D)
elif (X // D) % 2 == 0:
    if K % 2 == 0:
        ans = X % D
    else:
        ans = abs((X % D) - D)
else:
    if K % 2 ==1:
        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 :
    k -= x//d
    if k%2 == 0:
        print(x % d)
    else :
        print(abs(x % d - d))

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

if X > 0:
    div, mod = divmod(X, D)
    if div >= K:
        answer = X - K*D
    else:
        if (K - div)%2 == 0:
            answer = X - div*D
        else:
            answer = abs(X - (div+1)*D)
elif X < 0:
    div, mod = divmod(-X, D)
    if div >= K:
        answer = abs(X + K*D)
    else:
        if (K - div)%2 == 0:
            answer = abs(X + div*D)
        else:
            answer = X + (div+1)*D
else:
    if K%2 == 0:
        answer = 0
    else:
        answer = D

print(answer)