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

"""
繰り返しやった時に絶対値が最小になればよい。
毎回0に近づく動きをさせよう。
"""

list_in = list(map(int,input().split()))
xin = list_in[0]
kin = list_in[1]
din = list_in[2]

now_place = abs(xin)
rem = kin

cnt = min(now_place//din, kin)
now_place -= din * cnt
rem -= cnt

if rem %2 == 1:
    now_place = now_place - din

ans = abs(now_place)

print(ans)

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

x=abs(x)
ans=0
if x>=d*k:
  ans=x-k*d
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 = list(map(int, input().split()))

x = abs(x)
p = 0

if x/d >= k:
    p = x-(k*d)

else:
    p = x%d
    if (k - int(x/d))%2 == 1:
        p = p-d
print(abs(p))

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

x = abs(x)

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

else:
  print(abs((x - int(x/d)*d)-d))

Solution (Python):
def ii():
    return int(input())
def mi():
    return map(int, input().split())
def li():
    return list(mi())

l = li()
x, k, d = l[0], l[1], l[2]
x = abs(x)
calc = x // d
mi = min(k, calc)
k -= mi
x -= mi * d

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

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










n = 0  
result = X + D*(K-2*n)








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

elif X < D*K:
    p = int(X/D)  
    result=X-D*p
    if (K-p) % 2 == 0:
        print(result)
    elif (K-p)%2 ==1:
        print(abs(result-D))

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

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

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


if x < 0:
    x *= -1

if k * d < x:
    ans = x - k * d
else:
    q, r = divmod(x, d)
    k -= q
    if k & 1:
        ans = d - r
    else:
        ans = r

print(ans)

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

x = abs(x)

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

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

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

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

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

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

Solution (Python):



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

X = abs(X)

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

n = X//D
x = X%D
k = K-n

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

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

X = abs(X)

if X >= D:
    temp = X // D
    temp = min(temp, K)
    ans1 = X - D * temp
    if temp == K:
        print(ans1)
        exit(0)
    if (K-temp) % 2 == 0:
        print(ans1)
        exit(0)
    else:
        print(abs(ans1 - D))
    exit(0)
else: 
    if K % 2 == 0:
        print(X)
        exit(0)
    else:
        print(abs(X - D))
        exit(0)

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

X = abs(X) 

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

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

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

print(x)

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

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

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

if X<0:
    X = X*(-1)

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

else:
    print(abs(D - X % D))

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

X = abs(X)

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

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

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)
"""
Y = X (mod D)
|Y - X|/ D = K (mod 2)
x = p * D + r (p, r == Z, 0 <= r < D)




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

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

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

Solution (Python):
import math
import sys

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

if x<0:
  x*=-1

num=math.floor(x/d)

if num>k:
  print(x-k*d)
  sys.exit()

n=x-num*d
remaining=k-num

if remaining%2==0:
  if n>0:
    print(n)
    
  else:
    print(n*-1)
    
else:
  if n>0:
    n-=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)
      
  else:
    n+=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)

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

x = abs(x)
kk = k - int(x/d)
if kk <= 0:
    print(x - k * d)
    exit()

a = x - int(x/d)*d
if kk % 2 == 0:
    ans = a
else:
    ans = abs(a - d)

print(ans)

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

x = abs(x)

n = x // d

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

kk = k - n

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

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

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
if k * d < x:
    print(x - k * d)
    exit()

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

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

k-=x//d
x%=d

if k&1:
  print(-x+d)
else:
  print(x)

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

    pos1 = abs(X - (X//D)*D)
    pos2 = abs(X - (X//D+1)*D)

    if pos1 > pos2:
        K -= X//D+1
    else:
        K -= X//D
    
    if K % 2 == 0:
        return min(pos1, pos2)
    else:
        return abs(min(pos1, pos2) - D)

print(C())

Solution (Python):
x,k,d=map(int,input().split())
x=abs(x)
r=k-x//d 
m=x%d 

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

Solution (Python):
x,k,d = map(int,input().split())
x = abs(x)
num = x//d
if k >= num:
    x -= num*d
    cnt = k-num
else:
    x -= k*d
    print(x)
    exit()
if abs(x) > abs(x-d):
    x -= d
    cnt -= 1

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

Solution (Python):
x,k,d = map(int,input().split())
x = abs(x)
num = x//d
if k >= num:
    x -= num*d
    cnt = k-num
else:
    x -= k*d
    print(x)
    exit()

if abs(x) > abs(x-d):
    x -= d
    cnt -= 1

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

Solution (Python):
x,k,d = map(int,input().split())
x = abs(x)
m = min(x//d,k)
x -= d*m
k -= m

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

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

Abs_X = abs(X)
step  = Abs_X // D

if step < K:
    if (K - step) % 2 == 0:
        answer = min(abs(X + step*D), abs(X - step*D))
    else:
        answer = min(abs(X + (step+1)*D), abs(X - (step+1)*D))
else:
    answer = min(abs(X + D*K), abs(X - D*K))

print(answer)

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

Solution (Python):
X,K,D=map(int,input().split())
zettaitix = abs(X)
if zettaitix >= K*D:
    kotae = abs(zettaitix-(K*D))
elif zettaitix < K*D:
    kai, mod = divmod(zettaitix, D)
    ds = kai*D
    if (K-kai)%2==0:
        kotae = abs(zettaitix-ds)
    elif (K-kai)%2!=0:
        kotae = abs(zettaitix-ds-D)
print(kotae)

Solution (Python):
X,K,D=map(int,input().split())
kai = 0
xy = abs(X)
if xy >= K*D:
    kotae = abs(xy-(K*D))
if xy < K*D:
    kai, mod = divmod(xy, D)
    ds = kai*D
    if (K-kai)%2==0:
        kotae = abs(xy-ds)
    if (K-kai)%2!=0:
        kotae = abs(xy-ds-D)
print(kotae)

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

x = abs(x)


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


step_num = x // d + 1

remain_steps = k - step_num

if remain_steps % 2 == 0:
    ans = abs(x - step_num * d)
else:
    ans = x - (step_num - 1) * d

print(ans)

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

p = x // d
if p > k:
  print(x - k * d)
else:
  min = [x, x]
  min[p%2] = x - p * d
  min[(p+1)%2] = abs(min[p%2] - d)
  print(min[k%2])

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

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

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

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

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

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

max_x = X + K * D
min_x = X - K * D

res = 0

if max_x <= 0:
  res = abs(max_x)
  print(res)
  exit()
elif min_x >= 0:
  res = abs(min_x)
  print(res)
  exit()

a = (K*D -X) / (2*D)

max_x = 2 * math.ceil(a) * D + min_x
min_x = 2 * math.floor(a) * D + min_x
res = min(abs(max_x), abs(min_x))
print(res)

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

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

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



def main():
    import sys
    input = sys.stdin.readline

    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)


if __name__ == '__main__':
    main()

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

Solution (Python):
inp=list(map(int,input().split()))
x=inp[0]
k=inp[1]
d=inp[2]
if x<0:
    flag=1
else:
    flag=0
if x==0:
    if k%2==0:
        print (0)
    else:
        print (d)
else:
    ini=x
    x=abs(x)
    a=x%d
    z=(x-a)//d
    if z>=k:
        if flag==1:
            final=ini+k*d
        else:
            final=ini-k*d
        print (abs(final))
    else:
        if flag==1:
            a=-1*a
        k-=z
        final=a
        if k%2==0:
            print (abs(final))
        else:
            if final<0:
                final+=d
                print (abs(final))
            else:
                final-=d
                print (abs(final))

Solution (Python):
x, k, d = map(int, input().split())
x_abs = abs(x)
if x_abs >= k * d:
  print(x_abs - k * d)
else:
  n = x_abs // d
  if (k - n) & 1:
    print((n + 1) * d - x_abs)
  else:
    print(x_abs - n * d)

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

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

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

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

X = abs(X)
time = X//D
rest = K - time

if rest <= 0:
    print(X-K*D)
elif rest%2 == 0:
    print(min(X-time*D, abs(X-(time+2)*D)))
else:
    print(min(X-(time-1)*D, abs(X-(time+1)*D)))

Solution (Python):
x,k,d=map(int,input().split())
x=abs(x)
if x>=k*d:
    print(x-k*d)
else:
    l=x%d
    s=x//d
    rem=k-s
    if rem&1: print(d-l)
    else: print(l)

Solution (Python):
x, k, d = input().split(' ')
x = int(x)
k = int(k)
d = int(d)
x = x - k * d
ans = abs(x)
low = 0
high = k
d = d * 2
while low <= high:
	mid = (low + high) // 2
	pos = x + mid * d
	ans = min(ans, abs(pos))
	if pos == 0:
		break
	elif pos > 0:
		high = mid - 1
	else:
		low = mid + 1
print(ans)

Solution (Python):






import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *


BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def main():
    x,k,d=map(int,input().split())
    x=abs(x)
    n=x//d
    if k<=n:
        print (abs(x-k*d))
    else:
        l,r=abs(x-(n+1)*d),abs(x-n*d)
        if k%2==n%2:
            print (r)
        else:
            print (l)

if __name__ == "__main__":
    main()

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

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

m_n = p_n - d
ans = ""
if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = k
else:
    if k <= p_n_num:
        ans = x - (k*d)
    else:
        if (p_n_num - k) % 2 == 0:
            ans = p_n
        else:
            ans = m_n
print(abs(ans))

Solution (Python):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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


def solve(X, K, D):
    X = abs(X)

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


print(solve(X, K, D))

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

    X = abs(X)

    r, m = divmod(X, D)

    if r <= K:
        mi = X - D*r
        K -= r
    else:
        mi = X - D*K
        K = 0
    
    K = K % 2

    if K == 0:
        print(mi)
    else:
        print(min(abs(mi-D), abs(mi+D)))

if __name__ == "__main__":
    main2()

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

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

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

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

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

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

cur = abs(x)
rem = k

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

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

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


def Repetition(X, K, D):
    if K%2 == 0:
        return X
    else:
        return abs(abs(X) - D)

X = abs(X)
if X < D:
    print(Repetition(X, K, D))
    exit()

n = X//D
if n >= K:
    print(X - D*K)
else:
    X = X - D*n
    K = K - n
    print(Repetition(X, K, D))

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

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

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

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

x = abs(x)
kk = x // d

if kk >= k:
    print(str(x - (d * k)))
    exit()

k -= kk

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

Solution (Python):
import sys

input = sys.stdin.readline


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

    X = abs(X)
    q, r = divmod(X, D)
    if K < q:
        ans = X - D * K
    else:
        if (K - q) % 2 == 0:
            ans = r
        else:
            ans = D - r

    print(ans)


if __name__ == "__main__":
    main()

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

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

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    X, K, D = readnums()
    if X <= 0:
        X = -X
    if X // D >= K:
        print(X - K * D)
    else:
        a1 = X % D
        a2 = D - X % D
        if (K - X // D) % 2:
            print(a2)
        else:
            print(a1)


if __name__ == "__main__":
    main()

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

def calc(M):
	return ( -1 * D * M ) + ( D * ( K - M ) )


MAX_X = 1e15
L = 0
R = K
curMin = MAX_X + 1
sumTmp = 0
while True:
	M = ((L+R)//2)

	sumTmp = calc( M )
	

	if abs(curMin - abs(X+sumTmp)) < 1:
		
		break

	
	

	if 0 < X + sumTmp:
		
		
		L = M + 1
		
	elif X == sumTmp:
		L = R = M
		curMin = 0
		break
	else:
		
		
		
		
		R = M - 1

	if L < 0 or R < 0:
		L = R = 0
		break
	elif L > K or R > K:
		L = R = K
		break
	elif R < L:
		
		
		L,R = R,L

	curMin = min(abs(curMin), abs(X+sumTmp))




print(abs( X + calc(L)))

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    X, K, D = readnums()
    if X > 0:
        if X // D >= K:
            print(X - K * D)
        else:
            a1 = X % D
            a2 = D - X % D
            if (K - X // D) % 2:
                print(a2)
            else:
                print(a1)
    else:
        X = -X
        if X // D >= K:
            print(X - K * D)
        else:
            a1 = X % D
            a2 = D - X % D
            if (K - X // D) % 2:
                print(a2)
            else:
                print(a1)


if __name__ == "__main__":
    main()

Solution (Python):



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



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

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

    val = [0] * 100000000
    val[0] = X
    
    
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = abs(X) // D
        
        if syo > 3 :
            if X > 0 :
                val[0] = X - (syo-2) * D
            else :
                val[0] = X + (syo - 2) * D
            
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 2
            else:
                limit = 3
        for j in val :
            
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

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

Solution (Python):
line = input()

strL = line.split()
X = abs(int(strL[0]))
K = int(strL[1])
D = int(strL[2])

mod = X % D
div = int(X / D)

if div > K:
    ans = X - K * D
elif (K - div) % 2 != 0:
    ans = mod - D
else:
    ans = mod

print(abs(int(ans)))

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

def sgn(x):
    if x > 0:
        return 1
    if x < 0:
        return -1
    return 0

if x > 0:
    x_greedy = x - k*d
else:
    x_greedy = x + k*d

if sgn(x) == sgn(x_greedy):
    print(abs(x_greedy))
    exit()

x_r = x % d
x_l = x_r -d

r_parity = (abs(x - x_r) // d) %2

if k%2 == r_parity:
    print(abs(x_r))
else:
    print(abs(x_l))

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

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

a = abs(x % d)
a_sho = abs(x // d)
b = abs(a-d)

if k <= a_sho:
    ans = abs(x) - (k*d)
else:
    
    if (k - a_sho) %2 == 0:
        ans = a
    else:
        ans = b
print(ans)

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

def solve(x,k,d):
    x=abs(x)
    if (x>0 and x/d>=k):
        x=x-k*d
        return x
    y=x//d
    x-=y*d
    k-=y
    if k%2==0:
        return abs(x)
    return min(abs(x+d),abs(x-d))
print(solve(x,k,d))

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

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

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

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

move_count = k - move_count

if move_count % 2 == 0:
    print(current)
else:
    print(abs(current - d))

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

X = abs(X)

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

Solution (Python):
import numpy

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

ans = 0
if numpy.abs(X) > longest:
    ans = numpy.abs(X) - longest
else:
    m = numpy.abs(X) // D
    if (K - m) % 2 == 0:
        ans = numpy.abs(X) - D * m
    else:
        ans = numpy.abs(X) - D * (m + 1)

ans = numpy.abs(ans)
print(ans)

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

x = abs(x)

num = x // d 

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

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

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)
else:
    div = abs(X) // D
    if (K-div) % 2 == 0:
        print(abs(X) - D * div)
    else:
        print(abs(abs(X) - D * (div + 1)))

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

Solution (Python):
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10


    x,k,d = LMIIS()

    absX = abs(x)
    
    if k*d <= abs(x):
        print(absX-k*d)
        return
    
    num = absX // d
    k -= num
    
    if k & 1:
        num += 1
    print(abs(absX-num*d))

     
    



    
main()

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

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

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