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):
a = list(map(int, input().split()))
x = a[0]
k = a[1]
d = a[2]
x = abs(x)

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

Solution (Python):


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


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


print(ans)

Solution (Python):



import sys
import math

def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))


def main():
    X, K, D = i2nn()
    absX = abs(X)
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    try:
        a, c = divmod(absX, D)
    except:
        print(X)
        return
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return


main()

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

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

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

xdivd=x//d

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

Solution (Python):
T = input()
T = T.split(" ")
x = int(T[0])
k = int(T[1])
d = int(T[2])
val = (x+k*d)//(2*d)
if val>=0 and val<=k:
    print(min(abs(-2*d*val+x+k*d), abs(-2*d*(val+1)+x+k*d)))
elif val<0:
    print(abs(x+k*d))
else:
    print(abs(x-k*d))

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

if X==0:
    if K%2==1:
        print(D)
    else:
        print(0)
else:
    a=X%D
    aa=D-a
    aa=abs(aa)
    b=X//D
    

    if X>0:
        n=K-b
        
        if n>=0:
            if n%2==1:
                print(aa)
            else:
                print(a)
        else:
            print(X-D*K)
    if X<0:
        n=K+b        
        if n>=0:
            if n%2==1:
                print(aa)
            else:
                print(a)
        else:
            print(abs(X+D*K))

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

a = int(X/D)

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

Solution (Python):
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
 
 
def main():
    X,K,D = map(int,input().split())
    N = abs(X)//D
    if N>=K:
        ans = abs(X)-K*D
    else:
        if (K-N)%2==0:
            ans = abs(X)-N*D
        else:
            ans = (N+1)*D-abs(X)
    print(ans)
        
 
if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

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

xx=x
kk=k

if (x >=0):
  k_int=int(x/d)
  if (k_int >= k):
    xmin=abs(x-k*d)
  else:
    xx1=x-k_int*d
    xx2=xx1-d
    if ((k-k_int) % 2 == 0): 
      xmin=abs(xx1)
    else: xmin=abs(xx2)

if (x <0):
  k_int=int(abs(x)/d)
  if (k_int >= k):
    xmin=abs(x+k*d)
  else:
    xx1=x+k_int*d
    xx2=xx1+d
    if ((k-k_int) % 2 == 0): 
      xmin=abs(xx1)
    else: xmin=abs(xx2)
      
      
print (xmin)

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

Solution (Python):
X, K, D = (int(x) for x in input().split())
X = abs(X)
count = X // D
if K < count:
    count = K
X = abs(X) - D*count
rest = K - count
if rest%2 == 1:
        X -= D
print(abs(X))

Solution (Python):
import sys
import math
X, K, D = map(int, input().split())
if X == 0 and K % 2 == 0:
    print(X)
    sys.exit()
elif X == 0 and K % 2 == 1:
    print(D)
    sys.exit()

if abs(X // D) >= K:
    ans = X - (D * K) * (X // abs(X))
    ans = abs(ans)
else:
    K -= abs(X//D)
    ans = X - (D * abs(X//D)) * (X // abs(X))
    if K % 2 == 0 and K == 0:
        ans = abs(ans)
    elif K % 2 == 0 and K >= 2:
        ans = min([abs(ans), abs(ans-D*2), abs(ans+D*2)])
    else:
        ans = min(abs(ans+D), abs(ans-D))
print(ans)

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

x = abs(X)

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

Solution (Python):
x, k, d = map(int, input().split())
if(x < 0):
    x = -x

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

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

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

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

    
if __name__ == '__main__':
    main()

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

if x > 0:
    kaisu = x//d
    if x//d <= k:
        k -= kaisu
        x -= kaisu*d
    else:
        x -= k*d
        k = 0
else:
    kaisu = -(x//d) 
    if kaisu <= k:
        k -= kaisu
        x += kaisu*d
    else:
        x += k*d
        k = 0

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

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

r = x%d
l = r-d

lc = abs(x-l)//d
rc = abs(x-r)//d

ans = min(abs(x-d*k), abs(x+d*k))
if(k>=lc):
    if((k-lc)%2): ans = min(ans, min(abs(l+d), abs(l-d)))
    else: ans = min(ans, abs(l))
if(k>=rc):
    if((k-rc)%2): ans = min(ans, min(abs(r+d), abs(r-d)))
    else: ans = min(ans, abs(r))
print(ans)

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

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

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


if x<0:
	d*=-1

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

d*=2
k/=2


v1=x-k*d
n=x/d
v2=x-n*d if (n<=k) else v1
v3=x-(n+1)*d if(n+1<=k) else v1

print min(abs(v1),abs(v2),abs(v3))

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

syou = abs(X) // D


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

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

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

X = abs(X)
k = X//D


if K - k <=0:
    print(X - D*K)
elif (K - k) % 2 != 0:
    print(D - X + D*k)
else:
    print(X - D*k)

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

else:
	if X<0:
		temp=X+d*D
		if (K-d)%2==0:
			ans=temp
		else:
			ans=temp+D
	else:
		temp=X-d*D
		if (K-d)%2==0:
			ans=temp
		else:
			ans=temp-D

print(abs(ans))

Solution (Python):
import sys
import math
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
def end_of_loop():raise StopIteration
    
    
def main():
    X, K, D = MI()
    
    X = abs(X)
    if K*D <= X:
        print(X-K*D)
        sys.exit()
    syou = X // D
    amari = X % D
    if (K - syou) % 2 == 0:
        print(amari)
    else:
        print(abs(amari-D))

if __name__ == "__main__":
    main()

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

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

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

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

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

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

Solution (Python):
x,k,d=map(int,input().split())
if x==0:
    if d%2==0:
        print(0)
    else:
        print(d)
else:
    cou=0
    ok=True
    if x>0:
        xx=x
        while x>0:
            if xx-k*d>0:
                ans=x-k*d
                ok=False
                break
            cou+=1
            x-=d
            
        xx=x+d
        if ok:
            if cou%2==0 and k%2==0:
                ans=x
            elif cou%2==0 and k%2==1:
                ans=xx
            elif cou%2==1 and k%2==0:
                ans=xx
            else:
                ans=x
    
    if x<0:
        xx=x
        while x<0:
            if xx+k*d<0:
                ans=xx+k*d
                ok=False
                break
            cou+=1
            x+=d
            
        xx=x-d
        if ok:
            if cou%2==0 and k%2==0:
                ans=x
            elif cou%2==0 and k%2==1:
                ans=xx
            elif cou%2==1 and k%2==0:
                ans=xx
            else:
                ans=x
    print(abs(ans))

Solution (Python):
temparr = input()
temparr = temparr.split()
a = abs(int(temparr[0]))
b = int(temparr[1])
c = int(temparr[2])
rem = a // c 

if rem > b :
    print(a - b * c)

else:
    a -=  rem * c 
    b -= rem 
    v1 = a - c 
    v2 = v1 + c 
    if b & 1 :
        print(abs(v1) )
    else:
        print(abs(v2))

Solution (Python):


import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

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

    r = 1
    if X < 0:
        r = -1
        X = -X
    a, b = divmod(X, D)

    diff = K - a
    if diff <= 0:
        print(abs(r*(X - K*D)))
        return

    if diff & 1:
        b -= D

    print(abs(r*b))



if __name__ == "__main__":
    resolve()

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

    now = x
    i = 0
    if (now == 0):
        if k%2 == 0:
            print(0)
        else:
            print(d)
    elif (now > 0):
        if (now - d * k >=0):
            print(now - d * k)
            return
        i = int(now/d)
        now = now%d
        if (k-i)%2 == 0:
            print(now)
        else:
            print(d-now)
    elif (now < 0):
        if (now + d * k <=0):
            print(-(now + d * k))
            return
        i = int((-now)/d)
        now = -((-now)%d)
        if (k-i)%2 == 0:
            print(-now)
        else:
            print(now+d)
    
if __name__=='__main__':
    main2()

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

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

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

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

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

if K <= abs(X) // D:
    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())
x=abs(x)
if d*k<x:
    exit(print(x-d*k))
l=0
r=k
while r-l>1:
    m=l+r>>1
    if d*m>=x:
        r=m
    else:
        l=m
if(k-r)%2:
    print(min(abs(d*~-r-x),abs(d*-~r-x)))
else:
    a=d*r-x
    if r<k:
        a=min(a,abs(d*(r-2)-x),abs(d*(r+2)-x))
    print(a)

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

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

K2= K - (abs(X) // D)


X= abs(X) - abs(((K-K2)*D))

if K2 % 2 == 1:
    X= abs(X) - D


print(abs(X))

Solution (Python):
def point(x):
    return (abs(x), x)

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

    if X < 0:
        k = abs(X) // D
        k = min(k, K)
        ans = X + D*k
        K -= k
        
        if K >= 1 and point(ans + D) < point(ans):
            ans += D
            K -= 1
        
        if K % 2 == 1:
            ans = ans - D if ans > 0 else ans + D

    else:
        k = X // D
        k = min(k, K)
        ans = X - D*k
        K -= k

        
        if K >= 1 and point(ans - D) < point(ans):
            ans -= D
            K -= 1
        
        if K % 2 == 1:
            ans = ans - D if ans > 0 else ans + D

    print(abs(ans))

if __name__ == '__main__':
    main()

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

l=0
r=K

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

while r-l > 1:
    k = (l+r)//2

    if D*(2*k-K)+X > 0:
        r=(l+r)//2
    else:
        l=(l+r)//2
    
    if D*(2*k-K)+X ==0:
        print(0)
        exit()
k=l
l=abs(D*(2*k-K)+X)
k=r
r=abs(D*(2*k-K)+X)

print(min(r,l))

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

Solution (Python):
x, k, d = map(int, input().split())
flag = False
if x < 0:
    x = -x
    flag = True

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

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

X = abs(X)

d, r = divmod(X, D)
if K >= d:
    if (K - abs(d)) % 2 == 0:
        print(min(r, abs(X - D*K)))
    else:
        print(min(abs(r - D), abs(X - D*K)))
else:
     print(abs(X - D*K))

Solution (Python):


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

T_min = abs(X)//D
ans = 0

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


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

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

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

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

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

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

x = abs(x)
if x // d > k:
    print(x - (d*k))
else:
    num = k - x // d
    if num % 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)

a = x // d

if k <= a:
    ans = x - k * d
    print(abs(ans))
    exit()

x -= a * d
x = abs(x)
k -= a


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

Solution (Python):

import sys
import numpy as np


def solve(X: int, K: int, D: int):
    if X<0:
        X *= -1
    a = X % D
    b = X // D
    if K<b:
        ans = X - K * D
    else:
        if ((K-b) % 2)==0:
            ans = a
        else:
            ans = -(a-D)
    print(ans)
    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())

if X < 0:
    X = -1 * X

x = X % D
N = X // D

if N > K:
    print(X - D * K)
else:
    judge = (K - N) % 2
    if judge == 0:
        print(x)
    else:
        print((x-D)*-1)

Solution (Python):
X, K, D = map(int, input().split())
if X <= 0:
    X *= -1

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

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

Solution (Python):
def main():
    x, k, d= map(int, input().split())
    if x == 0:
        if k%2 == 0:
            print(0)
            return
        else:
            print(d)
            return
    else:
        if x < 0:
            x = -1*x
        if k*d <= x:
            print(x-k*d)
            return
        else:
            y = x//d
            p = x-d*y
            nokori = k - y
            if nokori%2 == 0:
                print(p)
            else:
                print(d-p)
        

if __name__ == '__main__':
    main()

Solution (Python):
import math
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    if X >= K * D:
        return X - K * D
    overzero_K = math.ceil(X/D)
    overzero_X1 = X - overzero_K * D
    overzero_X2 = overzero_X1 + D
    left_K = K - overzero_K
    if left_K % 2 == 1: return overzero_X2
    else: return overzero_X1

print(abs(main()))

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

n_to_move = X // D
  
if K >= n_to_move:
  pre_pos = X % D
  n_move = X // D
    
  n_nokori_move = K - n_move
    
  if n_nokori_move % 2 == 0:
    last_pos = pre_pos
      
  else:
    last_pos = min(abs(pre_pos + D), abs(pre_pos - D))
	
else:
  if X >= 0:
    last_pos = X - K * D
    

print(last_pos)

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

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

Solution (Python):
import sys
x, k, d = map(int, input().split())
if x>0 and x-k*d>=0:
  print(x-k*d)
  sys.exit()
if x<0 and x+k*d<=0:
  print(-x-k*d)
  sys.exit()
a=0
b=0
for i in range(1,k+1):
  if abs(x)-i*d<=0:
    a=i
    b=i-1
    break
if (k-a)%2 ==0:
  print(-abs(x)+a*d)
else:
  print(abs(x)-b*d)

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    X, K, D = map(int, input().split())
    M = abs(X)%D
    S = abs(X)//D
    C = K - S 
    if C < 0:
        ans = abs(X) - K*D
    else:
        if M <= abs(M-D):
            if C%2 == 0:
                ans = M
            else:
                ans = abs(M - D)
        else:
            C -= 1
            if C%2 != 0:
                ans = M
            else:
                ans = abs(M - D)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

x,k,d=vfunc(int)(reader()[0])

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

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

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


def li():
    return [int(x) for x in input().split()]


X, K, D = li()

if X < 0:
    X *= -1

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

print(ans)

Solution (Python):
x,k,d = map(int,input().split())
ax = abs(x)
ad = abs(d)
import sys
if x+d*k<0:
    print(-(x+d*k))
elif 0<x-d*k:
    print(x-d*k)
else:
    m1 = ax%ad
    m2 = d-m1
    mm = min(ax%ad,d-ax%ad)
    if mm == m1:
        if (ax//ad)%2==k%2:
            print(m1)
        else:
            print(m2)
        sys.exit()
    if mm == m2:
        if (ax//ad)%2==(k+1)%2:
            print(m2)
        else:
            print(m1)
        sys.exit()

Solution (Python):
X, K, D = map(int, input().split())
if X < 0:
    X = -1 * X

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

Solution (Python):
def solve():
    x, k, d = map(int, input().split())
    l = x - k * d
    r = x + k * d
    if l > 0:
        print(l)
        return
    elif r < 0:
        print(-r)
        return
    a1 = r - r // (2 * d) * (2 * d)
    a2 = a1 - (2 * d)
    ans = min(abs(a1), abs(a2))
    print(ans)


if __name__ == "__main__":
    solve()

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

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

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

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

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

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

count = abs(x) // d

if k > count+1:
    ans = min(abs(abs(x) - count * d),abs(abs(x) - (count + 1) * d))
    if min(abs(abs(x) - count * d),abs(abs(x) - (count + 1) * d)) == abs(abs(x) - (count + 1) * d):
        k += 1
    if (k - count) % 2 == 1:
        ans = abs(ans - d)
    print(ans)
else:
    print(abs(abs(x) - k * d))

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 - k) % 2 == 0:
        print(x%D)
    else:
        print(abs(x%D-D))

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

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

x=abs(x)

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

Solution (Python):
import math

dates = [int(i) for i in input().split(" ")]
X = dates[0]
K = dates[1]
D = dates[2]
numDiv = math.ceil(abs(X) / D)

if numDiv < K:
    if X >= 0:
        X -= numDiv * D
        K -= numDiv
    else:
        X += numDiv * D
        K -= numDiv
else:
    if X >= 0:
        X -= K * D
        K = 0
    else:
        X += K * D
        K = 0

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

Solution (Python):
import sys
x,k,d = map(int, sys.stdin.readline().rstrip("\n").split())
t,d_large = divmod(x, d)
t = abs(t)
if k <= t:
    if x >= 0:
        print(abs(x-d*k))
    else:
        print(abs(x+d*k))
else:
    d_small = abs(d_large - d)
    dd = t-k
    if dd % 2 == 0:
        print(d_large)
    else:
        print(d_small)

Solution (Python):
from math import ceil, floor

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

X = abs(X)

r = floor(X / D)

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

l = r + 1

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

Solution (Python):








    






mod = int(1e9+7)

INF = 1<<50

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

def main():
    x,k,d = readInt()
    count = abs(x) // d
    mn = 0
    if count>k:
        mn = abs(x) - (d * k)
        print(mn)
        return
    else:
        mn = abs(x) % d
    ans = INF
    if (k-count)%2==0:
        ans = mn
    else:
        ans = abs(mn-d)
    print(ans)
    return

if __name__=='__main__':
    main()

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

if x < 0:
    x = -x

a = x // d

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

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

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

if k<n:
  print(x-k*d)
  exit()

if n%2==0:
  if k%2==0:
    ans = x - n*d
  else:
    ans = (n+1)*d - x
else:
  if k%2==1:
    ans = x - n*d
  else:
    ans = (n+1)*d - x
    
print(ans)

Solution (Python):
from sys import stdin,stdout
from collections import defaultdict
import math


x,n,d=map(int,input().split())
ans=x
if x>0:
    if x-n*d<0:
        c=x//d
        n=n-c
        ans=x-c*d
        a=abs(ans-d)
        b=ans
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x-n*d)
elif x==0:
    if n%2==0:
        print(x)
    else:
        print(x+d)
else:
    if x+n*d>0:
        c=abs(x)//d
        n=n-c
        ans=x+c*d
        a=ans+d
        b=abs(ans)
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(abs(x+n*d))

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

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

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



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

Solution (Python):

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

    if X < 0:
        X = -X
    
    

    def f(n: int):
        k = K - 2*n
        return X-k*D

    def is_ok(arg):
        
    
        if f(arg) >= 0:
            return True
        else:
            return False

    def meguru_bisect(ng, ok):
        '''
        初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
        まずis_okを定義すべし
        ng ok は  とり得る最小の値-1 とり得る最大の値+1
        最大最小が逆の場合はよしなにひっくり返す
        '''
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok

    n = meguru_bisect(0, K//2)
    
    ans = min(abs(f(max(0, n-1))),
        abs(f(n)),
        abs(f(min(K//2+1, n+1)))
    )
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):

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


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

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