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.



Sample input:
6 2 4


Sample output:
2

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if X + K * D <= 0:
    print(-X - K * D)
elif X - K * D >= 0:
    print(X - K * D)
else:
    div = X // D
    mod = X % D
    if (K-div) % 2 == 0:
        print(mod)
    else:
        print(abs(mod-D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
#0に行けない場合
if K*D <= abs(X):
    ans = abs(X) - K*D
else:#0に行ける場合
    i = abs(X) // D #i:最も0に近づくまでにかかる移動回数
    t = abs(X)-D*i #t:0を踏まずに最も0に近づいた時の距離
    if (K-i)%2==0:
        ans = t
    else:
        ans = D-t
print(ans)

Submission status: Accepted

Solution code:
x,K,D = map(int,input().split())
X = abs(x)
total_distance = D * K
if X >= total_distance:
    print(X - total_distance)
else:
    to_zero_times = X // D #0に近付くまでに使った回数
    from_zero_distance1 = X % D #０になるべく近付いた時の０からの距離
    from_zero_distance2 = D - from_zero_distance1
    left_times = K - to_zero_times #残り回数
    if left_times % 2 == 0:
        print(from_zero_distance1)
    else:
        print(from_zero_distance2)

Submission status: Accepted

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


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


if f(0) >= 0:
    print(f(0))
    exit()

if f(K) <= 0:
    print(-f(K))
    exit()

left = 0
right = K
while right - left > 1:
    mid = int((right + left) / 2)
    if f(mid) < 0:
        left = mid
    else:
        right = mid

print(min(-f(left), f(right)))

Submission status: Accepted

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

a = (k * d - x) / (2 * d)

if abs(x) < k * d:
   if abs(int(a) + 1 - a) >= abs(int(a) - a):
      print(abs(x + 2 * int(a) * d - k * d))
   else:
      print(abs(x + 2 * (int(a) + 1) * d - k * d))

elif x < 0:
   print(abs(x + k * d))
else:
   print(x - k * d)

Submission status: Accepted

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

Submission status: Accepted

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

if x < 0:
    flag = -1
else:
    flag = 1

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

print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if k*d <= x:
    ans = x-k*d
else:
    # 残り移動回数
    count = k - x//d

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if k*d <= x:
    ans = x-k*d
else:
    # 残り移動回数
    count = k - x//d

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())


from itertools import combinations
from collections import Counter as cc
def main():
    x, k , d = mi()
    x = abs(x)
    ans = max(x%d, x-k*d)
    k -= x//d
    if k > 0 and k%2 == 1:
        ans = abs(ans-d)
    print(ans)
    





if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X , K , D = map(int,input().split())
ans = 0
tmp = 0
tmp2 = 0
if abs(X) >= abs(D) * K:
	ans = abs(X) - abs(D) * K
else:
	tmp = abs(X) // abs(D)
	tmp2 = abs(X) - tmp * abs(D)

	if (K - tmp) % 2 ==0:
		ans = tmp2
	else:
		ans = abs(tmp2 -abs(D))
print(ans)

Submission status: Accepted

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

X = abs(X)
_n = X // D

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
x=abs(X)
a=x//D
b=x%D
c=abs(b-D)
if a%2==0:
    even=b
    odd=c
else:
    odd=b
    even=c
if a>K:
    print(abs(X)-K*D)
elif K%2==0:
    print(even)
else:
    print(odd)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    X, K, D = LI()
    X = abs(X)
    if X - K * D > 0:
        ans = X - K * D
    else:
        mi = X % D
        _mi = mi - D
        if abs(mi) > abs(_mi):
            mi = _mi # can be <0
        n_move = (X - mi) // D
        if n_move % 2 != K % 2:
            ans = min(abs(mi + D), abs(mi - D))
        else:
            a2 = abs(mi - D)
            ans = min(abs(mi), a2)
    print(ans)
main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8
x, k, d = map(int, input().split())

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=10**9+7
EPS=1e-6
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]


#-------------------------code---------------------------#
#vsInput()

x,k,d=value()

x=abs(x)

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

# print(x,k)

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

print(ans)

Submission status: Accepted

Solution code:
x, k, d = list(map(int, input().rstrip().split(" ")))
x = abs(x)
x1 = x%d
k1 = x//d
x2 = d - x1
if k < k1:
  print(x-k*d)
else:
  if (k - k1)%2 == 0:
    print(x1)
  else:
    print(x2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a = [int(s) for s in input().split()]
if a[0] < 0:
  b = abs(a[0]) // a[2]
  c = a[0] + a[2] * int(b)
  d = int(c) + a[2]
  if a[1] <= int(b) or a[1] == int(b) + 1:
    print(abs(a[0] + a[1] * a[2]))
  else:
    if abs(int(c)) < abs(int(d)):
      if (a[1] - int(b)) % 2 == 0:
        print(abs(c))
      else:
        print(abs(d))
    else:
      if (a[1] - int(b) - 1) % 2 == 0:
        print(abs(d))
      else:
        print(abs(c))
else:
  b = a[0] // a[2]
  c = a[0] - a[2] * int(b)
  d = int(c) - a[2]
  if a[1] <= int(b) or a[1] == int(b) + 1:
    print(abs(a[0] - a[1] * a[2]))
  else:
    if abs(int(c)) < abs(int(d)):
      if (a[1] - int(b)) % 2 == 0:
        print(abs(c))
      else:
        print(abs(d))
    else:
      if (a[1] - int(b) - 1) % 2 == 0:
        print(abs(d))
      else:
        print(abs(c))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

K -= X // D
X -= X // D * D

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
m=min(k,x//d) 
x-=m*d
k-=m
if k%2:
  x=abs(x-d)
print(x)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

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

Submission status: Accepted

Solution code:
x,k,d = map(int, input().split())
x = abs(x)
m = min(k,x//d)
x -= m*d
k -= m
if k%2: x = abs(x-d)
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    if X/D > K:
        print(X-K*D)
    else:
        E = int(X/D)
        K -=E
        X -=E*D
        if(K%2==0):
            print(X)
        else:
            print(abs(X-D))
if __name__ == "__main__":
    main()

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

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

Submission status: Accepted

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

def sgn(x):
  if x > 0:return 1
  if x < 0:return -1
  return 0

if X > 0:
  x_greedy = X - K*D
else:
  x_greedy = X + K*D
  
if sgn(X) == sgn(x_greedy):
  print(abs(x_greedy))
  exit()
x_r = X%D
x_l = x_r - D

r_parity = (abs(X-x_r)//D)%2

if K%2 == r_parity:
  print(abs(x_r))
else:
  print(abs(x_l))

Submission status: Accepted

Solution code:
X,K,D=list(map(int, input().split()))
X=abs(X)
if X-K*D>0:
    print(X-K*D)
    exit()
L=[]
if K%2==0:
    L.append(X%(2*D))
    L.append(2*D-X%(2*D))
    print(min(L))
    exit()
if K%2==1:
    L.append((X+D)%(2*D))
    L.append(2*D-(X+D)%(2*D))
    print(min(L))
    exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if(x<0):
    x=x*-1
if(x>=k*d):
    print(x-(k*d))
else:
    r=x%d
    g=x//d
    k=k-g;
    if(k%2==0):
        print(r)
    else:
        ans=abs(r-d)
        print(ans)

Submission status: Accepted

Solution code:
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
    if b==0:

        return a
    else:
        return gcd(b,a%b)
def power(x, p,m):
    res = 1
    while p:
        if p & 1:
            res = (res * x) % m
        x = (x * x) % m
        p >>= 1
    return res
def inar():
    return [int(k) for k in input().split()]

def lcm(num1,num2):
    return (num1*num2)//gcd(num1,num2)

def main():
    #t=int(input())
    t=1
    for _ in range(t):

        #n=int(input())
        x,k,d=inar()
        #arr=inar()
        if abs(x//d)>=k:
            #print(x//d)
            print(abs(x)-k*d)
        else:
            value=x % d
            k-=abs(x//d)
            if k % 2==0:
                print(value)
            else:
                print(min(abs(value-d),abs(value+d)))



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()
    #threadin.Thread(target=main).start()

Submission status: Accepted

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

q, r = divmod(X, D)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

A = input().split()
X = float(A[0])
K = int(A[1])
D = int(A[2])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
y=X//D
if K<y:
    y=K
X=X-(D*y)
K=K-y

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

step = abs(x)//abs(d)
y = abs(x)-abs(d)*step
k = k-step

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

Submission status: Accepted

Solution code:
s = list(map(int, input().split()))
r = s[0] // s[2] 
t = s[0] % s[2] 
if s[0] > 0 and s[0] - s[1] * s[2] > 0:
    print(s[0] - s[1] * s[2])
elif s[0] < 0 and s[0] + s[1] * s[2] < 0:
    print(-s[0] - s[1] * s[2])
else:
    if s[0] > 0:
        r = s[0] // s[2] 
        t = s[0] % s[2] 
        if (s[1] - r) % 2 == 1:
            print(-t + s[2])
        else:
            print(t)
    elif s[0] < 0:
        v = (-s[0]) // s[2]
        w = -((-s[0]) % s[2])
        if (s[1] - v) % 2 == 1:
            print(w+s[2])
        else:
            print(-w)
    else:
        if s[1] % 2 == 1:
            print(s[2])
        else:
            print(s[0])

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

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

x=abs(x)
zx=x
if x<d*k:
    wk,x=divmod(abs(x),d)
    k-=wk    
else:
    x-=d*k
    k=0

for i in range(k):
    xm=abs(x+d)
    xu=abs(x-d)
    x=min(xm,xu)
    k-=1
    if zx+x==d:
        if k%2!=0:
            x=zx
            break
        else:
            break
    zx=x
    
print(x)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

x,k,d = inpl()
x = abs(x)
if x - k*d >= 0:
    print(x - k*d)
    quit()
res1 = x - x//d*d
res2 = abs(x - (x//d+1) *d)
if (k-x//d)%2:
    print(res2)
else:
    print(res1)

Submission status: Accepted

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

if step > K :
  print(X - D * K)

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

Submission status: Accepted

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