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):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

x, k, d = rm()
t = abs(x) // d
if t > k:
    if x > 0:
        print(abs(x - k*d))
    else:
        print(abs(x + k*d))
    exit()
if x > 0:
    if (k - t) & 1:
        print(abs(x - t*d - d))
    else:
        print(abs(x - t*d))
else:
    if (k - t) & 1:
        print(abs(x + t*d + d))
    else:
        print(abs(x + t*d))

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

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

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

if x < 0:
  x *= -1
  
if x / k > d:
  print(x - k*d)
  sys.exit()

count = k - x // d

if count % 2 == 0:
  print(x - (d * (x//d)))
else:
  print(-(x - (d * (x//d))-d))

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

ans = 0
step = abs(X) // D
if step >= K:
    ans = abs(X) - D*K
else:
    cur = abs(X)%D
    rest = K - step
    
    
    if rest%2 != 0:
        ans = min(abs(cur+D), abs(cur-D))
    else:
        ans = cur

print(ans)

Solution (Python):

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

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

else:
    K2 = abs(X) // D
    ato = K-K2
    now = abs(X) - D*K2
    if ato % 2 ==0:
        out = now
    else:
        out = abs(now - D)

print(out)

Solution (Python):
X, K, D = map(int, input().split())
ans = None
abx = abs(X)
if abx/D > K:
     ans = abx - D*K
else:
    tmp = abx//D
    ans = abs(abx - D*tmp)
    if (K - tmp) % 2 == 1:
        ans = abs(ans - D)

print(ans)

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

x_ = abs(x)

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

a = x_ % d
b = - (a - d)
k_ = x_ // d
l = k - k_

if l % 2 == 0:
    print(a)
else:
    print(b)

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

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

if x < 0:
    x = -x

p = (x + d - 1) // d

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

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

if x<=0:
  x*=(-1)
shou=x//d
amb=x%d
if k<=shou:
  print(x-k*d)
  exit()
else:
  if (k-shou)%2==0:
    print(amb)
    exit()
  else:
    print(d-amb)
    exit()

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

m=int(abs(X)/D)

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

print(abs(ans))

Solution (Python):
x,k,d = map(int,input().split())
n = (-x+k*d)//(2*d)
cur = float('inf')
if n < 0:
    print(abs(x-k*d))
else:
    if n == 0:
        L = [0,1]
    else:
        if n >= k:
            L = [k-1,k]
        else:
            L = [n-1,n,n+1]
    for i in range(len(L)):
        cur = min(cur, abs(x+L[i]*d-(k-L[i])*d))
    print(cur)

Solution (Python):
X,K,D = list(map(int,input().split()))
x = abs(X)
a = abs(X)//D
b = x - D*a

if a  > K:
    print(abs(x-D*K))
else:
    if (K-a) %2 ==0:
        print(abs(b))
    else:
        print(abs(abs(b)-D))

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

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

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

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def main():
    x, k, d = MII()
    if k*d <= abs(x):
        if x >= 0:
            print(x-k*d)
        else:
            print(abs(x+k*d))
    else:
        m = x//d
        n = x%d
        if (k-m)%2==0:
            print(n)
        else:
            print(abs(n-d))

if __name__ == '__main__':
    main()

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

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

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

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

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

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

Solution (Python):
x, k, d = [int(x) for x in input().split()]
if x < 0: x = -x
jmps = x//d
if (jmps >= k):
    x = x - d*k
    print(x)
else:
    k -= (x//d)
    x = x - (x//d)*d

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

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

Solution (Python):
input = raw_input

x, k, d = map(int, input().split(" "))
is_plus = x >= 0
x = abs(x)

r = x // d
if r > k:
    y = x - k*d
else:
    y1 = x - r*d
    y2 = x - (r+1)*d
    k2 = k-r
    if k2 % 2 == 0:
        y = y1
    else:
        y = y2

y = abs(y)
print(y)

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

Solution (Python):
def main():
    orig, count, dist = [int(x) for x in input().split()]
    orig = abs(orig)
    q, mod = divmod(orig, dist)
    if q > count:
        return orig - dist * count
    else:
        if (count - q) % 2 == 0:
            return mod
        else:
            return abs(mod - dist)


if __name__ == '__main__':
    print(main())

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

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

x = abs(X)
n = x // D
e = x % D

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

Solution (Python):
X,K,D = (int(x) for x in input().split())
if X > 0:
    num = int(X/D)
    ans = 0
    if num >= K:
        ans = X - D*K
    else:
        a = X - num*D
        b = X - (num+1)*D
        if (K-num)%2 == 0:
            ans = a
        else:
            ans = b
else:
    num = int(-X/D)
    ans = 0
    if num >= K:
        ans = X + D*K
    else:
        a = X + num*D
        b = X + (num+1)*D
        if (K-num)%2 == 0:
            ans = a
        else:
            ans = b
print(abs(ans))

Solution (Python):





def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return x

def str_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(input()))
    return x

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

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

def print_space(l):
    return print(" ".join([str(x) for x in l]))


import numpy as np




X,K,D = int_map()

if X == 0:
    if K%2 == 0:
        print(0)
        exit()
    else:
        print(D)
        exit()
elif X>0:
    if D*K < X:
        print(X-D*K)
        exit()
    else:
        a = X%D
        N = X//D
        if (K-N)%2 == 0:
            print(a)
            exit()
        else:
            print(D-a)
            exit()
else:
    X = abs(X)
    if D*K < X:
        print(X-D*K)
        exit()
    else:
        a = X%D
        N = X//D
        if (K-N)%2 == 0:
            print(a)
            exit()
        else:
            print(D-a)
            exit()

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

Solution (Python):
import sys
from io import StringIO
import unittest


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

    m = abs(x) // d

    if k < m:
        print(abs(x) - k * d)
    else:
        x = abs(x) - m * d
        k -= m

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


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 k*d < abs(x):
    ans = abs(x)-k*d
    print(ans)
    exit()
x = abs(x)
n = x//d
ans = x-n*d
nn = k-n
if nn % 2 == 0:
    print(ans)
else:
    print(abs(ans-d))

Solution (Python):
x, k, d = [int(w) for w in input().split()]
x = abs(x)

t = x//d

ans = 0
if k <= t:
    ans = x-k*d
else:
    ans = x-d*t
    k -= t
    if k % 2 == 1:
        ans -= d

print(abs(ans))

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

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

a = abs(x) // d
b = abs(x) // d + 1

if a > k:
    print(abs(x) - k * d)
else:
    if b < a:
        a = b

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

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

ans = None

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

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

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

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

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

print(abs(ans))

Solution (Python):






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

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

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

main()

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

minimum_try = min(abs(X) // D, K)
tmp_goal = abs(abs(X) - minimum_try*D)

if (K-minimum_try)%2==0:
  res1 = tmp_goal
else:
  res1 = abs(tmp_goal-D)

minimum_try = min((abs(X) // D) + 1, K)
tmp_goal = abs(abs(X) - minimum_try*D)

if (K-minimum_try)%2==0:
  res2 = tmp_goal
else:
  res2 = abs(tmp_goal-D)

print(min(res1, res2))

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

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

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

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

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

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

minusFLG = 0
if X < 0:
    minusFLG = 1
    X = abs(X)

minX = X % D
minXmove = X // D


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


zanMove = K - minXmove
if zanMove % 2 == 0:
    print(minX)
else:
    print(abs(minX - D))

Solution (Python):
ns = [int(ls) for ls in input().split()]

X = ns[0]
K = ns[1]
D = ns[2]

if X + K * D <= 0:
  print(abs(X + K * D))
elif X - K * D >= 0:
  print(X - K * D)
elif X + (K - 2) * D <= 0:
  print(min(abs(X + K * D),abs(X + (K - 2) * D)))
elif X - (K - 2) * D >= 0:
  print(min(abs(X - K * D),abs(X - (K - 2) * D)))
else:
  if K % 2 == 1:
    K -= 1
    X += D
  n = X // (D * 2)
  print(min([abs(ns) for ns in
             [X - (2 * a * D) for a in list(range(n-2,n+3))]]))

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

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

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

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

Solution (Python):
import math

def main():
    x,k,d = map(int,input().split())
    x = abs(x)
    req = int(math.ceil(x/d))-1
    if req >= k:
        print(x-k*d)
    else:
        x -= req*d
        k -= req
        x1 = x
        x2 = abs(x1-d)
        if x1 <= x2:
            if k%2 == 0:
                print(x1)
            else:
                print(x2)
        else:
            if k%2 == 1:
                print(x2)
            else:
                print(x1)


main()

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

bigger = 0
smaller = 0

if x >= 0:
    bigger = x - min(x // d, k) * d
    smaller = bigger - d
else:
    smaller = x + min((-x) // d, k) * d
    bigger = smaller + d

if abs(x) // d >= k:
    bigger = abs(bigger)
    smaller = abs(smaller)
    
    if x >= 0:
        print(bigger)
    else:
        print(smaller)
else:
    k -= abs(x) // d
    bigger = abs(bigger)
    smaller = abs(smaller)

    if x >= 0:
        if k % 2 == 0:
            print(bigger)
        else:
            print(smaller)
    else:
        if k % 2 == 0:
            print(smaller)
        else:
            print(bigger)

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

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

Solution (Python):
import sys
import math

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

a = x-k*d
b = x+k*d

if(a >= 0):
	print(a)
elif(b <= 0):
	print(abs(b))
else:
	b = b % (d * 2)
	print(min(abs(b), abs(b - d * 2)))

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

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

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

Solution (Python):
import sys

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

num = x // d

if k < num:
    print(x - d * k)
    sys.exit()
    
num -= k

if num % 2 == 0:
    print(x % d)
else:
    print(d - x % d)

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

x = abs(x)

t = x // d

if t > k:
  ans = x - k * d
else:
  pos = x - t * d
  r = k - t
  if r % 2 == 0:
    ans = pos
  else:
    ans = abs(pos - d)

print(ans)

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

mod = abs(X) % D
ura = D - mod
if mod <= ura:
  if abs(X)-D*K > mod:
    ans = abs(X)-D*K
  else:
    n = (abs(X)-mod) // D
    if (K-n) % 2 == 0:
      ans = mod
    else:
      ans = ura
else:
  if D*K-abs(X) < ura:
    ans = abs(X)-D*K
  else:
    n = (abs(X)+ura) // D
    if (K-n) % 2 == 0:
      ans = ura
    else:
      ans = mod
print(ans)

Solution (Python):
import sys
X, K, D = map(int, input().split())
if X < 0:
    X *= -1
if K <= X // D:
    X -= D * K
    print(X)
    sys.exit()
N = X // D
K -= N
X -= N * D
if K % 2 == 0:
    print(X)
else:
    X -= D
    print(-X)

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

Solution (Python):
X,K,D=map(int,input().split())
X=abs(X)
if X>=0:
    temp=X//D
    if temp>=K:
        last=X-K*D
        print(last)
        exit()
    else:
        ktemp=K-round(temp)
        if ktemp%2==0:
            last=X-D*(round(temp))
            print(abs(last))
            exit()
        else:
            last=X-D*(round(temp))
            last=min(abs(last+D),abs(last-D))
            print(last)
            exit()

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

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

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

Solution (Python):
X,K,D=map(int,input().strip().split())
X=abs(X)
a=X//D
b=X%D

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

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

passSinaiCount = abs(x)//d

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

passSinaiDistance = abs(x) - passSinaiCount * d
passSuruDistance = d - passSinaiDistance

nokoriCount = k - passSinaiCount
if nokoriCount % 2 == 0:
    print(passSinaiDistance)
else:
    print(passSuruDistance)

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

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

x = abs(x)
div,mod = divmod(x,d)

cnt = min(k,div)
k -= cnt
ans = x - cnt * d

if k > 0:
    ans -= (k%2) * d

print(abs(ans))

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

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


if __name__ == '__main__':
    main()

Solution (Python):

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

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

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

X=abs(x)

y=X//d

if k<=y:
  ans=X-k*d
  print(ans)
  
else:
  ans1=X-y*d
  ans2=abs(X-y*d-d)
  if (k-y)%2==0:
    print(ans1)
  else:
    print(ans2)

Solution (Python):

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

a=abs(X)//D

if K<a:
    print(abs(X)-K*D)

elif (K-a)%2==0:
    print(abs(X)%D)

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

Solution (Python):

import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

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

if abs(D * K) > abs(X):
    is_over = True
else:
    is_over = False

if not (is_over):
    print(abs(X) - abs(D * K))
else:
    m = abs(X) // abs(D)
    r = K - m
    val1 = abs(X) % abs(D)
    val2 = val1 - abs(D)
    
    if r % 2 == 0:
        print(abs(val1))
    else:
        print(abs(val2))

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

X = abs(X)

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

Solution (Python):
import copy
import math
import time
import statistics
import math
import itertools
import bisect
from decimal import *


def get_int():
    return int(input())

def get_string():
    return input()

def get_int_list():
    return [int(x) for x in input().split()]

def get_string_list():
    return input().split()

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

def get_string_char_list():
    return list(str(input()))


'''
while (idx < n) and ():

    idx += 1
'''

def main():

    start = time.time()

    x, k, d = get_int_multi()

    wk = k * d

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

    print(ans)



if __name__ == '__main__':
    main()

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

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

x=abs(x)
idoukaisuu=x//d
nokorikyori=x%d

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

x,k,d=MI()
if x>0:
    c=(x+d-1)//d
    if k<c:
        ans=abs(x-d*k)
    else:
        if k&1==c&1:
            ans=abs(x-d*c)
        else:
            ans=abs(x-d*(c-1))
else:
    c=(-x+d-1)//d
    if k<c:
        ans=abs(x+d*k)
    else:
        if k&1==c&1:
            ans=abs(x+d*c)
        else:
            ans=abs(x+d*(c-1))
print(ans)

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

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


def read():
    X, K, D = map(int, input().strip().split())
    return X, K, D


def solve(X, K, D):
    m = (-X + K*D) / (2*D)
    mc = min(max(0, math.ceil(m)), K)
    mf = min(max(0, math.floor(m)), K)
    ans = min(abs(X - K*D + 2 * mc * D), abs(X - K*D + 2 * mf * D))
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

if __name__ == "__main__":
    main()

Solution (Python):
X,K,D = [int(x) for x in input().split()]
X = abs(X)
a = X//D
if a >= K:
    print(X-K*D)
    exit()
else:
    K = K-a
if K % 2 == 0:
    print(X%D)
else:
    print(D-X%D)

Solution (Python):
import sys
sys.setrecursionlimit(4100000)
import math
import itertools
INF = float('inf')
from heapq import heapify, heappop, heappush

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

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


if __name__ == '__main__':
    main()

Solution (Python):
X,K,D=map(int,input().split())
"""
XからK回移動しなければならない
Dは一回の移動距離
"""
kaisu=abs(X)//abs(D)

one=abs(abs(X)-abs(D)*kaisu)
two=abs(abs(X)-abs(D)*(kaisu+1))


if K<kaisu:
    ans=abs(X)-abs(D)*K
elif K==kaisu:
    ans=one
elif K>kaisu:
    
    
    if (K-kaisu)%2==0:
        ans=one
    elif (K-kaisu)%2!=0:
        ans=two
print(ans)

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

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

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    X, K, D = map(int, readline().split())
    X = abs(X)
    if X // D >= K:
        ans = X - D * K
    else:
        if (K - X // D) % 2 == 0:
            ans = X - X // D * D
        else:
            ans = abs(X - X // D * D - D)


    print(ans)

if __name__ == '__main__':
    main()

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

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

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

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

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

Solution (Python):

import sys,math,collections,itertools
input = sys.stdin.readline


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


L = abs(X)//D
if L>=K:
    ans = abs(X) - K*D

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

Solution (Python):


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

ans = 0

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

print(ans)

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

ans_1 = abs(X - X//D * D)
ans_2 = abs(ans_1-D)

if K <= X//D:
    print(X-K*D)

elif (K - X//D) % 2 == 0:
    print(ans_1)
else:
    print(ans_2)

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