Problem:
 Score : 300 points    Problem Statement Takahashi, who lives on the number line, is now at coordinate X . He will make exactly K moves of distance D in the positive or negative direction.  More specifically, in one move, he can go from coordinate x to x + D or x - D .  He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.  Find the minimum possible absolute value of the coordinate of the destination.      Constraints  -10^{15} \leq X \leq 10^{15}  1 \leq K \leq 10^{15}  1 \leq D \leq 10^{15}  All values in input are integers.         Input Input is given from Standard Input in the following format:  X  K  D       Output Print the minimum possible absolute value of the coordinate of the destination.        Sample Input 1 6 2 4      Sample Output 1 2  Takahashi is now at coordinate 6 . It is optimal to make the following moves:   Move from coordinate 6 to ( 6 - 4 = ) 2 .  Move from coordinate 2 to ( 2 - 4 = ) -2 .   Here, the absolute value of the coordinate of the destination is 2 , and we cannot make it smaller.       Sample Input 2 7 4 3      Sample Output 2 1  Takahashi is now at coordinate 7 . It is optimal to make, for example, the following moves:   Move from coordinate 7 to 4 .  Move from coordinate 4 to 7 .  Move from coordinate 7 to 4 .  Move from coordinate 4 to 1 .   Here, the absolute value of the coordinate of the destination is 1 , and we cannot make it smaller.       Sample Input 3 10 1 2      Sample Output 3 8       Sample Input 4 1000000000000000 1000000000000000 1000000000000000      Sample Output 4 1000000000000000  The answer can be enormous.

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

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

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

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


print(min(abs(x-m*d),abs(x-(m+1)*d)))

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

x,k,d = na()
x = abs(x)

y = x%d
yy = abs(y-d)

c = x//d

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

if c%2 == k%2:
    print(y)
else:
    print(yy)

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    X, K, D = map(int, input().split())
    nearP = 0
    if abs(X) // D > K:
        if X >= 0: print(abs(X - K * D))
        else: print(abs(X + K * D))
    else:
        if X >= 0:
            move = X // D
            nearP = X % D
            if (K - move) % 2 == 0: print(nearP)
            else: print(abs(nearP - D))
        else:
            move = abs(X) // D
            nearM = X + move * D
            if (K - move) % 2 == 0: print(abs(nearM))
            else: print(abs(nearM + D))

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import sys

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

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

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

num = abs(x)//d

dist = abs(x) - min(num,k)*d

if num >= k:
    print(dist)
else:
    if (k-num)%2 == 0:
        print(dist)
    else:
        print(abs(dist-d))

Solution (Python):

import sys
def main(X, K, D):
    X = abs(X)
    k = X // D
    X -= D * min(K, k)
    K -= min(K, k)

    ans = X - D if K & 1 else X
    print(abs(ans))


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

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

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    X, K, D = read_ints()
    if X < 0:
        X = -X
    closest_K = X // D
    if K <= closest_K:
        return abs(X-K*D)
    if (K-closest_K)%2 == 0:
        return abs(X-closest_K*D)
    return abs(X-(closest_K+1)*D)


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

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

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

def f(x):
    return 2*D*x+X-K*D

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

if f(0)==0 or f(K)==0:
    print(0)
    exit()

if f(0)>0:
    print(f(0))
elif f(K)<0:
    print(abs(f(K)))
else:
    m = (K*D-X)//(2*D)
    ans = 10**18
    
    for i in range(m-100, m+100):
        if 0<=i<=K:
            ans = min(ans, abs(f(i)))
    
    print(ans)

Solution (Python):
def main():

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

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


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

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

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

if __name__ == "__main__":
    main()

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

x=abs(x)
t=x//d

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

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

ok = 0
ng = k+1
while abs(ok-ng)>1:
    m = (ok+ng)//2
    if x-m*d+(k-m)*d>=0:
        ok = m
    else:
        ng = m
ans = min(ans, abs(x-ok*d+(k-ok)*d))
if ok<k:
    ok += 1
    ans = min(ans, abs(x-ok*d+(k-ok)*d))
print(ans)

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

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

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

    print(abs(x))

main()

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

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

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



def main():
    x, k, d = map(int, input().split())
    
    x = abs(x)
    if abs(x) > k*d:
        ans = abs(x-k*d)
    else:
        if x == 0:
            if k%2:
                ans = d
            else:
                ans = 0
        else:
            key = x//d
            k -= key
            x -= key*d
            if k%2:
                ans = min(abs(x-d), abs(x+d))
            else:
                ans = abs(x)
    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys, math
import io, os

from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write('\n'.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
from fractions import Fraction

INF = float('inf')
mod = int(1e9)+7


x,k,d=mdata()
x=abs(x)
if x>=k*d:
    out(x-k*d)
else:
    t=x//d
    if (t+k)%2==0:
        out(x-t*d)
    else:
        out((t+1)*d-x)

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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


x,k,d = l_in()

if x < 0:
    x = -x

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

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

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

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

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

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

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

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

Solution (Python):
X,K,D=map(int,input().split())
P = X-D*K
Q = 2*D
if X-D*K<=0<=X+D*K:
    U=(X-D*K)%(Q)
    print(min(U,Q-U))
    
elif 0<X-D*K:
    print(X-D*K)
    
    
elif X+D*K<0:
    print(-X-D*K)

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

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

X = abs(X)

if X//D <= K:
    K -= X//D
    X %= D

    X -= D*(K%2)

    print(abs(X))
else:
    print(X - D*K)

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

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

print(ans)

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

X,K,D = MI()
X = abs(X)

q = X//D
r = X % D

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

if (q-K) % 2 == 0:
    print(r)
else:
    print(D-r)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]



def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


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)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


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


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





X,K,D = IL()

if K%2:
  if X>=0:
    X -= D
  else:
    X += D
  K -= 1

D *= 2
K //= 2

best1 = X%D
best2 = X%D-D

if abs(best1)<abs(best2):
  best = best1
else:
  best = best2
  
if abs(X-best)//D < K:
  print(abs(best))
else:
  if X>best:
    print(abs(X-D*K))
  else:
    print(abs(X+D*K))

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

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

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

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

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

ma=x+k*d
mi=x-k*d

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

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

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

if X < 0:
    X *= -1

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

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

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

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

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

if K == 0:
    print(X)
    exit()

if abs(X-D) < X:
    X = abs(X-D)
    K -= 1

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

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

Solution (Python):
x, k, d = map(int, input().split())
l = x - k * d
r = x + k * d
if l <= 0 <= r:
  y = r % (2 * d)
  print(min(y, 2 * d - y))
elif r <= 0:
  print(-r)
else:
  print(l)

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    if X < 0:
        X *= -1

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

    print(ans)

    return


if __name__ == '__main__':
    main()

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

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

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

def f(m):
    ret = x
    ret -= d * m
    ret += d * (k - m)
    return abs(ret)

l = 0
r = k

for _ in range(1000):
    c1 = (l * 2 + r) // 3
    c2 = (l + 2 * r) // 3
    if f(c1) > f(c2):
        l = c1
    else:
        r = c2
res = 10 ** 1000
for i in range(l - 1000, r + 1000):
    if i < 0 or i > k:
        continue
    res = min(res, f(i))
print(res)

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

Solution (Python):
def ceil(a,b):
    return (a+b-1)//b
X,K,D = map(int,input().split())
n = ceil(D*K-X,2*D)
n = max(0,n)
cand = [0,K,n,n-1]
ans = 10**15
for c in cand:
    if c<0 or c>K:
        continue
    x = abs((2*c-K)*D+X)
    ans = min(ans,x)
print(ans)

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

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

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

now = abs(X)
rest = K

step1 = min(now//D, K)
rest -= step1
now -= step1 * D

if rest <= 0:
    print(abs(now))
else:
    if rest % 2 == 1:
        now = abs(now - D)
        print(now)
    else:
        print(now)

Solution (Python):
def main():
    X, K, D = map( int, input().split())
    if X < 0:
        X = -X
    c = min(X//D, K)
    X -= c*D
    K -= c
    if K%2 == 0:
        print(X)
    else:
        print(D-X)
    
if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush

from functools import reduce,lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): 
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    

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

    x=abs(x)
    if x-k*d>=0:
        print(x-k*d)
        return 
    ans=float("inf")
    if (x//d)%2==k%2:
        ans=min(ans,x%d)
    else:
        ans=min(ans,abs(x%d-d))
    print(ans)




if __name__ == "__main__":
    main()

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

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

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

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

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

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

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

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

  x=abs(X)
  c1=(x+D-1)//D

  K-=c1
  x1=c1*D-x

  if K%2==0:
    return x1

  return D-x1


print(main())

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

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

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

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

INF = float("inf")


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

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    x, k, d = map(int, readline().split())
    cur = abs(x)
    rem = k
    cnt = min(cur // d, k)
    cur -= cnt * d
    rem -= cnt
    if rem % 2 == 0:
        ans = cur
    else:
        ans = abs(cur - d)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):


import sys
readline = sys.stdin.readline
read = sys.stdin.read

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

if x < 0: x = -x

q,r = x//d,x%d

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

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

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

Solution (Python):


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


x,k,d=MI()

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

if x<0 and x+k*d<0:
    ans=x+k*d
    ans=abs(ans)
    print(ans)
    exit()
if x>0 and x-k*d>0:
    ans=x-k*d
    print(ans)
    exit()
    
ans=x%(2*d)
if k%2==1:
    ans=min(abs(ans-d),abs(ans+d))
    print(ans)
    exit()
else:
    ans=min(abs(ans-2*d),abs(ans),abs(ans+2*d))
    print(ans)
    exit()

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

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

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

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

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)
k = X // D
rt = X - k * D
lt = abs(rt - D)
if k > K:
    print(X - K * D)

elif k % 2 == K % 2:
    print(rt)

else:
    print(lt)

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

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

x -= p * d
k -= p
x = abs(x)
ans = [x, abs(x - d)]
print(ans[k % 2])

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

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

Solution (Python):
def solve(x, k, d):
    x = abs(x)
    n, m = divmod(x, d)

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

    if n % 2 == k % 2:
        return m

    return abs(m - d)


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

print(solve(x, k, d))

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

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

p_min_cnt = x//d
rem = k - p_min_cnt
p_min = x%d

if rem%2 == 0:
    print(p_min)
else:
    m_min = abs(p_min-d)
    print(m_min)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

need = X // D
t = min(need, K)

X -= D * t
K -= t

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

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

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

Solution (Python):
X, K, D = map(int, input().split())
mi = min(abs(X - K * D), abs(X + K * D))
for a in (X - K * D) % (2 * D), (X - K * D) % (2 * D) - 2 * D:
    b = (X - a) // D
    if abs(b) <= K:
        mi = min(mi, abs(a))
print(mi)

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

X = abs(X)

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

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

if abs(X) >= D*K:
    print(abs(X)-D*K)
else:
    q, r = divmod(X, D)
    if q & 1 == K & 1:
        print(r)
    else:
        print(D-r)

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

if X<0:
    X *= -1
n = X//D
m = min(K,n)

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

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

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

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

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

a = abs(x)//d

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

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

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

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

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

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(X: int, K: int, D: int):
    X = abs(X)
    mx = K * D
    tmp = X % D
    
    if mx < X - tmp:
        ret = X - mx
    elif (X - tmp) // D == K:
        ret = tmp
    else:
        d = X - tmp
        n = d // D
        if n % 2 == K % 2:
            ret = tmp
        else:
            ret = abs(tmp - D)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    K = int(next(tokens))  
    D = int(next(tokens))  
    solve(X, K, D)

if __name__ == '__main__':
    main()

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


lap, rem = divmod(x, d)
if lap >= k:
    print(x - k * d)
    exit()

nokori = k - lap
rem0, rem1 = rem, d - rem
if nokori % 2 == 0:
    print(rem0)
else:
    print(rem1)

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

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

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