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


if x == 0:

    if k % 2 == 0:
        ans = 0
    else:
        ans = d

elif x > 0:
    amari = x % d
    kouho1 = amari
    kouho2 = amari - d
    kyori1 = abs(x - kouho1)
    kyori2 = abs(x - kouho2)
    if kyori1 // d > k:
        ans = x - k * d

    elif (kyori1 // d) % 2 == 1:
        if k % 2 == 1:
            ans = abs(kouho1)
        else:
            ans = abs(kouho2)
    else:
        if k % 2 == 0:
            ans = abs(kouho1)
        else:
            ans = abs(kouho2)
else:
    x *= -1
    amari = x % d
    kyori1 = abs(x - amari)
    kyori2 = abs(x - (amari - d))
    if kyori1 // d > k:
        ans = abs(x - k * d)
    elif (kyori1 // d) % 2 == 1:
        if k % 2 == 1:
            ans = abs(amari)
        else:
            ans = abs(amari - d)
    else:
        if k % 2 == 0:
            ans = abs(amari)
        else:
            ans = abs(amari - d)
print(ans)

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

Solution (Python):
X,K,D = (abs(int(T)) for T in input().split())
if K<=(X//D):
    Now = X-K*D
    Count = 0
    print(Now)
else:
    Now = X-(X//D)*D
    Count = K-(X//D)
    if abs(Now)<abs(Now-D):
        if Count%2==0:
            print(abs(Now))
        else:
            print(abs(Now-D))
    else:
        if Count%2==1:
            print(abs(Now-D))
        else:
            print(abs(Now))

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

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

if k < tmp:
  result = abs(abs(x) - d*k)
elif tmp%2 == k%2:
  result = abs(abs(x) - d*tmp)
else:
  a = result = abs(abs(x) - d*tmp)
  result = abs(abs(a) - d)

print(result)

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

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

Solution (Python):
def main():

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

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

        else:
            print(d-ax%d)
    else:
        print(abs(x)-k*d)

if __name__ == "__main__":
    main()

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

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

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

X = abs(X)
Q, R = divmod(X, D)

if Q >= K:
    output = X - (K*D)
else :
    if (K - Q) % 2 == 1:
        output = abs(D - R)
    else :
        output = R
        
print(output)

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

def solve():
    if X // D >= K:
        return X - D*K
    if ((X//D)%2 == 0) ^ (K%2 == 0):
        return D - (X - D*(X//D))
    else:
        return X - D*(X//D)

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

Solution (Python):








i = list(map(int, input().split()))
x = i[0]
k = i[1]
d = i[2]
c=x-d*k
if c>=0:
    print(c)
else:
    p=-c//(2*d)
    ans = 10000000000000000000000000000000000000000
    for i in range(-100,100):
        if 0<=p+i and p+i<=k:
            ans=min(ans,abs(c+2*d*(p+i)))
    if ans == 10000000000000000000000000000000000000000:
        print(abs(c+2*d*k))
    else:
        print(ans)

Solution (Python):
import math
n , st , m = map(int , input().split())
n = abs(n)
t = abs(n)
n = (n-m*st)
tot = n//m
if n>=0:
	print(n)
else:
	rem = t%m
	tot = t//m
	if((st-tot)%2)!=0:
		print(abs(m-rem))
	else:
		print(abs(rem))

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

Solution (Python):
def resolve():
    '''
    code here
    '''
    X, K, D = [int(item) for item in input().split()]

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

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


if __name__ == "__main__":
    resolve()

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

k = abs(X) // D

if k > K:
    ans = min(abs(X - K * D),abs(X + K * D))
else:
    Xk = min(abs(X - k * D), abs(X + k * D))
    if (K - k) % 2 == 0:
        ans = Xk
    else:
        ans = min(abs(Xk-D),abs(Xk+D))

print(ans)

Solution (Python):

number=list(map(int,input().split()))
X,K,D=number[0],number[1],number[2]
res=0
t=0
ans=0
if X<0:
    X=X*(-1)
if X>K*D:
    print(X-K*D)
elif X<=K*D:
    key=int(X/D)
    key+=1
    res=X-(D*key)
    t=K-key
    t=int(t%2)
    if t==0:
        ans=res*(-1)
    else:
        ans=res+D
        
    print(ans)

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

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

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

Solution (Python):
import math
x=input()
x0=x.split()
x=int(x0[0])
k=int(x0[1])
d=int(x0[2])
if abs(x)>abs(k*d):
    print(abs(x)-abs(k*d))
else:
    c=math.floor(abs(x)/d)
    if abs(x)-c*d<abs(abs(x)-(c+1)*d):
        if (k-c)%2==0:
            print(abs(x)-c*d)
        else:
            print(abs(abs(x)-(c+1)*d))
    else:
        if (k-c)%2==0:
            print(abs(x)-c*d)
        else:
            print(abs(abs(x)-(c+1)*d))

Solution (Python):


numbers = input().split(" ")
x = int(numbers[0])
k = int(numbers[1])
d = int(numbers[2])

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

Solution (Python):
X,K,D= map(int,input().split())
X = abs(X)
X2 = X
I = 0
K2 = X//D
if X == 0:
    if K%2 ==0:
        print(0)
    else:
        print(D)
elif K <= K2:
    print(X - K*D)
else:
    X = X - K2*D
    for i in range(K2,K+1):
        X -= D
        X2 = X
        if X <= 0:
            X += D
            I = i
            break
    if (K-I)%2 == 0:
        print(abs(X))
    else:
        print(abs(X2))

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

Solution (Python):
from collections import Counter
import copy
from itertools import combinations
import math


def A(s):
  if s == 'RRR':
    return 3
  elif s == 'RRS':
    return 2
  elif s == 'RSR':
    return 1
  elif s == 'SRR':
    return 2
  elif s == 'RSS':
    return 1
  elif s == 'SRS':
    return 1
  elif s == 'SSR':
    return 1
  elif s == 'SSS':
    return 0
  l = 0
  max_l = 0
  for i in range(len(s)):
    if i == 0 and s[i] == 'R':
      l += 1
    elif s[i - 1] == s[i] and s[i] == 'R':
      l += 1
    else:
      l = 0
    if l > max_l:
      max_l = l
  return max_l


def B(N, Ls):

  def _can_tri(a, b, c):
    if a == b or a == c or b == c:
      return False
    tmp_a, tmp_b, tmp_c = sorted([a, b, c])
    if tmp_a + tmp_b > tmp_c:
      return True
    return False

  ans = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        if _can_tri(Ls[i], Ls[j], Ls[k]):
          
          ans += 1
  return ans


def C(X, K, D):
  abs_x = int(abs(X))
  if K * D <= abs_x:
    return abs_x - K * D
  else:
    a = K - math.floor(abs_x / D)
    
    if a % 2 == 0:
      return abs_x % D
    else:
      return D - abs_x % D


def main():
  
  
  
  
  
  X, K, D = [int(x) for x in input().split(' ')]
  print(C(X, K, D))


if __name__ == '__main__':
  main()

Solution (Python):
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy


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

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

if __name__ == '__main__':
	main()

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

dv,md=divmod(x,d)  
k-=dv
if k%2==0:
  print(md)
else:
  print(abs(md-d))

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

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

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

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

X = abs(X)

minNeedCount = X // D
rightRest = X % D
leftRest = abs(rightRest - D)

if K < minNeedCount:
  res = X - K*D
else:
  if (K-minNeedCount) % 2 == 0:
    res = rightRest
  else:
    res = leftRest

print(res)

Solution (Python):


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

    cnt = abs(X) // D
    if X >= 0:
        X -= min(K, cnt) * D
    else:
        X += min(K, cnt) * D

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


if __name__ == "__main__":
    main()

Solution (Python):
X,K,D=map(int,input().split())
kai = 0
xy = abs(X)
if xy >= K*D:
    kotae = abs(xy-(K*D))
if xy < K*D:
    kai, mod = divmod(xy, D)
    ds = kai*D
    if (K-kai)%2==0:
        kotae = abs(xy-ds)
    if (K-kai)%2!=0:
        kouho1 = abs(xy-ds-D)
        kouho2 = abs(xy-ds+D)
        if kouho1>=kouho2:
            kotae = kouho2
        if kouho1<kouho2:
            kotae = kouho1
print(kotae)

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

A = list(map(int, input().split()))
x = A[0]
k = A[1]
d = A[2]
if x<0:
    x*=-1
if x==0:
    if k%2==0:
        print(0)
    else: print(d)
else:
    if x-k*d<0:
        m=x//d+1
        k-=m
        
        if k%2==0:
            print(abs(x-m*d))
        elif k%2==1:
            print (abs(x-(m-1)*d))
        
    else:
        print(x-k*d)

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

x,k,d = nm()

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

import math


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

Solution (Python):
import math
def main():
  X, K, D = map(int, input().split())
  if abs(X) >= K * D:
    print(abs(X)-K*D)
  else:
    Y = math.ceil(abs(X) / D)
    if (K-Y) % 2 == 0:
      print(D*Y-abs(X))
    else:
      Z = abs(X)-D*(Y-1)
      print(Z)
main()

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

Solution (Python):

import datetime
from decimal import Decimal, ROUND_DOWN
import heapq
from fractions import gcd
from functools import reduce
from collections import deque

import itertools
import collections
import math
import sys
sys.setrecursionlimit(2000000)









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










shou = x//d
amari = x % d

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





print(ans)

Solution (Python):
v = input().split(" ")
x = int(v[0])
k = int(v[1])
d = int(v[2])

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

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

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

k -= x // d
k %= 2
x %= d
print(abs(x - k * d))

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

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

Solution (Python):
import math
while True:
    try:
        x, k, d = map(int, input().split());
        if x == 0:
            k %= 2;
            if k:
                print(abs(x - d));
            else :
                print(0);
        elif abs(x) - k * d > 0:
            print(abs(x) - k * d);
        elif x > 0:
            k -= x // d;
            k %= 2;
            x -= x // d * d;
            if k:
                print(abs(x - d));
            else:
                print(x);
        else:
            num = abs(x) // d + 1;
            x += num * d;
            k -= num;
            k %= 2;
            if k:
                print(abs(x - d));
            else:
                print(x);
    except EOFError:
        break;

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

first_move = abs(x)//d

if first_move >= k:
    print(abs(x)-d*k)
else:
    k -= first_move
    x_abs = abs(x) - first_move*d

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

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

x = abs(x)

ans = x % d

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

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

Solution (Python):

import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def findMaximum(low, high):
    
    if low == high:
        return fun(low)

        
    
    if high == low + 1 and fun(low) >= fun(high):
        return fun(high);

        
    
    if high == low + 1 and fun(low) < fun(high):
        return fun(low)

    mid = (low + high) // 2  

    
    
    
    if fun(mid) <= fun(mid+1) and fun(mid) <= fun(mid-1):
        return fun(mid)

        
    
    if fun(mid) >= fun(mid+1) and fun(mid) <= fun(mid-1):
        return findMaximum(mid+1,high)
    else:  
        return findMaximum(low, mid-1)

def fun(zz):
    return abs(x+d*(2*zz-k))

x,k,d = MAP()

l = 0
r = k

print(findMaximum(l, r))

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

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

if x<0:
  x=-x


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

Solution (Python):
def main():
    x, k, d = [int(e) for e in input().split()]

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


if __name__ == '__main__':
    main()

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

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

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

print(s)

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

X = abs(X)

if K*D < X:
    ret = X - K*D
else:
    j = X//D
    if (K - j) % 2 == 0:
        ret = X - j*D
    else:
        ret = (j+1)*D - X

print(ret)

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

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

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

from math import floor, ceil
def do(x, k, d):
    t1, t2 = floor(x/d), ceil(x/d)
    ogk = k
    ogx = x

    x -= t1*d
    k -= t1
    ans1 = 10**16
    if k%2 == 0:
        ans1 = abs(x)
    else:
        ans1 = min(abs(x + d), abs(x - d))

    x, k = ogx, ogk
    x -= t2*d
    k -= t2
    ans2 = 10**16
    if k%2 == 0:
        ans2 = abs(x)
    else:
        ans2 = min(abs(x + d), abs(x - d))
    
    print(min(ans1, ans2))


if x > 0:
    if x - k*d < 0:
        q = round(x/d)
        do(x, k, d)
    else:
        print(x - k * d)

elif x < 0:
    if x + k*d > 0:
        do(abs(x), k, d)
    else:
        print(abs(x + k*d))

else:
    
    if k%2:
        print(d)
    else:
        print(0)

Solution (Python):




import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict


MOD = 10 ** 6 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    x, k, d = read_ints()
    x = abs(x)
    cnt = x // d
    if cnt < k:
        x -= cnt * d
    else:
        cnt = k
        x -= cnt * d
    if abs(x - d) < x and (k-cnt)%2 == 1:
        x = abs(x - d)
        cnt += 1
    
    if (k - cnt) % 2 == 0:
        print(x)
    elif x == 0:
        print(d)
    else:
        print(min(abs(x - d), x + d)) 

if __name__ ==  '__main__':
    Main()

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

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

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

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

print(ans)

Solution (Python):

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

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

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

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

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

Solution (Python):

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

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

while X >= 0 :
    X -= D
    cnt += 1

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

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

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

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

Solution (Python):
X, K, D = list(map(int, input().split()))
X = abs(X)
syo, amari = divmod(X, D)
if amari > (D-amari):
    syo = syo +1
if syo >= K:
    print(X - K*D)
else: 
    remain_num = K - syo
    position = abs(X - syo*D)
    if remain_num%2 == 1:
        position = abs(position-D)
    print(position)

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

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

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

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

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

tmp = (x+d*k)/(2*d)
tmp = int(tmp)
ans = []
for i in [0,k,tmp]:
    for j in range(-10,10):
        a = i+j
        if a<0 or k<a:
            continue
        y = x+d*k - 2*d*a
        ans.append(abs(y))
ans = min(ans)
print(ans)

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

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

if X > 0:
    k = X // D
    if k <= K:
        X -= D * k
        K -= k
    else:
        X -= D * K
        K = 0
        print(abs(X))
        exit()

    cur_abs = abs(X)
    while K > 0:
        X -= D
        if abs(X) > cur_abs:
            X += D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)


elif X < 0:
    k = abs(X) // D
    if k <= K:
        X += D * k
        K -= k
    else:
        X += D * K
        K = 0
        print(abs(X))
        exit()

    cur_abs = abs(X)
    while K > 0:
        X += D
        if abs(X) > cur_abs:
            X -= D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)


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

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

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

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

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

Solution (Python):
from math import *

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

A = (K*D + X) / (2 * D)

if K <= A:
    print(abs(X-K*D))
elif A <= 0:
    print(abs(X+K*D))
else:
    print(min(abs(X-2*ceil(A)*D+K*D) ,abs(X-2*floor(A)*D+K*D)) )

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

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

x=abs(x)

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

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

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

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

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 = list(map(int, input().split()))


x = abs(x)


move_near_0 = min(x // d, k)
x -= d * move_near_0
k -= move_near_0


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

Solution (Python):
v = input().split(" ")
x = int(v[0])
k = int(v[1])
d = int(v[2])

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


print(ans)

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