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

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

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

else:
    tempx=x-(x//d)*d
    nokorik=k-(x//d)

    migi=tempx+d
    hidari=abs(tempx-d)

    ans1=tempx
    ans2=min(migi,hidari)

    if(nokorik %2 ==0):
        print(ans1)
    else:
        print(ans2)

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

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

if k != 0 and k % 2 == 1:
    b = abs(x-d)
    c = abs(x+d)
    x = min(b,c)

print(abs(x))

Solution (Python):

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

ans = 0
if abs(K * D) < abs(X):
    ans = (abs(X) - abs(K * D))
else:
    num = X//D 
    tmp = (abs(X) - abs(num * D))
    if X < 0:
        tmp *= (-1)
    
    if (K - num) % 2 == 1:  
        ans = min([abs(tmp - D), abs(tmp + D)])
    else:
        ans = abs(tmp)
        
print(ans)

Solution (Python):
a = list(input().split(" "))
x = int(a[0])
if x < 0:
    x *= -1
k = int(a[1])
d = int(a[2])
c = x // d

if c >= k:
    x -= d * k
else:
    x -= d * c
    if (k - c) % 2 == 1:
        x = (x - d) * -1

print(x)

Solution (Python):
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def Rtest(T):
    case,err=0,0
    for i in range(T):
        inp=INP()
        a1,ls=naive(*inp)
        a2=solve(*inp)
        if a1!=a2:
            print((a1,a2),inp)
            err+=1
        case+=1
    print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'
    
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase

read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()


show_flg=False
show_flg=True

ans=0


x,k,d=LI()
s=int((k*d-x)/(2*d))

ans=min(abs(x+k*d),abs(x-k*d))
for t in range(max(0,s-2),min(s+2,k)+1):
    ans=min(ans,abs(x+(2*t-k)*d))

print(ans)

Solution (Python):
import math
x, k, d = map(int, input().split())
if k * d <= abs(x):
    print(abs(x) - k * d)
    exit()
if x < 0:
    x = abs(x)
move = math.ceil(x/d)
x = x - d * move
k -= move
if k % 2 == 0:
    print(abs(x))
else:
    x = 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)
    else:
        print(d)
    exit()

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

if x % d > 0:

    y = x // d + 1
else:
    y = x // d

ans1 = x % d 
ans2 = d - ans1


if x % d == 0:
    if (k - y) % 2 == 1:
        
        print(ans2)
    else:
        print(ans1)
    exit()

if (k - y) % 2 == 1:

    print(ans1)
else:
    print(ans2)

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

    if X == 0:
        minCount = 0
    else:
        minCount = abs(X) // D
    
    if minCount >= K:
        print(min(abs(X - D * K), abs(X + D * K)))
        return

    absplusD = abs(X + D * minCount)
    absminusD = abs(X - D * minCount)
    if(min(absplusD, absminusD) == absminusD):
        X = X - D * minCount
    else:
        X = X + D * minCount
        
    K = K - minCount

    plusX = X + D
    minusX = X - D

    if min(abs(X), abs(plusX), abs(minusX)) == abs(X):
        if K % 2 == 0:
            print(abs(X))
        else:
            print(min(abs(plusX), abs(minusX)))

    else:
        if (K - 1) % 2 == 0:
            print(min(abs(plusX), abs(minusX)))
        else:
            print(abs(X))

if __name__ == "__main__":
    main()

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

x = abs(x)

moves = x//d

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

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

x=abs(x)
i=x//d

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

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


abs_n = abs(n)
min_move = abs_n // d 

if k <= min_move:
  if n < 0:
    ans = abs(n + k * d)
  elif n > 0:
    ans = n - k * d
else:
  if (k - min_move) % 2 == 0: 
    ans = abs_n % d 
  else:
    ans = d - abs_n % d 
print(ans)

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

if (__name__=='__main__'):
  main()

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

def main():
    X, K, D = map(int, input().rstrip().split())
    X = abs(X)
    c = X // D
    if c < K:   
        X = X % D
        K = K - c
        K = K % 2
    ans = X - (D * K)
    print(abs(ans))
if __name__ == "__main__":
    main()

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

Solution (Python):
X,D,C = map(int,input().split())
count=abs(X)
a = abs(X//C)
b = abs(X%C)
if a>D:
  print(count-D*C)
elif a==D:
  print(b)
elif a<D:
  D = D-a
  if D%2==0:
    print(b)
  else:
    print(min(abs(b-C),abs(b+C)))

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

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

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

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

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

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

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





idou = abs(x) // d

if idou > k:
  idou = k
  
minus = abs(x - d * idou)
plus = abs(x + d * idou)


if minus < plus:
  x -= d * idou
else:
  x += d * idou

nokori = (k - idou) % 2

minus = abs(x - d * nokori)
plus = abs(x + d * nokori)

if minus < plus:
  x -= d * nokori
else:
  x += d * nokori
  
print(abs(x))

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

xx = abs(xx)

if dd * kk <= xx:
    ans = xx - dd*kk
else:
    aa_int = int(xx/dd)
    remaining_move = kk - aa_int
    if (remaining_move % 2) == 0:
        ans = xx - dd*aa_int
    else:
        ans = xx - dd*(aa_int+1)

print(abs(ans))

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

X = int(XKD[0])
K = int(XKD[1])
D = int(XKD[2])
mini_X = X
count = 0
flag = 0

div_ans = abs(X) // D
if X < 0:
    if X + K*D <= 0:
        ans = X + K*D
        print(abs(ans))
        flag = 1
    else:
        pass
elif X > 0:
    if X -K*D >= 0:
        ans = X -K*D
        print(abs(ans))
        flag = 1
    else:
        pass
if flag == 0:
    K -= div_ans 
    X = abs(X) - div_ans*D 
    X_kari = abs(X - D)
    if X > X_kari:
        K -= 1
        if K % 2 == 0:
            ans = X_kari
        else:
            ans = X
    else: 
        if K % 2 == 0:
            ans = X
        else:
            ans = X_kari
    print(ans)

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

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

Solution (Python):
import math


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


Y = abs(X) // D

Mm = abs(X) - Y*D
Mp = (Y+1)*D - abs(X)

if (Mm <= Mp):
    M0 = Mm
    M1 = Mp
    Z = Y
else:
    M0 = Mp
    M1 = Mm
    Z = Y + 1

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

Solution (Python):


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

goal=X-K*D
y1=X%D
y2=y1-D

if y1<=goal:
    print(int(goal))
else:
    if y1<=abs(y2):
        L=(X-y1)/D
    else:
        L=(X-y2)/D
    if (K-L)%2==0:
        print(int(min(y1,abs(y2))))
    else:
        print(int(max(y1,abs(y2))))

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

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

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

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

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

if K % 2 == 1:
    K -= 1
    if abs(X - D) <= abs(X + D):
        X = X - D
    elif abs(X - D) > abs(X + D):
        X = X + D

if X == 0:
    print(0)

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

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

div = x // d
sur = x % d

ans = 0
if k > div:
	residual = k - div
	if residual % 2 == 0:
		ans = sur
	else:
		ans = d - sur
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 - d * k)
else:
    k -= x // d
    if k % 2 == 0:
        print(x % d)
    else:
        print(abs(x % d - d))

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

ideal_k = x // d
ideal_result = x - (ideal_k * d)

ideal_result_list = [ideal_result-d, ideal_result, ideal_result+d]
diff = [abs(each) for each in ideal_result_list]
i =  diff.index(min(diff))

ideal_result -= ((-i+1) * d)
ideal_k = abs((x - ideal_result) // d)




x = abs(x)

if k - ideal_k < 0:
    result = x - (k * d)
    result = x - (k * d)
elif (k - ideal_k) % 2 == 0:
    result = ideal_result
else:
    if ideal_result < 0:
        result = ideal_result + d
    else:
        result = ideal_result - d

print(abs(result))

Solution (Python):
import math
y, k, d = map(int, input().split())
if y >= 0:
    x = y
else:
    x = -y
i = math.floor(x / d)
if i >= k:
    p = x - k * d
elif i == x / d:
    if i + k == 2 * int((i + k) / 2):
        p = 0
    else:
        p = d
else:
    if (2 * i + 1) * d == 2 * x:
        p = x - i * d
    else:
        if i + k == 2 * int((i + k) / 2):
            p = x - i * d
        else:
            p = (i + 1) * d - x
print(int(p))

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

low = (x-k*d)//(2*d)
high = (x+k*d)//(2*d)
amari = (x+k*d)%(2*d)

if high < 0:
  print(abs(x+k*d))
elif low >= 0:
  print(abs(x-k*d))
else:
  print(min(abs(amari-2*d),abs(amari)))

Solution (Python):
import math
import sys

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

a = X - K * D
if a > 0:
    print(a)
    sys.exit()
if a + 2 * K * D < 0:
    print(abs(a + 2 * K * D))
    sys.exit()
else:
    b = a / (-2 * D)
    p = math.floor(b)
    q = math.ceil(b)
    r = -(a + 2 * D * p)
    s = a + 2 * D * q
    print(min(r,s))
    sys.exit()

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

if abs(a)>=abs(b*c):
    print(abs(abs(a)-abs(b*c)))
    exit()

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

Solution (Python):
import sys

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

X,K,D = LI()
X = abs(X)
sho = X // D
amari = X % D

if (sho>=K):
    saigo = X - (K*D)
    print(abs(saigo))
else:
    nokori = K - abs(sho)
    if nokori % 2 == 0:
        print(abs(amari))
    else:
        ans = min(amari+D,amari-D)
        print(abs(ans))

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

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

minc = x - d * k
maxc = x + d * k

if (maxc <= 0):
    ans = abs(maxc)
elif (minc >= 0):
    ans = minc
else:
    minc = abs(minc)
    d *= 2
    if (minc % d != 0):
        ans = minc % d
    else:
        ans = 0
    if (maxc % d != 0):
        ans = min(maxc % d, ans)
    else:
        ans = 0
        
print(ans)

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

if abs(X) >= K * D:
  print(abs(X) - K*D)
else:
  plus_times = abs(X)//D
  left_times = K - plus_times
  plus_min = abs(X) - (plus_times * D)
  minus_max = plus_min - D
  
  

  if left_times % 2 == 0:
    print(abs(plus_min))
  else:
    print(abs(minus_max))

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

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

    if k >= abs(x) // d:
        a = abs(x) - d * (abs(x) // d)
        b = abs(a - d)
        left_number = k - (abs(x) // d)
        if left_number % 2 == 0:
            minimum = a
        else:
            minimum = b

    else:
        minimum = abs(x) - k * d

    print(minimum)


if __name__ == '__main__':
    main()

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

Solution (Python):


x, k, d = map(int, input().split(' '))
if x == 0:
	if k&1:
		print(d)
	else:
		print("0")
elif x < 0:
	_max = x + k*d
	if _max <= 0:
		print(abs(_max))
	else:
		k -= (abs(x)//d)
		x += d*(abs(x)//d)
		if k > 0:
			nxt = x + d
			if k&1:
				print(abs(nxt))
			else:
				print(abs(x))
		else:
			print(abs(x))
else:
	_max = x - k*d
	if _max >= 0:
		print(abs(_max))
	else:
		k -= (abs(x)//d)
		x -= d*(abs(x)//d)
		if k > 0:
			nex = x - d
			if k&1:
				print(abs(nex))
			else:
				print(abs(x))
		else:
			print(abs(x))

Solution (Python):
def main():
    x, k, d = map(int, input().split())
    abs_x = abs(x)
    if abs_x == d:
        if k % 2 == 0:
            print(abs_x)
            return 0
        else:
            print(0)    
            return 0

    if d > abs_x:
        if k % 2 == 0:
            print(abs_x)
            return 0
        else:
            print(d-abs_x)    
            return 0
    
    s = abs_x // d

    if s <= k:
        if abs(abs_x - s*d) <= abs(abs_x - (s+1)*d):
            if (k-s) % 2 == 0:
                print(abs(abs_x - s*d))
            else:
                print(min(abs(abs_x - s*d + d), abs(abs_x - s*d - d)))

        elif abs(abs_x - s*d) > abs(abs_x - (s+1)*d):
            if (k-(s+1)) % 2 == 0:
                print(abs(abs_x - (s+1)*d))
            else:
                print(min(abs(abs_x - (s+1)*d + d), abs(abs_x - (s+1)*d - d)))

    else:
        print(min(abs(abs_x + k*d), abs(abs_x - k*d)))
if __name__ == '__main__':
    main()

Solution (Python):

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



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

  ans =x-d*k
  print(ans)

if x < 0:
  capa_iter = abs(-x // d)
  if capa_iter < k:
    if (k - capa_iter) % 2 == 0:
      print(abs(x + capa_iter*d))
    else:
      print(min([abs(x + capa_iter*d+d),abs(x + capa_iter*d -  d)]))
    exit()

  ans =abs(x+d*k)
  print(ans)

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

if x // d > k:
  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())
c=[]
k=K
if X<0:
  X=-X
if X-K*D>0:
  print(X-K*D)
else:
  a=X//D
  if a%2==K%2:
    print(min((X-D*a),abs(X-D*(a+2))))
  else:
    print(min((X-D*(a-1)),abs(X-D*(a+1))))

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


def print_ans(X, K, D):
    """Test Case
    >>> print_ans(6, 2, 4)
    2
    >>> print_ans(7, 4, 3)
    1
    >>> print_ans(10, 1, 2)
    8
    >>> print_ans(1000000000000000,1000000000000000,1000000000000000)
    1000000000000000
    """
    
    if K % 2 == 0 and D >= abs(X):
        print(abs(X))
    elif K % 2 != 0 and D >= abs(X):
        print(min(abs(X-D), abs(X+D)))
    elif abs(X) - D * K >= 0:
        print(abs(abs(X) - D * K))
    else:
        
        tmp, _ = divmod(abs(X), D)
        rest_K = K - tmp
        a = abs(abs(X) - tmp * D)
        b = abs(abs(X) - (tmp + 1) * D)
        if rest_K % 2 == 0:
            print(a)
        else:
            print(b)


if __name__ == '__main__':
    X, K, D = map(int, input().rstrip().split())
    print_ans(X, K, D)

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

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

Solution (Python):
import math
[X,K,D] = input().split()
X = int(X)
K = int(K)
D = int(D)
c = 0
N = min(math.floor(abs(X)/D),K)
if abs(abs(X) - N*D) > abs(abs(X) - (N-1)*D) :
    N = N - 1
if abs(X - N*D) < abs(X + N*D):
    Z = X - N*D
else:
    Z = X + N*D

if (K - N) % 2 == 1 :
    if abs(Z + D) < abs(Z - D) :
        Z = Z + D
    else:
        Z = Z - D


print(abs(Z))

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

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

else:

  y = abs(x)//d
  a = y + 1


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

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

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

elif (k - abs(x)//d)%2 == 0:
    print(abs(x)%d)
    
elif (k - abs(x)//d)%2 == 1:
    print(abs(abs(x)%d - d))

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

Solution (Python):
from math import floor

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

x = abs(x)

decim = x/d

if (decim > k):
    print(abs(x - k*d))
    exit(0)
else:
    if decim <= 1:
        if (k % 2 == 1):
            print(abs(x - d))
        else:
            print(abs(x))
    else:
        if k % 2 == 0:
            lower = int(decim) if ((int(decim) % 2) == 0) else int(decim) - 1
        else:
            lower = 2*floor(decim/2)-1
        upper = lower + 2
        print(min(abs(x - lower*d), abs(x - upper*d)))

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

if x - k*d >= 0:
    print(x - k*d)
else:
    m = x//d
    a = x - d*m
    b = d - a

    if (k-m) % 2:
        print(b)
    else:
        print(a)

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

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

x = abs(x)
y = x % d
l = x // d

m = k - l
if m <= 0:
    x -= d * k
else:
    if abs(y-d) < abs(y):
        y -= d
        m -= 1
        if m % 2 == 0:
            x = y
        else:
            x = y + d
    else:
        if m % 2 == 0:
            x = y
        else:
            x = y - d

print(abs(x))

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

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

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

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

if(x<0):
    x = -x

c = x%d
a = x//d

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

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

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

ans = 0

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

print(ans)

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

def binary_search():
    low = 0
    high = K

    while low <= high:
        mid  = (low + high)//2
        guess = mid
        if X + D*guess >= 0:
            if abs(X+D*guess) < D:
                return mid
            else:
                high = mid -1
        else:
            low = mid + 1
    return None

if X >= 0:
    X = -X

if X + D*K < 0:
    print(abs(X+D*K))
else:
    n = binary_search()
    if (K -n)%2 == 0:
        print(abs(X + D*n))
    else:
        print(abs(X + D*(n-1)))

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

ans = 0

if X < 0:
    X = -X

count = X // D

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

    if K == 0:
        ans = X
    else:
        ans = min(X, 2 * D - X)

print(ans)

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

i = x//d
def calc(t):
    return abs(x - t * d)
if k%2 ==0:
    if i%2==0:
        print(min(calc(i), calc(i+2)))
        exit(0)
    else:
        print(min(calc(i-1), calc(i + 1)))
        exit(0)
else:
    if i%2==0:
        print(min(calc(i-1), calc(i+1)))
        exit(0)
    else:
        print(min(calc(i), calc(i + 2)))
        exit(0)

Solution (Python):
L=list(map(int,input().split()))
X,K,D = abs(L[0]),L[1],L[2]
Y = X-K*D
W = (K-X//D)%2
if Y >= 0:
    Z = Y
elif Y < 0:
    if W == 1:
        Z = D - X % D
    else:
        Z = X % D
print(Z)

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

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

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

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

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

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

if y >= 0:
    print(y)
    exit()

while k > 0:
    if x >= 0:
        x -= d
    else:
        x += d

    if abs(x) >= abs(x_pre):
        if k % 2 == 0:
            print(abs(x_pre))
            exit()
        else:
            print(abs(abs(x_pre) - d))
            exit()
    else:
        x_pre = x
        k -= 1

print(abs(x_pre))

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

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

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

hu = False
if X < 0:
    X = abs(X)

idou_kaisu = X//D 
amari = X%D 

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

three_list = []

for i in range(K):
    three_list.append(X)
    
    if len(three_list) >= 4:
        three_list.pop(0)
    if len(three_list) >= 3:
        if three_list[0] == three_list[2]: 
            if (K-i) % 2 == 0: 
                print(abs(three_list[0]))
            else:
                print(abs(three_list[1]))
            exit()
    if X >= 0:
        X -= D
    else:
        X +=D
print(abs(X))

Solution (Python):
x,k,d=map(int,input().split())
n=min(max(round(-x/(2*d)+k/2),0),k)
print(abs(x+(2*n-k)*d))

Solution (Python):
X,K,D=map(int, input().split())
n=(K*D-X)//(2*D)

if n<0:
    ans=abs(X-K*D)
elif n>=K:
    ans=abs(X+K*D)
else:
    ans1=abs(X+(2*n-K)*D)
    ans2=abs(X+(2*(n+1)-K)*D)
    ans=min(ans1,ans2)
    
print(ans)

Solution (Python):

import math

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

cross = abs(-(-X//D))

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

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

print(ans)