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= [int(y) for y in input().split()]
if x<0:
    x=-x

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

Solution (Python):
import math
x, k, d = [int(i) for i in input().split()]
def f(i):
    return int(math.fabs(x + (2 * i - k) * d))
def ts(l, r):
    while l + 3 <= r:
        m1 = l + (r - l) // 3
        m2 = r - (r - l) // 3
        if f(m1) < f(m2):
        	r = m2
        elif f(m1) > f(m2):
        	l = m1
        else:
            l = m1
            r = m2
    sol = f(r)
    for i in range(l, r):
      sol = min(sol, f(i))
    return sol
print(ts(0, k))

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

Solution (Python):





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



x = abs(x)


if x >= k * d:
    ans = x - k * d

else:
    
    cnt = x // d
    _k = k - cnt

    _x = x - d * cnt

    
    
    if _k % 2 == 0:
        ans = _x
    else:
        ans = abs(_x - d)

print(ans)

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

X = abs(X)
t = X // D
now = X - t*D

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

print(ans)

Solution (Python):

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

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

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

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

print(ans)

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

minus = X < 0
if minus:
  X = -X

R = X // D

if K <= R:
  result = X - D * K
else:
  n = K - R
  n %= 2
  P = X - D * R
  if n == 0:
    result = P
  else:
    result = abs(P - D)
    

print(result)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

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

x = abs(x)


def func(mid): 
    if x-d*mid < 0:
        return True
    else:
        return False

def binary_search(): 
    ok = 10**18
    ng = -1
    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if func(mid):
            ok = mid
        else:
            ng = mid

    if abs(x-d*ok) <= abs(x-d*ng):
        return ok
    else:
        return ng

if x-k*d >= 0:
    print(x-k*d)
else:
    times = binary_search()
    ans = abs(x-d*times)
    if even(k-times):
        print(ans)
    else:
        print(min(abs(ans+d),abs(ans-d)))

Solution (Python):
l = list(map(int,input().split()))
x = abs(l[0])
k = l[1]
d = l[2]
prev = 0

n = x//d

if k%2 == 0 and n%2 == 1:
  n += 1
elif k%2 == 1 and n%2 == 0:
  n += 1

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

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

X = abs(X)
a = int(X/D)
ans = 0

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

ans = int(abs(ans))
print(ans)

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

x = abs(x)

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

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

Solution (Python):









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

    pmin = x - k * d
    if pmin >= 0:
        return pmin

    pmax = x + k * d
    if pmax <= 0:
        return pmax

    dd, dm = divmod(x, d)
    remains = k - dd
    if remains & 1:
        return abs(dm - d)
    else:
        return dm


def main():
    print(solve())


if __name__ == "__main__":
    main()

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

if abs(X) % D == 0:
    cnt = abs(X) // D
else:
    cnt = (abs(X) // D) + 1

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

print(abs(ans))

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

if abs(x)//d <= k and k>1000 :
  z = k - (abs(x)//d)
  x = abs(x)%d
  if z % 2 == 0:
    print(x)
    exit()
  else:
    if abs(x) + d <= abs(abs(x) - d):
      print(abs(x)+d)
      exit()
    else:
      print(abs(abs(x)-d))
      exit()
elif abs(x)//d > k and k>1000 :
  x = abs(x) - k*d
  print(abs(x))
  exit()
else:
  for i in range(1,k+1):
    if abs(x) >= abs(abs(x) - d):
      x = abs(x) - d
    else:
      if (k-i-1)%2 == 0:
        print(abs(x))
        exit()
      else:
        if abs(x) + d <= abs(abs(x) - d):
          print(abs(x)+d)
          exit()
        else:
          print(abs(abs(x)-d))
          exit()
print(abs(x))

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

t = abs(X) // D

if X < 0: sign = -1
else: sign = 1

if K < t:
    x = X - sign*K*D
else:
    x = X - sign*t*D
    if (K - t) % 2 == 1:
        x = D - abs(x)

print(abs(x))

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

X = abs(X)

a = X // D

X -= min(a, K) * D
K -= min(a, K)

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

K %= 2

if K == 1:
    if abs(X + D) < abs(X - D):
        X += D
    else:
        X -= D

result = abs(X)

print(result)

Solution (Python):


x,k,d=map(int,input().split())
x=abs(x)
f = x//d;

ans = 0

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

Solution (Python):














import math
import copy



INF = 10**18 
pi = math.pi

MOD = 1000000007








def my_pow(N, a, M):
    if(a == 0):
        return 1
    else:
        if(a % 2 == 0):
            tempo = my_pow(N, a/2, M)
            return (tempo * tempo) % M
        else:
            tempo = my_pow(N, a - 1, M)
            return (tempo * N) % M





def my_combination(N, a, M):
    res = 1

    for i in range(0, a):
        res *= N - i
        res %= M

    for i in range(0, a):
        res *= my_pow(i + 1, M - 2, M)
        res %= M

    return res





def my_combination_table(N, M, v):
    if(len(v) < N + 1):
        l = N + 1 - len(v)
        tempo = [1] * l
        v.extend(tempo)

    for i in range(1, N + 1):
        v[i] = v[i - 1] * (N - (i - 1))
        v[i] %= M

        v[i] *= my_pow(i, M - 2, M)
        v[i] %= M

    return







a1, a2, a3 = input().split()
X = int(a1)
K = int(a2)
D  = int (a3)


if (X - K * D >= 0):
   print(X - K * D)
   exit(0)
elif (X + K * D <= 0):
   print(abs(X + K * D))
   exit(0)
else:
    i = (X + K * D) // (2 * D)
    res = abs(X - K * D)
    
    if (0 <= i and i <= K):
        res = min(res, abs(X + K * D - 2 * i * D))   

    if (0 <= i + 1 and i + 1 <= K):
        res = min(res, abs(X + K * D - 2 * (i + 1) * D))

    if (0 <= i - 1 and i - 1 <= K):
        res = min(res, abs(X + K * D - 2 * (i - 1) * D))

    print(res)
    exit(0)
   
   
    






print(res)

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

M = int(abs(X)/D)

if abs(abs(X) - D*M) <  abs(abs(X) - D*(M+1)): cand = M
else: cand = M + 1

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

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

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

x,k,d = I()
a = abs(x) % d
b = abs(x) //d
if x == d:
    if k % 2 ==0:
        print(x)
        sys.exit()
    else:
        print(0)
        sys.exit()

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

Solution (Python):



            





    

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

minans=abs(x)+d
while(k>0):
    if(x==0):
        if(k%2==1):
            minans=d
        else:
            minans=0
        break
    if(x<0):
        x=x*(-1)
    steps= x//d
    if(steps<=k):
        k=k-steps
        x=x-d*steps
        
        if(k%2==1):
            minans=min(abs(x-d),minans)
        else:
            minans=min(x,abs(x-2*d),minans)
        break
    else:
        x=x-k*d
        k=0
        minans=min(x,minans)
        break
print(minans)

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

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

    if abs(X) // D >= K:
        print(abs(X) - D * K)
    else:
        t = abs(X) // D
        if X == 0 and K % 2 != 0:
            print(D)
        elif X == 0 and K % 2 == 0:
            print("0")
        else:
            if abs(X) - D*t < abs(abs(X) - D*(t+1)):
                if (K - t) % 2 == 0:
                    print(abs(X) - D*t)
                else:
                    print(abs(abs(X) - D*(t+1)))
            else:
                if (K - (t+1)) % 2 != 0:
                    print(abs(X) - D*t)
                else:
                    print(abs(abs(X) - D*(t+1)))

Solution (Python):

import sys
input = sys.stdin.readline  
X,K,D = map(int, input().split())
q1,min1 =divmod(X,D)
q1 =abs(q1)
if min1 >=0:
    min2 =min1 -D
else:
    min2 =min1 +D

ans =0
if K <= q1:
    ans =abs(X) -K *D
elif K %2 ==0 and q1 %2 ==0:
    ans =abs(min1)
elif K %2 ==1 and q1 %2 ==1:
    ans =abs(min1)
elif K %2 ==0 and q1 %2 ==1:
    ans =abs(min2)
elif K %2 ==1 and q1 %2 ==0:
    ans =abs(min2)
print(ans)

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

X = abs(X)
_n = X // D

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

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

Solution (Python):
a, b, c = map(int, input().split())
a = abs(a)
if b*c <= a:
    print(a-b*c)
else:
    kaisuu = a//c
    saisyo = a % c
    b = b-kaisuu
    if b % 2 == 1:
        print(abs(c-saisyo))
    else:
        print(abs(saisyo))

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

Solution (Python):

n = input()

n_list = n.split()

x = int(n_list[0])
k = int(n_list[1])
d = int(n_list[2])

def count(x, k, d):
	
	
	x = abs(x)
	
	
	if x / d > k:
		
		return x - d * k
	
	
	
	
	cnt = int(x / d)
	
	c = k - cnt 

	
	x = x - d * cnt

	
	
	if c % 2 == 0:
		return x
	
	
	return min(abs(x + d), abs(x - d))
	

print('{}'.format(count(x,k,d)))

Solution (Python):
import math

start, kai, step = map(int, input().split(" "))

if 0 <= abs(start) - (kai * step):
    print(abs(start) - (kai * step))
    exit()

ans1 = start % step
ans2 = abs(ans1 - step)
if (kai - abs(start // step)) % 2 == 0:
    print(abs(ans1))
else:
    print(abs(ans2))

Solution (Python):
[X,K,D]=list(map(int, input().split()))
abx=round(abs(X))
n=round(abx//D)
if n>K:
    ans=abx-K*D
else:
    x2=abx-n*D
    x1=(n+1)*D-abx
    if x2>=x1:
        if (K-n-1)%2==0:
            ans=x1
        else:
            ans=x2
    else:
        if (K-n)%2==0:
            ans=x2
        else:
            ans=x1
print(ans)

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

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

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

print(place)

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

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

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

Solution (Python):

def solve():
    X = abs(x)
    a = X//D
    if a <= K:
        if (K-a)%2 == 0:
            ans = X%D
        else:
            ans = min(abs((X%D)+D), abs((X%D)-D))
    else:
        ans = min(abs(X-K*D), abs(X+K*D))
    print(ans)

if __name__ == "__main__":
    x,K,D = list(map(int, input().split()))
    solve()

Solution (Python):



import collections
import sys
import copy
import re
import math


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


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

    a = math.ceil(X/D)

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


if __name__ == '__main__':
    main()

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

Solution (Python):
inpl = lambda: list(map(int,input().split()))
X, K, D = inpl()
X = abs(X)
if X + D >= K * D:
    print(abs(X-K*D))
else:
    if K % 2:
        K += 1
        X += D
    E = D*2
    R = X % E
    print(min(R, E-R))

Solution (Python):
import math
x,k,d = (int(x) for x in input().split())
if x < 0:
    x *= -1

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

Solution (Python):
from collections import deque
import collections

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

X,K,D = MI()
flag = 1
if(X<0):
  flag = -1

n = int(X/D)
if(abs(n) < K):
  X = X - n*D
  K = K - n
else:
  X = X - K*D*flag
  print(abs(X))
  exit()

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

Solution (Python):
import sys

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

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

Appr = X // D
Near = X - (X // D) * D

if (K - Appr) % 2 != 0:
    print(abs(Near - D))
else:
    print(abs(Near))

Solution (Python):
x, k, d = map(int, input().split())
p = (x + d * k) // (2 * d)

def f(a):
  return abs(x + d * (k - 2 * a))

if p > k or p < 0:
  print(min(f(0), f(k)))
else:
  print(min(f(p), f(p + 1)))

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

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

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


if d>=abs(x):
    if k%2==0:
        p=abs(x)
    else:
        if x>=0:
            p=abs(x-d)
        else:
            p=abs(x+d)
    print(p)
    exit(0)
else:
    cnt=abs(x)//d

    if cnt>=k:
        cnt=k

    if x>=0:
        p=x-d*cnt
    else:
        p=x+d*cnt

    k-=cnt

    if k==0:
        print(abs(p))
        exit(0)

    if k%2==0:
        print(abs(p))
    else:
        if p>=0:
            p=abs(p-d)
        else:
            p=abs(p+d)

        print(p)

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

tmp = abs(x) % d
cal = abs(x) // d
if tmp > abs(tmp - d):
    cal += 1
    tmp = abs(tmp - d)

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

Solution (Python):
S = list(map(int, input().split()))
X = S[0];
K = S[1];
D = S[2];
if (K % 2) == 1:
	X1 = X - D
else:
	X1 = X
L1 = X1 % (2*D)
L2 = L1 - 2*D
ans = 1000000000000000000
if abs(X - L1) < K * D:
	ans = min(ans, L1)
if abs(X - L2) < K * D:
	ans = min(ans, -L2)
ans = min(ans, abs(X - K*D))
ans = min(ans, abs(X + K*D))
print(ans)

Solution (Python):
import math
X, K ,D =map(int, input().split())
ansp =ansm = float('inf')
n = math.ceil(abs(X)/D)
r = K - n


if r>0:
    if r%2 ==0:
        ansp = abs(X-D*n)
    else:
        ansp = abs(X-D*n+D)
else:
    ansp = abs(X-K*D)


if r>0:
    if r%2 ==0:
        ansm = abs(X+D*n)
    else:
        ansm = abs(X+D*n-D)
else:
    ansm = abs(X+K*D)

print(min(ansp,ansm))

Solution (Python):

import math

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

x = abs(x_org)
ans = 0 

def a(n):
    global x
    global d
    ret = x-d*(n-1)
    return ret 

n = x//d
l = math.floor(n)
r = l+1 

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

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

Solution (Python):

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

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


if __name__ == '__main__':
    main()

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

Solution (Python):

def main():
    x,k,d = map(int,input().split())
    x = abs(x)
    temp1 = x // d
    temp2 = x // d + 1
    if abs(x - d * temp1) < abs(x - d * temp2):
        temp = temp1
    else:
        temp = temp2

    if k <= temp:
        print(abs(x - d * k))
        return

    left = k - temp
    if left % 2 == 0:
        print(abs(x - d * temp))
        return
    else:
        answer = min(abs(x -d * temp + d),abs(x - d * temp - d ))
        print(answer)
        return


if __name__ == '__main__':
    main()

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

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

left = abs(x - d * (x // d + 1))
right = abs(x - d * (x // d))

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

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

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

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

X = abs(X)

final = -1

q, n = X % D, X // D

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

Solution (Python):
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
	exit(0)	
with decimal.localcontext() as ctx:
    ctx.prec = 150
    t=floor(decimal.Decimal(abs(x))/decimal.Decimal(d))
    

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

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

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

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

abs_x = abs(X)
num = int(abs_x / D)


def calc_min():
    if K < num:
        
        if X < 0:
            return X + D * K
        return X - D * K

    
    base_pos = X - D * num if X > 0 else X + D * num

    if (K - num) % 2 == 0:
        return base_pos
    else:
        return base_pos - D if base_pos > 0 else base_pos + D


print(abs(calc_min()))

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

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

if( x == 0 ):
    if( k % 2 == 0 ):
        print("{}".format(0))
        exit()
    else:
        print("{}".format(abs(d)))
        exit()

if( x > 0 ) :
    if( x - k * d >= 0 ) :
        ret = x - k * d
        print("{}".format(ret))
        exit()
    rem = x % d
    cost = x // d
    rem2 = abs(rem - d)
    remc = k - cost
    if( remc % 2 == 0 ) : 
        ret = rem
        print("{}".format(ret))
        exit()
    else:
        ret = rem2
        print("{}".format(ret))
        exit()
elif( x < 0 ) :
    if( x + k * d <= 0 ) :
        ret = abs( x + k * d )
        print("{}".format(ret))
        exit()
    rem = (-x) % d
    cost = (-x) // d
    rem *= -1
    rem2 = rem + d
    remc = k - cost
    if( remc % 2 == 0 ):
        ret = abs(rem)
        print("{}".format(ret))
        exit()
    else :
        ret = rem2
        print("{}".format(ret))
        exit()

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

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

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

b = abs(X)//D
if b <= K:
    K -= b
else:
    b = K
    K = 0
    
    
if X > 0:
    X -= D*b
elif X < 0:
    X += D*b
    
if K == 0:
    print(abs(X))
else:
    if X == 0:
        if K % 2 == 0:
            print(X)
        else:
            print(D)
    elif X > 0:
        if abs(X-D) < abs(X):
            X -= D
            K -= 1
        if K % 2 == 0:
            print(abs(X))
        else:
            print(min(abs(X-D),abs(X+D)))
    elif X < 0:
        if abs(X+D) < abs(X):
            X += D
            K -= 1
        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())
if D*K<abs(X):
    ans=abs(X)-D*K
else:
    p=abs(X)//D
    q=abs(X)%D
    if (K-p)%2==1:
        ans=abs(D-q)
    else:
        ans=q
print(ans)

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

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

print(abs(ans))

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

x=abs(x)

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

Solution (Python):
x, k,d = map(int, input().split(" "))
m = abs(x) // d
p,q= k%2, m%2
if k<=m or p==q:
    if x < 0:
        x += d * min(m,k)
    else:
        x -= d * min(m,k)
else:
    if x < 0:
        l = x + d * m - d
        r = x + d * m + d

        x = min(abs(l), abs(r))

    else:
        l = x - d * m - d
        r = x - d * m + d
        x=min(abs(l),abs(r))


print(abs(x))

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

low = 0
high = K

while low <= high:
    mid = (low + high) //2
    i = mid
    j = K-mid
    ans_pre = (X + D*i - D*j)
    if ans_pre > 0:
        high = mid-1
    else:
        low = mid+1
        

if low > K :
    low = high
if high < 0:
    high = low
    
ans_h = min(abs((X + D*high - D*(K-high))), abs(X + D*(K-high)-D*high))
ans_l = min(abs((X + D*low - D*(K-low))), abs(X + D*(K-low)-D*low))
    
print(min(ans_l, ans_h))

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

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

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

Solution (Python):

import io, sys


def main():
    X,K,D = list(map(int, sys.stdin.readline().split()))

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

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


if __name__ == "__main__":
    main()

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

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

d = int(abs(X)/D)
if X > 0:
    r = X - d*D
    if abs(r-D) < abs(r):
        r = r-D
    min_count = int((X-r)/D)
else:
    r = X + d*D
    if abs(r+D) < abs(r):
        r = r+D
    min_count = int(abs(X-r)/D)

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

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

g = abs(X) % D

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

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

sho, amari = abs(x)//d, abs(x)%d
if k <= sho:
  print((abs(x)-(k*d)))
  sys.exit()
else:
  x = amari
  k = k-sho
  
if x == abs(x-d):
  print(x)
elif x < abs(x-d):
  if k%2==0:
    print(x)
  else:
    print(abs(x-d))
else:
  if k%2==1:
    print(abs(x-d))
  else:
    print(x)

Solution (Python):

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

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



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

Solution (Python):
def zet(num):
    if num<0:
        return -num
    else:
        return num

x,k,d = map(int, input().split())
x = zet(x)
amari = x%d
syou = x//d
if syou > k:
    syou = k

sa = k-syou
genzai = zet(x) - syou * d
if sa == 0:
    print(genzai)
    exit()
    
if sa % 2 == 0:
    print(genzai)
else:
    print(zet(genzai - d))

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

if abs(X) < (K * D):
    q = abs(X) // D
    r = abs(X) % D
    if (K - q) % 2 == 0:
        ans = r
    else:
        ans = min(abs(r + D), abs(r - D))
elif X > 0:
    ans = X - (K * D)
elif X < 0:
    ans = abs(-(abs(X) - (K * D)))

print(ans)