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):
import math
x,k,d=map(int,input().split())
count=int(math.sqrt(x*x)/d)
if k<=count:
    count=k
    k=2
else:
    k=k-count
ans=int(math.sqrt(x*x)-d*count)
if k%2==1:
    ans=math.sqrt((d-ans)**2)
print(int(ans))

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

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

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

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

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

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

Solution (Python):
import sys
x, k, d = map(int,input().split())
x = abs(x)
num = abs(x // d)
if k <= num:
    ans = x - k*d
else:
    num2 = k-num
    if num2 %2 ==0:
        ans = x - num*d
    else:
        ans = abs(x-(num+1)*d)

print(ans)

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

if a > b * c:
    print(abs(a - b*c))
else:
    n = a // c
    surplus = a % c
    m = b - n
    if m % 2 == 0:
        print(surplus)
    else:
        print(abs(min(surplus + c, surplus - c)))

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())
X = abs(X)
a = 0

a = X//D
if a <= K:
    K -= a
else:
    a = K
    K = 0

X -= D*a
    
if K == 0:
    print(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 = (int(x) for x in input().split())
X = abs(X)

straight = min(K, X // D)
K -= straight
X -= straight * D

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

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

ans=1e+15

for i in range(K):
	init_X=X
	init_X+=i*D-(K-i)*D
	if abs(init_X)<ans:
		ans=abs(init_X)

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

left=X-K*D
right=X+K*D

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

elif right<=0:
	print(-right)
else:
	print(left)

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


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

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


       

print(abs(m))

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


x = abs(x);
if (k * d <= x):
    print(x - k * d);
else:
    k1 = x // d;
    x -= k1 * d;
    k -= k1;
    k %= 2;
    x = abs(x - k * d);
    print(x)

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

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

if abs(X) < (K * D):
    a = abs(X) % D
    b = abs(D - a)
    c = abs(X) // D + 1
    if (K - c) % 2 == 0:
        goal = b
    else:
        goal = a
else:
    goal = abs(X) - ( D * K )

print(goal)

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

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

        print(abs(d-(x-d*(x//d))))

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

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

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

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

if (x >= 0) :
  x -= d * hi
else :
  x += d * hi

k -= hi;

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

if (x >= 0) :
  x -= d * hi
else :
  x += d * hi

k -= hi;

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

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

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

Solution (Python):
import math

def solve(x,k,d):
	x = abs(x)
	if x >= k*d:
		return x-k*d
	else:
		n = x//d
		x -= n*d
		k -= n
		if k%2==0: return x
		else: return abs(x-d)

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

Solution (Python):
x, k, d = map(int, input().split(" "))
mini = x - k * d
maxi = x + k * d
t = maxi - (maxi // (2 * d)) * (2 * d)
if mini >= 0:
    print(mini)
elif maxi <= 0:
    print(- maxi)
else:
    print(min(t, abs(t - 2 * d)))

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

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

x=abs(x)

move=min(k, x//d)

k-=move
x-=move*d

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

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

if X < 0:
    X = -X

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

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

X = abs(X)

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

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

x=x if x>0 else -x

if k*d<x:
    print(x-k*d)
else:
    rma=(k*d-x+2*d-1)//(2*d)
    rmi=(k*d-x)//(2*d)

    print(min(abs(x-(k-2*rmi)*d),abs(x-(k-2*rma)*d)))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    X, K, D = [int(x) for x in input().strip().split()]
    ans = 0
    X = abs(X)
    if X >= K * D:
        print(abs(X-K*D))
    else:
        X, K = X - D * (X // D), K - X // D
        if K % 2:
            print(abs(X-D))
        else:
            print(X)

if __name__ == '__main__':
    main()

Solution (Python):

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

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

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

x = abs(x)

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

print(ans)

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



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

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

x=abs(x)

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

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

x = abs(x)

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

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

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


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

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline().strip()


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

def main():
    INF = 999999999999999999999999
    MOD = 10**9+7

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

    if k*d < x:
        print(x - k*d)
        return
    
    y = (x+d-1) // d

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

main()

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

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

Solution (Python):
from sys import setrecursionlimit

setrecursionlimit(10 ** 8)


def main(x, k, d):
    for i in range(k):
        if abs(x + d) < abs(x - d):
            x = x + d
        else:
            x = x - d

        if abs(x - d) > abs(x) and abs(x + d) > abs(x):
            if (k - i - 1) % 2 == 1:
                if abs(x - d) < abs(x + d):
                    x = x - d
                else:
                    x = x + d
            break

    print(abs(x))


def sub(x, k, d):
    print(abs(x) - d * k)


a, b, c = map(int, input().split())

if abs(a) - b * c >= 0:
    sub(a, b, c)
else:
    main(a, b, c)

Solution (Python):
pos, times, ranges = map(int, input().split())
first_try = abs(pos) // ranges

if first_try >= times: 
    print(abs(pos) - times * ranges)
else: 
    remain_times = times - first_try
    second_pos = abs(pos) - first_try * ranges
    if remain_times % 2 == 0: 
        print(abs(second_pos))
    else:
        if abs(second_pos + ranges) <= abs(second_pos - ranges):
            print(abs(second_pos + ranges))
        else:
            print(abs(second_pos - ranges))

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

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

t = x/d

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

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

t = float(abs(x/d))

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

Solution (Python):
import math

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

dis_X = abs(X)
stps = int(dis_X/D)

if K < stps:
  print(dis_X - K*D)
else:
  diff_stps = K - stps
  if diff_stps % 2 : 
    print( (stps+1)*D - dis_X )
  else: 
    print( dis_X - stps*D )

Solution (Python):
inp = list(map(int,input().split(" ")))
location = abs(inp[0])
times = inp[1]
distance = inp[2]

x = float(abs(location / distance))


if x >= times:
    print(abs(location - (times*distance)))
elif (times-int(x))%2==0:
    print(abs(location-abs(int(x)*distance)))
else:
    print(abs(location-abs(int(x)*distance)-distance))

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

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

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

else:
  print(-(x % d - d))
  exit()

Solution (Python):
import math

x, k, d = map(int, input().split())
left = 0
right =k
x = abs(x)
while(left<right):
    mid = (left+right)//2
    if(x-(d*mid)<0):
        right = mid
    else:
        left = mid+1
low= int(left)
high = int(left-1)
v1 = abs(x-(low*d))
v2 = abs(x-(high*d))
if(k%2==0):
    if(v1<=v2):
        if(low%2==0):
            print(v1)
        else:
            print(v2)
    else:
        if(high%2==0):
            print(v2)
        else:
            print(v1)
else:
    if(v1<=v2):
        if(low%2==1):
            print(v1)
        else:
            print(v2)
    else:
        if(high%2==1):
            print(v2)
        else:
            print(v1)

Solution (Python):
import sys
x,k,d = map(int,input().split())
x=abs(x)
c = int(x/d)
le = int(k-abs(c))
if x==0:
    if k%2==0:
        print(0)
    else:
        print(d)
    sys.exit()
else:
    if le>=0:
        ne = -d*c+x
    else:
        if x>0:
            ne = -d*k+x
        else:
            ne = d*k-x
        print(abs(ne))
        sys.exit()
        
if le%2==0:
    print(abs(ne))
else:
    ans1=abs(ne-d)
    ans2=abs(ne+d)
    print(min(ans1,ans2))

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

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

y = x // d


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

else:
    print(abs(x - k * d))

Solution (Python):




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

    X = abs(X)
    div, mod = X // D, X % D
    if K <= div:
        print(X - K * D)
    else:
        print(abs(mod - D * ((K - div) % 2 == 1)))


if __name__ == "__main__":
    main()

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

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

else:
    t=x//d
    r=x-t*d
    l=x-(t+1)*d
    if (k-t)%2==1:
        print(abs(l))
    else:
        print(abs(r))

Solution (Python):
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    if K % 2 == 1:
        X = abs(X - D)
        K -= 1
    div = X // (D*2)
    ans = min(X - min(div, K//2) * (D*2),
              abs(X - min(div+1, K//2) * (D*2)))
    print(ans)


if __name__ == "__main__":
    main()

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

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

else:
  print(X - (K * D))

Solution (Python):

import sys
from itertools import chain



def solve(X: int, K: int, D: int):
    X = abs(X)
    n = X // D
    if n >= K:
        return X - D * K
    m = X % D
    if (K - n) % 2 == 0:
        return m
    else:
        return D-m


def main():
    tokens = chain(*(line.split() for line in sys.stdin))
    X = int(next(tokens))  
    K = int(next(tokens))  
    D = int(next(tokens))  
    ans = solve(X, K, D)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
X, K ,D = input().split()
X = abs(int(X))
K = int(K)
D = int(D)
Q, mod = divmod(X, D)
if K < Q:
    ans = X - K*D
else:
    Rest = (K - Q)%2
    if Rest == 0:
         ans = mod
    else:
         ans = D - mod
print(ans)

Solution (Python):
X, K, D = [int(a) for a in list(input().split())]
X = abs(X) 
q = X//D
r = X%D
if q >= K:
    print(X - D*K)
    exit()
rem = K - q
if rem % 2 == 0:
    print(r)
else:
    print(abs(D-r))

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

x=abs(x)

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

print(ans)

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

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

Solution (Python):

X,K,D = map(int,input().split())
X = abs(X)
step = min(K,int(X/D))
K -= step
X -= step * D
if (K % 2 == 0):
    out = X
else:
    out = D - X
   

print(out)

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

X = abs(X)

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

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

Solution (Python):


x, k, d = map(int, input().split())
if x < 0:
    x *= -1
t1 = x % d
t2 = t1 - d
n = (x - t1) // d
if k < n:
    print(x - k * d)
else:
    n = k - n
    if n % 2 == 0:
        print(t1)
    else:
        print(-t2)

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

Solution (Python):

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



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

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

n = abs(x)//d 

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

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

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

print(x)

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

if X < 0:
    X = -X
a = X // D
b = X % D

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

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

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

if x>=k*d:
    print(x-k*d)
    exit()
else:
    kk=x//d
    kkk=kk+1
    ans=float("inf")
    ans2=float("inf")
    xx=k-kk
    if xx%2==1:
        ans=abs(x-kk*d-d)
    else:
        ans=abs(x-kk*d)
    y=k-kkk
    if y%2==1:
        ans2=abs(x-kkk*d+d)
    else:
        ans2=abs(x-kkk*d)
    print(min(ans,ans2))

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

ans = 0

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

print(ans)

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

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

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy








def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

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

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

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

maxDist=K*D
if(zX>maxDist):
  print(zX-maxDist)
else:
  sabun=maxDist-zX
  ans1 = sabun % (D*2)
  ans2 = abs(ans1 - (D*2))
  if(ans1 > ans2):
    print(ans2)
  else:
    print(ans1)

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 % D
    else:
        ans = abs(X % D - D)
print(ans)

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 % D
    else:
        ans = abs(X % D - D)
print(ans)

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 == 0:
        if K % 2 == 0:
            ans = X % D
        else:
            ans = abs(X % D - D)
    else:
        if K % 2 == 0:
            ans = abs(X % D - D)
        else:
            ans = X % D

print(ans)

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

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

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

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

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

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

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

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

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

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

print(ans)

Solution (Python):
def is_x(x,k,d):
    if x==0:
        if k%2==0:
            print(0)
            exit()
        else:
            print(d)
            exit()

def x_is_mosbat(x,k,d):
    if x<0:
        x=-x
    p=x//d
    if p>=k:
        print(x-d*k)
        exit()
    else:
        x=x-d*p
        k-=p
        if abs(x-d)<x:
            x=x-d
            k-=1
        if k%2==0:
            print(abs(x))
            exit()
        else:
            print(abs(x-d)) if x>0 else print(x+d)
            exit()
    
x,k,d=map(int,input().split())
is_x(x,k,d)
x_is_mosbat(x,k,d)

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

Solution (Python):
x,k,d = map(int, raw_input().split())
def f(x,k,d):
	if k == 0:
		return abs(x)

	if abs(x) <= d:
		if k % 2 == 0:
			if k <= 2:
				return abs(x)
			else:
				return min(abs(x),abs(x-2*d), abs(x+2*d) )
		else:
			return min(abs(x+d), abs(x-d))
	else:
		if x > d:
			if x/d <= k:
				return f(x - (x/d) * d, k - x/d, d)
			elif x / d > k:
				
				return f(x - k*d, 0, d)
		elif x < -d:
			return f(-x, k, d)

print f(x,k,d)

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

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

Solution (Python):
n, k, d = map(int,input().split(' '))
n = abs(n)
ans = []
cur = n // d
if cur >= k: print (n - k * d)
else:
    n -= (cur * d)
    ans.append(n)
    ans.append(abs(n - d))
    k -= cur
    print (ans[k & 1])

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

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


if X == 0:
    if K % 2 == 0:
        print(0)

    else:
        print(D)

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

    elif X - D * K > 0:
        print(abs(X - D * K))

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


def read():
    X, K, D = map(int, input().strip().split())
    return X, K, D


def solve(X, K, D):
    X = abs(X)
    k = X // D 
    if K % 2 == 0:
        if k % 2 == 0:
            i = min(X // D, K)
        else:
            i = min(X // D + 1, K)
    else:
        if k % 2 == 0:
            i = min(X // D + 1, K)
        else:
            i = min(X // D, K)
    ans = X - i * D
    return abs(ans)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

Xa = abs(X)
Xs = Xa // D
Xm = Xa % D

if (K <= Xs):
    print(abs(Xa-D*K))
elif (K-Xs) % 2 == 0:
    print(abs(Xm))
else:
    print(abs(D-Xm))

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

a = x // d

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

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

X = abs(Xa)
Xs = X // D
Xm = X % D

if K < Xs:

    print(abs(X-K*D))










elif ((K-Xs) % 2 == 0):
    print(abs(Xm))
else:
    print(abs(D-Xm))

Solution (Python):


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    X, K, D = list(map(int, input().split()))

    return X, K, D


def main(X: int, K: int, D: int) -> None:
    """
    メイン処理.

    Args:\n
        X (int): 座標(-10^{15} <= X <= 10^{15})
        K (int): 移動回数(1 <= K <= 10^{15})
        D (int): 移動距離(1 <= D <= 10^{15})
    """
    X = abs(X)

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

    if K % 2 == 0:
        ans = X
    else:
        ans = D - X

    
    print(ans)


if __name__ == "__main__":
    
    X, K, D = get_input()

    
    main(X, K, D)

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

X = abs(Xa)
Xs = X // D
Xm = X % D

if K < Xs:

    print(abs(X-K*D))
elif Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif ((K-Xs) % 2 == 0):
    print(abs(Xm))
else:
    print(abs(D-Xm))

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

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


x=abs(x)
if x-d*k >= 0:
    ans=abs(x-d*k)
else:
    k_dash=x//d
    R=x-k_dash*d
    L=R-d

    if (k-k_dash)%2 == 0:
        ans=abs(R)
    else:
        ans=abs(L)

print(ans)

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

print(ans)

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