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):
a,b,c=map(int,input().split())
a1=(abs(a)-0)%c
if(a==0):
    if(b%2==0):
        print(0)
    else:
        print(c)
if(a>0):
    a2=a1-c
    ma1=int(a/c)
    ma2=ma1+1
    a3=abs(a2)
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(a-(b*c))
if(a<0):
    a2=abs(a1-c)
    ma1=((a1-(2*a1))-(a))
    ma1=int(ma1/c)
    ma2=ma1+1
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(abs(a+(b*c)))

Solution (Python):
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    x = abs(x)

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




if __name__ == '__main__':
    main()

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

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

Solution (Python):










import sys
def _p(*_a):
  
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

def _pa(_aa):
  
  for _a in _aa: _p(_a)

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

X = abs(X)


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



a = X // D
k = K - a
x = X - (a * D)

if k % 2 == 0:
  ans = x
else:
  ans = x - D

print(abs(ans))

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())

X,K,D = MI()
X = abs(X)

q = X//D
r = X % D

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

print(r if (q-K) % 2 == 0 else D-r)

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

x = abs(x)

ans = 0

t = x // d
s = x % d

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

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
x,k,d=map(int,input().split())
if k%2==1:
  if x>0:
    x-=d
  else:
    x+=d
  k-=1

a=x%(2*d)
b=a-2*d
ans=abs(abs(x)-k*d)
if abs(x-a)//d<=k:
  ans=min(ans,abs(a))
if abs(x-b)//d<=k:
  ans=min(ans,abs(b))
print(ans)

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

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

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



if __name__ == "__main__":
    solve()

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

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

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 = (d*(a+1)) - x
print(ans)

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

t = abs(X) // D
s = abs(X) % D

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

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

if X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(abs(D))
    exit(0)
X = abs(X)
if X > 0:
    k = X - K * D
    if k >= 0:
        print(k)
    else:
        kouho1 = X % D
        kouho2 = abs(kouho1 - D)
        sho = X // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)

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

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

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

x = abs(x)

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

Solution (Python):
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
	exit(0)
t=decimal.Decimal(abs(x))//decimal.Decimal(d)
if (k<t):
	if (x>0):
		print(abs(x-k*d))
	else:
		print(abs(x+k*d))
else:
	if (x>0):
		x=x-t*d
		k-=t
		if k%2==0:
			print(abs(x))
		else:
			print(abs(x-d))
	else:
		x=x+t*d
		k-=t
		if k%2==0:
			print(abs(x))
		else:
			print(abs(x+d))

Solution (Python):



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

KOE=K%2

A=abs(X)//D
M=abs(X)%D



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

else :
    if A%2==KOE:
        print(M)
    else :
        print(min(abs(M+D),abs(M-D)))

Solution (Python):
def f(v, x, d, k):
  return x + d * (2 * v - k)


def swap(x, y):
  r = x
  x = y
  y = r


lt = input().split(' ')
x = int(lt[0])
k = int(lt[1])
d = int(lt[2])

lf = 1
r = k




ans = abs(f(0, x, d, k))


while (lf <= r):
  mid = (lf + r) // 2
  nt = f(mid, x, d, k)
  cur = f(mid - 1, x, d, k)
  
  
  
  
  
  
  
  nt = abs(nt)
  cur = abs(cur)
  if (nt > cur):
    ans = min(ans, abs(cur))
    r = mid - 1
  else:
    lf = mid + 1
    ans = min(ans, abs(nt))

print(abs(ans))

Solution (Python):
X,K,D = list(map(int, input().split()))
co,ans = divmod(abs(X),D)
if (K - co) > 0:
    if (K - co) % 2 == 0:
        pass
    else:
        ans = abs(ans) - D
else:
    ans = abs(X) - K*D

print(abs(ans))

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

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

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

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

print(ans)

Solution (Python):

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

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

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

X = abs(X)

m = X % D
l = X // D

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

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

Solution (Python):
a,b,c=map(int,input().split())
a=abs(a)
temp=a//c
if(temp>b):
    temp=temp-b
    if(a%c==0):
        if(temp>b):
            print(a-(b*c))
        else:
            print(temp*c)
    else:
        print(a-(b*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))

Solution (Python):



import sys
import math

def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))


def main():
    X, K, D = i2nn()
    absX = abs(X)
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    a, c = divmod(absX, D)
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return


main()

Solution (Python):

import sys
input = sys.stdin.readline



def main():
    x, k, d = [int(i) for i in input().strip().split()]
    div = x // d
    L = x - d * k
    R = x + d * k
    if L >= 0:
        print(L)
        return
    if R < 0:
        print(-R)
        return
    if k <= div:
        ans = x - (d * k)
    else:
        k = k - div
        even = x % d
        odd = abs(d - even)
        if k % 2 == 1:
            ans = odd
        else:
            ans = even
    print(ans)
    return


if __name__ == "__main__":
    main()

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

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

Solution (Python):



import sys
import math

def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))


def main():
    X, K, D = i2nn()
    absX = abs(X)
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    a, c = divmod(absX, D)
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return


main()

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

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

print(ans)

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

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

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

Solution (Python):



import sys
import math

def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))


def main():
    X, K, D = i2nn()
    absX = abs(X)
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    try:
        a, c = divmod(absX, D)
    except:
        print(X)
        return
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return


main()

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

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

a = x//d
xd = x%d
gk = [0, 0]
if a%2 == 1:
    gk[1] = xd
    gk[0] = abs(xd-d)
else:
    gk[1] = abs(xd-d)
    gk[0] = xd

if k%2 == 0:
    print(gk[0])
else:
    print(gk[1])

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

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

Solution (Python):

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

X=abs(X)
ii=int(X/D)
if K<=ii:
    X-=K*D
else:
    X-=ii*D
    if (K-ii)%2==1:
        X-=D

print(abs(X))

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

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

else:
    cent = x % d
    num = x // d

if d / 2 > cent and cent != 0:
    cent = d - cent
    num += 1

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

rem = k - num

if rem % 2 == 0:
    print(cent)
    exit(0)
else:
    print(d - cent)

Solution (Python):
import sys




x,k,d=map(int,input().split())
x=abs(x)
if(abs(x%d)<abs(x%d-d)):
  min_num=x%d
  min_num2=x%d-d
else:
  min_num=x%d-d
  min_num2=x%d

kaisuu=(x-min_num)/d
if(kaisuu>k):
  print(abs(x-d*k))
  sys.exit()
if((k%2==0 and kaisuu%2==0)or(k%2==1 and kaisuu%2==1)):
  print(abs(min_num))
else:
  print(abs(min_num2))

Solution (Python):
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    global x,k,d
    i = min(k,abs(x//d))
    if abs(x-d*i)<abs(x+d*i):
        x -= d*i
    else:
        x += d*i
    k -= i
    if k%2==1:
        if abs(x-d)<abs(x+d):
            print(abs(x-d))
        else:
            print(abs(x+d))
    else:
        print(abs(x))
    return

if __name__=='__main__':
    x,k,d = IL()
    solve()

Solution (Python):
import numpy as np

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

num = int(abs(x)/d)
ans = 0

if move <= num:
    if np.sign(x) == 1:
        ans = x-d*move
    else:
        ans = x+d*move

elif (move-num)%2==0:
    if np.sign(x) == 1:
        ans = x-d*num
    else:
        ans = x+d*num

elif (move-num)%2!=0:
    if np.sign(x) == 1:
        ans = x-d*(num+1)
    else:
        ans = x+d*(num+1)


print(abs(ans))

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

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

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

Solution (Python):
import math

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

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

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

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

if abs(x)<d :
    if k%2==0 :
        ans=abs(x)
    else :
        x1,x2=x-d,x+d
        ans=abs(x2)
        if abs(x1)<abs(x2) :
            ans=abs(x1)

else :
    if x>0 :
        X=x%d
        K=k-(x//d)
        if K%2==0 and K>0 :
            ans=abs(X)
        elif K%2==1 and K>0 :
            x3,x4=X-d,X+d
            ans=abs(x4)
            if abs(x3)<x4 :
                ans=abs(x3)
        else :
            ans=abs(x-k*d)

    else :
        Y=abs(x)%d
        K=k-(abs(x)//d)
        if K%2==0 and K>0 :
            ans=abs(-Y)
        elif K%2==1 and K>0 :
            y1,y2=-Y+d,-Y-d
            ans=abs(y1)
            if abs(y2)<y1 :
                ans=abs(y2)
        else :
            ans=abs(x+k*d)

print(ans)

Solution (Python):
def main():
    X, K, D = map(int, input().split())
    if abs(X) == D:
        if K % 2 == 0:
            print(abs(X))
        else:
            print(0)
        return

    diff = K * D - abs(X)
    if abs(diff) <= D:
        print(abs(diff))
        return
    elif diff < 0:
        print(abs(diff))
        return
    count_minus = abs(diff // (2 * D))
    if X > 0:
        dest = X - (K - count_minus) * D + count_minus * D
        dest2 = X - (K - count_minus - 1) * D + (count_minus + 1) * D
    else:
        dest = X + (K - count_minus) * D - count_minus * D
        dest2 = X + (K - count_minus - 1) * D - (count_minus + 1) * D

    print(abs(dest) if abs(dest) < abs(dest2) else abs(dest2))

if __name__ == '__main__':
    main()

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

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

alpha = X%D
count = K-(X//D)
if(alpha>abs(alpha-D)):
  count +=1
  alpha = abs(alpha-D)
  
beta = count%2
if beta==1:
  print(abs(alpha-D))
else:
  print(alpha)

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

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

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


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

Solution (Python):
X, K, D = map(int, input().split())
abs_x = abs(X)
hikukazu = abs(X) // D
count = K - (hikukazu)
if K > hikukazu:
    if count % 2 == 0:
        hikukazu = hikukazu
    else:
        hikukazu = hikukazu + 1
else:
    hikukazu = K



    

abs_x = abs_x - (D*hikukazu)
    

print(abs(abs_x))

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

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

k = round(abs(X) / D)

if k > K:
    if k == 1:
        print(abs(abs(X) - K * D))
    else:
        print(min(abs(abs(X) - K * D), abs(abs(X) - (K - 2) * D)))

else:
    if k % 2 == K % 2:
        print(min(abs(abs(X) - k * D), abs(abs(X) - (k + 2) * D), abs(abs(X) - (k - 2) * D)))

    else:
        print(min(abs(abs(X) - (k + 1) * D), abs(abs(X) - (k - 1) * D), abs(abs(X) - (k + 3) * D), abs(abs(X) - (k - 3) * D)))

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

if abs(X) > D:
    d = abs(X) // D
    y = abs(X) % D
    if d > K:
        a1 = abs(X) - (D * K)
        print(abs(a1))
        exit()

    a1 = abs(X) - (D * d)
    a2 = abs(X) - (D * (d+1))
    if abs(a1) < abs(a2):
        ans = a1
        yobi = a2
        K = K - d
    else:
        ans = a2
        yobi = a1
        K = K - (d+1)
    if K % 2 == 0:
        print(abs(ans))
    else:
        print(abs(yobi))
     
else:
    a1 = abs(X)
    a2 = abs(abs(X) - D)
    if a1 < a2:
        ans = a1
        yobi = a2
    else:
        ans = a2
        yobi = a1
        K = K - 1
    if K % 2 == 0:
        print(abs(ans))
    else:
        print(abs(yobi))

Solution (Python):


L = input().split()
LS = [int(i) for i in L]
X = abs(LS[0])
K = LS[1]
D = LS[2]

length = X % D
move = X // D

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

Solution (Python):
XKD=list(input().split(' '))
X=int(XKD[0])
K=int(XKD[1])
D=int(XKD[2])
AX=abs(X)

if AX>K*D:
    print(AX-K*D)
else:
    K2=AX//D
    if (K-K2)%2==0:
        P=AX-K2*D
        print(P)
    else:
        P=abs(AX-(K2+1)*D)
        print(P)

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

X = abs(X)
now = X
over_zero_step = 0
over_zero = 0
not_over_zero = now
flg = 0

if X < abs(K * D):
    q = X // D
    now = X - q * D
    now = abs(now)

    for i in range(q+1, K+1):
        nxt = now - D
        if nxt < 0:
            over_zero = nxt
            not_over_zero = now
            over_zero_step = i
            break
        now = nxt
else:
    remaining_step = 0
    nearest_point = X - K * D
    flg = 1

if flg == 0:
        
    if abs(over_zero) <= not_over_zero:
        nearest_point = over_zero
        nearest_point_step = over_zero_step
        next_nearest_point = not_over_zero
        remaining_step = K - nearest_point_step
    else:
        nearest_point = not_over_zero
        nearest_point_step = over_zero_step - 1
        next_nearest_point = over_zero
        remaining_step = K - nearest_point_step


if remaining_step % 2 == 0:
    print(abs(nearest_point))
else:
    print(abs(next_nearest_point))

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

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

print(ans)

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


if __name__ == '__main__':
    Qc()

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

Solution (Python):
param_x, param_k, param_d = map(int, input().split())

param_x_tmp = param_x

if param_x > 0:
  int_times_nega_posi = -1
else:
  int_times_nega_posi =  1
  
if (param_x*int_times_nega_posi*-1) - (param_k*param_d) > 0:
  print((param_x*int_times_nega_posi*-1) - (param_k*param_d))
  
else:
  int_time_before_cross_0 = int((param_x*int_times_nega_posi*-1) / param_d)
  
  
  param_x_tmp = param_x + int_time_before_cross_0*param_d*int_times_nega_posi
  
  
  int_move = param_k - int_time_before_cross_0 
  int_move_devide_2_no_amari = int_move % 2
  print(abs(param_x_tmp + param_d*int_move_devide_2_no_amari*int_times_nega_posi))

Solution (Python):

import sys


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

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

    if K%2 == 1:
        X -= D
        ans = min(abs(X), abs(X+2*D))
    else:
        ans = min(X, abs(X - K * D))

    Kh = K // 2

    N = X // (D*2)
    ans = min(ans, abs(X - D * 2 * (N - 1)))
    ans = min(ans, abs(X - D * 2 * N))
    if N < Kh:
        ans = min(ans, abs(X - D * 2 * (N + 1)))

    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    K = int(next(tokens))  
    D = int(next(tokens))  
    solve(X, K, D)

if __name__ == '__main__':
    main()

Solution (Python):
xx, kk, d = map(int, input().split())


def do_solve(x, k):
    if -d == x == d:
        return 0 if k % 2 != 0 else x
    elif -d < x < d:
        return x if k % 2 == 0 else (x - d if x > 0 else x + d)

    ax = abs(xx) // d
    dd = d * ax
    if x > 0:
        return do_solve(x - dd, k - ax)
    else:
        return do_solve(x + dd, k - ax)

    
    
    
    


def solve():
    if xx == 0:
        return d

    ax = abs(xx) // d
    if kk < ax:
        if xx > 0:
            return xx - d * kk
        else:
            return xx + d * kk
        

    return do_solve(xx, kk)


print(abs(solve()))

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

if X < 0:
    X *= -1

quotient = X // D

if quotient >= K:
    dis = X - K * D
else:
    remainder = X % D
    if remainder < D - remainder:
        dis = remainder
        if (K - quotient) % 2 != 0:
            dis = D-remainder
    else:
        dis = D - remainder
        if (K - quotient) % 2 == 0:
            dis = remainder

print(dis)

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

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

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

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

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

l=K-kk

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

Solution (Python):
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations


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())
n = (K-X//D)//2

if X - K*D > 0:
    ans = X-K*D
elif X + D*K < 0:
    ans = abs(X+D*K)
else:
    ans = float('inf')
    for i in range(-1, 2):
        res = X + D*(2*(n+i)-K)
        ans = min(ans, abs(res))
        

print(ans)

Solution (Python):
lst = input().split()

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

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

if a < 0:
   a = 0

if a > K:
   a = K

def f(x):
   return X - D * K + 2 * D * x

lst2 = [abs(f(a))]

if a + 1 <= K:
   lst2.append(abs(f(a+1)))

if 0 < a:
   lst2.append(abs(f(a-1)))

result = min(lst2)

print(result)

Solution (Python):


from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

ans = None
if x > d:
    q, r = divmod(x, d)
    if k <= q:
        ans = x - k * d
    else:
        y = x - q * d
        if (k - q) % 2 == 0:
            ans = y
        else:
            ans = min(abs(y + d), abs(y - d))
else:
    if k % 2 == 0:
        ans = x
    else:
        ans = abs(x - d)

print(ans)

Solution (Python):
import math
def func():

    i1=list(map(int,input().split()))
    
    
    
    a=abs(i1[0])
    b=i1[1]
    c=i1[2]

    l=math.floor(a/c)
    m=b-l
    n=a-c*l
    if l>=b: z=a-c*b
    else:
        z=a-(c*l)
        if m%2==1:
            z=z-c
    print(abs(z))

func()

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

r_div = abs(X) // D
r_mod = abs(X) % D

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

print(ans)

Solution (Python):
X, K, D = input().split()
X, K, D = int(X), int(K), int(D)
count = 0
if X < 0:
  X = -X
  L = X
else:
  L = X
if K < L // D:
  L = L - K * D
  K = 0
elif K == L // D:

  L = L % D
  K = 0
else:

  temp = L
  L = L % D - D
  K = K - temp // D - 1
if L == 0:
  if K % 2 == 0:
    print(0)
    exit()
  else:
    print(D)
    exit()
if K == 0:
  print(abs(L))
  exit()
if abs(L) >= L + D:
  goal = L + D
  if K % 2 != 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()
elif abs(L) < L + D:
  goal = abs(L)
  if K % 2 == 0:
    print(abs(L))
    exit()
  else:
    print(L + D)
    exit()

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

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

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