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

Solution (Python):
import os

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

x = abs(x)

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

Solution (Python):
import math

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

KL = (X+K*D)/(2*D)

if 0 <= KL < K:
    ans1 = X - math.floor(KL)*D + (K-math.floor(KL))*D
    ans2 = X - math.ceil(KL)*D + (K-math.ceil(KL))*D
    if abs(ans1) < abs(ans2):
        print(abs(ans1))
    else:
        print(abs(ans2))
elif 0 > KL:
    print(abs(X+K*D))
else:
    print(abs(X-K*D))

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

num = abs(X)//D
if num >= K:
    X = abs(X) - K*D

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

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

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

print(abs(ans))

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


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

elif x < 0:
    neededmove = abs(x//d)
    amari = x%d
    if abs(amari + d) < abs(amari):
        amari = abs(amari + d)
        neededmove += 1
    if k < neededmove:
        print(abs(x+k*d))
    elif k == neededmove:
        print(abs(amari))
    else:
        hoge = k - neededmove
        if hoge % 2 == 0:
            print(abs(amari))
        else:
            if amari <= 0:
                print(abs(amari + d))
            else:
                print(abs(amari-d))

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

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):
def main():
    x,k,d=map(int,input().split())
    x = abs(x)
    c = x//d
    if k < c:
        print(x-k*d)
    else:
        x = x%d
        k -= c
        x = [x, x-d]
        print(abs(x[k%2]))

if __name__ == '__main__':
    main()

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

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

count = abs(X) // D


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

if X < 0:
    print(abs(X + (K * D)))
else:
    print(X - (K * D))

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)

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

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

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

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

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

left = k - (abs(x) // d)

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

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

shoki = X // D
if X >= K * D:
  print(abs(X - K * D))
elif (K-shoki) % 2 == 1:
  ans = X - (D * shoki) - D
  print(abs(ans))
else:
  ans = X - (D * shoki)
  print(abs(ans))

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

sho = abs(X)//D
amari = abs(X) % D

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

if sho % 2 == K % 2:
    print(amari)
else:
    print(D - amari)

Solution (Python):
import sys
import bisect as bi
import math
from collections import defaultdict as dd
input=sys.stdin.readline

def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
for _ in range(1):
    x,k,d=cin()
    x=abs(x)
    jumps=x//d
    des1=x%d
    if(k<=jumps):
        ans=x-k*d
    else:
        if((k-jumps)%2==0):
            ans=des1
        else:
            ans=d-des1
    print(ans)

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

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

K -= X//D

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

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

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

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

X = abs(X)

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

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

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

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

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

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

X = abs(X)

tmp = int(X/D)

if tmp <= K:
    X -= tmp*D
    K -= tmp

    tmp = K % 2

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

print(X)

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

N = abs(X)//D

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

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

x=abs(x)

saisho=x//d

if(k<saisho):
    print(x-k*d)
else:
    nokori=k-saisho
    if(nokori%2==0):
        print(x-saisho*d)
    else:
        print(d*(saisho+1)-x)

Solution (Python):
import sys
x, k, d = [int(i) for i in input().split(" ")]
if x < 0:
    x *= -1
if x-k*d >= 0:
    print(x-k*d)
    sys.exit(0)
k -= (x//d)
x = x%d
k %= 2
if k == 0:
    print(x)
else:
    print(abs(x-d))

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

x=abs(x)
a = x//d
b = x%d

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

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

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

X = abs(X)

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

mov = X // D    

if abs(X-mov*D) > abs(X-(mov+1)*D):
    mov += 1

rem = abs(X-mov*D)
mov = K - mov   
mov = mov % 2

print(abs(rem-mov*D))

Solution (Python):



global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    
    X, K, D = list(map(int, lines[0].split()))
    
    
    
    
    
    
    
    

    
    all_plus = X + D * K
    
    all_minus = X - D * K

    
    if all_plus * all_minus >= 0:
        result = min(abs(all_plus), abs(all_minus))
        return [result]

    
    q, mod = divmod(X, D*2)

    

    
    if K % 2 == 0:
        values = list()
        values.append(abs(mod - D*2))
        values.append(abs(mod))
        values.append(abs(mod + D*2))
        result = min(values)

    
    else:
        values = list()
        values.append(abs(mod - D*3))
        values.append(abs(mod - D*1))
        values.append(abs(mod + D*1))
        values.append(abs(mod + D*3))
        result = min(values)

    return [result]



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['6 2 4']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['7 4 3']
        lines_export = [1]
    if pattern == 3:
        lines_input = ['10 1 2']
        lines_export = [8]
    if pattern == 4:
        lines_input = ['1000000000000000 1000000000000000 1000000000000000']
        lines_export = [1000000000000000]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

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

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

Solution (Python):
X,K,D=list(map(int,input().split()))
tmp=abs(X)//D
if tmp>=K:
   if X>0:
      X-=K*D
   else:
      X+=K*D
   print(abs(X))
   exit()
else:
   K-=tmp
   if X>0:
      X-=tmp*D
   else:
      X+=tmp*D
   if K%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())

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

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

now = abs(X)
move_count = K
move_size = D

moved_count, surplus = divmod(now, D)

if move_count - moved_count < 0:
    now -= move_count * move_size
    move_count = 0
else:
    move_count -= moved_count
    now = surplus

move_count %= 2
if move_count == 0:
    print(abs(now))
    exit()

if now <= 0:
    now += move_size
elif now > 0:
    now -= move_size
print(abs(now))

Solution (Python):
x, k, d = [int(i) for i in input().split()]
m = abs(x) // d
sgn = 1 if x >= 0 else -1
if k <= m:
    result = x - sgn * k * d
else:
    excess = k - m
    moves = m + (excess % 2)
    result = x - sgn * moves * d
print(abs(result))

Solution (Python):





import itertools

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

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

if res > 0:
    res -= saigoni_hituyo * d
else:
    res += saigoni_hituyo * d

print(abs(res))

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

first_step = abs(x // d)
rem_k = k - first_step

now = x
if k < first_step:
    if x < 0:
        now = x + (d * k)
    else:
        now = x - (d * k)
else:
    if x < 0:
        now = x + (d * first_step)
        if rem_k % 2 == 1:
            if now < 0:
                now += d
            else:
                now -= d
    else:
        now = x - (d * first_step)
        if rem_k % 2 == 1:
            if now < 0:
                now += d
            else:
                now -= d

print(abs(now))

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

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


print(ans)

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

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

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

Solution (Python):


import os,sys
from io import BytesIO, IOBase

def main():
    x,k,d = map(int,input().split())
    if x < 0:
        y = x+k*d
    else:
        y = x-k*d
    if x==0:
        if k%2:
            print(d)
        else:
            print(0)
    elif y*x >= 0:
        print(abs(y))
    else:
        z = abs(x)%d
        z1 = d-abs(x)%d
        if ((abs(x)-z)//d)%2 == k%2:
            print(z)
        else:
            print(z1)


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

if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):


import copy
import random
import bisect 
import fractions 
import math
import sys
import collections
from decimal import Decimal 

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

X, K, D = LI()

"""
X 
K回数 D移動
"""

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

else:
    ans = tmp

print(ans)

Solution (Python):


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




if X == 0:
    ans = [D, 0]

elif X>0:
    if X == D:
        ans = [0, D]
    elif X-D<0:
        ans = [D-X, X]
    else:
        if X-D*K>=0:
            print(X-D*K)
            exit()
        else:
            plus = X%D
            minus = D-X%D
            if (X//D)%2 == 1:
                ans = [plus, minus]
            else:
                ans = [minus, plus]

else:
    T = -1*X
    if T == D:
        ans = [0, T]
    elif T-D<0:
        ans = [D-T, T]
    else:
        if T-D*K>=0:
            print(T-D*K)
            exit()
        else:
            plus = T%D
            minus = D-T%D
            if (T//D)%2 == 1:
                ans = [plus, minus]
            else:
                ans = [minus, plus]

if K%2 == 1:
    print(ans[0])
else:
    print(ans[1])

Solution (Python):
def solve():
    x, k, d = map(int, input().split())
    x = abs(x)
    ref, a = divmod(x, d)
    if k < ref:
        print(x - k * d)
    elif ref % 2 == k % 2:
        print(a)
    else:
        print(d - a)

solve()

Solution (Python):
import math

x,k,d=map(int, input().split())
if abs(x) > k*d:
    Ans = abs(x)-k*d
    print(Ans)
else:
    move = math.floor(abs(x) / d)
    oufuku = k - move
    if oufuku % 2 == 0:
        Ans =abs(abs(x)-d*move)
    else:
        Ans=abs(abs(x)-d*(move+1))
    print(Ans)

Solution (Python):

from decimal import Decimal
x, k, d = map(Decimal, input().split())

def solve(x, k, d):
    if x < 0:
        x = -x

    if x > k*d:
        return x - k*d

    x, k = x - d*(x//d), k - x//d
    if k%2 == 0:
        return x
    else:
        return d - x

print(solve(x, k, d))

Solution (Python):
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))

def main():
    x, k, d = ril()
    a = 2 * d
    b = - x + k * d
    f = lambda i : a * i - b
    if b <= 0:
        ans = -b
    elif a * k < b:
        ans = -f(k)

    elif b % a == 0:
        ans = 0
    else:
        x1 = b // a
        x2 = x1 + 1
        ans = min(-f(x1), f(x2))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    x,k,d = map(int, input().split())
    x = abs(x)
    n = x//d
    ans = x
    if n >= k:
        print(x-k*d)
        exit()

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

if __name__=="__main__":
    main()

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

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

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

a = abs(x) // d
b = abs(x) % d

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

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

X = abs(X)

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

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

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

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

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

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

Solution (Python):
import sys
X,K,D=map(int, sys.stdin.readline().split())
X=abs(X)
a,b=X/D,X%D
if a<K:
    K-=a
    if K%2==0:
        print b
    else:
        print abs(b-D)
else:
    print X-D*K

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

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

dx=x//d
if dx<k:
    x-=d*dx
    k-=dx
else:
    x-=d*k
    k=0

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

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

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

first_step = min(X // D, K)

X -= first_step * D
K -= first_step

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

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

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

X,K,D=(int(x) for x in input().rstrip('\n').split())
if X>0 and X-K*D>=0:
  print(X-K*D)
elif X<0 and X+K*D<=0:
  print(-X-K*D)
else:
  if X>0:
    x = X%D
    y=X//D
    if (K-y)%2==0:
      print(x)
    else:
      print(D-x)
  elif X<0:
    a = (-1)*X
    x = a%D
    y = a//D
    if (K-y)%2==0:
      print(x)
    else:
      print(D-x)
  else:
    if K%2==0:
      print(0)
    else:
      print(D)

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

k = X // D
if abs(X - k * D) <= abs(X - (k + 1) * D):
    best_k = k
else:
    best_k = k + 1

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

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

if abs(X)//D > K:
    if X < 0:
        X += D*K
    if X > 0:
        X -= D*K

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

print(abs(X))

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


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

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

Solution (Python):






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





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

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

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

if x < 0:
    x = -x

to_zero = x//d

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

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

dx = abs(x)
y = dx // d

if k >= y:
    dx = dx - y * d
    if (k-y) % 2 == 0:
        print(dx)
    else:
        dx = abs(dx - d)
        print(dx)
elif k < y:
    dx = dx - k * d
    print(dx)

Solution (Python):
X,K,D=map(int,input().split())
X=abs(X)
n1=max((K-X/D)//2,0)
n2=min(n1+1,K//2)
n3=max(n1-1,0)
d1=abs(X-D*(K-2*n1))
d2=abs(X-D*(K-2*n2))
d3=abs(X-D*(K-2*n3))
print(int(min(d1,d2,d3)))

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

Solution (Python):

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

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

new_X = abs(X) % D
new_K = K - abs(X) // D

if new_K % 2 == 0:
    print(new_X)
else:
    print(abs(new_X-D))

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

Solution (Python):

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

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

else:
    if x >= 0:
        near = x- d*(abs(x) // abs(d))
    else:
        near = x+ d*(abs(x) // abs(d))

    if rest % 2 == 0:
        print(abs(near))
    else:
        if near >= 0:
            print(abs(near - d))
        else:
            print(abs(near + d))

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


if x<0:
  x=(-1)*x
if d<0:
  d=(-1)*d
  
s=math.floor(x/d)

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

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

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

x=abs(x)

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

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

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

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

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

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

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

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

        if(times%2 == K%2):
            print(abs(min_l))
        else:
            print(abs(min_r))
    else:
        print(abs(X+D*K))

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