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

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

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

if X<0:
    X = -X
if X - K*D >= 0:
    print(X-K*D)
else:
    i = 0
    while True:
        i+=1
        X -= D
        if X < 0:
            break
    if (K-i) % 2 == 0:
        print(abs(X))
    else:
        print(D-abs(X))

Solution (Python):
first_place, K, Distance = map(int, input().split())
first_place = abs(first_place)
test = first_place
if first_place - Distance*K > 0:
    print(first_place -Distance*K)
else:
    for i in range(1,K+1):
        test -= Distance
        if test <= 0:
            a = i
            
            break
    if (K-a)%2 == 0 or K-a == 0:
        print(abs(first_place - Distance*a))
    else:
        print(abs(first_place - Distance*(a-1)))

Solution (Python):
from collections import deque

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


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

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

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

Solution (Python):
if __name__ == "__main__":
    X, K, D = map(lambda x: abs(int(x)), input().split())

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

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

c = abs(X) // D
k = min(c, K)
K -= k
if K == 0:
    if X > 0:
        print(abs(X-D*k))
        exit()
    elif X < 0:
        print(abs(X+D*k))
        exit()

if X > 0:
    X -= D*k
    if abs(X-D) < abs(X):
        X -= D
        K -= 1
elif X < 0:
    X += D*k
    if abs(X+D) < abs(X):
        X += D
        K -= 1

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

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

    def solve(x, k, d):
        """
        https://atcoder.jp/contests/abc175/editorial/52
        """

        x = abs(x)

        straight = int(min(k, x / d))
        k -= straight
        x -= straight * d

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

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_3()

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

    def solve(x, k, d):
        
        x = abs(x)

        
        if k < (x // d):
            return x - (k * d)
        
        else:
            
            current_x = x - d * (x // d)
            k = k - (x // d)

            
            if (k % 2) == 0:
                
                return current_x
            else:
                
                
                return abs(current_x - d)

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_2()

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

    def solve(x, k, d):
        
        x = abs(x)

        
        if k < (x // d):
            return x - (k * d)
        
        else:
            
            current_x = x - d * (x // d)
            k = k - (x // d)

            

            if (k % 2) == 0:
                
                return current_x
            else:
                
                
                return abs(current_x - d)

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_1()

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

Solution (Python):
import sys
import math

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

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

a = X // D if X > 0 else math.ceil(X / D)
k = K - a
k = k % 2

print(abs(X - D * a - k * D if X > 0 else X - D * a + k * D))

Solution (Python):
import sys


def input():
    return sys.stdin.readline().rstrip('\n')


def calc(X, K, D):
    X = abs(X)
    n = X // D
    if K <= n:
        return abs(X - K * D)
    if (K - n) % 2 == 0:
        return abs(X - n * D)
    else:
        return abs(X - (n + 1) * D)


(X, K, D) = tuple([int(s) for s in input().split()])
print(calc(X, K, D))

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

result = 0
if abs(X) > K * D:
    result = abs(X) - K * D
else:
    y = int(round(abs(X) / D))
    resX = abs(abs(X) - D * y)
    resK = K - y
    if resK % 2 == 1:
        result = abs(resX - D)
    else:
        result = resX
print(result)

Solution (Python):
def solve():
  X, K, D = map(int, input().split())
  X = abs(X)
  if X>=K*D:
    return X-K*D
  X -= D*(K%2)
  r =  X%(D*2)
  return min(r,2*D-r)
print(solve())

Solution (Python):
import math

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

abs_x = abs(x)

max_len = math.ceil(abs_x/d)*d
min_len = max_len-d

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

if (math.ceil(abs_x/d)-1)%2 == k%2:
    print(abs(abs_x-min_len))
else:
    print(abs(abs_x-max_len))

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

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

cur = abs(x)  
rem = k  

cnt = min(cur // d, k)  
cur -= d * cnt
rem -= cnt


if rem > 0:
    if rem % 2 == 1:
        cur = cur - d

ans = abs(cur)  
print(ans)

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

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

if D * K < abs(X):
    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())

cur = abs(x)
rem = k

cnt = min(cur // d, k)
cur -= d * cnt
rem -= cnt

if rem > 0:
    if rem % 2 == 1:
        cur -= d

ans = abs(cur)
print(ans)

Solution (Python):
import sys
import math
import fractions
import bisect
import queue
import heapq
import collections
import itertools
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint=====================================
N, K = map(int, input().split())
N, M = map(int, input().split())
A, B = map(int, input().split())

H, W = map(int, input().split())

L, R = map(int, input().split())

1行のstring=================================
S, T = input().split()

1行の整数配列================================
P = list(map(int,input().split()))

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
D = list(map(int,input().split()))

L = list(map(int,input().split()))
R = list(map(int,input().split()))

T = list(map(int,input().split()))

改行あり整数==================================
X = []
Y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    X.append(x1)
    Y.append(y1)

A = []
for i in range(N):
    a=int(input())
    A.append(a)

Pair = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    Pair.append((x1, y1))
    
2次元整数配列=====================================
A = []
for i in range(N):
    tmp = list(map(int,input().split()))
    A.append(tmp)


N行M列の初期化====================================
dp = [[VAL] * RETU for i in range(GYO)]

'''





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

X = abs(X)
amari = K - X // D

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

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

Solution (Python):
from __future__ import division, print_function






from sys import stdin, stdout
import bisect            
import math
import heapq
i_m=9223372036854775807
def inin():
    return int(input())
def stin():
    return input()
def spin():
    return map(int,stin().split())
def lin():                           
    return list(map(int,stin().split()))
def matrix(n):
    
    return [list(map(int,input().split()))for i in range(n)]


def calculate_sum(a, N): 
    
    m = N / a 
    
    sum = m * (m + 1) / 2
    
    ans = a * sum
    return ans
def series(N):
    return (N*(N+1))//2

def count2Dmatrix(i,list):
    return sum(c.count(i) for c in list)

def modinv(n,p):
    return pow(n,p-2,p)

def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x
def LCM (x, y):
    return (x * y) // GCD(x, y)
    
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
def isprime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            
    return j

 
"""*******************************************************"""
x, k, d = spin()
x = abs(x)
m = x//d
ans = 0
if m > k:
    ans = x - k * d
else :
    x -= d*m
    k -= m
    if k%2==1:
        x -= d
        ans = abs(x)
    else :
        ans = abs(x)
print(ans)

Solution (Python):

line = list(map(int, input().split( )))
iniabs = abs(line[0])

lstep = iniabs//line[2]
ldis = iniabs%line[2]
odeven = (line[1] - lstep)%2
if lstep > line[1]:
  print(iniabs - line[2]*line[1])
elif odeven == 0:
  print(ldis)
else:
  print(line[2] - ldis)

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

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

Solution (Python):

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

x,k,d=input2()
abs_x=abs(x)
count=min(abs_x//d,k) 
abs_x-=d*count 
rem=k-count


if rem>0:
	if rem%2==1:
		abs_x-=d 

ans=abs(abs_x)
print(ans)

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

Solution (Python):
it = lambda: list(map(int, input().strip().split()))

def solve():
    X, K, D = it()
    X = abs(X)
    if X - K * D >= 0:
        return X - K * D
    K = K - X // D
    X = X - X // D * D
    assert K >= 0
    assert X >= 0
    return abs(X) if K % 2 == 0 else abs(X - D)


if __name__ == '__main__':
    ans = solve()
    print(ans)

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

min_p = abs(x)//d
min_n = min_p + 1
rest_p = abs(x)- d*min_p
rest_n = rest_p - d

if k < min_p:
  print(abs(x) - k*d)
elif (k-min_p) % 2 == 0:
  print(rest_p)
else:
  print(abs(rest_n))

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

x = abs(x)
kk = x // d
amari = x % d

if kk >= k:
    ans = (kk - k) * d + amari
elif (k - kk) % 2 == 1:
    ans = abs(amari - d)
else:
    ans = amari

print(ans)

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

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

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

Solution (Python):
import math

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

if X > K*D:
    print(X-K*D)
else:
    c=math.floor(X/D)
    print( abs(X-D*c-((K-c)%2)*D) )

Solution (Python):


class main:
  def function(x, k, d):
    if x >= k * d:
      ans = x - k * d
    else:
      r = x // d
      n = k - r
      if n % 2 == 0:
        ans = x % d
      else:
        ans = abs(x % d - d)
    return ans

if __name__ == "__main__":
  x, k, d = map(int, input().split())
  x = abs(x)
  ans = main.function(x, k, d)
  print(ans)

Solution (Python):

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

Solution (Python):

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

i = min(X//D, K) 
X = X - i*D
K = K - i

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

Solution (Python):
import sys

def input(): return sys.stdin.readline().rstrip("\r\n")
def List(): return list(map(int, input().split()))
def Num(): return int(input())


x, k, d = List()
x = abs(x)
if d * k > x:
    distance = x % d
    do = k - (x // d)
    if do & 1:
        print(d - distance)
    else:
        print(distance)
else:
    print(x - k * d)

Solution (Python):
import sys


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

if D * K <= abs(X):
	print(abs(X) - D * K)
else:
	distance = abs(X) % D

	
	count = K - (abs(X) // D)

	if count % 2 == 1:
		print(abs(D - distance))
	else:
		print(distance)

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

Solution (Python):
import math

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

X = abs(X)

if X > K * D:
    print(X-K*D)
else:
    c=math.floor(X / D)
    print(abs(X - D * c - ((K - c) % 2) *D))

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

if X > K*D:
    print( X - K*D )
else:
    a = X//D
    K -= a
    X -= D*a
    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:
    if (x // d - k) % 2 == 0:
        print(x % d)
    else:
        print(d - x % d)

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

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

print(ans)

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

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

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

print(abs(X))

Solution (Python):
def main():
    X,K,D = map(int,input().split())
    cnt = 0
    
    
    if X == 0:
        s = 0
        t = s + D
        cnt += 1
        if cnt == K:
            return t
    else:
        X = abs(X)
        cnt += (X//D)
        if cnt >= K:
            ans = X - (K*D)
            return ans
        s = X - (cnt*D)
        t = s - D
        t = abs(t)
        cnt += 1
        if cnt == K:
            return t

    nokori = K - cnt
    if nokori % 2 == 0:
        return t
    else:
        return s

print(main())

Solution (Python):

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

X = abs(X)

n = X // D
x = X % D

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

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

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

Solution (Python):
input_line = input()
data = input_line.split(" ")
x = abs(int(data[0]))
k = int(data[1])
d = int(data[2])
a = x // d
b = x % d
res = abs(a)
if res >= k :
  ans = x - k * d
else :
  num = k - res
  if num % 2 == 1 :
    ans = abs(b - d)
  else :
    ans = b
print(ans)

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

Solution (Python):



def next_line():
    return input()


def next_int():
    return int(input())


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


def next_int_array_multi_lines(size):
    return [int(input()) for _ in range(size)]


def next_str_array(size):
    return [input() for _ in range(size)]


def main():
    x, k, d = map(int, input().split())
    f = False
    if x < 0:
        f = True
        x = -x
    min_to_eq = x // d
    res = 0
    if min_to_eq >= k:
        
        res = x - d * k
    else:
        k -= min_to_eq
        x = x - d * min_to_eq
        if k % 2 != 0:
            res = x - d
        else:
            res = x
    if f:
        res = -res
    print(abs(res))


if __name__ == '__main__':
    main()

Solution (Python):
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if r<=0:
  print(-p)
elif 0<r<k:
  s=min(p-2*d*q,-p+2*d*(q+1))
  print(s)
else:
  print(-x-k*d)

Solution (Python):
X, K, D = (int(x) for x in input().split())
X = abs(X) 
L = X//D 
if K <= L:
    Min = X - (K*D)
else:
    Min = X - (L*D)
    temp = K - L 
    if temp % 2 == 1:
        Min = abs(Min) - D
print(abs(Min))

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

Solution (Python):
X, K, D = map(int, input().split())
x = abs(X)
q = x // D
if q <= K:
    x -= q * D
    k = K - q
else:
    x -= K * D
    k = 0

if k % 2 == 0:
    print(abs(x))
else:
    print(abs(x - D))

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

Solution (Python):

x, k, d = map(int, input().split())
x = abs(x)
a = x // d
b = x % d
ans = 0

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

print(ans)

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

def y(l): return abs(x + l * d - (k - l) * d)

lf = min(max(0, (k - x / d) / 2), k)
l = int(lf)
if lf - l > 0 and l + 1 <= k:
    print(min(y(l), y(l + 1)))
else:
    print(y(l))

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

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

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

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

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

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

a = X // D
b = X % D

remain_count = K - a

if remain_count < 0:
    ans = X - K * D
else:
    if remain_count % 2 == 0:
        ans = b
    else:
        ans = abs(b - D)
    
print(ans)

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

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

Solution (Python):
var = input()
x = int(var.split()[0])
k = int(var.split()[1])
d = int(var.split()[2])
x = abs(x)
cnt = x // d
if (k <= cnt) :
  print(x - d * k)
else :
  kk = k - cnt
  if (kk % 2 == 0) :
    print(x - d * cnt)
  else :
    print(d - (x - d * cnt))

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

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

x = A[0]
k = A[1]
d = A[2]

first_k = abs(x) // d
if k < first_k:
  final_min = abs(x) - k * d
else:
  first_min = abs(x) % d
  second_min = abs(x) - d * (first_k + 1)

  if abs(first_min) < abs(second_min):
    first_k += 1
    final_min = second_min
  else:
    final_min = first_min
  remain_k = k - first_k
  if remain_k % 2 != 0:
    plus = final_min + d
    minus = final_min - d
    if abs(plus) > abs(minus):
      final_min = abs(minus)
    else:
      final_min = abs(plus)

print(abs(final_min))

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

a=X//D
b=X%D

if K<=a:
    ans=X-K*D
elif (K-a)%2==0:
    ans=b
else:
    ans=D-b
print(ans)

Solution (Python):
import os
import sys
from io import BytesIO, IOBase


def main():
    x,k,d = map(int,input().split())
    
    t = abs(x)//d
    
    if t<=k and x<0:
        x+=t*d
        k-=t
    elif t<=k and x>0:
        x-=t*d
        k-=t
    elif t>k and x<0:
        x+=k*d
        k-=k
    elif t>k and x>0:
        x-=k*d
        k-=k
    
    while k:
        if x<0:
            if x+d<0:
                x+=d
            elif abs(x+d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x+d))
                break
            elif abs(x+d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 != 0:
                print(abs(x+d))
                break
        else:
            if x-d>0:
                x-=d
            elif abs(x-d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x-d))
                break
            elif abs(x-d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 != 0:
                print(abs(x-d))
                break
        k-=1
    else:
        print(abs(x))
            
        
    


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


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")





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

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

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

Solution (Python):
import os
import sys
from io import BytesIO, IOBase


def main():
    x,k,d = map(int,input().split())
    
    t = abs(x)//d
    
    if t<=k and x<0:
        x+=t*d
        k-=t
    elif t<=k and x>0:
        x-=t*d
        k-=t
    elif t>k and x<0:
        x+=k*d
        k-=k
    elif t>k and x>0:
        x-=k*d
        k-=k
    
    while k:
        if x<0:
            if x+d<0:
                x+=d
            elif abs(x+d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x+d))
                break
            elif abs(x+d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 != 0:
                print(abs(x+d))
                break
        else:
            if x-d>0:
                x-=d
            elif abs(x-d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x-d))
                break
            elif abs(x-d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 != 0:
                print(abs(x-d))
                break
        k-=1
    else:
        print(abs(x))
            
        
    

























































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

Solution (Python):
X, K, D = map(int, input().split())
k = abs(X)//D
dk = K-k
dd = min(abs(X-k*D), abs(X+k*D))

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

Solution (Python):


import sys






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






















X_abs = abs(X)

if X_abs > K * D:
    print(X_abs - (K * D))
    sys.exit()
else:
    j = X_abs // D
    i = X_abs - (j * D)
    if (K - j) % 2 == 0:
        print(i)
    else:
        print(abs(i - D))

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

Solution (Python):
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

x, k, d = rm()
c = abs(x)//d
if k<c:
    print(abs(x)-d*k)
else:
    e=abs(x)-d*c
    if c%2==k%2:
        print(e)
    else:
        print(d-e)

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

X = abs(X)

Y = min(K, X // D)
K -= Y
X -= Y * D

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

print(ans)

Solution (Python):
def c_walking_takahashi():
    X, K, D = [int(i) for i in input().split()]
    x = abs(X)
    need_move, r = divmod(x, D)  
    if need_move >= K:
        return x - K * D  
    can_move = K - need_move
    
    if can_move % 2 == 0:
        return r
    return D - r

print(c_walking_takahashi())

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

a = [abs(x // d), x % d]

if a[0] > k:
   ans = min(abs(x+k*d) ,abs(x-k*d))
else:
    b = (k- a[0]) % 2
    ans = min(abs(a[1]+b*d) ,abs(a[1]-b*d))

print(ans)

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)
D = abs(D)
if X > K * D:
    X = X - K*D
else:
    quo = X//D
    X = X - D*quo
    res = K - quo
    if res % 2 == 1:
        X = X - D
    else:
        pass

print(abs(X))