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 sys
x, k, d = map(int, input().split())


isEven = (k%2 == 0)


div = abs(x//d)



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

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

Solution (Python):
import sys

X, K, D = [int(x) for x in input().strip().split(" ")]

X = abs(X)
k, x = divmod(X, D)

if K <= k:
    print(X - D * K)
    sys.exit(0)

X = x
K -= k

if K % 2 == 0:
    print(abs(X))
    sys.exit(0)

print(abs(X - D))

Solution (Python):
data = input().split(" ")
x0 = int(data[0])
k  = int(data[1])
d  = int(data[2])

const = x0 - k*d
if const >= 0:
  print(const)
else:
  ii = int(abs(const)/(2*d))
  if ii >= k:
    print( abs(const+2*d*(k)) )
  else:
    x_1 = abs(const+2*d*(ii-1))
    x_2 = abs(const+2*d*(ii))
    x_3 = abs(const+2*d*(ii+1))
    x = [x_1,x_2,x_3]
    x.sort()
    print(x[0])

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

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

Solution (Python):



input_line = input().rstrip().split(" ")
Answer = 0

X = int(input_line[0])
K = int(input_line[1])
D = int(input_line[2])



if X > 0 and X - K * D >= 0:
    Answer = X - K * D
elif X < 0 and X + K * D <= 0:
    Answer = X + K * D



elif X >= 0:
    nearpin = X - abs(int(X / D)) * D
    nokori = K - abs(int(X / D))
    if nokori % 2 == 0:
        Answer = nearpin
    else:
        Answer = nearpin - D
elif X < 0:
    nearpin = X + abs(int(X / D)) * D
    nokori = K - abs(int(X / D))
    if nokori % 2 == 0:
        Answer = nearpin
    else:
        Answer = nearpin + D

print(abs(Answer))

Solution (Python):
import sys
import math
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
x,k,d=get_ints()
x=abs(x)
a=x-k*d
ans=0
if a>=0:
    ans=a
else:
    c=k-x//d
    if c%2==0:
        ans=abs(x-(x//d)*d)
    else:
        ans=min(abs(x-(x//d)*d+d),abs(x-(x//d)*d-d))
print(ans)

Solution (Python):
X, K, D = [int(x) for x in input().split()]
if(D*K+X)>0:
  if(X-D*K)>=0:
    print(X-D*K)
  else:
    x = int((D*K+X) // (2*D))
    ans0 = abs(-2*D*(x-1)+(D*K+X))
    ans1 = abs(-2*D*x+(D*K+X))
    ans2 = abs(-2*D*(x+1)+(D*K+X))
    ans3 = abs(-2*D*(x+2)+(D*K+X))
    print(min(ans0, ans1, ans2, ans3))
elif(D*K+X)<=0:
  print(abs(D*K+X))

Solution (Python):


[x, k, d] = list(map(int, input().split()))
x = abs(x)
howMany = x // d
remainder = x % d
stepLeft = k - howMany
if k < howMany:
    print(abs(x) - k * d)
elif stepLeft % 2 == 0:
    print(remainder)
elif stepLeft % 2 == 1:
    print(abs(d - remainder))

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

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

if abs(X)/D >= K:
    X = abs(X)-D*K
    print(X)
else:
    X = min(abs(X-D*S), abs(X+D*S))
    K = K - S
    for k in range(K):
        if min(abs(X+D), abs(X-D)) < abs(X):
            X = min(abs(X+D), abs(X-D))
            K -= 1
        else:
            if K%2 == 0:
                print(X)
                break
            else:
                X = min(abs(X+D), abs(X-D))
                print(X)
                break

    if K == 0:
        print(X)

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

def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    cnt = x // d
    point = x % d
    if x == 0:
        if k % 2 == 0:
            point = 0
        else:
            point = d
    else:
        
        if cnt > k:
            point = x - k * d
        
        else:
            
            if point == 0:
                if (k - cnt) % 2 == 0:
                    point = 0
                else:
                    point = d
                print(point)
                exit()
            else:
                if abs(point - d) < abs(point):
                    point = point - d
                    cnt += 1
            
            if (k - cnt) % 2 == 0:
                pass
            else:
                point = min(abs(point - d), abs(point + d))
    print(abs(point))

if __name__ == '__main__':
    main()

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

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

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

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

X = abs(X)

n = X // D
m = K - n

A1 = abs(X - n*D)
A2 = abs(X - (n+1)*D)

Ans = A2

if m > 1:
    if A1 <= A2:
        if m % 2 == 0:
            Ans = A1
    else:
        if m % 2 == 0:
            Ans = A1
else:
    Ans = abs(X - K*D)

print(Ans)

Solution (Python):
def resolve():
    X, K, D = map(int, input().split())
    X = abs(X)
    q, mod = divmod(X, D)
    
    
    if q>=K:
        
        answer = X - K*D
    else:
        
        positive_abs_min = X-q*D
        negative_abs_min = D - positive_abs_min
        
        rest_k = K - q
        if rest_k%2 == 0:
            answer = positive_abs_min
        else:
            answer = negative_abs_min
    print(answer)
resolve()

Solution (Python):
def function(X, K, D):
    kai = int(abs(X)/D)
    if kai >= K:
        if X > 0:
            ans = X-K*D
        else:
            ans = X+K*D
    else:
        K = K-kai
        if X > 0:
            X = X-kai*D
        else:
            X = X+kai*D
        if K % 2 == 1:
            if X > 0:
                ans = X-D
            else:
                ans = X+D
        else:
            ans = X
    return abs(ans)


if __name__ == '__main__':
    X, K, D = input().split()
    X = int(X)
    K = int(K)
    D = int(D)
    print(function(X, K, D))

Solution (Python):

"""
Created on Sat Aug 15 17:44:21 2020

@author: Paras
"""

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

moves = abs(x)//d
if moves>=k:
    print(abs(x)-(k*d))
    
else:
    closest1 = abs(x)-moves*d
    closest2 = closest1-d
    if closest1==abs(closest2):
        if closest1==0:
            leftmoves = k-moves
            if leftmoves%2==0:
                print("0")
            else:
                print(d)
        else:
            print(closest1)
    else:
        if (k-moves)%2==0:
            print(abs(closest1))
        else:
            print(abs(closest2))

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

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

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

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

Solution (Python):

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

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


main()

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

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

print(ans)

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

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

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

xi = abs(x)
if abs(x) > d*k:
  m = min(abs(x+d*k),abs(x-d*k))
else:
  n = ceil(xi / d)
  if (n+k) % 2 == 0:
    m = abs(xi-d*n)
  else:
    m = abs(xi-d*(n-1))
print(m)

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

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

q, mod = divmod(abs(X), abs(D))

ans = 0
if q < K:
    amari = K - q
    l = amari % 2
    if l == 0:
        ans = mod
    else:
        ans = D - mod
else:
    div = q - K
    ans = div * D + mod

print(ans)

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

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

ans = 0
count = X // D



if X >= 0:
    if K <= count:
        ans = X - D * K
    else:
        ans = X - D * count
        if ((K - count) % 2 == 1):
            ans = ans - D
else:
    if K <= (-1) * count:
        ans = X + D * K
    elif X % D == 0:
        ans = X - D * count 
        if ((K + count) % 2 == 1):
            ans = ans + D
    else:
        ans = X - D * (count + 1) 
        if ((K + count + 1) % 2 == 1):
            ans = ans + D

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

print(ans)

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


if __name__ == "__main__":
    print(main())

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

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

if x==0 and k%2==0:print(0)
if x==0 and k%2==1:print(d)

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

Solution (Python):

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

s = abs(x)//d
 

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

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

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

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

Solution (Python):
X, K, D = (int(i) for i in input().split())
X = max(X, -X)
k, x = divmod(X, D)
if k >= K:
    print(X - K * D)
else:
    if (K - k) % 2 == 0:
        print(abs(x))
    else:
        print(abs(x-D))

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

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


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

if D > X:
    bef = X
    nex = X - D

    if K % 2 == 0:
        print(abs(bef))
    else:
        print(abs(nex))

else: 
    bef = X % D
    nex = bef - D

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

Solution (Python):
x, k, d = map(int, input().split())
t = abs(x) // d
a = []
a.append(t - 3)
a.append(t - 2)
a.append(t - 1)
a.append(t)
a.append(t + 1)
a.append(t + 2)
a.append(t + 3)
b = []

for i in a:
    b.append(-i)
for i in b:
    a.append(i)

a.append(k)
a.append(k - 1)
a.append(-k)
a.append(-k + 1)

res = abs(x)
if k % 2:
    res += d
for i in a:
    if i % 2 == k % 2 and abs(i) <= k:
        res = min(res, abs(x - i * d))
print(res)

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


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

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

print(X)

Solution (Python):
import math

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

X = abs(X)


n = math.floor(X/D)

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

print(ans)

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

x = abs(x)
t = min(k, x//d)

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

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	
	x, k, d = tin()
	
	na = abs(x) // d
	
	if na >= k:
		return min(abs(x - d*k), abs(x+d*k))
	else:
		xa = x - d*na if x >= 0 else x+d*na
		ka = k-na
		kb = ka%2
		
		if kb==1:
			return min(abs(xa+d), abs(xa-d))
		else:
			return min(abs(xa-2*d),abs(xa+2*d),abs(xa))
		
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

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

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

print(ans)

Solution (Python):
import sys

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

p = X
_p = 0
k = 0

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

if p < 0:
    for i in range(K):
        _p = p
        p += D
        k += 1
        if p > 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
elif p > 0:
    for i in range(K):
        _p = p
        p -= D
        k += 1
        if p < 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Solution (Python):
import itertools

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

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

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

X = abs(X)

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

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


def solve(X, K, D):
    if abs(X) >= D * K:
        return abs(X) - (D * K)

    least_hops = abs(X) // D

    hops_left = K - least_hops
    dst = abs(X) - (D * (least_hops + hops_left % 2))

    return abs(dst)


if __name__ == "__main__":
    main()

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

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

Solution (Python):
x,k,d=map(int,input().split())
xa=abs(x)
kz=xa//d
xz=xa-kz*d

if xz*2>d:
  xzz=d-xz
  kz+=1
else:
  xzz=xz
rest=k-kz
if rest<0:
  print(xa-k*d)
elif rest%2==0:
  print(xzz)
else:
  print(d-xzz)

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

Solution (Python):
import decimal    

data = input().split()
X = decimal.Decimal(data[0])
K = decimal.Decimal(data[1])
D = decimal.Decimal(data[2])
if X < 0:
    X *= -1
if X/D-X//D > 0.5:
    y = X//D + 1
else:
    y = X//D
if y > K:
    y = K
X -= y * D

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

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

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

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

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

print(ans)

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

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

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

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

x = abs(x)

n = x // d

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

k -= n

x = x % d

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

dist= (X/D-K)/2


if -dist > K :
    i = K
elif dist > 0 :
    i = 0
else :
    i = -round(dist)


print(abs(X + i*D - ((K-i)*D)))

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

tmp = X // D
d1 = X - (D * tmp)
d2 = X - (D * (tmp + 1))

if tmp >= K:
  ans = X - (D * K)
else:
  if (tmp % 2) ^ (K % 2) == 0:
    ans = d1
  else:
    ans = d2

print(abs(ans))

Solution (Python):







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

num = k*d

x = abs(x)
flag = 0

if x >= num:
    print(x - num)
else:
    if x > d:
        j = int(x / d)
    else:
        if x*2 > d:
            j = 1
            flag = 1
        else:
            j = 0

    if j == 0:
        b = x
    elif j == 1 and flag == 1:
        b = d - x
    else:
        b = x % (d*j)
    min = min(b, d - b)
    max = max(b, d - b)
    if min == d - b:
        j += 1

    if (k - j) % 2 == 0:
        print(min)
    else:
        print(max)

Solution (Python):
import math
N=input().split()
X=int(math.fabs(int(N[0])))
K=int(N[1])
D=int(N[2])
if X-K*D>=0:
    print(X-K*D)
else:
    syou=X//D
    x=X-syou*D
    y=X-(syou+1)*D
    if K%2==0:
        if syou%2==0:
            print(x)
        else:
            print(int(math.fabs(y)))
    else:
        if syou%2==0:
            print(int(math.fabs(y)))
        else:
            print(x)

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

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)
k1 = X // D
if k1 >= K:
    print(abs(X - D * K))
    exit()

k = K - k1
x = X % D

while k > 0:
    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()
    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

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



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

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

Solution (Python):
import sys
import math


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

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

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

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

Solution (Python):

from decimal import Decimal
X,K,D= map(Decimal, input().split())

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

Solution (Python):



import sys


def get_ints():
    return map(int, sys.stdin.readline().strip().split())


def main():
    x, k, d = get_ints()
    ax = abs(x)
    tf = k % 2
    if ax < abs(ax-d) and tf == 0:
        print(x)
    elif ax < abs(ax-d):
        print(abs(x-d))
    else:
        tmp_k, mod_d = divmod(ax, d)
        if tmp_k < k:
            ax -= d * tmp_k
            k -= tmp_k
            tf = k % 2
            if tf == 1:
                ax = abs(ax - d)
            print(ax)
        else:
            ax -= d * k
            print(ax)


if __name__ == "__main__":
    main()

Solution (Python):
X, K, D = map(int, input().split())
if 0 <= X:
    ans = X-D*K
    if 0 < ans:
        print(ans)
        exit(0)
if X < 0:
    ans = X+D*K
    if ans < 0:
        print(abs(ans))
        exit(0)

for i in range(K):
    if 0 <= X:
      absX = abs(X - D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X - D*(i+1))
              print(ans)
              exit(0)
    if X < 0:
      absX = abs(X + D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X + D*(i+1))
              print(ans)
              exit(0)

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

if x-k*d > 0:
    print(x-k*d)
    exit
elif x+k*d < 0:
    print(-1*(x+k*d))
    exit
else:
    m = 2 * math.ceil(((k-2)*d-x)/(2*d))- k
    print(min(abs(x+m*d), abs(x+(m+2)*d)))

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

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