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 K * D < abs(X):
    ans = abs(X) - K * D
else:
    if abs(X) // D % 2 == 0:
        if K % 2 == 0:
            ans = abs(X) % D
        else:
            ans = abs(abs(X) % D - D)
    else:
        if K % 2 == 0:
            ans = abs(abs(X) % D - D)
        else:
            ans = abs(X) % D

print(ans)

Submission status: Accepted

Solution code:
# for #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase


# region fastio

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

# endregion
class union_find:
    def __init__(self, n):
        self.n = n
        self.rank = [0]*n
        self.parent = [int(j) for j in range(n)]
    
    def union(self,i,j):
        i = self.find(i)
        j = self.find(j)
        if self.rank[i] == self.rank[j]:
            self.parent[i] = j
            self.rank[j] += 1

        elif self.rank[i] > self.rank[j]:
            self.parent[j] = i
        else:
            self.parent[i] = j

    def find(self, i):
        temp = i
        if self.parent[temp] != temp:
            self.parent[temp] = self.find(self.parent[temp])
        return self.parent[temp]
from math import log2, ceil
from collections import deque, Counter as CC, defaultdict as dd
MOD = int(1e9+7)
def main():
# Enter your code here. Read input from STDIN. Print output to STDOUT
    # for t in range(int(input())):
        
        # n = int(input())
        x,k,d = [int(j) for j in input().split()]
        x = abs(x)
        n = (x//d)
        if(k<n):
            ans = x-k*d
        else:
            k = k-n
            a = x-n*d
            if(k%2):
                ans = d-a
            else:
                ans = a
        print(ans)













        



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  k=k-sa
  for i in range(k):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=mbk if (k-i)%2==0 else bk1
        break
      else:
        bk3=max(mansbk,pansbk) 
    else:
      ans=0 if (k-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

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

dif = abs(x)//d
if dif > k:
    print(abs(x)-k*d)
else:
    res = k-dif
    if res%2 == 0:
        print(abs(x)-dif*d)
    else:
        print(abs(abs(x)-(dif+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:
    xx=-(x-d*k)
    print(min(xx%(2*d),-(xx%(2*d)-2*d)))

Submission status: Accepted

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

if x==0:
  if k&1:
    print(d)
  else:
    print(0)
  exit()

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

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:
X, K, D = map(int, input().split())

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

Submission status: Accepted

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

def main():
    X, K, D = map(int, input().split())
    X = X if X > 0 else -X
    nK = K - (X // D)
    if nK < 0:
        print(X - K * D)
    else:
        pos = X - ((X // D) * D)
        if nK % 2 != 0:
            print(abs(pos - D))
        else:
            print(pos)


main()

Submission status: Accepted

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

Submission status: Accepted

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

if X >= D * K:
  print(X - D * K)
else:
  K -= int(X / D)
  # for i in range(0, int(X/D)):
  #   X -= D
  X %= D
  if K % 2:
    print(D - X)
  else:
    print(X)

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
min_=X-D*K
max_=X+D*K
if min_>=0:
  print(min_)
elif max_<=0:
  print(-max_)
else:
  c1=max_%(2*D)
  c2=c1-2*D
  print(min(abs(c1), abs(c2)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
X,K,D = map(int, input().split())
if X < 0: X = abs(X) #Xがマイナスならプラスに変換しておく

if X // D > K:
    X = X - (K * D)
    print(X)
    sys.exit() #ここはOK

if (X // D) <= K:
    if X % D >= abs((X % D)-D):
        if (K - (X // D)) % 2 == 1:
            print(abs((X % D)-D))
            sys.exit()
        else:
            print(X % D)
            sys.exit()
    else:
        if (K - (X // D)) % 2 == 1:
            print(abs((X % D)-D))
            sys.exit()
        else:
            print(X % D)
            sys.exit()

Submission status: Accepted

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

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    a = (X+K*D)//(2*D)
    if a >= K:
        a = K
        ans = abs(X-K*D)
    else:
        ans_1 = abs(X+K*D-2*a*D)
        ans_2 = abs(X+K*D-2*(a+1)*D)
        ans = min(ans_1, ans_2)
print(ans)

Submission status: Accepted

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

val = n % d

left = 0
if n >= 0:
	if n - (k * d) < 0:
		left = k - ((n - val)/d)
		if left % 2 == 0:
			print(val)
		else:
			print(abs(val - d))
	else:
		print(abs(n - (k * d)))
else:
	if n + (k * d) > 0:
		left = k - ((val - n)/d)
		if left % 2 == 0:
			print(val)
		else:
			print(abs(val - d))
	else:
		print(abs(n + (k*d)))

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	x,k,d=MI()
	x=abs(x)
	a=x%d
	b=d-a

	ans=0

	t=k-(x-a)//d

	if t<0:
		ans=x-k*d
	else:
		if t%2:
			ans=b
		else:
			ans=a
	print(ans)






if __name__ == "__main__":
	main()

Submission status: Accepted

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

X = abs(X)

a = X // D
d1 = X - (D * a)
d2 = X - (D * (a + 1))
 
if X - (D * K) >= 0:
    ans = X - (D * K)
else:
    if (K-a) % 2 == 0:
        ans = d1
    else:
        ans = d2

print(abs(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

if x>=k*d:
    print(x-k*d)
else:
    c=x//d
    x_r=x-d*c
    x_l=x-d*(c+1)
    k=k-c
    if k%2==1:
        print(abs(x_l))
    else:
        print(x_r)

Submission status: Accepted

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

# 絶対値を取れば正の座標だけ考えれば済む
X = abs(X)

# D>Xなら移動1回だけで原点飛び越える
if D > X:
  #偶数の場合はそのままで奇数の場合はD-X
  if K % 2 == 0:
    print(X)
  else:
    print(abs(D-X))
    
# K回移動してもまだ正のときは最後の座標
else:
  if X//D >= K:
    print(X - K*D)
# その後の移動の検討
  else:
    tmp = X - (X//D)*D
    if (K - X//D) % 2 == 0:
      print(tmp)
    else:
      print(min(abs(tmp-D),tmp+D))

Submission status: Accepted

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

diff = abs(X)

if K * D <= diff:
    if X >= 0:
        print(X - D * K)
    else:
        print(-1 * (X + D * K))

else:
    mod = diff // D
    if X >= 0:
        plus = X - D * mod
        minus = plus - D
        if (K - mod) % 2 == 0:
            print(plus)
        else:
            print(-minus)
    else:
        minus = X + D * mod
        plus = minus + D
        if (K - mod) % 2 == 0:
            print(-minus)
        else:
            print(plus)

Submission status: Accepted

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

X = abs(X)

a = X // D
d1 = X - (D * a)
d2 = X - (D * (a + 1))
 
if a >= K:
    ans = X - (D * K)
else:
    if (a % 2) ^ (K % 2) == 0:
        ans = d1
    else:
        ans = d2
 
print(abs(ans))

Submission status: Accepted

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

X=abs(X)
a=min(K,X//D)
ans=X-a*D
if (K-a)%2:
    ans=D-ans

print(ans)

Submission status: Accepted

Solution code:
import math
x, k, d = map(int, input().split())
x = abs(x)
# math.floor(x/d)+1回!absの方向に行く
# k>x/dのときx-math.floor(x/d)*dとx-(math.floor(x/d)+1)*dのうち小さいほうが答え
if k > x/d:
    if (k-math.floor(x/d)) % 2 == 0:
        print(abs(x-math.floor(x/d)*d))

# ここがちがうよ
    else:
        print(abs(x-math.floor(x/d+1)*d))
#     if x-math.floor(x/d)*d < abs(x-(math.floor(x/d)+1)*d):
#         print(x-math.floor(x/d)*d)
#     else:
#         print(abs(x-(math.floor(x/d)+1)*d))
else:
    print(x-k*d)

Submission status: Accepted

Solution code:
x, k, d = [int(z) for z in input().strip().split()]
if k * d < 0:
    print(y)
step = int(abs(x) / d)
# print(step)
if step > k:
    if x > 0:
        print(abs(x - k * d))
    else:
        print(abs(x + k * d))
else:
    if step % 2 == k % 2:
        if x > 0:
            print(abs(x - step * d))
        else:
            print(abs(x + step * d))
    else:
        if x > 0:
            print(abs(x - step * d - d))
        else:
            print(abs(x + step * d + d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Walking Takahashi

def main(x, k, d):
    q, r = divmod(x, d)

    h = 0
    if -(r - d) < r:
        h = d - r
    else:
        h = -r
    q = (x + h) // d

    if k < abs(q):
        if x < 0:
            return -(x + k * d)
        else:
            return x - k * d

    rk = k - q
    if rk % 2 == 0:
        return abs(h)
    else:
        if 0 < h:
            return abs(h - d)
        else:
            return abs(h + d)


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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!


import math
X, K, D = list(map(int, input().split()))
flag = 0

step = math.floor(abs(X)/D)
if abs(abs(X) - D *step) < abs(abs(X)-D * (step + 1)):
    step += 1
    flag = 1

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

Submission status: Accepted

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

X=abs(X)
a=min(K,X//D)
X-=a*D
K-=a
#print(X,K)
if K%2:
    X=abs(X-D)
ans=X
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def li():
    return [int(x) for x in input().split()]

x, k, d = li()

x = abs(x)

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

xr = x % d
xl = xr - d

r_parity = (x - xr) // d % 2
k_parity = k % 2
if r_parity == k_parity:
    print(xr)
else:
    print(abs(xl))

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(abs(x + k * d))
else:
    dis = x % d
    dis_p = d - dis
    t = k - x // d

    if t % 2 == 0:
        print(dis)
    else:
        print(dis_p)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
import math
X, K, D = list(map(int, input().split(" ")))

x = abs(X)
k = int(math.floor(x/D))
if k>=K:
    print(abs(x - K*D))
    exit()
x -= k*D
if (K-k)%2==0:
    print(abs(x))
else:
    print(abs(x - D))

Submission status: Accepted

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

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

if x >= k*d:
    ans = x - k*d
else:
    moves = x // d
    if k % 2 == moves % 2:
        ans = x - moves*d
    else:
        ans = abs(x - (moves+1)*d)
print(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:
  count = X//D
  if (K-count)%2 == 0:
    ans = X-(count*D)
  else:
    ans = min(X-(count*D)-D,X-(count*D)+D)
print(abs(ans))

Submission status: Accepted

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

# if x >= 0:
if abs(x) >= k * d:
    print(abs(x) - k * d)
else:
    q, mod = divmod(x, d)
    k -= q
    if k % 2 == 0:
        print(mod)
    else:
        print(abs(mod - d))
# else:
#     if abs(x) >= k * d:
#         print(abs(x) - k * d)
#     else:
#         q, mod = divmod(abs(x), d)
#         k -= q
#         if k % 2 == 0:
#             print(mod)
#         else:
#             print(abs(mod - d))

Submission status: Accepted

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

cnt = X // D
ans1 = X - cnt * D
ans2 = abs(ans1 - D)

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

Submission status: Accepted

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


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

Submission status: Accepted

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

x, cnt = abs(x), abs(x) // d
if k % 2 == cnt % 2:
    ans = abs(x - d * min(k, cnt))
elif k % 2 != cnt % 2:
    ans = abs(x - d * min(k, cnt + 1))
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

def resolve():
    X, K, D = map(int, input().split())
    x = abs(X)
    cnt = x // D

    if K < cnt:
        ans = x - (K * D)
    else:
        ans = x - (cnt * D)
        if (K - cnt) % 2:
            ans -= D
    print(abs(ans))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:

XKD=input().split(" ")
X=int(XKD[0])
K=int(XKD[1])
D=int(XKD[2])

X=abs(X)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import *
import sys,bisect,copyreg,copy,statistics,os
def inp(): return sys.stdin.readline().strip()
def IIX(): return (int(x) for x in sys.stdin.readline().split())
def II(): return (int(inp()))
def LI(): return list(map(int, inp().split()))
def LS(): return list(map(str, inp().split()))
def L(x):return list(x)
def out(var): return sys.stdout.write(str(var))


x,k,d=LI()

x=abs(x)

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

Submission status: Accepted

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

x, cnt = abs(x), abs(x) // d
if k % 2 == cnt % 2:
    ans = abs(x - d * min(k,cnt))
elif k % 2 != cnt % 2:
    ans = abs(x - d * min(k,(cnt + 1)))
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
X, K, D = (list(map(int, input('').split(' '))))
X = np.abs(X)

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

Submission status: Accepted

Solution code:
list = list(map(int, input().split())) 
hajime=list[0]
kaisuu=list[1]
hohaba=list[2]
if hajime>=0:
  ayumi=hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime-hohaba*kaisuu
  kouho_2=hajime-hohaba*ayumi
  kouho_3=hajime-hohaba*(ayumi+1)
 
  
  
  if kaisuu <= ayumi:
    print(kouho_1)
  else:
    if nokori%2==0:
      print(kouho_2)
    else:
      print(-kouho_3)
      
if hajime<0:  
  ayumi=-hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime+hohaba*kaisuu
  kouho_2=hajime+hohaba*ayumi
  kouho_3=hajime+hohaba*(ayumi+1)
  if kaisuu<=ayumi:
    
    print(-kouho_1)  
  else:
    if nokori%2==0:
      print(-kouho_2)
    else:
      print(kouho_3)

Submission status: Accepted

Solution code:
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)
else:
    if C%2==1:print(d-mod)
    else:print(mod)

Submission status: Accepted

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

# if x == 0:
#     if k % 2 == 0:
#         print(0)
#     else:
#         print(d)
if x >= 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))
elif x < 0:
    if abs(x) >= k * d:
        print(abs(x + k * d))
    else:
        q, mod = divmod(abs(x), d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))

Submission status: Accepted

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

if x == 0:
    if k % 2 == 0:
        print(0)
    else:
        print(d)
elif x > 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))
elif x < 0:
    if abs(x) >= k * d:
        print(abs(x + k * d))
    else:
        q, mod = divmod(abs(x), d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))

Submission status: Accepted

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

n = abs(x) // d
if k - n <= 0:
    print(abs(x)-k*d)
elif (k-n) % 2 == 0:
    print(abs(x) - n * d)
else:
    print(abs(abs(x)-n*d-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:
    ans1 = x % d
    check = x // d
    ans2 = ans1 - d
    if((k-check)%2==0):
        print(ans1)
    else:
        print(abs(ans2))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


x, k, d = map(int, readline().split())
x = abs(x)

a = min((x // d),k)
k -= a

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

Submission status: Accepted

Solution code:
meta = [int(s) for s in input().split(' ')]

now = meta[0]
times = meta[1]
distance = meta[2]

time = times
i = 0
while i < times:
    # 何回移動するか決める
    if time > 0:
        # 残り距離を移動距離で割った数と移動可能回数の少ない方を回数にする
        time = min([int(abs(now) / distance), times])
        # 移動回数分カウンタ回す
        i += time
    # 残り距離 < 移動距離になる時
    else:
        # 残り移動可能回数が偶数の時はその位置で終わり
        if (times - i) % 2 == 0:
            print(abs(now))
            exit()
        # 奇数の時はあと1回だけ
        else:
            time = 1
            i = times

    # 現在位置の正負に合わせて移動実行
    if now > 0:
        now -= distance * time
    else:
        now += distance * time

print(abs(now))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
x,k,d=map(int,input().split())
if x + k*d < 0:
    print(abs(x+k*d))
    sys.exit()
elif x - k*d > 0:
    print(abs(x- k*d))
    sys.exit()
else:
    n0 = -x//d
    ans = [n0-3,n0-2,n0-1,n0,n0+1,n0+2,n0+3]
    if k%2==0:
        ans = [i for i in ans if i%2==0]
    else:
        ans = [i for i in ans if i%2!=0]
    ans = [abs(x+i*d) for i in ans]
print(min(ans))

Submission status: Accepted

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

X = abs(X)
temp = X // D
if K%2 != temp%2:
    temp = temp + 1

if temp > K:
    temp = K

temp = D * temp

print(abs(X - temp))

Submission status: Accepted

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


N = abs(int(X / D))
if N >= K:
    print(min( abs(X - D * K), abs(X + D * K) ))
    sys.exit()

if X < 0:
    X = X + D * N
    ans1 = abs(X)
    ans2 = abs(X + D)
    if (K - N) % 2 == 0:
        print(ans1)
    else:
        print(ans2)

else:
    X = X - D * N
    ans1 = abs(X)
    ans2 = abs(X - D)
    if (K - N) % 2 == 0:
        print(ans1)
    else:
        print(ans2)

Submission status: Accepted

Solution code:
meta = [int(s) for s in input().split(' ')]

now = meta[0]
times = meta[1]
distance = meta[2]

out = 0
i = 0
while i < times:
    #何回移動するか決める
    #残り距離を移動距離で割った数と移動可能回数の少ない方を回数にする
    time = min([int(abs(now) / distance), times])
    #移動回数分カウンタ回す
    i += time

    #残り距離 < 移動距離になる時
    if time == 0:
        #残り移動可能回数が偶数の時はその位置で終わり
        if (times - i) % 2 == 0:
            print(abs(now))
            exit()
        #奇数の時はあと1回だけ
        else:
            time = 1
            i = times
#            print('kisuu')

    #現在位置の正負に合わせて移動実行    
    if now > 0:
        now -= distance * time
    else:
        now += distance * time
    
#    print('i: ' + str(i) + ', now: ' + str(now))

print(abs(now))

Submission status: Accepted

Solution code:
def resolve():
	import math
	X, K, D = map(int, input().split())
	X, K, D = abs(X), abs(K), abs(D)
	n = math.floor(X/D)
	A = X - n*D
	k = K - n
	if k < 0:
		print(X - (K*D))
	elif k%2 == 0:
		print(A)
	else:
		print(abs(A-D))
resolve()

Submission status: Accepted

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

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

k -= cnt
x = abs(x) - cnt*d

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

Submission status: Accepted

Solution code:
import sys
X, K, D = map(int, input().split())
X = abs(X)
num = X - K*D
if 0<num:
  print(num)
  sys.exit(0)

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

Submission status: Accepted

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

if K < cnt:
    ans = x - (K * D)
else:
    ans = x - (cnt * D)
    if (K - cnt) % 2 != 0:
        ans -= D
print(abs(ans))

Submission status: Accepted

Solution code:
#-------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

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")
 
# ------------------- fast io --------------------

def solve(x, k, d):
    if x > 0:
        req = x // d
        if k <= req:
            return abs(x - k * d)
        else:
            k -= req
            cur = x % d
            return abs(cur) if k % 2 == 0 else abs(cur - d)
    else:
        req = -x // d
        if k <= req:
            return abs(x + k * d)
        else:
            k -= req
            cur = -(-x % d)
            return abs(cur) if k % 2 == 0 else abs(cur + d)


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

Submission status: Accepted

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

result = 0

abs_x = abs(x)
firstNearestIterNum = abs_x // d

if firstNearestIterNum >= k:
	result = abs_x - (d * k)
else:
	if (k - firstNearestIterNum) % 2 == 1:
		result = abs_x - (d * (firstNearestIterNum + 1))
	else:
		result = abs_x - d * firstNearestIterNum

print(abs(result))

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect

INF = 10**9+7

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import math

if __name__ == '__main__':
    l = list(map(int, input().split()))
    X = int(l[0])
    K = int(l[1])
    D = int(l[2])
    
    if D*K <= abs(X):
        minVal=abs(X) - D*K
    else:
        n=math.floor(abs(X)/D)+1
        # while abs(X) - D*n > 0:
        #    n=n+1
        
        if (K-n)%2==0:
            minVal = abs(abs(X) - D*n)
        else:
            minVal = abs(abs(X) - D*(n-1))
        
    print(minVal)