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):
init_pos, move_max, move_dist = [int(x) for x in input().split(' ')]
if init_pos < 0:
    init_pos *= -1

move_cnt = min(init_pos // move_dist, move_max)

moved_pos = init_pos - move_cnt * move_dist
min_abs = moved_pos

if move_max - move_cnt > 0:
    dests = [moved_pos, move_dist - moved_pos]
    min_abs = dests[(move_max - move_cnt) % 2]

print(min_abs)

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

Solution (Python):
from collections import deque
import sys, copy,itertools
input = sys.stdin.readline

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

x = abs(x)

q = x // d
mod = x % d

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

print(abs(ans))

Solution (Python):


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

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

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

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

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

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

a = (K * D - X) / (2*D)
a1, a2 = math.floor(a), math.ceil(a)

left = 0
right = K
def f(a):
    return X - K * D + 2 * a * D

mid = None
flag = 0
while left+1 < right:
    mid = (left + right) // 2

    if f(mid) == 0:
        flag = 1
        break
    elif f(mid) < 0:
        left = mid
    else:
        right = mid

if flag:
    print(abs(X - K * D + 2 * mid * D))
else:
    res1 = abs(X - K * D + 2 * left * D)
    res2 = abs(X - K * D + 2 * right * D)
    print(min(res1, res2))

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:
  if x > 0:
    cnt = (x - (x % d)) // d
  elif x < 0:
    cnt = (-x - (-x % d)) // d
  else:
    cnt = 0
  if (k - cnt) % 2 == 0:
    if x > 0:
      print(x % d)
    elif x < 0:
      print(-x % d)
    else:
      print(0)
  else:
    if x > 0:
      print(d - (x % d))
    elif x < 0:
      print(d - (-x % d))
    else:
      print(d)

Solution (Python):
import math

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

    s = int(X / D)
    if X - D*s >= X - D*(s+1):
        v0 = abs(X - D*s)
        v1 = abs(X - D*(s+1))
    else:
        s = s+1
        v1 = abs(X - D*s)
        v0 = abs(X - D*(s+1))

    ans = 0
    if K < s:
        ans = X - (K * D)
    else:
        if (K - s) % 2 == 0:
            ans = v0
        else:
            ans = v1
    print(ans)

if __name__ == "__main__":
    resolve()

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

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

if X+K*D < 0:
  print(abs(X+K*D))
elif X-K*D > 0:
  print(abs(X-K*D))
elif X+K*D == 0:
  print(0)
elif X-K*D == 0:
  print(0)
else:
  if X < 0:
    K1 = int(-X/D)+1
    K2 = K-K1
    if not K2 % 2:
      print(abs(X+K1*D))
    else:
      print(min(abs(X+K1*D+D), abs(X+K1*D-D)))
  else:
    K1 = int(X/D)+1
    K2 = K-K1
    if not K2 %2:
      print(abs(X-K1*D))
    else:
      print(min(abs(X-K1*D+D), abs(X-K1*D-D)))

Solution (Python):
from sys import stdin
X, K, D = [int(i) for i in stdin.readline().split()]
pos = X
if abs(X) >= K * D:
    pos = abs(X) - (K * D)
else:
    res = abs(X) % D
    res_2 = abs(D) - res
    per = abs(X) // D
    if (K - per) % 2 == 0:
        pos = res
    else:
        pos = res_2
print(pos)

Solution (Python):


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

def sol(x, k, d):

    x = abs(x)

    temp = x // d

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

    x -= temp * d
    k -= temp

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

print (sol(x, k, d))

Solution (Python):

def resolve():
    X, K, D = list(map(int, input().split()))
    X = -X if X < 0 else X
    m = X//D
    if m < K:
        if X%D > D-X%D:
            nmove = m+1
            print(D-X%D if (K-nmove)%2==0 else X%D)
        else:
            nmove = m
            print(X%D if (K-nmove)%2==0 else D-X%D)
    else:
        nmove = m
        print(X-K*+D)


if '__main__' == __name__:
    resolve()

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

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

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

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

need = abs(x)//d

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

while x>0 and k>0:
    x-=d
    k-=1

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

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

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

print(ans)

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

def solve():
    if (0 - x) >= k * d:
        return abs(x + k * d)
    s = (0 - x) // (d)
    if (k - s) % 2 == 0:
        return abs(x + d * s)
    return abs(x + d * (s + 1))

print(solve())

Solution (Python):


from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key
from bisect import bisect_left


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


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

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

def bindex(a,x):
    i = bisect_left(a,x)
    if i != len(a) and a[i] == x:
        return i
    return None

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    X, K, D = mulinputs()
    X = abs(X)
    p = int(X//D)
    q = K - p
    r = X - (D*p)
    if q == 0:
        print(abs(r))
    elif q < 0:
        print(abs(X-(D*K)))
    elif q%2==0:
        print(abs(r))
    else:
        print(min(abs(r+D), abs(r-D)))

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

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

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

print(ans)

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

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

    if x > 0:

        if x >= d:
            steps = min(k, x // d)
            k -= steps
            x -= steps * d

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

            return
    
    if x < 0:
        if  x <= -d:
            steps = min(k, -x // d)
            k -= steps
            x += steps * d

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

            return

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


    print(abs(x))

solve()

Solution (Python):
import math













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

def calculate(x, k, d):
    x = abs(x)
    d = abs(d)
    n = x / d
    kk = math.floor(n)

    if k <= kk:
        return x - k * d

    k = k - kk

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


result = calculate(X, K, D)

print(result)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = sys.argv[-1]!='ONLINE_JUDGE'

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    pass
    
    

def main():
    X,K,D=mis()
    X = abs(X)
    ZP = X%D
    if abs(X - ZP)//D > K:
        dprint('too far')
        print(X - D*K)
        return
    ZN = ZP - D
    X, K = ZP, K - abs(X - ZP)//D
    if K&1:
        dprint('odd')
        print(abs(ZN))
    else:
        dprint('even')
        print(abs(ZP))


main()

Solution (Python):
def check():
    X, K, D = list(map(int, input().split(' ')))
    
    if K*D <= abs(X):
        if X > 0:
            return X - K*D
        else:
            return X + K*D
            
    N = abs(X) // D
    signX = -1 if X < 0 else 1
    X = X * signX
    
    nearest_point = X - N*D
    
    if (K-N) % 2 == 0:
        return nearest_point
    else:
        return nearest_point - D

print(abs(check()))

Solution (Python):
import sys,math
try:sys.stdin,sys.stdout=open('input.txt','r'),open('out.txt','w')
except:pass
from sys import stdin,stdout;mod=int(1e9 + 7);from statistics import mode
from collections import *;from math import ceil,floor,inf,factorial,gcd,log2,sqrt,log
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()


def lcm(a,b): return (a*b)//gcd(a,b)
def setbits(n):return bin(n).count('1')
def resetbits(n):return bin(n).count('0')
def modinv(n,p):return pow(n,p-2,p)
def ncr(n,r):
    num,den=1,1;r=min(n,n-r)
    for i in range(r):num*=(n-i);den*=(i+1)
    return num//den
def ncr_p(n, r, p):
    num,den=1,1;r=min(r,n-r)
    for i in range(r):num = (num * (n - i)) % p ;den = (den * (i + 1)) % p
    return (num * modinv(den,p)) % p
def isPrime(num) :
    if num<=1:return False
    if num==2 or n==3:return True
    if (num % 2 == 0 or num % 3 == 0) :return False
    m = int(num**0.5)+1
    for i in range(5,m,6):
        if (num % i == 0 or num % (i + 2) == 0) :return False
    return True
def bin_search(arr, low, high, val):
    while low <= high:
        mid = low + (high - low) // 2;
        if arr[mid] == val:return mid
        elif arr[mid] < val:low = mid + 1
        else:high = mid - 1
    return -1
def sumofdigit(num):
    count=0;
    while num : count+=num % 10;num //= 10;
    return count;
def inputmatrix():
    r,c=iia();mat=[0]*r;
    for i in range(r):mat[i]=iia();
    return r,c,mat;
def prefix_sum(n,arr):
    for i in range(1,n):arr[i]+=arr[i-1]
    return arr;
def binomial(n, k):
    if 0 <= k <= n:
        ntok = 1;ktok = 1
        for t in range(1, min(k, n - k) + 1):ntok *= n;ktok *= t;n -= 1
        return ntok // ktok
    else:return 0
def divisors(n):
    res = [];
    for i in range(1,ceil(sqrt(n))+1):
        if n%i == 0:
            if i==n//i:res.append(i)
            else:res.append(i);res.append(n//i)
    return res;


x,k,d=iia()

xx=abs(x)

if k*d<=xx:
    ans1=abs(xx-k*d )
    print(ans1)
else:
    ans1,ans2=0,0
    step=xx//d
    rs=k-step

    f=abs(xx-step*d)
    s=abs(xx-step*d-d)
    if rs%2==0:
        a=f
    else:
        a=s
    print(a)

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

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

x = abs(x)

inf = float('INF')

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

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

ans = abs(ans)

print(ans)

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

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

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

        print(abs(X%D))
    else:
        print(abs(X%D-D))

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

if least>=K:
    ans = X - K*D
elif (K-least)%2==0:
    ans = X - least*D
elif (K-least)%2==1:
    ans = -1*(X-(least+1)*D)

print(ans)

Solution (Python):
def main(X, K, D):
    x = abs(X)
    k = K

    shou, amari = x // D, x % D
    if shou >= k:   
        x -= k * D
        k = 0
    else:
        k -= (shou + 1)
        x -= (shou + 1) * D

    if k % 2:    
        print(abs(x + D))
    else:   
        print(abs(x))

if __name__ == '__main__':
    X, K, D = list(map(int, input().split()))
    main(X, K, D)

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

a = input().split()
x = abs(int(a[0]))
count = int(a[1])
d = int(a[2])

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

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

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
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 TUPLE(): return tuple(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 

from decimal import *



X, K, D = MAP()

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

flg = X//abs(X)
n = abs(X)//D

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

if K <= n:
	print(abs(X-K*D*flg))
	exit()

X -= flg*n*D
if X == 0:
	flg = 1
else:
	flg = X//abs(X)
K -= n

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

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

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
mod = x % d
max_move = x - k * d

if mod < max_move:
    res = max_move
else:
    rest = k - x // d
    if rest % 2 == 0:
        res = mod
    else:
        res = abs(mod - d)

print(res)

Solution (Python):
import sys
input = sys.stdin.readline
x,k,d=map(int,input().split())
x=abs(x)
if x<d:
    if k&1==0:
        print(x)
    else:
        print(abs(x-d))
    sys.exit()

val = x//d

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

x-=(val*d)
k-=val
if k&1==0:
    print(x)

else:
    ans = min(x+d,abs(x-d))
    print(ans)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

X,K,D = I()
if X >= 0:
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        f = 0
    else:
        f = 1
    if (X//D)%2 != (K-X//D)%2:
        if f == 0:
            f = 1
        else:
            f = 0
    if f == 0:
        print(abs(num1))
    else:
        print(abs(num2))
else:
    X *= -1
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        f = 0
    else:
        f = 1
    if (X//D)%2 != (K-X//D)%2:
        if f == 0:
            f = 1
        else:
            f = 0
    if f == 0:
        print(abs(num1))
    else:
        print(abs(num2))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]


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


    
if __name__ == "__main__":
    main()

Solution (Python):
v = input().split(" ")
x = int(v[0])
k = int(v[1])
d = int(v[2])

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


print(ans)

Solution (Python):

import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Y = abs(X)
n = Y // D 
s = Y % D

N = K-n
if N < 0:
    print(Y-K*D)
    sys.exit()
    
if N % 2 == 0:
    print(s)
else:
    print(abs(D)-s)

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

x = abs(X)
k = K

xd = min(x // D + 1, K)
x -= D * xd
k -= xd

if k % 2 == 1:
    x += D

print(abs(x))

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

Solution (Python):

import math
from queue import PriorityQueue
import bisect
import copy
import time
from collections import Counter


x,k,d=map(int,input().split())
x=abs(x)
if x%d==0:
	c=x//d
	if c==k:
		print(0)
	elif c<k:
		k=k-c
		if k%2==0:
			print(0)
		else:
			print(d)
	else:
		print(x-k*d)
else:
	c1=x-(x//d)*d
	c2=abs(c1-d)
	cc1=x//d
	cc2=cc1+1
	if k==cc1:
		print(c1)
	elif k==cc2:
		print(c2)
	elif cc1<k:
		k=k-cc1
		if k%2==0:
			print(c1)
		else:
			print(c2)
	else:
		print(x-k*d)

Solution (Python):


def dbg(s):
    
    pass


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

k_steps = abs(X)//D
x = absX - K*D
mod = abs(X) % D
if k_steps >= K:
    print(x)
    exit()

remain_steps = K - k_steps

if remain_steps % 2 == 0:
    x = mod
else:
    x = D - mod
print(x)

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

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

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

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

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

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

Solution (Python):

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

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

X = max(X, -X)

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