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 collections
import bisect
import itertools



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))




def main():
    x, k, d = ns()
    x = abs(x)
    ans = INF
    base = 0

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    x,k,d = LI()
    if x == 0:
        if k % 2 == 1:
            print(d)
        else:
            print(0)
        return
    else:
        s = abs(x) // x
    c = abs(x) // d

    ans = 0
    if c >= k:
        ans = x - d * k * s
    else:
        ans = x - d * c * s
        if (k-c) % 2 == 1:
            ans = ans - d * s
    print(abs(ans))
if __name__ == '__main__':
    main()

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

Solution (Python):
from sys import stdin
import math
import re
import queue

input = stdin.readline

MOD = 1000000007
INF = 122337203685477580


def solve():
    x,k,d = map(int, input().split())
    move = k*d
    x = abs(x)
    if x > k*d:
        print(x -k*d)
    else:
        q = x//d
        if abs(q - k)%2 == 0:
            print(x%d)
        else:
            print(d - x%d)
            
if __name__ == '__main__':
    solve()

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

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

    x,k,d=map(int, readline().rstrip().split())
    if x < 0:
        x*= -1
    
    if x==0:
        if k%2==0:
            print(0)
        else:
            print(d)
        return
    
    aa = x // d

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


    
    
    
    

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

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

print(abs(ans))

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

pos_min = X - K * D
pos_max = X + K * D

if X <= K * D and X >= -K * D:
    if K % 2 == 0:
        print(min([X % (2 * D), 2 * D - X % (2 * D)]))
    else:
        print(min([(X - D) % (2 * D), 2 * D - (X - D) % (2 * D)]))
elif pos_min > 0:
    print(pos_min)
else:
    print(-pos_max)

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

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')







def solve():
    X, K, D = MI()
    X = abs(X)
    z = X // D
    if z >= K:
        print(X - K * D)
        return
    
    K -= z
    
    X = X - z * D
    X2 = abs(X - D)
    
    if K % 2 == 0:
        print(X)
    else:
        print(X2)


if __name__ == '__main__':
    solve()

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

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

Solution (Python):
from sys import stdin

def main():

    input = stdin.readline

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

    X = abs(X)

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

    print(ans)

if __name__ == "__main__":
    main()

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

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

move = abs(x)//d
amari = abs(x)%d 

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

Solution (Python):
import sys

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

ans = 9999999
if abs(X) >= K * D:
    ans = abs(X) - K*D
else:
    A = abs(X) % D
    B = K - (abs(X) // D)

    if B % 2 == 0:
        ans = A
    else:
        ans = abs(A - D)

print(ans)

Solution (Python):
def abs(a):
	if a<0:
		return -a
	return a
x,k,d=map(int,input().split())
if x==0:
	if k%2==1:
		print(d)
	else:
		print(0)
if x>0:
	if k*d<=x:
		print(x-k*d)
		exit(0)
	r=x%d
	steps=(x-r)//d
	l=abs(r-d)

	if((k-steps)%2==0):
		print(r)
	else:
		print(l)
if x<0:
	if k*d<=abs(x):
		print(abs(x+k*d))
		exit(0)
	l=abs(x)%d
	steps=(abs(x)-abs(l))/d
	r=-l+d

	if (k-steps)%2==0:
		print(l)
	else:
		print(r)

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

X=abs(X)

q=X//D
r=X%D

if r < D-r:
    n=q
    ans1=r
    ans2=D-r
else:
    n=q+1
    ans1=D-r
    ans2=r

if K <= n:
    ans=X-K*D
    ans=abs(ans)
else:
    d=K-n
    if d%2==0:
        ans = ans1
    else:
        ans = ans2
print(ans)

Solution (Python):
x,k,d=(int(i) for i in input().split())
c=(abs(x)//d)
if(x<0):
    x+=(min(k,c)*d)
else:
    x-=(min(k,c)*d)
k=max(0,k-c)
if(k%2==1 and x<0):
    print(abs(x+d))
elif(k%2==1 and x>0):
    print(abs(x-d))
elif(k%2==1 and x==0):
    print(x+d)
else:
    print(abs(x))

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

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

p = X // D

if abs(p) > K:
    ans = min(abs(X - K * D), abs(X + K * D))

else:
    m0 = abs(X - D * p)
    m1 = abs(X - D * (p + 1))

    if abs(m0) < abs(m1):
        if (K - p) % 2 == 0:
            ans = m0
        else:
            ans = m1
    else:
        if (K - p - 1) % 2 == 0:
            ans = m1
        else:
            ans = m0
print(ans)

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

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

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
n = x//d
p = x - d*n
m = abs(p-d)

if(k <= n):
  print(x-d*k)
else:
  ama = n-k
  if(ama%2 == 0):
    print(p)
  else:
    print(m)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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

n = abs(X) // D

if K <= n:
    if X < 0:
        print(abs(X + D*K))
        sys.exit()
    else:
        print(abs(X - D*K))
        sys.exit()
else:
    if (K-n) % 2 == 0:
        if X < 0:
            print(abs(X + n * D))
            sys.exit()
        else:
            print(abs(X - n * D))
            sys.exit()
    else:
        if X < 0:
            print(abs(X + (n+1) * D))
            sys.exit()
        else:
            print(abs(X - (n+1) * D))
            sys.exit()

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

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

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

x = abs(x)

wari = x//d
amari = k-wari
p = x - (d*wari)

if wari >= k:
    print(x-(d*k))
else:
    if amari%2 == 0:
        print(p)
    else:
        print(d-p)

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

def solve():
    if X > K*D:
        return X - K*D
    elif -1*X > K*D:
        return -X - K*D
    elif X >= 0:
        first = X // D
        rem = K - first
        if rem % 2 == 0:
            return X % D
        else:
            return D - (X % D)
    else: 
        first = (-1*X) // D
        rem = K - first
        if rem % 2 == 0:
            return (-1 * X) % D
        else:
            return D - (-1 * X) % D

print(solve())

Solution (Python):
import math
import sys

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

x = abs(x)

v = math.floor(x / d)

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

x -= v * d
k -= v
x = abs(x)

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

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

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools










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


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

Solution (Python):
x, k, d = [list(map(int, s.split())) for s in open(0)][0]
x = abs(x)

ans = x
if x > k * d:
    ans = x - k * d
else:
    m = x//d
    if m%2 == k%2:
        ans = x - m * d
    else:
        ans = (m+1) * d - x
print(ans)

Solution (Python):
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


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


if __name__ == '__main__':
    main()

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

if X < 0:
    X *= -1

n = X // D
m = X % D

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

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

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

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

print(abs(ans))

Solution (Python):
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    x, k, d = map(int, input().split())

    x = abs(x)
    d = abs(d)
    ans = 0

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

if __name__ == '__main__':
    solve()

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

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


if __name__ == "__main__":
    main()

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

division, mod = divmod(abs(X), D)

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

print(ans)

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):
ini=input("").split(" ")
x=abs(int(ini[0]))
k=int(ini[1])
d=int(ini[2])
if(k%2==1):
    k-=1
    x=abs(x-d)
if((x-k*d)>=0):
    print(x-k*d)
else:
    c=int(x/(2*d))
    if(abs(x-c*2*d)<abs(x-c*2*d-2*d)):
        print(x-c*2*d)
    else:
        print(abs(x-c*2*d-2*d))

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

t = X // D

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

K -= t
X -= t * D

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

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

M = X // D
X = X - D * min(M, K)
K = max(0, K - M)

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

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

jump=abs(x//d)

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

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

X = abs(X)
fl = X // D
cl = fl + 1

if fl > K:
    ans = X - K * D
elif K % 2 == fl % 2:
    ans = X - fl * D
else:
    ans = cl * D - X

print(ans)

Solution (Python):
X, K, D = map(int, input().split())
if X <= 0:
    X *= -1

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

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

sho = X // D
yo = X % D

if sho > K:
    print(X - K * D)
elif sho == K:
    print(yo)
else:
    if (K - sho) % 2 == 0:
        print(yo)
    else:
        print(abs(yo - D))

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

x = abs(x)

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

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

ans=0
if X==0:
    if K%2==0:
        ans=0
    else:
        ans=D
else:
    cnt=abs(X)//D
    goal1=abs(X)%D
    goal2=abs(goal1-D)

    if cnt>K:
        ans=abs(X)-D*K
    else:
        amari=K-cnt
        if amari%2==0:
            ans=goal1
        else:
            ans=goal2

print(ans)

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

if abs(K*D) <= abs(X):
    ans = abs(abs(X) - abs(K*D))
    print(ans)
    exit()
    
tmp1 = X%D
tmp2 = abs(tmp1-D)

cnt = X//D

check = K - cnt
if check%2 == 0:
    print(tmp1)
else:
    print(tmp2)

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

print(min(ans))

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

    kyori = abs(x)
    kaisuu = kyori // d
    sa = k - kaisuu
    amari = kyori % d

    if kaisuu >= k:
        print(kyori- k*d)
    else:
        if sa % 2 == 0:
            print(amari)
        else:
            print(d - amari)




if __name__ == "__main__":
    main()

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






startN = (D * K - X) // (2 * D)

N1 = startN + 1
N2 = startN
N3 = startN - 1

if N1 < 0:
    N1 = 0
if N2 < 0:
    N2 = 0
if N3 < 0:
    N3 = 0
if N1 > K:
    N1 = K
if N2 > K:
    N2 = K
if N3 > K:
    N3 = K

def getScore(n):
    score = X + n * D - D * (K - n)
    if score < 0:
        score = score * -1
    return score

score1 = getScore(N1)
score2 = getScore(N2)
score3 = getScore(N3)

minScore = score1
if minScore > score2:
    minScore = score2
if minScore > score3:
    minScore = score3

print(minScore)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





X, K, D = getNM()
X = abs(X)
time = X // D

if K < time:
    print(abs(X - (D * K)))
    exit()
elif (K - time) % 2 == 0:
    print(abs(X - (D * time)))
    exit()
else:
    print(abs((D * (time + 1)) - X))
    exit()

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

X = abs(X)

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

near0_X = X % D
after_K = K - X//D

if after_K % 2 == 0:
    print(near0_X)
else:
    print(D-near0_X)

Solution (Python):
X,K,D = map(int,input().split())
if X < 0:
    X *= -1
if D < 0:
    D *= -1
if abs(K*D) > X:
    if K % 2 == 0:
        mini = X//(2*D)
        bigger = X//(2*D)+1
        ans = min(abs(X-(2*D*mini)),abs(X-(2*D*bigger)))
    else:
        smaller_start = X-D
        bigger_start = X+D
        smaller_mini = smaller_start//(2*D)
        smaller_big = smaller_start//(2*D)+1
        bigger_mini = bigger_start//(2*D)
        bigger_big = bigger_start//(2*D)+1
        ans = min(abs(smaller_start-2*D*smaller_mini),abs(smaller_start-2*D*smaller_big),abs(bigger_start-2*D*bigger_mini),abs(bigger_start-2*D*bigger_mini))
else:
    ans = abs(X-K*D)
print(ans)

Solution (Python):

X, K, D = map(int, input().split())
if abs(X) > K*D:
    res = abs(X) - K*D
else:
    q, r = divmod(abs(X), D)
    if (K - q)%2==0:
        res = r
    else:
        res = D - r
print(res)

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

x = abs(x)

ans = x - r*d

if ans >=0:
    print(ans)
else:
    print(min(abs(ans%(d*2)),abs(d*2-ans%(d*2))))

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math
import bisect

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

x,k,d = readInts()

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

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




'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''

x,k,d=map(int, input().split())
aaa=abs(x//d)
if aaa>=k:
    print(min(abs(x-k*d),abs(x+k*d)))
    sys.exit()
else:
    if (k-aaa)%2==0:
        print(min(abs(x-(aaa)*d),abs(x+(aaa)*d)))
        sys.exit()
    else:
        print(min([abs(x-(aaa+1)*d),abs(x+(aaa+1)*d),abs(x-(aaa-1)*d),abs(x+(aaa-1)*d)]))

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

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

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

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

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

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

X = abs(X)

low = X // D

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

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

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

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

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

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

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

Solution (Python):
def main():
    s = input().split(' ')
    x = int(s[0])
    k = int(s[1])
    d = int(s[2])

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

if __name__ == '__main__':
    main()

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

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

ans = 0
if DK <= abs(X):
    ans = abs(X) - DK
else:
    q = X // D
    r = X % D
    if (K - q) % 2 == 0:
        ans = r
    else:
        ans = D - r
print(ans)

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

print(ans)

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

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

Solution (Python):
X,K,D = map(int,input().split())
X = max(X,X*(-1))
plusmin = X%D
minusmin = -plusmin+D
totalmin = min(plusmin,minusmin)
if totalmin == plusmin:
    num = X//D
else:
    num = X//D+1
    
if K<num:
    answer = X-D*K
elif K==num+1:
    answer = max(plusmin,minusmin)
elif K%2==num%2:
    answer = totalmin
else :
    answer = max(plusmin,minusmin)
    
print(answer)

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

N = X//D
if K<=N:
    ans = X-K*D
else:
    K -= N
    X = X%D
    ans = D-X if K&1 else X
print(ans)

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

if x >= 0:
    cnt = x // d
    if cnt >= k:
        pos = x - k * d
        print(pos)
    else:
        pos1 = x - cnt * d 
        nokori = k - cnt
        if nokori % 2 == 0:
            print(pos1)
        else:
            pos2 = abs(pos1 - d)
            print(pos2)
else:
    x = -x
    cnt = x // d
    if cnt >= k:
        pos = x - k * d
        print(pos)
    else:
        pos1 = x - cnt * d 
        nokori = k - cnt
        if nokori % 2 == 0:
            print(pos1)
        else:
            pos2 = abs(pos1 - d)
            print(pos2)

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

kd = k * d

if(x + kd < 0):
    print(0 - x - kd)
elif(x - kd > 0):
    print(x - kd)
else:
    if(x < 0):
        x *= -1
    if(k%2 == 1):
        x += d
    xd = x % (d*2)
    xd2 = d*2 - xd
    print(min(xd,xd2))

Solution (Python):
l=input().split()
x=int(l[0])
k=int(l[1])
d=int(l[2])
x=abs(x)
l=[]
l.append(abs(x-k*(d)))
z=x-(k*d)
if(z<0):
    rem=(z%(2*d))
    l.append(rem)
    l.append(2*d-rem)
print(min(l))

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


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

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

Solution (Python):

"""
@author: Saurav Sihag
"""

rr = lambda: input().strip()
rri = lambda: int(rr())

rrm = lambda: [int(x) for x in rr().split()]


from sys import stdin, stdout, setrecursionlimit

def sol():
    x, k, d=rrm()
    x=abs(x)
    a=x//d
    if a>k:
        print(abs(x-k*d))
        return
    rk=k-a
    xa=x-a*d
    
    if rk%2==1:
        xa-=d
    print(abs(xa))
    return

    

def main():
    
    
        ans=sol()
        

main()

Solution (Python):
import sys, math, bisect


if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )
    """
    X, K, D = map(int, input().strip().split() )


    if(X<0):
        X *= -1
    moved = X//D

    if(moved >= K):
        x_end = X-D*K
        print(x_end)
    else:
        x_0 = X-D*moved
        if(x_0 > D/2):
            moved += 1
            x_0 -= D
        move_remain = K-moved
        if(move_remain>0):
            if(move_remain%2!=0):
                if(x_0>=0):
                    x_0 -= D
                else:
                    x_0 += D

        print( abs(x_0) )

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

cnt = min(k, abs(x) // d)

if x > 0:
    k -= cnt
    x -= d*cnt
    if abs(x - d) < abs(x) and k > 0:
        k -= 1
        x -= d
else:
    k -= cnt
    x += d*cnt
    if abs(x + d) < abs(x) and k > 0:
        k -= 1
        x += d

if k == 0:
    print(abs(x))
    sys.exit()

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

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

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

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

Solution (Python):
import math
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors


x,k,d = inm()
x = abs(x)
ans = x - k * d
if ans < 0:
    c = x//d
    k -= c
    k = k%2
    ans = x - d * c
    ans -=  d * k
print(abs(ans))

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

Xmin = X - D*K
Xmax = X + D*K

if Xmin * Xmax <= 0:
    ans = min(Xmax%(2*D), 2*D - (Xmax%(2*D)))
elif Xmin > 0:
    ans = Xmin
else:
    ans = -Xmax

print(ans)

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


x= abs(x)

if k%2==1:
	x-= d
	k-=1

if k*d<x:
	print(x-k*d)

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