AtCoder Beginner Contest 175 - Walking Takahashi

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-----
6 2 4

-----Sample Output-----
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.

Source: atcoder | Difficulty: EASY


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


(X, K, D) = list(map(int, input().split()))
X = abs(X)
c = min(K, X // D)
K -= c
X -= D * c
if K == 0:
	print(X)
elif K % 2:
	print(abs(X - D))
else:
	print(X)


(X, K, D) = map(int, input().split())
X = abs(X)
a = min(K, X // D)
X -= a * D
K -= a
if K % 2:
	X = abs(X - D)
ans = X
print(ans)


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


(x, k, d) = map(int, input().split())
x = abs(x)
(div, mod) = divmod(x, d)
C = k - div
if C <= 0:
	print(x - d * k)
elif C % 2 == 1:
	print(d - mod)
else:
	print(mod)


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


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


(x, k, d) = map(int, input().split())
(x, cnt) = (abs(x), abs(x) // d)
ans = abs(x - d * min(k, cnt + (k % 2 != cnt % 2)))
print(ans)


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


(X, K, D) = map(int, input().split())
X = abs(X)
num1 = min(X // D, K)
X -= D * num1
K -= num1
if K % 2 == 1:
	X = D - X
print(X)


(X, K, D) = map(int, input().split())
dist = abs(X)
rem = K
count = min(dist // D, K)
dist -= count * D
rem -= count
if rem % 2 == 1:
	dist -= D
print(abs(dist))


(a, b, c) = map(int, input().split())
d = abs(a)
count = min(d // c, b)
d -= c * count
b -= count
if b % 2 == 1:
	d -= c
print(abs(d))


(X, K, D) = map(int, input().split())
X = abs(X)
m = min(K, X // D)
X -= D * m
K -= m
K %= 2
X -= D * K
print(abs(X))


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


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


import math
import collections
ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(map(int, input().split()))
(x, k, d) = mi()
x = abs(x)
count = min(k, x // d)
k -= count
x -= d * count
if k % 2 == 0:
	print(x)
else:
	print(d - x)


import math
(X, K, D) = list(map(int, input().split()))
X = int(math.fabs(X))
if K < X // D:
	ans = X - K * D
else:
	dis = X - D * (X // D)
	if K - X // D & 1:
		ans = dis - D
	else:
		ans = dis
print(int(math.fabs(ans)))


(x, k, d) = [int(i) for i in input().split()]
x = abs(x)
a = int(x / d)
if a > k:
	a = k
k -= a
x -= a * d
if k % 2 == 1:
	x = abs(x - d)
	k -= 1
print(x)


(x, k, d) = map(int, input().split())
cur = abs(x)
count = min(cur // d, k)
cur -= d * count
k -= count
if k % 2 == 1:
	cur -= d
print(abs(cur))


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


(X, K, D) = map(int, input().split())
par = K % 2
if X < -K * D:
	print(-X - K * D)
elif X > K * D:
	print(X - K * D)
elif par == 0:
	if X % (2 * D) < D:
		print(X % (2 * D))
	else:
		print(2 * D - X % (2 * D))
elif (X - D) % (2 * D) < D:
	print((X - D) % (2 * D))
else:
	print(2 * D - (X - D) % (2 * D))


import math
import collections
import itertools

def YesNo(Bool):
	if Bool:
		print('Yes')
	else:
		print('No')
	return

def resolve():
	(X, K, D) = list(map(int, input().split()))
	ans = 0
	if abs(X) - K * D < 0:
		tmp = X // D
		if (K - tmp) % 2 == 0:
			ans = abs(X - D * tmp)
		else:
			ans = abs(X - D * (tmp + 1))
	else:
		ans = abs(X) - K * D
	print(ans)
resolve()


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


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


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


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


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


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


def main(X, K, D):
	ans = 0
	X = abs(X)
	time = round(X // D)
	if time > K:
		time = K
	if time == 0:
		if X <= abs(X - D):
			time = 0
		else:
			time = 1
	ans = X - time * D
	if (K - time) % 2 != 0:
		ans = abs(ans) - D
	return abs(ans)

def __starting_point():
	(X, K, D) = list(map(int, input().split()))
	ans = main(X, K, D)
	print(ans)
__starting_point()


(x, k, d) = map(int, input().split())
lists = []
x = abs(x)
step = int(x / d)
if step >= k:
	answer = x - k * d
elif step < k:
	k_left = k - int(step)
	if k_left % 2 == 0:
		answer = x % d
	if k_left % 2 == 1:
		answer = min(abs(x % d - d), abs(x % d + d))
print(answer)


import sys
readline = sys.stdin.readline
(X, K, D) = map(int, readline().split())
X = abs(X)
if X - K * D > 0:
	print(X - K * D)
else:
	cnt = X // D
	point = X % D
	if (K - cnt) % 2 == 1:
		print(abs(point - D))
	else:
		print(point)


import math
(x, k, d) = map(int, input().split())
x = abs(x)
t = x // d
ans = 0
if t >= k:
	ans = x - k * d
elif (k - t) % 2 == 0:
	ans = x - t * d
else:
	ans = abs(x - (t + 1) * d)
print(ans)


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


(x, k, d) = map(int, input().split())
print(max(abs(x) - k * d, [x % d, d - x % d][(k - x // d) % 2]))


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


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


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


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


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


(x, k, d) = list(map(int, input().split()))
X = abs(x)
if k * d < X:
	ans = X - k * d
else:
	t = X // d
	t_a = k - t
	if t_a % 2 == 0:
		ans = X % d
	else:
		ans = abs(X % d - d)
print(ans)


from bisect import bisect_left, bisect_right

def main():
	s = [int(x) for x in input().split()]
	(x, k, d) = (s[0], s[1], s[2])
	tmp = abs(x) // d
	if tmp >= k:
		mini = abs(x) - d * k
	else:
		mini = abs(abs(x) - tmp * d)
		if (k - tmp) % 2 == 1:
			mini = abs(mini - d)
	print(mini)

def __starting_point():
	main()
__starting_point()


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


(X, K, D) = map(int, input().split())
cnt = min(abs(X) // D, K)
c_K = K
c_K -= cnt
rem = abs(X) - D * cnt
if c_K % 2 == 0:
	print(rem)
else:
	print(abs(rem - D))


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


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


(x, k, d) = map(int, input().split())
x = abs(x)
k_ = min(k, x // d)
k = k - k_
x = x - k_ * d
if k % 2 != 0:
	x = abs(x - d)
print(x)


(X, K, D) = list(map(int, input().split()))
if abs(X) > D * K:
	abso = abs(X) - D * K
	print(abso)
	return
div = abs(X) // D
mod = abs(X) % D
if (K - div) % 2 == 0:
	print(mod)
else:
	print(D - mod)


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


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


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


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


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

def __starting_point():
	main()
__starting_point()


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


(x, k, d) = map(abs, map(int, input().rstrip().split()))
print(max(x - k * d, (x % d, d - x % d)[k - x // d & 1]))


def input_int():
	return int(input())

def int1(x):
	return int(x) - 1

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

def input_to_int_tuple():
	return tuple(map(int, input().split()))

def input_to_int_tuple_minus1():
	return tuple(map(int1, input().split()))

def main():
	(X, K, D) = input_to_int_map()
	temp = abs(X) // D
	if K < temp:
		return int(abs(X) - K * D)
	if (K - temp) % 2 == 0:
		return int(abs(X) - D * temp)
	return int(abs(abs(X) - D * temp - D))

def __starting_point():
	print(main())
__starting_point()


import sys
array = list(map(int, input().strip().split()))
x = abs(array[0])
k = array[1]
d = array[2]
if d == 0:
	amari = abs(x)
else:
	move_cnt = int(x / d)
	if move_cnt <= k:
		k -= move_cnt
		amari = x - move_cnt * d
	else:
		amari = x - k * d
		k = 0
	if k > 0:
		if k % 2 == 1:
			if amari > 0:
				amari -= d
			else:
				amari += d
print(abs(amari))


(X, K, D) = map(int, input().split())
X = abs(X)
k = min(K, X // D)
X_ = X - k * D
K_ = K - k
if K_ % 2 != 0:
	ans = min(abs(X_ - D), abs(X_ + D))
else:
	ans = abs(X_)
print(ans)


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


(X, K, D) = map(int, input().split())
ans = float('inf')
X = abs(X)
before_0_cnt = X // D
if before_0_cnt >= K:
	print(X - K * D)
	return
rest = K - before_0_cnt
if rest % 2 == 0:
	print(X - before_0_cnt * D)
else:
	print(abs(X - before_0_cnt * D - D))


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


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


import sys
(X, K, D) = map(int, input().split())
if K * D < abs(X):
	print(abs(X) - K * D)
	return
tmp = X // D
X -= tmp * D
K -= tmp
if K % 2 == 0:
	print(abs(X) % D)
else:
	print(abs(X - D))


(X, K, D) = map(int, input().split())
cur = abs(X)
rem = K
cnt = min(cur // D, K)
cur -= D * cnt
rem -= cnt
print(-1 * (cur - D) if rem & 1 else cur)


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


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


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


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


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


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


[X, K, D] = [int(i) for i in input().split()]
X = abs(X)
if X - K * D > 0:
	print(X - K * D)
else:
	time = int(X / D)
	start = X - D * time
	time_re = K - time
	if time_re % 2 == 0:
		print(start)
	else:
		print(abs(start - D))


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


(X, K, D) = map(int, input().split())
import math
X = abs(X)
U = min(math.floor(X / D), K)
K = K - U
A = X - U * D
if K % 2 == 0:
	print(A)
else:
	print(abs(A - D))


import sys
(X, K, D) = list(map(int, input().split()))
X = abs(X)
c = min(K, X // D)
K -= c
X -= D * c
if K % 2 == 0:
	print(X)
else:
	print(abs(X - D))


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


(x, num, dis) = map(int, input().split())
if x < 0:
	x = x * -1
n = x // dis + 1
if num < n:
	print(abs(x - num * dis))
elif (num - n) % 2 == 0:
	print(abs(x - dis * n))
else:
	print(abs(x - dis * (n - 1)))


(x, k, d) = map(int, input().split())
x = abs(x)
(q, mod) = divmod(x, d)
if k <= q:
	ans = x - k * d
else:
	ans = mod
	ans -= (k - q) % 2 * d
print(abs(ans))


import sys
(X, K, D) = list(map(int, input().split()))
X = abs(X)
if X >= K * D:
	print(X - K * D)
else:
	k = K - X // D
	x = X - D * (X // D)
	if k % 2:
		print(abs(x - D))
	else:
		print(x)


(X, K, D) = [int(x) for x in input().split()]
X = abs(X)
s = min(K, X // D)
K = K - s
X = X - s * D
if K % 2 == 0:
	print(X)
else:
	print(D - X)


(a, b, c) = map(int, input().split())
x = (-a + b * c + 2 * c - 1) // (2 * c)
if x >= b:
	x = b
if x <= 0:
	x = 0

def f(x):
	return abs(a + (2 * x - b) * c)
print(min([f(x), f(max(x - 1, 0))]))


def solve():
	(x, k, d) = map(int, input().split())
	x = abs(x)
	a = x // d
	b = x % d
	p = x - d * k
	if p > b:
		print(p)
		return
	q = x - a * d
	if (k - a) % 2 == 0:
		print(abs(q))
	else:
		print(abs(q - d))
solve()


(X, K, D) = map(int, input().split())
if X < 0:
	X = -1 * X
if K % 2 == 1:
	K -= 1
	X -= D
A = X - K * D
low = 0
high = K // 2
while low <= high:
	mid = (low + high) // 2
	A = X - mid * 2 * D
	if abs(A) <= D:
		break
	elif A > D:
		low = mid + 1
	else:
		high = mid - 1
print(abs(A))


(x, k, d) = map(int, input().split())
cur = abs(x)
rem = k
cnt = min(cur // d, k)
cur -= d * cnt
rem -= cnt
if rem > 0:
	if rem % 2 == 1:
		cur = cur - d
ans = abs(cur)
print(ans)


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


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

def sign(x):
	return 1 if x >= 0 else -1
if abs(X) >= D * K:
	ans = X - sign(X) * D * K
else:
	l = abs(X) // D
	mod = abs(X) % D
	residual = K - l
	if residual % 2 == 0:
		ans = mod
	else:
		ans = mod - sign(mod) * D
print(abs(ans))


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


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


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


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


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


(x, k, d) = [abs(int(i)) for i in input().rstrip().split()]
print(max(x - k * d, (x % d, d - x % d)[k - x // d & 1]))


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


(x, k, d) = map(int, input().split())
n = min(max(round(-x / (2 * d) + k / 2), 0), k)
print(abs(x + (2 * n - k) * d))


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


import math
(X, K, D) = map(int, input().split())
X = abs(X)
if X > K * D:
	print(X - K * D)
else:
	c = math.floor(X / D)
	print(abs(X - D * c - (K - c) % 2 * D))


[x, k, d] = map(int, input().split())
t = (k * d - x) / (2 * d)
t = round(t)
if k < t:
	t = k
elif t < 0:
	t = 0
min = x - k * d + 2 * t * d
print(abs(min))


def c175(x, k, d):
	x = abs(x)
	ans = 0
	if x - k * d > 0:
		return x - k * d
	if x % d <= d - x % d:
		ans = x % d if (k - int(x // d)) % 2 == 0 else d - x % d
	else:
		ans = d - x % d if (k - 1 - int(x // d)) % 2 == 0 else x % d
	return ans

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

def __starting_point():
	main()
__starting_point()


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


(X, K, D) = map(int, input().split())
X = abs(X)
k1 = min(K, X // D)
X -= k1 * D
K -= k1
if K & 1:
	X -= D
print(abs(X))


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


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


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


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


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


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

def odd_or_eaven(x, d, k):
	if k % 2 == 0:
		print(abs(x))
	else:
		print(abs(x - d))
if abs(x) < abs(d):
	odd_or_eaven(x, d, k)
elif abs(x) - abs(k * d) > abs(d):
	print(abs(abs(x) - abs(k * d)))
else:
	mod = x % d
	temp = k - x // d
	odd_or_eaven(mod, d, temp)


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


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


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


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


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


(X, K, D) = map(int, input().split())
x = abs(X)
k = K
xd = min(x // D + 1, K)
x -= D * xd
k -= xd
if k % 2 == 1:
	x += D
print(abs(x))


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


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


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


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


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


(x, k, d) = list(map(int, input().split()))
ax = abs(x)
kd = k * d
if ax > kd:
	print(ax - kd)
else:
	num = ax // d
	if num % 2 == k % 2:
		print(ax - d * num)
	elif num == k:
		print(ax - d * num)
	else:
		print(abs(ax - d * num - d))


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


(x, k, d) = [int(s) for s in input().split()]
x = abs(x)
(q, m) = divmod(x, d)
if q >= k:
	x -= k * d
else:
	x = m
	if k - q & 1:
		x = d - x
print(x)


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


def main():
	(X, K, D) = input().split(' ')
	sign = '+'
	if X[0] == '-':
		sign = '-'
		X = X[1:]
	if bigint_compare(X, bigint_multiply(K, D)) >= 0:
		print(bigint_minus(X, bigint_multiply(K, D)))
		return 0
	div = bigint_divide(X, D)
	tmp_p0 = bigint_minus(X, bigint_multiply(D, div))
	tmp_k = bigint_minus(K, div)
	tmp_p1 = bigint_minus(D, tmp_p0)
	if int(tmp_k[-1]) % 2 == 0:
		print(tmp_p0)
	elif int(tmp_k[-1]) % 2 == 1:
		print(tmp_p1)

def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if '.' in n:
		l = n.find('.')
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == '0':
			head_zeros += 1
		else:
			break
	return n[head_zeros:]

def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	elif a > b:
		return 1
	elif a < b:
		return -1
	else:
		return 0

def bigint_plus(a, b):
	a = str(a)
	b = str(b)
	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b
	ans = ''
	carry = 0
	for i in range(d):
		s = int(a[-i - 1]) + int(b[-i - 1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans
	return ans

def bigint_minus(a, b):
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ''
	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = '-'
	m = '0' * (len(M) - len(m)) + m
	ans = ''
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i - 1]) - int(m[-i - 1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans
	return sign + delete_head_zeros(ans)

def bigint_multiply(a, b):
	a = str(a)
	b = str(b)
	md = []
	for j in range(len(b)):
		carry = 0
		mj = ''
		for i in range(len(a)):
			m = int(a[-i - 1]) * int(b[-j - 1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)
	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + '0' * k, ans)
	return ans

def bigint_divide(a, b, d=0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0
	ans = ''
	r = ''
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += '.'
			r += '0'
		else:
			r += '0'
		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)
	return delete_head_zeros(ans)
main()


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


(x, k, d) = map(int, input().split())
t = min(abs(x) // d, k)
u = abs(x) - d * t
print(abs(u - d * ((k - t) % 2)))


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


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


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


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


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(rs())

def rs_():
	return [_ for _ in rs().split()]

def ri_():
	return [int(_) for _ in rs().split()]
(X, K, D) = ri_()
x = abs(X)
if K * D <= x:
	ans = x - K * D
elif K % 2 == 0:
	ans = min(x % (2 * D), abs(x % (2 * D) - 2 * D))
else:
	ans = min(x % (2 * D) + D, abs(x % (2 * D) - D))
print(ans)


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
(x, k, d) = [int(i) for i in input().split()]
tmp = 0
res = 0
x = abs(x)
tmp = min(k, x // d)
res = x - tmp * d
if (k - tmp) % 2 == 1:
	res = abs(res - d)
print(res)


import numpy as np
(X, K, D) = map(int, input().split())
ans = 0
if X >= 0:
	if int(X / D) <= K:
		if int(X / D) % 2 == K % 2:
			ans = X % D
		else:
			ans = D - X % D
	else:
		ans = abs(X - K * D)
if X < 0:
	if abs(int(X / D)) <= K:
		if abs(int(X / D)) % 2 == K % 2:
			ans = abs(X) % D
		else:
			ans = D - abs(X) % D
	else:
		ans = abs(X + K * D)
print(ans)


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


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)


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


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


(x, k, d) = map(int, input().split())
p = k * d - x
q = p // (2 * d)
r = p / (2 * d)
if r <= 0:
	print(-p)
elif 0 < r < k:
	s = min(p - 2 * d * q, -p + 2 * d * (q + 1))
	print(s)
else:
	print(-x - k * d)


import sys

def input():
	return sys.stdin.readline().strip()
sys.setrecursionlimit(20000000)
MOD = 10 ** 9 + 7
INF = float('inf')

def main():
	(X, K, D) = list(map(int, input().split()))
	if abs(K * D) < abs(X):
		print(abs(X) - abs(K * D))
	else:
		m = X // D
		x = X - m * D
		k = K - m
		if k % 2 == 0:
			print(abs(x))
		else:
			print(abs(x - D))

def __starting_point():
	main()
__starting_point()


a = input().split(' ')
b = [x, k, d] = [int(a[0]), int(a[1]), int(a[2])]
ans = 0
if x - k * d > 0:
	ans = x - k * d
elif x + k * d < 0:
	ans = -k * d - x
elif k % 2 == 0:
	ans = x % (2 * d)
	if d < x % (2 * d):
		ans = 2 * d - x % (2 * d)
elif k % 2 == 1:
	ans = (x + d) % (2 * d)
	if d < (x + d) % (2 * d):
		ans = 2 * d - (x + d) % (2 * d)
print(ans)


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


(x, k, d) = [int(i) for i in input().split()]
if x < 0:
	x = -x
l = min(k, x // d)
k -= l
x -= l * d
if k % 2 == 0:
	print(x)
else:
	print(d - x)


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


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


(X, K, D) = map(int, input().split())
l = round((D * K - X) / (2 * D))
if l < 0:
	l = 0
elif l > K:
	l = K
print(abs(X + D * l - D * (K - l)))


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


def main():
	(x, k, d) = map(int, input().split(' '))
	if abs(x) % d == 0:
		exceed_num = abs(x) // d
	else:
		exceed_num = abs(x) // d + 1
	if k <= exceed_num:
		print(abs(abs(x) - d * k))
	elif (k - exceed_num) % 2 == 0:
		print(abs(abs(x) - d * exceed_num))
	else:
		print(abs(abs(x) - d * (exceed_num - 1)))

def __starting_point():
	main()
__starting_point()


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

def sign(x):
	return 1 if x >= 0 else -1
ans = abs(X) - K * D
if abs(X) - K * D < 0:
	divs = abs(X) // D
	mods = abs(X) % D
	nums = K - divs
	ans = abs(mods - nums % 2 * D)
print(ans)


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


(X, K, D) = map(int, input().split())
(B, A) = divmod(abs(X), D)
if B <= K:
	print(abs(A - (K - B) % 2 * D))
else:
	print(abs(X) - K * D)


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


def main():
	(X, K, D) = map(int, input().split())
	X = abs(X)
	rem = K
	cnt = min(X // D, K)
	X -= D * cnt
	rem -= cnt
	if rem > 0:
		if rem % 2 == 1:
			X = X - D
	print(abs(X))

def __starting_point():
	main()
__starting_point()


(X, K, D) = map(int, input().split())
X = abs(X)
if X > K * D:
	print(X - K * D)
	exit()
M = X // D
X -= M * D
K -= M
if K & 1:
	print(D - X)
else:
	print(X)


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


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


(x, k, d) = map(int, input().split())
xx = abs(x)
tmp = xx
tmp = xx // d
cc = min(tmp, k)
xx -= d * cc
k -= cc
k &= 1
xx -= d * k
print(abs(xx))


(X, K, D) = map(int, input().split())
X = abs(X) - K % 2 * D
E = D * 2
print(max(min(X % E, -X % E), X - D * (K - K % 2)))