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()) 
tmp = k

ans = abs(x)%d
k = k - int(abs(x)/d)

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

for i in range(k):
    if abs(ans) <= d and (k-i)%2 == 0:
        print(abs(ans))
        exit()
    elif ans >= 0:
        ans = ans - d
    elif ans < 0:
        ans = ans + d

print(abs(ans))

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

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

Solution (Python):
import math

def abc175c_walking_takahashi():
    x, k, d = map(int, input().split())
    x = -x if x < 0 else x
    num = math.ceil(x / d)
    if num > k:
        print(x-k*d)
        return
    else:
        k -= num
        x -= num*d
        if k % 2 == 0:
            print(abs(x))
        else:
            print(x+d)
abc175c_walking_takahashi()

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

Solution (Python):
def resolve():
    x, k, d = map(int, input().split())
    x = abs(x)
    a = x // d
    b = x % d
    if a > k:
        print(x - k * d)
        return
    k -= a

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

resolve()

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

if X < 0:
    X = -X

cnt = 0
Q = X//D
R = X%D

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

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

Solution (Python):
import math

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

if x < 0:
    x *= -1

if x-k*d < 0:
    if (k-(math.floor(x/d)))%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())
dist = k * d
if x > 0:
    rang = x - dist
else:
    rang = x + dist
    
if x > 0 and rang > 0:
    print(rang)
    quit()
elif x <= 0 and rang <= 0:
    print(abs(rang))
    quit()
num1 = list(divmod(x, d))
num2 = [num1[0] + 1, x - d * (num1[0] + 1)]
if num1[0] % 2 == 0:
    num = [num1, num2]
else:
    num = [num2, num1]
    
print(abs(num[k % 2][1]))

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

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

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

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

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

num_moved = min(abs(x) // d, k)
num_remained = k - num_moved
if num_moved > k:
  now = x % d
else:
  tmpa = x + d*num_moved
  tmpb = x - d*num_moved
  now = min(abs(tmpa), abs(tmpb))


if num_remained % 2 == 0:
  result = abs(now)
else:
  tmpa = now + d
  tmpb = now - d
  if abs(tmpa) > abs(tmpb):
    result = abs(tmpb)
  else:
    result = abs(tmpa)
print(result)

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


if x < 0:
  x *= -1

if x > k * d:
  print(x-k*d)
  sys.exit()

tmp = x//d
x -= tmp * d
k -= tmp

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

Solution (Python):


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

p = abs(x)

if p/d < k:
  k = k - (p // d)
  p = p % d

  if k % 2 == 0:
    print(abs(p))
  elif p < 0:
    print(abs(p + d))
  else:
    print(abs(p - d))

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

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

import numpy as np

if np.abs(X) > K*D:
    print(np.abs(X)-K*D)
else:
    a = X // D
    b = a + 1
    if a % 2 != K % 2:
        print(np.abs(b*D - X))
    else:
        print(np.abs(X - a*D))

Solution (Python):


x, k, d = map(int, input().split(' '))
m = (k * d - x) / (2 * d)
ans = 0

def score(c):
    return abs(x - k * d + 2 * d * c)

if m > k:
    ans = k
else:
    if m < 0:
        ans = 0
    else:
        ans = int(m)
    if score(ans) > score(ans+1):
        ans += 1

print(score(ans))

Solution (Python):
L = list(map(int, input().split()))
X = L[0]
K = L[1]
D = L[2]

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

if X<0 and X>-D*K:
    N = int(abs(X)/D)
    e = -X - N*D
    if (K-N) % 2 == 0:
        print(abs(e))
    else:
        print(abs(e-D))

Solution (Python):

x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
if(x<=0):
    if(x+k*d<=0):print(abs(x+k*d))
    else:
        res=x%d
        k-=abs(res-x)//d
        if(k%2):
            ans1=res+d
            ans2=res-d
            if(abs(ans1)>abs(ans2)):print(abs(ans2))
            else:print(abs(ans1))
        else:print(abs(res))
else:
    if(x-k*d>0):print(abs(x-k*d))
    else:
        res=x%d
        k-=abs(res-x)//d
        if(k%2):
            ans1=res+d
            ans2=res-d
            if(abs(ans1)>abs(ans2)):print(abs(ans2))
            else:print(abs(ans1))
        else:print(abs(res))

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


p=x%d
kai=(x-p)//d
if kai%2==k%2:
    print(p)
else:
    print(abs(d-p))

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    X, K, D = map(int, input().split())

    d, m = divmod(X, D)
    if abs(d) >= K:
        print(abs(abs(X) - (K * D)))
    else:
        if (K - d) % 2:
            print(D - m)
        else:
            print(m)


main()

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

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

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

if n>=0:
    if k*m > n:
        
        x = n//m;
        left = n - x*m;
        right = abs(left - m);
        k = k - x;
        if k&1:
            print(right)
        else:
            print(left)
        
    else:
        print(n - k*m)
else:
    n = abs(n)
    k = abs(k)
    m = abs(m)
    if k*m > n:
        
        x = n//m;
        left = n - x*m;
        right = abs(left - m);
        k = k - x;
        if k&1:
            print(right)
        else:
            print(left)
        
    else:
        print(n - k*m)

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

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

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

X = abs(X)


k = X // D
k = min(K, k)
X = abs(X - k * D)
K = K - k
if K == 0:
    print(X)
    exit()


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


X = abs(X - D)
print(X)

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

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

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

m = X % D
if m != 0:
    a = X // D
    if (K - a) % 2 == 0:
        print(m)
    else:
        print(D - m)
else:
    a = X // D
    if (K - a) % 2 == 0:
        print(0)
    else:
        print(D)

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

x = abs(X)
syo = x // D 
zan = K - syo 
ima = x - (D * syo) 

if zan <= 0:
    ans = x - (K * D)
elif zan % 2 == 0:
    ans = min(ima, abs(ima - (D * 2)))
elif zan % 2 == 1:
    ans = min(ima + D, abs(ima - D))

print(ans)

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

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

if new_K <= 0:
    answer = abs(X) - K*D
else:
    if new_K % 2 == 0:
        answer = new_X
    else:
        answer = abs(new_X - D)
        
print(answer)

Solution (Python):
x,k,d = map(int,input().split())
sho =  abs(x)//d
amari = abs(x)%d
tmp = []
tmp.append([sho,abs(amari)])
tmp.append([sho+1,abs(amari-d)])


if k>=sho:
    if (k-sho)%2==0:
        print(tmp[0][1])
    else:
        print(tmp[1][1])
else:
    print(abs(x)-k*d)

Solution (Python):
import math

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

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

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

Solution (Python):
XKD = list(map(int,input().split()))
X = XKD[0]
K = XKD[1]
D = XKD[2]
X_list=[]

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

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

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

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

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

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

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

B, A = divmod(abs(X), D)

if B <= K:
    print(abs((A - ((K-B) % 2) * D)))

else:
    print(abs(X)-K*D)

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

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

N = abs(X)//D

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

Solution (Python):
x,k,d = map(int,input().split())
if x==0:
	print((k%2)*d)
	exit(0)
l = 1
h = k
ans = float('inf')
much = float('inf')
pos = x
if x>0 : di = -1
elif x<0: di=1
while l<=h:
	
	m = (l+h)//2
	
	tmp = x+di*m*d
	
	if tmp>0:
		if x>0 : l=m+1
		else: h = m-1
		
	elif tmp<0:
		if x>0: h = m-1
		else:l=m+1
		
	else:
		ans = 0
		much = m
		break
	if abs(tmp) < ans:
		ans = abs(tmp)
		much = m
		
	
reste = k-much
if reste%2==0: print(ans)
else:print(min(abs(ans+d), abs(ans-d)))

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array





sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    X, K, D = il()
    max_dist = K * D
    if max_dist > abs(X):
        c = abs(X) // D
        k = K - c
        if k % 2 == 0:
            print(abs(abs(X) - (c * D)))
        else:
            print(min(abs(abs(X) - (c * D) + D), abs(abs(X) - (c * D) - D)))
    else:
        print(abs(abs(X) - max_dist))


if __name__ == '__main__':
    main()

Solution (Python):
import sys




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




distance = abs(X)


if distance % D == 0 and K >= distance // D:
    
    remaining_move_count = K - distance // D
    if remaining_move_count % 2 == 0:
        print(0)
    else:
        
        print(D)
    sys.exit()


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




before_zero_count = distance // D

before_zero_point = distance - before_zero_count * D

remaining_move_count = K - before_zero_count
if remaining_move_count % 2 == 0:
    print(before_zero_point)
elif X < 0:
    print(D - before_zero_point)
else:
    print(abs(before_zero_point - D))

Solution (Python):


import sys
import math
from decimal import Decimal, ROUND_DOWN
from collections import deque
from itertools import combinations
import copy

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    (x, k, d) = [int(e) for e in lines[0].split(' ')]


    return (x, k, d)


def solve(x, k, d):

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

    if debug:
        log("x=%d, k=%d, d=%d, y=abs(x)=%d, m=%d" % (x, k, d, y, m))
    
    result = None
    if k <= m:
        result = y - k * d
    else:
        n = k - m
        if n % 2 == 0:
            result = y - m * d
        else:
            result = abs(y - m * d - d)
             

    return result
    


def main():
    
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Solution (Python):
import math
x, k, d = map(int, input().split())
i = math.floor((d*k - x)/(2*d))
if 0<= i <=k:
  print(min([abs(x-d*k+2*d*j) for j in range(i-1, i+2)]))
else:
  print(abs(abs(x)-abs(d*k)))

Solution (Python):
def INT():
    return int(input())

def MI():
    return map(int, input().split())

def LI():
    return list(map(int, input().split()))

X, K, D = MI()

if X < 0:
    X *= -1

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

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

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

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

dcnt=abs(x)//d
if abs(x)%d <= abs(abs(x)%d-d):
    dans=abs(x)%d
else:
    dans=abs(abs(x)%d-d)
    dcnt+=1


if dcnt<k:
    if (k-dcnt)%2==0:
        print(abs(dans))
    else:
        print(abs(dans-d))


else:
    print(abs(x)-d*k)

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

a = int(X/D)
b = K - a
result = X-D*a
if b%2==1:
    result = min(abs(result+D), abs(result-D))
print(result)

Solution (Python):
num_bars = 3
length_list = list(map(int,input().split()))

x = abs(length_list[0])
k = length_list[1]
d = length_list[2]

amari =  x % d
syou = x // d




val = x

if x - k * d > amari:
  val = x - k * d
elif (k - syou ) % 2 == 0:
  val = amari
else :
  val = d - amari
  
  
print(val)

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

n_min = abs(X // D)
n_remains = (K - n_min)

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

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

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

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

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

if k % 2 != 0:
  x -= d

print(abs(x))

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

Solution (Python):
from itertools import combinations
from collections import Counter
import math


def main():
    x, k, d = map(int, input().split())
    t = abs(x) // d

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


if __name__ == "__main__":
    main()

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

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

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

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


X,K,D = LI()

sign = np.sign(X)
x = abs(X)






if x//D>K:
    ans = sign * (x-K*D)

elif x%D==0:
    if (K - x//D) % 2 ==0:
        ans = 0
    else:
        ans = -1 * D
else:
    
    if (K - x//D) % 2 ==0:
        
        ans = sign * (x- (x//D)*D)
    else:
        
        ans = -1 * sign * (x - (x//D)*D -D)
print(abs(ans))

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

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

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

if X < 0 :
  X = -X

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

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

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

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

Solution (Python):

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

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


if __name__ == '__main__':
    main()

Solution (Python):
XKD = list(map(int, input().split()))

posi = XKD[0]

count = 0
factor = 0
if (XKD[1] > int(abs(posi) / XKD[2])):
    time = XKD[1] - int(abs(posi) / XKD[2])
    factor = int(abs(posi) / XKD[2])
else:
    time = 0
    factor = XKD[1]

if (posi >= 0):
    posi -= XKD[2] * factor
else:
    posi += XKD[2] * factor

if (posi >= 0):
    if (time % 2 == 0):
        print(abs(posi))
    else:
        print(abs(posi - XKD[2]))
else:
    if (time % 2 == 0):
        print(abs(posi))
    else:
        print(abs(posi + XKD[2]))

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

x=abs(x)
kai=x//d
if kai==k:
    print(x%d)
elif kai>k:
    print(x-k*d)
else:
  right=x%d
  left=abs(x%d-d)

  if (kai+k)%2==0:
      print(right)
  else:
      print(left)

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

need_jump = abs(X) // D


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


cand1 = abs(X) - D * need_jump



if (K - need_jump) % 2 == 1:
    ans = abs(cand1 - D)
else:
    ans = cand1
print(ans)

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

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

ans = 0
X = abs(X)
if X == K * D:
  ans = 0
elif X > K * D:
  ans = X - (K * D)
else:
  div = X // D
  temp = K - div
  if temp % 2 == 0:
    ans = X - (div * D)
  else:
    ans = abs(X - (div * D) - D)

print(ans)

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

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

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

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

if K < q:
  print(X - K * D)
else:
  print(abs(r - ((K - q) % 2) * D))

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

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

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

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

Solution (Python):
IN = list(map(int, input().split()))
X, K, D = IN[0], IN[1], IN[2]


mv = round(-X / D, 0)
if (abs(mv)>K): mv = mv * K / abs(mv)
residual_moves = K - abs(mv)

if residual_moves % 2 == 0:
    print (int(abs(X + D * mv)))
else:
    posa = abs(X+D*(mv+1))
    posb = abs(X+D*(mv-1))
    posc = abs(X+D*(mv+2))
    posd = abs(X+D*(mv-2))
    print(int(min(posa, posb, posc, posd)))

Solution (Python):
def is_odd(n):
    if n % 2:
        return True
    return False

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

ans = 0
if K >= X // D:
    if is_odd(X // D) is is_odd(K):
        ans = X % D
    else:
        ans = X % D - D
else:
    ans = X - D * K

print(abs(ans))

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

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

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

    exit()

if X > 0:
    if X % D == 0:
        a = X // D
    else:
        a = X // D + 1

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

if X < 0:
    if X % D == 0:
        a = -1 * X // D
    else:
        a = -1 * X // D + 1

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

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

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

print(ans)

Solution (Python):
X,K,D=map(int,input().split())
a=round(abs(X/D))
if K<a:
  print(abs(abs(X)-K*abs(D)))
elif (K-a)%2==0:
  l=[]
  l.append(abs(abs(X)-a*abs(D)))
  l.append(abs(abs(X)-(a+2)*abs(D)))
  l.append(abs(abs(X)-(a-2)*abs(D)))
  print(min(l))
elif (K-a)%2!=0:
  l=[]
  l.append(abs(abs(X)-(a+1)*abs(D)))
  l.append(abs(abs(X)-(a-1)*abs(D)))  
  print(min(l))