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

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

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

if x>=k*d:
    print(x-k*d)
else:
    c=x//d
    x_r=x-d*c
    x_l=x-d*(c+1)
    k=k-c
    if k%2==1:
        print(abs(x_l))
    else:
        print(x_r)

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


X = abs(X)


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

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

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

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

diff = abs(X)

if K * D <= diff:
    if X >= 0:
        print(X - D * K)
    else:
        print(-1 * (X + D * K))

else:
    mod = diff // D
    if X >= 0:
        plus = X - D * mod
        minus = plus - D
        if (K - mod) % 2 == 0:
            print(plus)
        else:
            print(-minus)
    else:
        minus = X + D * mod
        plus = minus + D
        if (K - mod) % 2 == 0:
            print(-minus)
        else:
            print(plus)

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

X = abs(X)

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

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

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

print(ans)

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


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


    else:
        print(abs(x-math.floor(x/d+1)*d))




else:
    print(x-k*d)

Solution (Python):
x, k, d = [int(z) for z in input().strip().split()]
if k * d < 0:
    print(y)
step = int(abs(x) / d)

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

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

Solution (Python):


def main(x, k, d):
    q, r = divmod(x, d)

    h = 0
    if -(r - d) < r:
        h = d - r
    else:
        h = -r
    q = (x + h) // d

    if k < abs(q):
        if x < 0:
            return -(x + k * d)
        else:
            return x - k * d

    rk = k - q
    if rk % 2 == 0:
        return abs(h)
    else:
        if 0 < h:
            return abs(h - d)
        else:
            return abs(h + d)


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

Solution (Python):




import math
X, K, D = list(map(int, input().split()))
flag = 0

step = math.floor(abs(X)/D)
if abs(abs(X) - D *step) < abs(abs(X)-D * (step + 1)):
    step += 1
    flag = 1

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

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

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

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

Solution (Python):
x,k,d=map(int,input().split())
print(max(abs(x)-k*d,(x%d,d-x%d)[k-x//d&1]))

Solution (Python):
def li():
    return [int(x) for x in input().split()]

x, k, d = li()

x = abs(x)

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

xr = x % d
xl = xr - d

r_parity = (x - xr) // d % 2
k_parity = k % 2
if r_parity == k_parity:
    print(xr)
else:
    print(abs(xl))

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

if x - k * d >= 0:
    print(x - k * d)
elif x + k * d <= 0:
    print(abs(x + k * d))
else:
    dis = x % d
    dis_p = d - dis
    t = k - x // d

    if t % 2 == 0:
        print(dis)
    else:
        print(dis_p)

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

if abs(x) >= k * d:
    print(abs(x) - k * d)
else:
    q, mod = divmod(x, d)
    if (k - q) % 2 == 0:
        print(mod)
    else:
        print(abs(mod - d))

Solution (Python):

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

x = abs(X)
k = int(math.floor(x/D))
if k>=K:
    print(abs(x - K*D))
    exit()
x -= k*D
if (K-k)%2==0:
    print(abs(x))
else:
    print(abs(x - D))

Solution (Python):
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

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

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

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

X = abs(X)

ans = 0

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

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


if abs(x) >= k * d:
    print(abs(x) - k * d)
else:
    q, mod = divmod(x, d)
    k -= q
    if k % 2 == 0:
        print(mod)
    else:
        print(abs(mod - d))

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

cnt = X // D
ans1 = X - cnt * D
ans2 = abs(ans1 - D)

if cnt < K:
  if (K - cnt)%2 == 0:
    print(ans1)
  else:
    print(ans2)
else:
    print(X - K * D)

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


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

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

x, cnt = abs(x), abs(x) // d
if k % 2 == cnt % 2:
    ans = abs(x - d * min(k, cnt))
elif k % 2 != cnt % 2:
    ans = abs(x - d * min(k, cnt + 1))
print(ans)

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

if x >= 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))
else:
    if abs(x) >= k * d:
        print(abs(x) - k * d)
    else:
        q, mod = divmod(abs(x), d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))

Solution (Python):

def resolve():
    X, K, D = map(int, input().split())
    x = abs(X)
    cnt = x // D

    if K < cnt:
        ans = x - (K * D)
    else:
        ans = x - (cnt * D)
        if (K - cnt) % 2:
            ans -= D
    print(abs(ans))


if __name__ == "__main__":
    resolve()

Solution (Python):

XKD=input().split(" ")
X=int(XKD[0])
K=int(XKD[1])
D=int(XKD[2])

X=abs(X)

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

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

x, cnt = abs(x), abs(x) // d
ans = abs(x - d * min(k, cnt + (k % 2 != cnt % 2)))
print(ans)

Solution (Python):
from math import *
import sys,bisect,copyreg,copy,statistics,os
def inp(): return sys.stdin.readline().strip()
def IIX(): return (int(x) for x in sys.stdin.readline().split())
def II(): return (int(inp()))
def LI(): return list(map(int, inp().split()))
def LS(): return list(map(str, inp().split()))
def L(x):return list(x)
def out(var): return sys.stdout.write(str(var))


x,k,d=LI()

x=abs(x)

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

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

x, cnt = abs(x), abs(x) // d
if k % 2 == cnt % 2:
    ans = abs(x - d * min(k,cnt))
elif k % 2 != cnt % 2:
    ans = abs(x - d * min(k,(cnt + 1)))
print(ans)

Solution (Python):
import numpy as np
X, K, D = (list(map(int, input('').split(' '))))
X = np.abs(X)

if np.abs(X)/D <= K:
    new_K = K - int(X/D)
    new_X = X - D*int(X/D)
    if new_K%2==0:
        print(new_X)
    else:
        print(np.abs(new_X-D))
else:
    print(np.abs(X-D*K))

Solution (Python):
list = list(map(int, input().split())) 
hajime=list[0]
kaisuu=list[1]
hohaba=list[2]
if hajime>=0:
  ayumi=hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime-hohaba*kaisuu
  kouho_2=hajime-hohaba*ayumi
  kouho_3=hajime-hohaba*(ayumi+1)
 
  
  
  if kaisuu <= ayumi:
    print(kouho_1)
  else:
    if nokori%2==0:
      print(kouho_2)
    else:
      print(-kouho_3)
      
if hajime<0:  
  ayumi=-hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime+hohaba*kaisuu
  kouho_2=hajime+hohaba*ayumi
  kouho_3=hajime+hohaba*(ayumi+1)
  if kaisuu<=ayumi:
    
    print(-kouho_1)  
  else:
    if nokori%2==0:
      print(-kouho_2)
    else:
      print(kouho_3)

Solution (Python):
x,k,d=map(int,input().split())
x=abs(x)
div,mod=divmod(x,d)
C=k-div
if C<=0:print(x-d*k)
else:
    if C%2==1:print(d-mod)
    else:print(mod)

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






if x >= 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))
elif x < 0:
    if abs(x) >= k * d:
        print(abs(x + k * d))
    else:
        q, mod = divmod(abs(x), d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))

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

if x == 0:
    if k % 2 == 0:
        print(0)
    else:
        print(d)
elif x > 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))
elif x < 0:
    if abs(x) >= k * d:
        print(abs(x + k * d))
    else:
        q, mod = divmod(abs(x), d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))

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

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

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((k-check)%2==0):
        print(ans1)
    else:
        print(abs(ans2))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

a = min((x // d),k)
k -= a

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

Solution (Python):
meta = [int(s) for s in input().split(' ')]

now = meta[0]
times = meta[1]
distance = meta[2]

time = times
i = 0
while i < times:
    
    if time > 0:
        
        time = min([int(abs(now) / distance), times])
        
        i += time
    
    else:
        
        if (times - i) % 2 == 0:
            print(abs(now))
            exit()
        
        else:
            time = 1
            i = times

    
    if now > 0:
        now -= distance * time
    else:
        now += distance * time

print(abs(now))

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
div, mod = divmod(x, d)
c = k - div
if c <= 0:
    ans = x - d * k
else:
    if c%2 == 1: ans = d - mod
    else: ans = mod
print(ans)

Solution (Python):
import sys
x,k,d=map(int,input().split())
if x + k*d < 0:
    print(abs(x+k*d))
    sys.exit()
elif x - k*d > 0:
    print(abs(x- k*d))
    sys.exit()
else:
    n0 = -x//d
    ans = [n0-3,n0-2,n0-1,n0,n0+1,n0+2,n0+3]
    if k%2==0:
        ans = [i for i in ans if i%2==0]
    else:
        ans = [i for i in ans if i%2!=0]
    ans = [abs(x+i*d) for i in ans]
print(min(ans))

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

X = abs(X)
temp = X // D
if K%2 != temp%2:
    temp = temp + 1

if temp > K:
    temp = K

temp = D * temp

print(abs(X - temp))

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


N = abs(int(X / D))
if N >= K:
    print(min( abs(X - D * K), abs(X + D * K) ))
    sys.exit()

if X < 0:
    X = X + D * N
    ans1 = abs(X)
    ans2 = abs(X + D)
    if (K - N) % 2 == 0:
        print(ans1)
    else:
        print(ans2)

else:
    X = X - D * N
    ans1 = abs(X)
    ans2 = abs(X - D)
    if (K - N) % 2 == 0:
        print(ans1)
    else:
        print(ans2)

Solution (Python):
meta = [int(s) for s in input().split(' ')]

now = meta[0]
times = meta[1]
distance = meta[2]

out = 0
i = 0
while i < times:
    
    
    time = min([int(abs(now) / distance), times])
    
    i += time

    
    if time == 0:
        
        if (times - i) % 2 == 0:
            print(abs(now))
            exit()
        
        else:
            time = 1
            i = times


    
    if now > 0:
        now -= distance * time
    else:
        now += distance * time
    


print(abs(now))

Solution (Python):
def resolve():
	import math
	X, K, D = map(int, input().split())
	X, K, D = abs(X), abs(K), abs(D)
	n = math.floor(X/D)
	A = X - n*D
	k = K - n
	if k < 0:
		print(X - (K*D))
	elif k%2 == 0:
		print(A)
	else:
		print(abs(A-D))
resolve()

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

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

k -= cnt
x = abs(x) - cnt*d

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

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

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

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

if K < cnt:
    ans = x - (K * D)
else:
    ans = x - (cnt * D)
    if (K - cnt) % 2 != 0:
        ans -= D
print(abs(ans))

Solution (Python):

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
 


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


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

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

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

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

result = 0

abs_x = abs(x)
firstNearestIterNum = abs_x // d

if firstNearestIterNum >= k:
	result = abs_x - (d * k)
else:
	if (k - firstNearestIterNum) % 2 == 1:
		result = abs_x - (d * (firstNearestIterNum + 1))
	else:
		result = abs_x - d * firstNearestIterNum

print(abs(result))

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect

INF = 10**9+7

if __name__ == "__main__":
    x,k,d = map(int,input().split())
    x = abs(x)
    y = x//d
    x -= min(y,k)*d
    k -= min(y,k)
    if k%2 == 0:
        print(x)
    else:
        print(abs(x-d))

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

Solution (Python):


import math

if __name__ == '__main__':
    l = list(map(int, input().split()))
    X = int(l[0])
    K = int(l[1])
    D = int(l[2])
    
    if D*K <= abs(X):
        minVal=abs(X) - D*K
    else:
        n=math.floor(abs(X)/D)+1
        
        
        
        if (K-n)%2==0:
            minVal = abs(abs(X) - D*n)
        else:
            minVal = abs(abs(X) - D*(n-1))
        
    print(minVal)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

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

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


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

Solution (Python):
nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]


current = abs(current)

res = current // move

if res >= num:
    print(current - move*num)
else:
    current = current - move * res
    num -= res
    if num%2 == 0:
        print(current)
    else:
        print(abs(current-move))

Solution (Python):
x, k, d = map(int, input().split())
abs_x = abs(x)
kd = k * d 
ans = 0
if abs_x > kd:
  ans = abs_x - kd
else:
  div, mod = divmod(abs_x, d)
  ans = abs(mod - (k - div) % 2 * d)

print(ans)

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

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

if X > K*D:
    ans = X - K*D

else:
    time = X//D + 1
    K = K - time
    X = X - time*D
    if K % 2 == 0:
        ans = abs(X)
    if K % 2 != 0:
        ans = X+D

print(ans)

Solution (Python):
x,k,d=map(int,input().split())
print(max(abs(x)-k*d,(x%d,d-x%d)[k-x//d&1]))

Solution (Python):
import sys


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


def task(X, K, D):
    if_steps = abs(X) / D
    if K <= if_steps:
        return abs(X) - K * D
    over_steps_delta = (K - if_steps) % 2
    over_steps = round(if_steps + over_steps_delta)
    below_steps = over_steps - 2
    dist_a = abs(abs(X) - over_steps * D)
    dist = dist_a
    if below_steps >= 0:
        dist_b = abs(abs(X) - below_steps * D)
        if dist_b < dist:
            dist = dist_b
    return dist


def main():
    [X, K, D] = [int(s) for s in read_input_line().split(' ')]
    result = task(X, K, D)
    print(result)


main()

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

Solution (Python):
from sys import stdin


def f(x, k, d):
    x = abs(x)
    if k == 0:
        return abs(x)

    tmp = x // d

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


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(f(X, K, D))

Solution (Python):
X,K,D = (abs(int(T)) for T in input().split())
if K<=(X//D):
    print(X-K*D)
else:
    Count = K-(X//D)
    if Count%2==0:
        print(X-(X//D)*D)
    else:
        print((X//D+1)*D-X)

Solution (Python):




import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """6 2 4"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """7 4 3"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 1 2"""
        output = """8"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(input, output)


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


if __name__ == "__main__":
    
    resolve()

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

X = abs(X)
num1 = min(X // D, K)
X -= D * num1
K -= num1

if K % 2 == 1:
	X = D - X

print(X)

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

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

if mx<=K:
    x = X-(mx-1)*D
    count -= mx-1
else:
    x = X-(K-1)*D
    count -= K-1

ans = abs(x)
while x>0 and count>0:
    x -= D
    count -= 1
    if abs(x)<ans and count%2==0:
        ans = abs(x)

print(ans)

Solution (Python):
from sys import stdin


def f(x, k, d):
    if k == 0:
        return x

    absX = abs(x)
    tmp = absX // d

    if tmp > k:
        return absX - d * k
    else:
        absX = absX - d * tmp
        k = k - tmp
        if k % 2 == 0:
            return absX
        else:
            return abs(absX - d)


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(f(X, K, D))

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

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

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

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

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)
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,n,d=map(int, input().split())
x=abs(x)
opt=x//d
if (n-opt)%2==0 and opt<n:
  print(x-(d*opt))
elif opt>=n:
  print(x-(d*n))
else:
  a=x-(d*opt)
  a=d-a
  print(a)

Solution (Python):
from sys import exit, stdin




input = stdin.readline

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

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

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

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

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

if x>=i[1]*i[2]:
    print(x-i[1]*i[2])
else:
    if (i[1]-x//i[2])%2 == 0:
        print(x%i[2])
    else:
        print(i[2]-x%i[2])

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

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

Solution (Python):



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

X=abs(X)

A=X//D
M=X%D

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

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

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

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd


x,k,d=map(int,input().split())
if x<0 and x+d*k<=0:
    print(abs(x+d*k))
elif x>0 and x-d*k>=0:
    print(x-d*k)
elif x<0:
    i=math.ceil((-1*x)/d)-1
    if (k-(i+1))%2==0:
        print(x+d*(i+1))
    else:
        print(abs(x+d*i))
elif x>0:
    i=math.ceil(x/d)-1
    if (k-(i+1))%2==0:
        print(abs(x-d*(i+1)))
    else:
        print(x-d*i)
elif x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

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

if(x < 0):
  i = int(abs(x)/d)
  if(k > i):
    k = k - i
    x = x + d*i
  else:
    x = x + d*k
    k = 0
  if(abs(x) > abs(x - d)):
    k += 1
    x = x - d
elif(x > 0):
  i = int(abs(x)/d)
  if(k > i):
    k = k - i
    x = x - d*i
  else:
    x = x - d*k
    k = 0
  if(abs(x) > abs(x + d)):
    k += 1
    x = x + d

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

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

print(ans)

Solution (Python):
import numpy as np

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

X = int(X)
if X < 0:
    X = -X
K = int(K)
D = int(D)



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

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

Solution (Python):
[X,K,D]=[int(_) for _ in input().split()]
X=abs(X)
Y=X//D
if Y <= K:
  X-=Y*D
  if (K-Y)%2 != 0:
    X = abs(X-D) if abs(X-D) < abs(X+D) else abs(X+D)
else:
  X-=K*D
print(X)

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

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