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

X = abs(X)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

Solution code:
import collections


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    count = x // d
    if count > k:
        ans = x - (k*d)
    else:
        if k % 2 == count % 2:
            ans = x - (count*d)
        else:
            ans = x - (count+1) * d
    print(abs(ans))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if x - k * d >= 0:
    print(x - k * d)
    exit()
n = (x - x % d) // d  # x>0で一番近づくところまでに費やす移動回数
k -= n
if 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)
ans=0
if x>k*d:
    ans=x-(k*d)
else:
    if k%2==(x//d)%2:
        ans=x%d
    else:
        ans=min(abs(x%d-d),abs(x%d+d))
print(ans)

Submission status: Accepted

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

x = X

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D=list(map(int,input().split()))
 
def sgn(x):
    if x>0:
        return 1
    elif x<0:
        return -1
    return 0
 
def solve():
    if X>0:
        x_greedy=X-K*D
    else:
        x_greedy=X+K*D
 
    if sgn(X)==sgn(x_greedy):
        return print(abs(x_greedy))
 
    else:
        x_r=X%D
        x_l=x_r-D
 
        r_parity=(abs(X-x_r)//D)%2
        if K%2==r_parity:
            return print(abs(x_r))
        else:
            return print(abs(x_l))
 
if __name__=='__main__':
    solve()

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
x=abs(X)
key=x//D
Key=x%D
if key>K:
  print(x-K*D)
  exit()
KEY=Key-D
if (K-key)%2==0:
  ans=abs(Key)
else:
  ans=abs(KEY)
print(ans)

Submission status: Accepted

Solution code:
import math

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

X = abs(X)

cand = math.floor(X / D)

min_v = 0

if cand > K:
    min_v = X - (K * D)
else:
    if cand % 2 == K % 2:
        min_v = X - cand * D
    else:
        min_v = abs(X - (cand + 1) * D)

print(min_v)

Submission status: Accepted

Solution code:

import sys
import math
# from collections import Counter
input = sys.stdin.readline
# import numpy as np

def inp():
    return(int(input()))

def inps():
    return str(input())
    
def inlt():
    return(list(map(int,input().split())))
    
def inlst():
    return(list(map(str,input().split())))
    
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split())) 

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

x, k, d = inlt()
print(ans(x, k, d))

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
ans = 10 ** 19
X = abs(X)
if X >= K * D:
    print(X - K * D)

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

Submission status: Accepted

Solution code:
import sys


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


def main():
    X,K,D =map(int,input().split())
    X= abs(X)
    if X >= K*D:
        print(X-K*D)

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








if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
def main():
	s = raw_input()
	l = s.split()
	x = int(l[0])
	k = int(l[1])
	d = int(l[2])
	value = x - k * d
	ans = x + k * d
	be = 0
	en = k
	while (be <= en):
	    mid = (be + en) // 2
	    diff = 2 * mid * d
	    if ((value + diff) >= 0):
	        ans = value + diff
	        en = mid - 1
	    else:
	        be = mid + 1

	ans2 = value
	be = 0
	en = k
	while (be <= en):
		mid = (be + en) / 2
		diff = 2 * mid * d
		if ((value + diff) < 0):
		    ans2 = value + diff
		    be = mid + 1
		else:
		    en = mid - 1
	print(int(min(abs(ans), abs(ans2))))	



if __name__ == '__main__':
	main()

Submission status: Accepted

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

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

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

        else:
            print(abs(abs(x)%d))

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

Submission status: Accepted

Solution code:

def main():
    x, k, d = map(int, input().split())
    # x > 0
    if x < 0:
        x = -1 * x
    #the number of d in x
    r = abs(x) // d
    k_rest = k - r
    if r > 0 and k_rest < 0:
        print(x - k * d)
        exit()

    x_r = x - d * r
    x_l = x_r - d

    # print(f"xr: {x_r}\nxl: {x_l}\nk: {k_rest}")

    if k_rest % 2 == 0:
        print(abs(x_r))
    else:
        print(abs(x_l))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))

MOD = 10**9 + 7

def main():
	x, k, d = read()
	if abs(x) >= k*d:print(abs(x) - k*d);return
	if x%d == 0:print([d, 0][(k - abs(x)//d)%2 == 0]);return
	y = abs(x)%d
	print([y, abs(y-d)][(k - abs(x)//d) %2])



if __name__ == "__main__": 
	main()

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

x,k,d=nii()

dist=abs(x)
num=dist//d

if num>k:
  print(abs(dist-d*k))
  exit()
else:
  k-=num
  dist-=d*num

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
if X > K*D:
    print(X-K*D)
else:
    if X%D != 0:
        ans1 = X%D
        ans2 = (-X)%D
    else:
        ans1 = X%D
        ans2 = D
    if ((X-ans1) //D)&1 == K&1:
        print(ans1)
    else:
        print(ans2)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

l = 0
r = K
min_distance = float('inf')
diff_set = set()

while (l <= r):
    n = (r + l) // 2
    diff = (2 * n - K) * D + X
    if min_distance == float('inf'):
        min_distance = abs(diff)
    if abs(diff) < min_distance :
        min_distance = abs(diff)

    if diff in diff_set:
        break
    else:
        diff_set.add(diff)

    if diff > 0:
        r = n - 1
    else:
        l = n + 1

print(min_distance)

Submission status: Accepted

Solution code:
def main():
    X, K, D = list(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))
        return
    
    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))
main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    x, k, d = list(map(int, input().split(" ")))
    dk = d*k
    if abs(x) < dk:
        div_count = abs(x)//d
        if (k - div_count)%2==0:
            ans = abs(x)%d
        else:
            ans = d - abs(x)%d
    else:
        ans = abs(dk - abs(x))
    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

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

# ==0はどちらに置いても良い
# 0を跨がない(そのまま引いて終了)
if x - d * k >= 0:
    ans = x - d * k

# 0を跨ぐ
else:
    # 0を跨ぐ直前まで何回動くことができるか x//dで求め(ギリギリまで引き続けることと同じなのであまり(k -= times)が偶数の場合は0を跨いだときの方が小さくなることはない)
    # 偶 2までたどり着いて2度3動く場合は2に戻るのが最適
    # 奇 2までたどり着いて1度3動く場合はabs|-1| == 1
    # 割り算なので 3までたどり着いて3動くということはない(余らない)
    
    # 座標と回数を反映
    times = x // d
    k = k - times
    x -= d * times
    if k % 2 == 0:
        ans = x
    else:
        ans = abs(x - d)
print(ans)

Submission status: Accepted

Solution code:
def main():
    X, K, D = map(int, input().split())
    if X < 0:
        X = -X
    num_of_moves = min(X // D, K)

    X -= D * num_of_moves
    K -= num_of_moves

    K %= 2

    ans = abs(X - D * K)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
 
x=abs(x)
 
if x>k*d:
    print(x-k*d)
else:
    r=k-x//d
    if r%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)
if X - K * D >= 0:
  print(X - K * D)
else:
  k = X // D
  if (K - k) % 2 == 0:
    print(X - k * D)
  else:
    print(abs(X - (k + 1) * D))

Submission status: Accepted

Solution code:
def WalkingTakahashi():
    
    data = list(map(int,input().split()))
    
    position = abs(data[0])
    
    m = min(data[1], position//data[2])
    
    position = position - data[2]*m
    
    data[1] = data[1] - m
    
    data[1] %= 2
    
    position = position - data[2]*data[1]
    
    print(abs(position))
    
if __name__ == '__main__':
    WalkingTakahashi()

Submission status: Accepted

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

X = abs(X)

K_main = X // D
K_sub = K - K_main

if (K_sub < 0):
  print(abs(X - D * K))
else:
  answer = abs(X - D * K_main)
  if (K_sub % 2 == 0):
    print(answer)
  else:
    print(abs(answer -D))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    X, K, D = map(int, rl().split())
    X = abs(X)
    
    if K < X // D:
        print(X - D * K)
        return
    
    ans = X - D * (X // D)
    K -= X // D
    if K % 2 == 0:
        print(ans)
    else:
        print(abs(ans - D))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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:
  a=(k*d-x)//(2*d)
  if (k-2*a)*d-x<=x+2*d+(2*a-k)*d:
    print((k-2*a)*d-x)
  else:
    print(x+2*d+(2*a-k)*d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
import numpy as np
res = 0

count = abs(X) // D
pseudo = np.sign(X) * (abs(X) - count * D)

if abs(X) >= K * D:
    res = abs(abs(X) - K * D)
elif (K-count) % 2 == 0:
    res = abs(pseudo)
else:
    res = min(abs(pseudo + D), abs(pseudo - D))

print(res)

Submission status: Accepted

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

def calcu(x,k,D):
    if x == 0:
        if k%2 == 0:
            return 0
        else:
            return D

    a = int(x/D)
    if k <= a:
        x = x - k*D
        return x
    else:
        x1 = x- a*D
        x2 = x1 - D
        k = k - a
        if k%2 == 0:
            return x1
        else:
            return abs(x2)

        


if X >= 0:
    print(calcu(X,K,D))
else:
    print(calcu(-X,K,D))

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
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 S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

X, K, D = MI()
a = abs(X) % D
b = D - a
c = abs(X) // D
ans = 0
if K <= c:
    ans = abs(X) - K * D
elif K > c and (K - c) % 2 == 0:
    ans = a
else:
    ans = b
print(ans)

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
def sign(x):
    if x> 0:return 1
    elif x<0 :return -1
    return 0

if x>0:
    x_distance = x - k*d
else:
    x_distance = x + k*d

if sign(x)==sign(x_distance):
    print(abs(x_distance))
    exit()

x_r= x_distance % d
x_l=x_r -d
kairi=(abs(x-x_r)//d) %2

if k%2==kairi:
    print(abs(x_r))
else:print(abs(x_l))

Submission status: Accepted

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

base = X - min(N, K) * D

if K <= N:
    res = base
else:
    if (K - N) % 2 == 0:
        res = min(base, abs(base - 2 * D))
    else:
        res = min(abs(base - D), base + D)

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

cur = abs(x)  # 座標を正にしてしまいます
rem = k  # 残りの移動回数です

cnt = min(cur // d, k)  # 0に向かって移動する回数です
cur -= d * cnt
rem -= cnt

# 0 % 2 = 0なので、残り回数の場合分けをする必要はないのですが、一応
if rem > 0:
    if rem % 2 == 1:
        cur = cur - d

ans = abs(cur)
print(ans)

Submission status: Accepted

Solution code:
a=list(map(int,input().split()))
if a[0]<0:
    a[0]=-a[0]
x=a[0]%a[2]
y=a[0]//a[2]
if y>=a[1]:
    print(a[0]-a[1]*a[2])
elif (a[1]-y)%2==1:
    print(a[2]-x)
else:
    print(x)

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)
syo = x//d
kk = k - syo
xx = x - syo*d

if k <= syo:
    ans = x-d*k
else:
    if kk%2 == 0:
        ans = xx
    else:
        ans = abs(xx-d)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math
INT_MIN=-10**13
import bisect as B
###############################################################################\
x,k,d=INPUT()
x=abs(x)
if x<0:
    if k*d+x<=0:
        print(k*d+x)
    else:
        p=x//d
        neg=abs(p*d+x)
        pos=abs((p+1)*d)
        if neg<pos:
            if (k-p)%2==0:
                print(neg)
            else:
                print(pos)
        else:
            if (k-p-1)%2==0:
                print(pos)
            else:
                print(neg)
elif x==0:
    if k%2==0:
        print(0)
    else:
        print(d)
else:
    if x-k*d>=0:
        print(x-k*d)
    else:
        p=x//d
        pos=x-p*d
        neg=abs(x-(p+1)*d)
        if pos<neg:
            if (k-p)%2==0:
                print(pos)
            else:
                print(neg)
        else:
            if (k-p-1)%2==0:
                print(neg)
            else:
                print(pos)

Submission status: Accepted

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

cur = abs(x)  # 座標を正にしてしまいます
rem = k  # 残りの移動回数です

cnt = min(cur // d, k)  # 0に向かって移動する回数です
cur -= d * cnt
rem -= cnt

# 残り回数が0回の場合分けをする必要はないのですが、一応
if rem > 0:
    if rem % 2 == 1:
        cur = abs(cur - d)

print(cur)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import sys
#####sorting a dictionary by the values#####
def dict_sort(ans):
    ans=sorted(ans.items(),reverse=True,key=lambda kv:(kv[1]))
##### naive method for testing prime or not#####
def is_prime(n):
    if n==1:
        return 0
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
#####greatest common divisor of two numbers#####
def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
#####least common multiplyer of two numbers#####
def lcm(a,b):
    return (a*b)//gcd(a,b)
#####function that return all the letters#####
def alphbates():
    return "abcdefghijklmnopqrstuvwxyz"
#####binary search#####
def binary_search(ls,n,flag):
    low=0
    hi=n-1
    while(low<=hi):
        mid=(low+hi)//2
        if ls[mid]==flag:
            return mid
        elif ls[mid]>flag:
            hi=mid-1
        else:
            low=mid+1
    return -1
#####taking an array/list as input#####
def inp():
    ls=list(map(int,input().split()))
    return ls
#####taking multiple inputs#####
def mult_inp():
    return map(int,input().split())
#####Main function starts from here#####
x,k,d=mult_inp()
if x==k==d:
    if x%2==0:
        print(x)
    else:
        print(0)
elif x==0:
    if d%2==0:
        print(0)
    else:
        print(k)
elif x>0:
    if x//d>k:
        print(x-k*d)
    else:
        k-=x//d
        if k%2==0:
            print(x-(x//d)*d)
        else:
            print(abs(x-(x//d)*d-d))
else:
    x=x*-1
    if x//d>k:
        print(x-k*d)
    else:
        k-=x//d
        if k%2==0:
            print(x-(x//d)*d)
        else:
            print(abs(x-(x//d)*d-d))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
ml = min(x//d, k)
mu = min(ml+1, k)
diff_ml = k - ml
diff_mu = k - mu
print(x-ml*d if diff_ml%2 == 0 else mu*d-x)

Submission status: Accepted

Solution code:
import math

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

x = abs(x)
k1 = math.floor(x/d)

if k < k1 :
        k1 = k
else :
        k1 = k1

k2 = k1 - k

y = x - k1 * d

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input():  return sys.stdin.readline().rstrip()
    def ii():     return int(input())
    def isp():    return input().split()
    def mi():     return map(int, input().split())
    def mi_0():   return map(lambda x: int(x)-1, input().split())
    def lmi():    return list(map(int, input().split()))
    def lmi_0():  return list(map(lambda x: int(x)-1, input().split()))
    def li():     return list(input())
    def debug(x): print(x, file=sys.stderr)
    # def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)
    
    
    x, k, d = mi()
    x = abs(x)
    if x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(d)
    else:
        if x - k * d >= 0:
            print(x - k * d)
        else:
            num, res = divmod(x, d)
            # res, abs(res-d) で座標絶対値は振動する
            if (k - num) % 2 == 0:
                print(res)
            else:
                print(abs(res - d))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
y=x-k*d
if y>=0:
  print(y)
  exit()
z=x%(2*d)
if k&1:
  a=abs(z-d)
  print(a)
else:
  a=min(abs(z-2*d),z)
  print(a)

Submission status: Accepted

Solution code:
X, K, d = map(int, input().split())
X=abs(X)
if d>=X and K%2==0:
    ans=X
elif d>=X and K%2==1:
    ans=d-X
elif d<X and K*d<X:
    ans=X-K*d
else:
    ans=X%d
    if (X-ans)%2!=K%2:
        ans=d-ans
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X, K, D = MAP()

if D*K < abs(X):
    print(abs(X)-D*K)
else:
    times, d = divmod(abs(X), D)
    if times%2 == K%2:
        print(d)
    else:
        print(abs(d-D))

Submission status: Accepted

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

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

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

Submission status: Accepted

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

a = x // d

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

Submission status: Accepted

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