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
readline = sys.stdin.readline

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

if X < 0:
  X = -X

s = X//D

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

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

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

a = abs(x) - k*d

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

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

Solution (Python):
import sys
import numpy as np
X, K, D = list(map(int, input().split()))
x = int(X)
D = int(D)
j = 0
j = abs(X//D)

if j > K:
    j = K


if x > 0:
    x = x - (D*j)
else:
    x = x + (D*j)

j = (K-j) % 2

if j==0:
    print(abs(x))
else:
    if x > 0:
        x = x - D
    else:
        x = x + D
    print(abs(x))

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

k_min=abs(x//d)
if k_min>=k:
  k_min=k
if x>0:
  d_min=x-((k_min)*d)
else:
  d_min=x+((k_min)*d)

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

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

X = abs(X)
aa = X // D

if K < aa :
    X -= K * D
else :
    if (K - aa) % 2 == 1 :
        X -= (aa+1) * D
    else :
        X -= aa * D

print(abs(X))

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

if d * k >= abs(x) and x != 0:
  
  near_0 = x // d
  if x < 0:
    near_0 += 1
  if (k - abs(near_0)) %2 == 0:
    print(abs(x - near_0 * d))
  else:
    hoge = x - near_0 * d + d
    huga = x - near_0 * d - d
    if abs(hoge) < abs(huga):
      print(abs(hoge))
    else:
      print(abs(huga))
else:
  if x == 0:
    if k%2 == 0:
      print(0)
    else:
      print(d)
  elif x > 0:
    print(abs(x-(d*k)))
  else:
    print(abs(x+(d*k)))

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

move_count = 0
D = abs(D)
X = abs(X)

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

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

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

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


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

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

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

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


if K * D < X:
    ans = X - (K * D)
else:
    
    
    rest = X % D
    
    if rest==0:
        
        
        toutatu = X / D
        if (K - toutatu)%2==0:
            ans = 0
        else:
            ans = D
    
    else:
        toutatu = X // D + 1
        
        
        if (K - toutatu)%2==0:
            ans = D-rest
        
        else:
            ans = rest

print(ans)

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

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

print(ans)

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

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

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

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

x = abs(x)
a = x % d
b = x % d - d
ans = abs(x - k * d)
if (x - a) // d <= k and ((x - a) // d - k) % 2 == 0:
    ans = min(ans, a)
if (x - b) // d <= k and ((x - b) // d - k) % 2 == 0:
    ans = min(ans, -b)
print(ans)

Solution (Python):
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    X, K, D = read_values()
    X = abs(X)
    k = X // D
    if k >= K:
        print(X - K * D)
    else:
        K -= k
        X -= k * D
        print(X if K % 2 == 0 else D - X)


if __name__ == "__main__":
    main()

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

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

Solution (Python):
x,k,d = [int(p) for p in input().split()]
x=abs(x)
ans = min(x%d,d-x%d);
if(ans==0):
    p = x//d
    t = k;
    k-=p;
    if(k<0):
        print((x-t*d))
    elif(k%2==0):
        print(0)
    else:
        print(d)
elif(k*d>x):
    p = x//d
    k-=p
    if(x%d==ans):
        if(k%2==0):
            print(ans)
        else:
            print(d-ans)
    else:
        k-=1
        if(k%2==0):
            print(ans)
        else:
            print(d-ans)
else:
    print((x-k*d))

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

Solution (Python):
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
if abs(X)>K*D:
    print(min(abs(X-K*D), abs(X+K*D)))
else:
    X=abs(X)
    N=X//D
    Min=10**16
    n=0
    for i in range(N-1, N+2):
        M=abs(X-i*D)
        if M<Min:
            Min=M
            n=i
    if (K-n)%2==0:
        print (Min)
    else:
        print(min(abs(Min+D), abs(Min-D)))

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


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

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

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

Solution (Python):
nums = list(map(int, input().strip().split(' ')))
x = nums[0]
k = nums[1]
d = nums[2]
x = abs(x)
if x >= k * d:
    print(x - (k * d))
else:
    ans = []
    ans.append(x)
    m = x // d
    k1 = x - (m * d)
    left = k - m
    if(left % 2 == 0):
        ans.append(abs(k1))
    else:
        ans.append(abs(k1 - d))
    sorted(ans)
    print(ans[len(ans) - 1])

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

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

Solution (Python):

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


v=abs(x)//d
if abs(x)>= d:
    if abs(x)//d >= k:
        if x >= 0:
            ans=abs(x-d*k)
        else:
            ans=abs(x+d*k)
    else:
        move=v*d
        dif=k-v
        if x >=0:
            
            
            x2=x-v*d
            
            
            if dif%2==0:
                ans=abs(x-v*d)
            else:
                ans=abs(x2-d)
        else:
            x2=x+v*d
            if dif%2==0:
                ans=abs(x+v*d)
            else:
                ans=abs(x2+d)
else:
    if x>=0:
        x2=x-d
        dif=k-1
        if dif==0:
            ans=abs(x2)
        elif dif%2==0:
            ans=abs(x2)
        else:
            ans=abs(x2+d)
    else:
        x2=x+d
        dif=k-1
        if dif==0:
            ans=abs(x2)
        elif dif%2==0:
            ans=abs(x2)
        else:
            ans=abs(x2-d)
print(ans)

Solution (Python):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    x = kwargs["x"]
    k = kwargs["k"]
    d = kwargs["d"]
    if k * d <= abs(x):
        res = abs(x) - k * d
    else:
        res = abs(abs(x) % d - (k - abs(x) // d) % 2 * d)

    return res

if __name__ == "__main__":
    kwargs = {}

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

    cout = main(kwargs)
    print(cout)

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

sign = 0
if x != 0:
  sign = int(x /abs(x))

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

Solution (Python):


import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	x, k, d = map(int, sys.stdin.readline().split())
	x = abs(x)
	if x == 0:
		if k % 2 == 0:
			print(0)
		else:
			print(d)
		exit()

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




main()

Solution (Python):
x,k,d = map(int,input().split())
ch = k%2
ans = abs(x)
cnt = 0
while True:
    if x == 0:
        if (cnt%2) == ch:
            print(0)
            break
        else:
            print(abs(d))
            break
    if x > 0:
        if x//d > k:
            print(x-k*d)
            break
        cnt += x//d
        x -= d*(x//d)
        xx = x - d
        if cnt%2 == ch:
            print(abs(x))
        else:
            print(abs(xx))
        break
    elif x < 0:
        if abs(x)//d > k:
            print(abs(x+k*d))
            break
        cnt += abs(x)//d
        x += d*(abs(x)//d)
        xx = x + d
        if cnt%2 == ch:
            print(abs(x))
        else:
            print(abs(xx))
        break

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

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

c = x//d

a = x - x//d * d
b = d - abs(a)

if x//d%2 == 0:
    odd = a
    even = b
else:
    odd = b
    even = a

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


print(abs(ans))

Solution (Python):

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

num = 0

x = abs(x)

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

    
print(num)

Solution (Python):




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



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


print(ans)

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

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


sys.setrecursionlimit(999999999)



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

    sho, amari = divmod(abs(x), d)

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

    if x < 0:
        x = -amari
    else:
        x = amari

    k -= sho

    if k % 2 == 1:
        ans = min(abs(x + d), abs(x - d))
    else:
        ans = x

    print(abs(ans))





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

    def test_input_1(self):
        test_input = """6 2 4"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """7 4 3"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """10 1 2"""
        output = """8"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(test_input, output)

    
    def test_1original_1(self):
        test_input = """7 3 3"""
        output = """2"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

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

x = abs(x)

a = x//d

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

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

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

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

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

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

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

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

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

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

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

 
if __name__ == "__main__":
  	main()

Solution (Python):

"""
Created on Sat Aug 15 17:00:46 2020

@author: saito
"""






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


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



print(ans)

Solution (Python):
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

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

m = Decimal(str(x/d)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
m = abs(m)
if k < m:
    m = k

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



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

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

Solution (Python):


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

X_abs = abs(X)
p = X_abs // D

if ( p > K ):
  print( X_abs - D*K )
  
else: 
  K_p = K-p
  
  if ( K_p % 2 == 0 ):
    print( X_abs - D*p )
    
  else:
    print( - X_abs + D*(p+1) )

Solution (Python):


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

X = abs(X)
n = X // D
if K - n <= 0:
    n = K

K -= n
X -= D * n

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

Solution (Python):
l=list(map(int,raw_input().split()))
x=int(l[0])
k=int(l[1])
d=int(l[2])
s = 0
e = (k/2)

ans = int(1e32)

while s<=e :
    m = (s+e)/2
    curr = abs(x+(k-2*m)*d)
    ans = min(curr,ans)
    left = abs(x+(k-2*(m-1))*d)
    right = abs(x+(k-2*(m+1))*d)

    if(left < curr or right < curr):
        if(left<right):
            e=m-1
        else:
            s=m+1
    else:
        break 

s=0
e=(k/2)
ans2= int(1e32)

while s<=e :
    m = (s+e)/2
    curr = abs(x-(k-2*m)*d)
    ans2 = min(curr,ans2)
    left = abs(x-(k-2*(m-1))*d)
    right = abs(x-(k-2*(m+1))*d)

    if(left < curr or right < curr):
        if(left<right):
            e=m-1
        else: 
            s=m+1
    else:
        break 

ans = min(ans2,ans)

print(ans)

Solution (Python):
import math

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

X = abs(X)

max_distance = K * distance

if max_distance <= X:
    print(X - max_distance)
else:
    
    over_zero_movecount = math.ceil(X / distance)

    
    over_zero_x = abs(X - over_zero_movecount * distance)
    
    over_zero_x_before = abs(X - (over_zero_movecount - 1) * distance)

    
    if (K - over_zero_movecount) % 2 == 0:
        print(over_zero_x)
    
    else:
        print(over_zero_x_before)

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

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

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

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

n, x= divmod(abs(X), D)
if n > K:
  print(min(abs(X - D * K), abs(X + D * K)))
  exit()
K = K - n

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

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

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

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

mini = math.ceil(X/D)

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

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

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


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

if X>=0:

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

Solution (Python):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy


 











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

X = abs(X)

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

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

Solution (Python):
A = list(map(int,input().split()))
X,K,D = A[0], A[1], A[2]

if abs(X) >= K*D:
    print(abs(X)-K*D)
elif X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
else:
    d = abs(X) // D
    
    amari = abs(X) % D
    
    hantai = D - amari
    
    
    if (K - d) % 2 == 0:
        print(amari)
    else:
        print(hantai)

Solution (Python):
from sys import stdin, stdout,stderr
import time,math
from collections import defaultdict as dd, Counter
 
def pn(ob):stdout.write(str(ob))
def inp():return stdin.readline()
 
def main():
	x, k, d = map(int, inp().split())
	if(x < 0):
		x *= -1

	if(x >= k * d):
		print(x - k * d)
	else:
		n = x // d
		if(n % 2 == k % 2):
			print(x % d)
		else:
			print(abs(x - (n + 1) * d))
		
  
if __name__ == '__main__':
	main()

Solution (Python):


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

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


xi = abs_calc(x)
a = xi // d
if a >= k:
    ans = xi - d * k
else:
    k  -= a
    ans = xi - d * a
    if  k %  2 != 0:
        ans =  d - ans
print(ans)

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

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

Solution (Python):
from sys import stdin,stdout
input = stdin.readline
def write(n,sep="\n"):
	stdout.write(str(n))
	stdout.write(sep)
def gil():
	return list(map(int, input().split()))
X, K, D = map(int, input().split())
X = abs(X)
if X >= K * D:
    print(abs(X - K * D))
    exit()
mov = X // D    
if abs(X-mov*D) > abs(X-(mov+1)*D):
    mov += 1
rem = abs(X-mov*D)
mov = K - mov   
mov = mov % 2
print(abs(rem-mov*D))

Solution (Python):

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


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

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
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 li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]





N, K, D = mi()
N = abs(N)

if K * D > N:
    num = N//D
    if not (num-K)%2:
        print(min(abs(N - D * (N//D)), abs(N - D * (N//D) - 2 * D)))
    else:
        print(min(abs(N - D * (N//D) + D), abs(N - D * (N//D) - D)))
else:
    print(N - D * K)

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
X,K,D=map(int,input().split())
L=min(X+D*K,X-D*K)
R=max(X+D*K,X-D*K)

if 0<=L:
    print(L)
elif R<=0:
    print(abs(R))
else:
    X=L
    start=0
    end=K
    while end-start>1:
        test=(end+start)//2
        if X+test*2*D<=0:
            start=test
        else:
            end=test
    ans1=abs(X+2*D*start)
    ans2=abs(X+2*D*end)
    print(min(ans1,ans2))

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

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

Solution (Python):
x,k,d = list(map(int,input().split()))
x = abs(x)
mst = x//d
if mst&1 != k&1:
    mst+=1
print(abs(x-(min(mst,k))*d))

Solution (Python):
def main():
    import sys
    X,K,D=list(map(int,sys.stdin.readline().split()))
    X=abs(X)
    if X-K*D>=0:
        print(X-K*D)
    else:
        count=X//D
        now=X%D
        if (K-count)%2==0:
            print(now)
        else:
            print(abs(now-D))

main()

Solution (Python):
x, k, d = (int(entry) for entry in input().split())
r = x
moves = k
if x < 0:
    r = -x

steps = r // d
if steps >= moves:
    print(r - moves * d)
    exit(0)

r -= steps * d
if abs(r - d) <= abs(r):
    steps += 1
    r -= d

moves -= steps
if moves % 2 != 0:
    moves -= 1
    if abs(r - d) < abs(r + d):
        r -= d
    else:
        r += d

print(abs(r))