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=list(map(int,input().split()))
 
def sgn(x):
    if x>0:
        return 1
    elif x<0:
        return -1
    return 0
 
def solve():
    if X>0:
        x_greedy=X-K*D
    else:
        x_greedy=X+K*D
 
    if sgn(X)==sgn(x_greedy):
        return print(abs(x_greedy))
 
    else:
        x_r=X%D
        x_l=x_r-D
 
        r_parity=(abs(X-x_r)//D)%2
        if K%2==r_parity:
            return print(abs(x_r))
        else:
            return print(abs(x_l))
 
if __name__=='__main__':
    solve()

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

Solution (Python):
import math

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

X = abs(X)

cand = math.floor(X / D)

min_v = 0

if cand > K:
    min_v = X - (K * D)
else:
    if cand % 2 == K % 2:
        min_v = X - cand * D
    else:
        min_v = abs(X - (cand + 1) * D)

print(min_v)

Solution (Python):

import sys
import math

input = sys.stdin.readline


def inp():
    return(int(input()))

def inps():
    return str(input())
    
def inlt():
    return(list(map(int,input().split())))
    
def inlst():
    return(list(map(str,input().split())))
    
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split())) 

def ans(x,k,d):
    if abs(x)<d:
        if k%2: return  d - abs(x)
        else: return abs(x)
    else:
        div = abs(x)//d
        if div>k: return abs(x) - d*k
        else:
            if (k-div)%2: return d - abs(x)%d
            else: return abs(x)%d 

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

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

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

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

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

Solution (Python):
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    X,K,D =map(int,input().split())
    X= abs(X)
    if X >= K*D:
        print(X-K*D)

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








if __name__ == "__main__":
    main()

Solution (Python):
import math
def main():
	s = raw_input()
	l = s.split()
	x = int(l[0])
	k = int(l[1])
	d = int(l[2])
	value = x - k * d
	ans = x + k * d
	be = 0
	en = k
	while (be <= en):
	    mid = (be + en) // 2
	    diff = 2 * mid * d
	    if ((value + diff) >= 0):
	        ans = value + diff
	        en = mid - 1
	    else:
	        be = mid + 1

	ans2 = value
	be = 0
	en = k
	while (be <= en):
		mid = (be + en) / 2
		diff = 2 * mid * d
		if ((value + diff) < 0):
		    ans2 = value + diff
		    be = mid + 1
		else:
		    en = mid - 1
	print(int(min(abs(ans), abs(ans2))))	



if __name__ == '__main__':
	main()

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==1:
        if k%2==0:
            print(abs(abs(x)%d-d))

        else:
            print(abs(abs(x)%d))

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

Solution (Python):

def main():
    x, k, d = map(int, input().split())
    
    if x < 0:
        x = -1 * x
    
    r = abs(x) // d
    k_rest = k - r
    if r > 0 and k_rest < 0:
        print(x - k * d)
        exit()

    x_r = x - d * r
    x_l = x_r - d

    

    if k_rest % 2 == 0:
        print(abs(x_r))
    else:
        print(abs(x_l))

if __name__ == "__main__":
    main()

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

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

Solution (Python):
import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))

MOD = 10**9 + 7

def main():
	x, k, d = read()
	if abs(x) >= k*d:print(abs(x) - k*d);return
	if x%d == 0:print([d, 0][(k - abs(x)//d)%2 == 0]);return
	y = abs(x)%d
	print([y, abs(y-d)][(k - abs(x)//d) %2])



if __name__ == "__main__": 
	main()

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

x,k,d=nii()

dist=abs(x)
num=dist//d

if num>k:
  print(abs(dist-d*k))
  exit()
else:
  k-=num
  dist-=d*num

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

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

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

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

cnt = min(cur // D, K)
cur -= D * cnt
rem -= cnt

print(-1*(cur - D) if rem & 1 else cur)

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
a = x // d
b = x % d
c = b - d

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

Solution (Python):
X,K,D = map(int,input().split())
X = abs(X)
if X > K*D:
    print(X-K*D)
else:
    if X%D != 0:
        ans1 = X%D
        ans2 = (-X)%D
    else:
        ans1 = X%D
        ans2 = D
    if ((X-ans1) //D)&1 == K&1:
        print(ans1)
    else:
        print(ans2)

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

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

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

Solution (Python):
import itertools

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

l = 0
r = K
min_distance = float('inf')
diff_set = set()

while (l <= r):
    n = (r + l) // 2
    diff = (2 * n - K) * D + X
    if min_distance == float('inf'):
        min_distance = abs(diff)
    if abs(diff) < min_distance :
        min_distance = abs(diff)

    if diff in diff_set:
        break
    else:
        diff_set.add(diff)

    if diff > 0:
        r = n - 1
    else:
        l = n + 1

print(min_distance)

Solution (Python):
def main():
    X, K, D = list(map(int, input().split()))
    
    def sgn(X):
        if X > 0:return 1
        if X < 0:return -1
        return 0
    
    if X > 0:
        x_greedy = X - K * D
    else:
        x_greedy = X + K * D
    
    if sgn(X) == sgn(x_greedy):
        print(abs(x_greedy))
        return
    
    x_r = X % D
    x_l = x_r - D

    r_parity = (abs(X - x_r) // D) % 2
    
    if K%2 == r_parity:
        print(abs(x_r))
    else:
        print(abs(x_l))
main()

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

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

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

Solution (Python):
def main():
    x, k, d = list(map(int, input().split(" ")))
    dk = d*k
    if abs(x) < dk:
        div_count = abs(x)//d
        if (k - div_count)%2==0:
            ans = abs(x)%d
        else:
            ans = d - abs(x)%d
    else:
        ans = abs(dk - abs(x))
    print(ans)

if __name__=="__main__":
    main()

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



if x - d * k >= 0:
    ans = x - d * k


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

Solution (Python):
def main():
    X, K, D = map(int, input().split())
    if X < 0:
        X = -X
    num_of_moves = min(X // D, K)

    X -= D * num_of_moves
    K -= num_of_moves

    K %= 2

    ans = abs(X - D * K)

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
def WalkingTakahashi():
    
    data = list(map(int,input().split()))
    
    position = abs(data[0])
    
    m = min(data[1], position//data[2])
    
    position = position - data[2]*m
    
    data[1] = data[1] - m
    
    data[1] %= 2
    
    position = position - data[2]*data[1]
    
    print(abs(position))
    
if __name__ == '__main__':
    WalkingTakahashi()

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

X = abs(X)

K_main = X // D
K_sub = K - K_main

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


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


if __name__ == '__main__':
    solve()

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

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

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

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

Solution (Python):
a,b,c=map(int,input().split())
x=(-a+b*c+2*c-1)//(2*c)
if(x>=b):x=b
if(x<=0):x=0
def f(x):
	return abs(a+(2*x-b)*c)
print(min([f(x),f(max(x-1,0))]))

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

Solution (Python):
X, K, D = map(int, input().split())
import numpy as np
res = 0

count = abs(X) // D
pseudo = np.sign(X) * (abs(X) - count * D)

if abs(X) >= K * D:
    res = abs(abs(X) - K * D)
elif (K-count) % 2 == 0:
    res = abs(pseudo)
else:
    res = min(abs(pseudo + D), abs(pseudo - D))

print(res)

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

def calcu(x,k,D):
    if x == 0:
        if k%2 == 0:
            return 0
        else:
            return D

    a = int(x/D)
    if k <= a:
        x = x - k*D
        return x
    else:
        x1 = x- a*D
        x2 = x1 - D
        k = k - a
        if k%2 == 0:
            return x1
        else:
            return abs(x2)

        


if X >= 0:
    print(calcu(X,K,D))
else:
    print(calcu(-X,K,D))

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

X, K, D = MI()
a = abs(X) % D
b = D - a
c = abs(X) // D
ans = 0
if K <= c:
    ans = abs(X) - K * D
elif K > c and (K - c) % 2 == 0:
    ans = a
else:
    ans = b
print(ans)

Solution (Python):
x,k,d=map(int,input().split())
def sign(x):
    if x> 0:return 1
    elif x<0 :return -1
    return 0

if x>0:
    x_distance = x - k*d
else:
    x_distance = x + k*d

if sign(x)==sign(x_distance):
    print(abs(x_distance))
    exit()

x_r= x_distance % d
x_l=x_r -d
kairi=(abs(x-x_r)//d) %2

if k%2==kairi:
    print(abs(x_r))
else:print(abs(x_l))

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

base = X - min(N, K) * D

if K <= N:
    res = base
else:
    if (K - N) % 2 == 0:
        res = min(base, abs(base - 2 * D))
    else:
        res = min(abs(base - D), base + D)

print(res)

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

ans = abs(cur)
print(ans)

Solution (Python):
a=list(map(int,input().split()))
if a[0]<0:
    a[0]=-a[0]
x=a[0]%a[2]
y=a[0]//a[2]
if y>=a[1]:
    print(a[0]-a[1]*a[2])
elif (a[1]-y)%2==1:
    print(a[2]-x)
else:
    print(x)

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

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

x = abs(x)
syo = x//d
kk = k - syo
xx = x - syo*d

if k <= syo:
    ans = x-d*k
else:
    if kk%2 == 0:
        ans = xx
    else:
        ans = abs(xx-d)

print(ans)

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


main()

Solution (Python):
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math
INT_MIN=-10**13
import bisect as B

x,k,d=INPUT()
x=abs(x)
if x<0:
    if k*d+x<=0:
        print(k*d+x)
    else:
        p=x//d
        neg=abs(p*d+x)
        pos=abs((p+1)*d)
        if neg<pos:
            if (k-p)%2==0:
                print(neg)
            else:
                print(pos)
        else:
            if (k-p-1)%2==0:
                print(pos)
            else:
                print(neg)
elif x==0:
    if k%2==0:
        print(0)
    else:
        print(d)
else:
    if x-k*d>=0:
        print(x-k*d)
    else:
        p=x//d
        pos=x-p*d
        neg=abs(x-(p+1)*d)
        if pos<neg:
            if (k-p)%2==0:
                print(pos)
            else:
                print(neg)
        else:
            if (k-p-1)%2==0:
                print(neg)
            else:
                print(pos)

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 = abs(cur - d)

print(cur)

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

Solution (Python):
import math
import sys

def dict_sort(ans):
    ans=sorted(ans.items(),reverse=True,key=lambda kv:(kv[1]))

def is_prime(n):
    if n==1:
        return 0
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True

def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)

def lcm(a,b):
    return (a*b)//gcd(a,b)

def alphbates():
    return "abcdefghijklmnopqrstuvwxyz"

def binary_search(ls,n,flag):
    low=0
    hi=n-1
    while(low<=hi):
        mid=(low+hi)//2
        if ls[mid]==flag:
            return mid
        elif ls[mid]>flag:
            hi=mid-1
        else:
            low=mid+1
    return -1

def inp():
    ls=list(map(int,input().split()))
    return ls

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

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

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

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

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

Solution (Python):
import math

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

x = abs(x)
k1 = math.floor(x/d)

if k < k1 :
        k1 = k
else :
        k1 = k1

k2 = k1 - k

y = x - k1 * d

if k2%2 == 0 :
        print(abs(y))
else :
        print(abs(y - d))

Solution (Python):
x,k,d=map(int,input().split())
x=abs(x)
m=min(k,x//d)
x-=d*m
k-=m
k%=2
x-=d*k
print(abs(x))

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

Solution (Python):


import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union































def main():
    Num = Union[int, float]
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input():  return sys.stdin.readline().rstrip()
    def ii():     return int(input())
    def isp():    return input().split()
    def mi():     return map(int, input().split())
    def mi_0():   return map(lambda x: int(x)-1, input().split())
    def lmi():    return list(map(int, input().split()))
    def lmi_0():  return list(map(lambda x: int(x)-1, input().split()))
    def li():     return list(input())
    def debug(x): print(x, file=sys.stderr)
    
    
    
    x, k, d = mi()
    x = abs(x)
    if x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(d)
    else:
        if x - k * d >= 0:
            print(x - k * d)
        else:
            num, res = divmod(x, d)
            
            if (k - num) % 2 == 0:
                print(res)
            else:
                print(abs(res - d))


if __name__ == "__main__":
    main()

Solution (Python):
x,k,d=map(int,input().split())
x=abs(x)
m=min(k,x//d)
x-=d*m
k-=m
k%=2
x-=d*k
print(abs(x))

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X, K, D = MAP()

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

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

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

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

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

a = x // d

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

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

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

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

print(ans)

Solution (Python):

import os
import sys
from io import BytesIO, IOBase




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


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


class union_find:
    def __init__(self, n):
        self.n = n
        self.rank = [0]*n
        self.parent = [int(j) for j in range(n)]
    
    def union(self,i,j):
        i = self.find(i)
        j = self.find(j)
        if self.rank[i] == self.rank[j]:
            self.parent[i] = j
            self.rank[j] += 1

        elif self.rank[i] > self.rank[j]:
            self.parent[j] = i
        else:
            self.parent[i] = j

    def find(self, i):
        temp = i
        if self.parent[temp] != temp:
            self.parent[temp] = self.find(self.parent[temp])
        return self.parent[temp]
from math import log2, ceil
from collections import deque, Counter as CC, defaultdict as dd
MOD = int(1e9+7)
def main():

    
        
        
        x,k,d = [int(j) for j in input().split()]
        x = abs(x)
        n = (x//d)
        if(k<n):
            ans = x-k*d
        else:
            k = k-n
            a = x-n*d
            if(k%2):
                ans = d-a
            else:
                ans = a
        print(ans)













        



if __name__ == "__main__":
    main()

Solution (Python):
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  k=k-sa
  for i in range(k):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=mbk if (k-i)%2==0 else bk1
        break
      else:
        bk3=max(mansbk,pansbk) 
    else:
      ans=0 if (k-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

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

m = min(K,X//D)
X -= D*m
K -= m
K %= 2
X -= D*K
print(abs(X))

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

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

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

x=abs(x)

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

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

if x==0:
  if k&1:
    print(d)
  else:
    print(0)
  exit()

if x<0 and x+k*d<0:
    print(-x-k*d)
    exit()
if x>0 and x-k*d>0:
    print(x-k*d)
    exit()
    
x=x%(2*d)
if k&1:
  print(min(abs(x-d),abs(x+d)))
  exit()
else:
  print(min(abs(x-2*d),x))
  exit()

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

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

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

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

def main():
    X, K, D = map(int, input().split())
    X = X if X > 0 else -X
    nK = K - (X // D)
    if nK < 0:
        print(X - K * D)
    else:
        pos = X - ((X // D) * D)
        if nK % 2 != 0:
            print(abs(pos - D))
        else:
            print(pos)


main()

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

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

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

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

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

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

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

if (X // D) <= K:
    if X % D >= abs((X % D)-D):
        if (K - (X // D)) % 2 == 1:
            print(abs((X % D)-D))
            sys.exit()
        else:
            print(X % D)
            sys.exit()
    else:
        if (K - (X // D)) % 2 == 1:
            print(abs((X % D)-D))
            sys.exit()
        else:
            print(X % D)
            sys.exit()

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

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    a = (X+K*D)//(2*D)
    if a >= K:
        a = K
        ans = abs(X-K*D)
    else:
        ans_1 = abs(X+K*D-2*a*D)
        ans_2 = abs(X+K*D-2*(a+1)*D)
        ans = min(ans_1, ans_2)
print(ans)

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

val = n % d

left = 0
if n >= 0:
	if n - (k * d) < 0:
		left = k - ((n - val)/d)
		if left % 2 == 0:
			print(val)
		else:
			print(abs(val - d))
	else:
		print(abs(n - (k * d)))
else:
	if n + (k * d) > 0:
		left = k - ((val - n)/d)
		if left % 2 == 0:
			print(val)
		else:
			print(abs(val - d))
	else:
		print(abs(n + (k*d)))

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))



def main():
	x,k,d=MI()
	x=abs(x)
	a=x%d
	b=d-a

	ans=0

	t=k-(x-a)//d

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






if __name__ == "__main__":
	main()

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

X = abs(X)

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

print(abs(ans))