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

X = abs(X)

if X >= K*D:
    print(X-K*D)
else:
    t,r = divmod(X-D, D)
    if r>0:
        t+=1
    K -= t
    
    if K%2==0:
        print(X - t*D)
    else:
        print(abs(X - (t+1)*D))

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

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

Solution (Python):







def solve(x,k,d):
  cand1 = x % d
  cand2 = -(abs(cand1) -  d)
  delta_c1 = abs(cand1 - x)
  delta_c2 = abs(cand2 - x)
  if abs(x//d) > k:
    return abs(x) - k*d
  elif (k-x//d) % 2 == 0:
    return cand1
  else:
    return cand2



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

if __name__ == '__main__':
  main()

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

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


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    X,K,D=mi()

    if X == 0:
        ans = D if K % 2 == 1 else 0
        print(ans)
        exit()
    
    n = abs(X)//D
    ans = INF
    for n2 in [n-1,n,n+1]:
        m = K-n2

        if X > 0:
            res = X - D*n2
        else:
            res = X + D*n2

        if m <= 0:
            if X > 0:
                res = X - D*K
            else:
                res = X + D*K
            ans = min(ans,abs(res))
            continue


        if m % 2 == 1:
            res = min(abs(res + D), abs(res-D))
        else:
            res = res
        
        ans = min(ans,abs(res))
    
    print(ans)






if __name__ == "__main__":
    main()

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

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

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

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

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

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

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

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

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

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

Solution (Python):


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

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

    if (K-N)%2!=0:
        ans=(N+1)*D-abs(X)
        if ans > abs(abs(X)-(N-1)*D):ans = abs(abs(X)-(N-2)*D)
        if ans > abs(abs(X)-(N+3)*D):ans = abs(abs(X)-(N+2)*D)

print(ans)

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

need, mod = divmod(x, d)
if k <= need:
    ans = x - k*d
else:
    amari = k - need
    if amari % 2 == 0:
        ans = mod
    else:
        ans = mod - d


print(abs(ans))

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


X = abs(X)

K1 = X // D
K2 = K1 + 1

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

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

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    x, k, d = map(int, input().split())
    if x == 0:
        if k % 2 == 0:
            print(abs(x))
        else:
            r = abs(abs(x) - d)
            print(r)
    elif abs(x) < d:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(abs(abs(x) - d))
    else:
        t1 = abs(x) // d
        if t1 > k:
            r = abs(x) - k * d
            print(r)
        else:
            k -= t1
            x = abs(x) - t1 * d
            if k % 2 == 0:
                print(x)
            else:
                print(abs(x - d))

if __name__ == '__main__':
    main()

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

Solution (Python):

def main():
    X, K, D = list(map(int, input().split()))
    right = X % D
    left = right - D
    if X in [left, right]:
        if K % 2 == 0:
            print(abs(X))
        else:
            if X == left:
                print(right)
            else:
                print(abs(left))
        return
    if X > right:
        to_right = (X - right) // D
        if to_right >= K:
            print(X - K * D)
        else: 
            if (K - to_right) % 2 == 0:
                print(right)
            else:
                print(abs(left))
    else:  
        to_left = (left - X) // D
        if to_left >= K:
            print(abs(X + K * D))
        else: 
            if (K - to_left) % 2 == 0:
                print(abs(left))
            else:
                print(right)


if __name__ == '__main__':
    main()

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

X = abs(X)
a=X%D
b=D-a

at=(X-a)//D
bt=at+1

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

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

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

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

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

a = abs(x) % d

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

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

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

x = X % D
k = K - abs(X//D)

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

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

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

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

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

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

Solution (Python):
def solve(x, k, d):
    if x > 0 and x - k * d > 0:
        return x - k * d
    elif x < 0 and x + k * d < 0:
        return abs(x + k * d)
    elif k % 2 == 0:
        a = x % (2 * d)
        return min(a, abs(a - 2 * d))
    else:
        a = (x - d) % (2 * d)
        return min(a, abs(a - 2 * d))





















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

Solution (Python):
"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""


import sys
from functools import lru_cache, cmp_to_key
from collections import defaultdict as dd, deque, Counter as C
from bisect import bisect_left as bl, bisect_right as br, bisect
from heapq import heapify, heappop, heappush
from math import ceil, log, floor, sqrt
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+end)
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]


x, k, d = sp()
if x < 0:
    neg = True
else:
    neg = False
x = abs(x)
mv = x // d
t1, t2 = x - mv * d, x - (mv + 1) * d
if k <= mv:
    out(abs(x - k * d))
    exit()
if mv & 1:
    if k & 1:
        out(abs(t1))
    else:
        out(abs(t2))
    exit()
if k & 1:
    out(abs(t2))
else:
    out(abs(t1))

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

Solution (Python):

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

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

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

def main(x,k,d):
    x=abs(x)
    q=x//d
    r=x%d
    if k<q:
        ans=x-k*d
    else:
        if (k-q)%2==0:
            ans=r
        else:
            ans=d-r
    return ans

if __name__=='__main__':
    x,k,d=readinput()
    ans=main(x,k,d)
    print(ans)

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

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

Solution (Python):


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

X = abs(X)

ans = 0
kan = X // D

if kan > K:
    ans = abs(X - K * D)
else:
    rest = K - kan
    if rest % 2 == 0:
        ans = abs(X - kan * D)
    else:
        ans = abs(X - (kan + 1) * D)

print("{}".format(ans))

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

ans = 0
if X - D * K >= 0:
    ans = X - D * K
elif X + D * K <= 0:
    ans = -(X + D * K)
else:
    n = int((K - X / D) / 2)
    ans = abs(X + D * (2 * n - K))
    n += 1
    ans = min(ans, abs(X + D * (2 * n - K)))

print(ans)

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

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

Solution (Python):




import sys
from io import StringIO
import unittest

def resolve():

    
    X, K, D = map(int, input().split())
    
    if abs(X)-K*D>0:
        print("{}".format(abs(X)-K*D))
    else:
        mod = abs(X)%D
        modk = int(abs(X)/D)
        if (K-modk)%2==0:
            print("{}".format(mod))
        else:
            print("{}".format(D - mod))

    


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """6 2 4"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """7 4 3"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 1 2"""
        output = """8"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(input, output)


if __name__ == "__main__":
	resolve()

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

Solution (Python):
import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    X, K, D = MI()


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



    
    print(ans)

if __name__ == "__main__":
	main()

Solution (Python):


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

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

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

k_i = abs(x)//d

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

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

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

print(res)

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

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

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

Solution (Python):

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

x = abs(x)

remain_k = k - x // d
r = x % d
l = d - r

if remain_k <= 0:
    print(x - k * d)
elif remain_k % 2 == 0:
    print(r)
else:
    print(l)

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

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

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

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

Solution (Python):
def main():
    X,K,D = map(int,input().split())
    X = abs(X)
    
    if X-D*K >= 0:
        print(X-D*K)
    else:
        k0 = X//D
        x0 = X%D
        x1 = abs(x0-D)
        if (K - k0)%2==0:
            print(x0)
        else:
            print(x1)
    
if __name__ == '__main__':
    main()

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

if x < 0:
    x = (-1)*x
if k*d <= x:
    ans = x-k*d
else:
    sho = x//d
    now = x-d*sho
    k -= sho
    if d-now < now:
        k -= 1
        now = d-now
    if k%2 == 0:
        ans = now
    else:
        ans = d-now
print(ans)

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

print(abs(X+plus*D-minus*D))

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

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


def f(C):
    NX = float("inf")
    if C > K:
        return NX

    if K % 2 == 0:
        if C % 2 == 0:
            NX = abs(abs(X) - C * D)
        else:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
    else:
        if C % 2 == 0:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
        else:
            NX = abs(abs(X) - C * D)

    return NX


C = min(abs(X) // D, K)

print(min(f(C), f(C + 1)))

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

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

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

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

if (abs(X)>=K*D):
    ans = abs(abs(X)-K*D)
else:
    
    m = abs(X)//D
    remk = K - m

    now = abs(X)-m*D

    if remk%2==0:
        ans = now
    else:
        ans = abs(now-D)



    
print(ans)

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

X = abs(X)
k1 = min(K, X//D)
X -= k1 * D
K -= k1
if K&1: X -= D

print(abs(X))

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

a, b = divmod(X, D)


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

_K = K - a

if _K % 2 == 0:
    ans = b
else:
    ans = abs(b - D)
print(ans)

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

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

print(n)

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



if abs(x)>k*d:
    print( abs(abs(x)-(k*d)) ) 
        
else:
    
    if k%2==0:
        
        kaisuu_odd_0 = abs(x)//(2*d) *2
        kyori_odd_0 = abs( abs(x)-kaisuu_odd_0*d )
        
        kaisuu_odd_1 = kaisuu_odd_0 + 2
        kyori_odd_1 = abs( abs(x)-kaisuu_odd_1*d )
        
        print(min(kyori_odd_0,kyori_odd_1))
        
    else:
        kaisuu_odd_0 = abs(x)//(2*d) *2 +1
        kyori_odd_0 = abs( abs(x)-kaisuu_odd_0*d )
        
        kaisuu_odd_1 = kaisuu_odd_0 - 2
        kyori_odd_1 = abs( abs(x)-kaisuu_odd_1*d )
        
        print(min(kyori_odd_0,kyori_odd_1))

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

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

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

minx=x-k*d
maxx=x+k*d
targetx=minx%(2*d)
if targetx>d:
    targetx-=2*d
if targetx==d:
    if x<0:
        targetx-=2*d

if minx>targetx:
    print(abs(minx))
    exit()
if maxx<targetx:
    print(abs(maxx))
    exit()
print(abs(targetx))

Solution (Python):
X,K,D = map(int, input().split())
if X < 0:
    X = -X
div, mod = divmod(X,D)
if K <= div:
    print(X - K*D)
else:
    remain = K - div
    if remain % 2 == 1:
        print(D - mod)
    else:
        print(mod)

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

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

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

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

Solution (Python):
import math

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

if (abs(X) >= K_kai * D):
    ans = abs(abs(X) - abs(K_kai * D))

else:
    times_low = math.floor(abs(X) / D)
    times_hig = times_low + 1
    
    
    if ((K_kai - times_low) % 2 == 0):
        ans = abs(abs(X) - times_low * abs(D))
            
    else:
        ans = abs(abs(X) - times_hig * abs(D))

print(ans)

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

Solution (Python):




def resolve():
    import sys
    input = sys.stdin.readline

    x, k, d = [int(x) for x in input().rstrip().split(" ")]

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

    
    step = x//(d)
    x -= d*step
    k -= step

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


if __name__ == "__main__":
    resolve()

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

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

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

x = abs(x)

n = x // d
q = x % d
p = q - d

if n < k:
    l = k - n

    if l % 2 == 0:
        print(q)

    else:
        print(abs(p))

else:
    e = x - k * d
    print(e)

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

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

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

if x < 0:
    x = abs(x)

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

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

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

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

Solution (Python):


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

if x < 0:
    x *= -1

num = x//d

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

Solution (Python):


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

    syo = X//D
    if syo >= K:
        ans = X - D*K
        print(ans)
        return

    
    plus = X - D*syo  
    minus = plus - D  
    restK = K - syo - 1

    if restK%2 == 0:
        print(abs(minus))
        return
    print(plus)
    return


if __name__ == "__main__":
    solve()