Problem:
 Score : 300 points    Problem Statement Takahashi, who lives on the number line, is now at coordinate X . He will make exactly K moves of distance D in the positive or negative direction.  More specifically, in one move, he can go from coordinate x to x + D or x - D .  He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.  Find the minimum possible absolute value of the coordinate of the destination.      Constraints  -10^{15} \leq X \leq 10^{15}  1 \leq K \leq 10^{15}  1 \leq D \leq 10^{15}  All values in input are integers.         Input Input is given from Standard Input in the following format:  X  K  D       Output Print the minimum possible absolute value of the coordinate of the destination.        Sample Input 1 6 2 4      Sample Output 1 2  Takahashi is now at coordinate 6 . It is optimal to make the following moves:   Move from coordinate 6 to ( 6 - 4 = ) 2 .  Move from coordinate 2 to ( 2 - 4 = ) -2 .   Here, the absolute value of the coordinate of the destination is 2 , and we cannot make it smaller.       Sample Input 2 7 4 3      Sample Output 2 1  Takahashi is now at coordinate 7 . It is optimal to make, for example, the following moves:   Move from coordinate 7 to 4 .  Move from coordinate 4 to 7 .  Move from coordinate 7 to 4 .  Move from coordinate 4 to 1 .   Here, the absolute value of the coordinate of the destination is 1 , and we cannot make it smaller.       Sample Input 3 10 1 2      Sample Output 3 8       Sample Input 4 1000000000000000 1000000000000000 1000000000000000      Sample Output 4 1000000000000000  The answer can be enormous.

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

times = abs(X) // D

if times < K:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
    K = K - times
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
else:
    if current_position >= 0:
        current_position = current_position - D * K
    elif current_position < 0:
        current_position = current_position + D * K
print(abs(current_position))

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

Solution (Python):
x, k, d = [int(i) for i in input().split()]
x = abs(x)
if k * d <= x:
    print(x - k*d)
else:
    t = x
    x %= d
    k = k - (t - x)//d
    if k & 1:
        print(abs(d - x))
    else:
        print(x)

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

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

min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    else:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, abs(X), K-i)
        break
print(min_score)

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

Solution (Python):


def solve():
	arr=[int(i)for i in input().split()]
	n=abs(arr[0]);k=arr[1];d=arr[2]
	diff=n//d
	
	if(diff>k):
		print(n-(d*k))
		return
	else:
		if((k-diff)%2==0):
			print(n-(diff*d))
			
		else:
			print(abs((n-(diff*d))-d))





	


	   
	    
	
	

test=1
while test:

	solve()
	test-=1

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

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

if abs(X)//D>=K:
  print(abs(X)-D*K)
else:
  for i in range(K):
    buf = X
    if X<0:
      X += D
    else:
      X -= D
    if X == before:
      if (K-i)%2 == 0:
        if X<0:
          X += D
        else:
          X -= D
      else:
        X = before
      break
    before=buf
  print(abs(X))

Solution (Python):

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

q = x // d

r = x % d

if(k <= q):
    print(x - k*d)
if(q < k):
    k2 = k - q
    
    if(k2 % 2):
        print(min(abs(r -d), r + d))
    
    else:
        print(min(abs(r - 2*d), r ))

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

Solution (Python):
x, k, d = [int(x) for x in input().split()]
ab = abs(x)
if ab-k*d>=0:
    print(ab-k*d)
else:
    t = ab//d
    k -= t
    ab -= d*t
    k %= 2
    if k==1:
        ab -= d
    print(abs(ab))

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


if abs(X) >= K*D:
    ans = abs(X) - K*D
else:
    t = (abs(X) - D*(K%2)) // (2*D)
    t = 2*t + K%2
    if X > 0:
        t = -t
    A = [t-2,t,t+2]
    ans = 3*D
    for a in A:
        if abs(X+D*a)<ans:
            ans = abs(X+D*a)


print(ans)

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

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

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

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

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

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

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

X = abs(X)

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

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

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

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

num_min_move = abs(X//D)
tmp = abs(X%D)

if num_min_move == K:
  print(tmp)
elif num_min_move > K:
  print(abs(X)-K*D)
else:
  out = tmp
  if (K - num_min_move) % 2 == 1:
    print(abs(tmp-D))
  else:
    print(tmp)

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

Solution (Python):
m,K,D = input().split()
m = int(m)
K = int(K)
D = int(D)
if K < 0 or D < 0:
  print('error')
if m < 0:
  m *= -1
a = (m - m % D) / D
if a > K:
  m -= D*K
  K = 0
else:
  K -= a
  if K % 2 == 0:
    m %= D
  else:
    m = (m % D - D) * -1
print(m)

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

print(abs(x))

Solution (Python):
import math
X, K, D = map(int, input().split())
ans = 10**16
minc_floor = math.floor(abs(X)/D)
minc_ceil = math.ceil(abs(X)/D)
min_kaisuu = K

if minc_ceil <= K:
    for k in range(minc_floor, minc_ceil+1):
        for flug in [-1, 1]:
            if abs(X + flug*k*D) < abs(ans):
                ans = abs(X + flug*k*D)

                
                min_kaisuu = k
                
    if (K-min_kaisuu) % 2 != 0:
        ans = min(abs(ans+D), abs(ans-D))
else:
    
    if abs(X+K*D) > abs(X-K*D):
        ans = X-K*D
    else:
        ans = X+K*D

print(abs(ans))

Solution (Python):
X, K, D = map(int, input().split())
def resolve_best_i(X, K, D):
    if abs(X - int(X / D) * D) < abs(X - (int(X / D)+1)*D):
        return abs(X - int(X / D) * D), int(X /D)
    else:
        return abs(X - (int(X / D)+1)*D), int(X /D) + 1
def make_most_min(X, K, D):
    mokuhyou = X
    best = 0
    if X >= 0:
        mokuhyou, best = resolve_best_i(X, K, D)
        if best > K:
            best_posi = abs(X - K * D)
            return best_posi
        if best % 2 == K % 2:
            return abs(X - best * D)
        else:
            return min(abs(X - (best+1) * (D)), abs(X - (best-1) * (D)))
if X >= 0:
    a = make_most_min(abs(X), K, abs(D))
else:
    a = make_most_min(-X, K, abs(D))
print(a)

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

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

x1 = abs(x)
mod1 = x1 % d
mod2 = mod1 - d
if mod1 > abs(mod2):
    mod = mod2
    modx = mod1
else:
    mod = mod1
    modx = mod2

a = abs(x1-mod) / d 

if k*d < x1-mod:
   print(x1-k*d)
elif (k-a) % 2 == 0:
    print(abs(mod))
else:
    print(abs(modx))

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

amari = abs(x) % d
shou = (abs(x)-amari)/d

if k <= shou:
    print(int(abs(x)-d*k))
else:
    nokorikaisuu = k-shou
    if nokorikaisuu % 2 == 1:
        print(int(abs(abs(x)-d*shou-d)))
    else:
        print(int(abs(x)-d*shou))

Solution (Python):
x,k,d=list(map(int,input().split()))
x=abs(x)
syou, amari = divmod(abs(x),d)
if x>0:
    if x-k*d==0:
        print(0)
    elif x-k*d<0:
        if amari==0:
            newk=k-syou
            if newk%2==0:
                print(0)
            else:
                print(d)
        elif amari!=0:
            newk=k-syou+1
            if newk%2==0:
                print(abs(x-d*(syou+1)))
            else:
                print(abs(x-d*syou))
    elif x-k*d>0:
        print(abs(x-k*d))
if x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

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


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

else:
    
    
    
    
    
    

    div = x//d

    a1 = abs(x - div*d)
    a2 = abs(x - (div+1)*d)

    if (div % 2) != (k % 2):
        print (a2)
    else:
        print (a1)

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

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

Solution (Python):
from math import ceil, floor
X, K, D = map(int, input().split())
ans = abs(X + K*D)
fapp = (K*D - X)/(2*D)

if fapp <= 0:
    list = [0]
elif fapp >= K:
    list = [K]

else:
    list = [ceil(fapp), floor(fapp)]
for f in list:
    abso = abs(X + f*D - (K-f)*D)
    if abso < ans:
        ans = abso

print(ans)

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

x, k, d = LI()
if d*k <= abs(x):
    print(abs(x) - d*k)
else:
    y = abs(x) // d
    if (k - y) % 2:
        print(abs(abs(x) - d*(y+1)))
    else:
        print(abs(x) - d*y)

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

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

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

X = abs(X)
q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    ans1 = D-mod
    ans2 = mod

    if ans1<ans2:
        if (K-q-1)%2==1:
            ans = ans2
        else:
            ans = ans1
    else:
        if (K-q)%2==1:
            ans = ans1
        else:
            ans = ans2



print(ans)

Solution (Python):




import sys
input = sys.stdin.readline



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

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

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

    bestp = X % D
    bestm = bestp - D

    ablep = False

    if abs(X-bestp)//D <= K:
        ablep = True

    ablem = False

    if abs(X-bestm)//D <= K:
        ablem = True

    if (not ablep) & (not ablem):
        if X >= 0:
            res = abs(X-K*D)
        else:
            res = abs(X+K*D)
        print(res)
        sys.exit()

    res = []

    if ablep:
        if X >= bestp:
            resid = K - (X - bestp) // D
        else:
            resid = K - (bestp - X) // D
        if resid%2 == 0:
            res.append(abs(bestp))
        else:
            res.append(abs(bestp-D))
            res.append(abs(bestp+D))

    if ablem:
        if X >= bestm:
            resid = K - (X - bestm) // D
        else:
            resid = K - (bestm - X) // D
        if resid%2 == 0:
            res.append(abs(bestm))
        else:
            res.append(abs(bestm-D))
            res.append(abs(bestm+D))

    res = min(res)
    print(res)



main()

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

Solution (Python):
s = input().split()
X=int(s[0])
K=int(s[1])
D=int(s[2])
q, mod = divmod(X, D)
if q < 0:
  q = -q
if 2*mod < D:
   if K < q:
      if X > 0:
         m = X - D*K
      else:
         m = X + D*K
   else:
       restmovement = (K-q)%2
       if restmovement==0:
          m = mod
       else:
          m = D - mod
else:
   if X > 0:
      if K < q+1:
         m = X - D*K
      else:
         restmovement = (K - q - 1)%2
         if restmovement==0:
                    m = D - mod
         else:
                    m = mod
   else:
       if K < q-1:
          m=X - D*K
       else:
          restmovement = (K+1-q)%2
          if restmovement==0:
                m = D-mod
          else:
                m = mod
if m < 0:
   print(-m)
else:
   print(m)

Solution (Python):
a,b,c=map(int, input().split()) 
k = b*c
l = abs(a)

if l > 0:
    if l > k:
        print(l-k)
    if l == k:
        print(0)
    if l < k:
        d = int(l//c)
        e = b - d
        if e%2== 0:
            print(l-c*d)
        else:
            f = d + 1
            print(f*c -l)
if l == 0:
    if b%2==0:
        print(0)
    else:
        print(c)

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

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

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

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

else:
  cnt = K-q
  if cnt%2 == 0:
    print(mod)
  if cnt%2 == 1:
    print(D-mod)

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

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

    a=abs(X//D)
    b=abs(X//D)+1
    kyori_a=X-a*D
    kyori_b=X-b*D

    if(K-a==0):
        print(abs(kyori_a))
        exit()
    if((K-a)%2==0):
        print(abs(kyori_a))
        exit()
    else:
        print(abs(kyori_b))
        exit()

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

Solution (Python):
from decimal import *
x, k, d = map(Decimal, input().split())

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

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

seki = K * D
amari = X % D

if 0 <= (X-seki) or (X+seki) <= 0 : 
    result = min(abs(X-seki),abs(X+seki))
else : 
    if X >= 0 :
        if K%2 == 0 :
            if ((abs(X)-amari)/D) % 2 == 0 :
                
                result = amari
            else :
                
                result = abs(amari-D)
        else :
            if ((abs(X)-amari)/D) % 2 == 1 :
                
                result = amari
            else :
                
                result = abs(amari-D)
    else :
        amari = abs(X)%D
        if K%2 == 0 :
            if ((abs(X)-amari)/D) % 2 == 0 :
                
                result = amari
            else :
                
                result = abs(-amari+D)
        else :
            if ((abs(X)-amari)/D) % 2 == 1 :
                
                result = amari
            else :
                
                result = abs(-amari+D)
    
print(result)

Solution (Python):

inp = list(map(int,raw_input().split()))
x = inp[0]
k = inp[1]
d = inp[2]
x = abs(x)
if(x >= k*d):
    print(x-k*d)
else:
    res=10000000000000000000000000000000000000
    tmp = int(x/d)
    for y in range(-100,100):
        if((y+tmp)<=k and y+tmp >=0):
            rem = k-y-tmp
            if(rem%2 == 0):
                res=min(res,abs(x-(y+tmp)*d))
    print(res)

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

if abs(X) >= K * D:
  if X >= 0:
    Ans = X - (K * D)
  else:
    Ans = abs(X + (K * D))
    
else:
  abX = abs(X)

  if (K - abX // D) % 2 == 0:
    Ans = abX - ((abX // D) * D)
  else:
    Ans = abs(abX - ((abX // D) * D) - D)
      






    
print(Ans)

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

sm = abs(x)//d+1

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

print(abs(ans))

Solution (Python):
[_x, _k, _d] = list(map(int, input().split(" ")))
_x = abs(_x)

if _x >= _d:
  _y = _x//_d
  if _k < _y:
    _y = _k
  _x = abs(_x - _d * _y)
  _k -= _y
if _k % 2 == 1:
  _x = abs(_x - _d)

print(_x)

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

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

    if zero_flag==0:
        d=abs(X)%D
        d_=abs(X)//D
        if (K-d_)%2==1:
            print(abs(D-d))
        else:
            print(abs(X)-abs(D*d_))
    else:
        print(abs(X)-K*D)

if __name__=="__main__":
    main()

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

Solution (Python):
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()

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

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

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

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

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

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

Solution (Python):
X, K, D = [int(n) for n in input().split()]
x = abs(X)
d = (x+D-1)//D
r = x % D
ans = 0
if d<=K:
    if X>0:
        X -= d*D
    else:
        X += d*D
    K -= d
    if K%2==0:
        ans = X
    else:
        if X>0:
            ans = X-D
        else:
            ans = X+D
else:
    if X>0:
        ans = X-K*D
    else:
        ans = X+K*D
print(abs(ans))

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

s =min(X//D,K)

X -= s*D
K -=s

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

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

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


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

	num= abs(x)//d
	if k<=num:
		if x<0:
			x=x+k*d
		else:
			x=x-k*d
		print(abs(x))
	else:
		if x<0:
			x=x+num*d
		else:
			x=x-num*d

		k=k-num

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

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

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

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

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

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

Solution (Python):




X,K,D=map(int,input().split())
b=0
Xabs=abs(X)
J=Xabs%D
N=Xabs//D

if N>K:
    b=Xabs-K*D

else:
    if (K-N)%2==0 :
        b=J
   
    
    
    
    else:
        b=D-J
        

print(b)

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

Solution (Python):
x,k,d=list(map(int,input().split()))
flag=0
if(x<0):
    flag=1
    x=abs(x)
if(x-k*d<0):
    t=(x//d)
    if(t&1==k&1):
        print(abs(x-t*d))
    else:
        print(abs(x-(t+1)*d))
else:
    print(abs(x-k*d))

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

if x<0:
  x *= -1

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

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

abs_x = abs(x)
ans = 0
left = 0

if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = d

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

print(ans)

Solution (Python):
X, K, D = map(int,input().split())
absX = abs(X)
t = abs(X // D)
remain_K = K - t
l = X % D
if remain_K < 0:
    a = abs(X - (K * D))
    b = abs(X + (K * D))
    if a < b:
        ans = a
    else:
        ans = b
else:
    if remain_K % 2 == 0:
        a = abs(l - (D * 2))
        b = abs(l + (D * 2))
        if a < b and a < l:
            ans = a
        elif a > b and b < l:
            ans = b
        else:
            ans = l
    else:
        a = abs(l - D)
        b = abs(l + D)
        if a < b:
            ans = a
        else:
            ans = b
print(ans)

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

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

k = x//D
if (K - k)%2 == 0:
    print(x - D*k)
    exit()
if x - D*(k-1) < abs(x - D*(k+1)):
    print(x - D*(k-1))
    exit()
else:
    print(abs(x - D*(k+1)))

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

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

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

if k % 2 == 0:
    print(a)
else:
    print(b)

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


x = abs(x)
min_pos = x % d
min_neg = min_pos - d

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

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

cou=x1//d
cou+=1
x1=x1-d*cou



if k<cou:
  x2=abs(x)-k*d
  print(x2)


else:
  a=cou%2
  b=k%2
  x4=x1*(-1)
  if a==b:
    print(abs(x1))
  if a!=b:
    print(x1+d)

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

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

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

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

Solution (Python):

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

if X >= 0:
  a= 1
else:
  a = -1

h = min(K, int(a*X/D))
K = K-h
X = X - a*D*h


      


flg = 0
minx = abs(X)
Xnow = X
Xprepre = 0
for i in range(K):
  if i != 0:
    Xprepre = Xpre
  Xpre = Xnow 
  X1 = Xnow -D
  X2 = Xnow +D
  if abs(X1) < abs(X2):
    Xnow = X1
  else:
    Xnow = X2
  
  if Xprepre == Xnow:
    flg = 1
    if (K-i+1)%2 == 0:
      print(abs(Xnow))
    else:
      print(abs(Xpre))
    break
if flg == 0:
  print(abs(Xnow))

Solution (Python):
import math

if __name__ == '__main__':
    s = input().split(' ')
    X, K, D = abs(int(s[0])), int(s[1]), int(s[2])

    a = X // D

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

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

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

r, s = divmod(x, d)
if r < 0:
    r = abs(r)
    if k < r:
        print(abs(x + k * d))
    elif r%2 == k%2:
        print(s)
    elif r%2 != k%2:
        print(abs(s-d))
else:
    if k <= r:
        print(abs(x - k * d))
    elif r%2 == k%2:
        print(s)
    elif r%2 != k%2:
        print(abs(s-d))