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: Wrong Answer

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

if x-k*d > 0:
    print(x-k*d)
    exit
elif x+k*d < 0:
    print(x+k*d)
    exit
else:
    m = 2 * math.ceil(((k-2)*d-x)/(2*d))- k
    print(min(abs(x+m*d), abs(x+(m+2)*d)))

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

if 2 * x <= d:
    ans = x
elif x <= d:
    if k % 2 == 0:
        ans = x
    else:
        ans = x - d
else:
    t = x // d
    if k >= t + 3:
        if (k - t) % 2 == 0:
            ans = min(abs(x - t * d), abs(x - (t + 2) * d))
        else:
            ans = min(abs(x - (t + 1) * d), abs(x - (t + 3) * d))
    else:
        ans = min(abs(x - k * d), abs(x - (k - 2) * d))

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

#直線のみに進行すればいい場合の回数
s = abs(x)//d

#最短回数よりも移動可能回数の方が少ない場合
if s >= k:
    print(abs(x) - k*d)

#最短経路回数よりも多く移動する場合
else:
    if (k-s) %2 == 0:
        print(abs(x) - s*d)
    else:
        print(d-(abs(x) - k*d))

Submission status: Wrong Answer

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

times = abs(X) // D

if times > 0 :
    if current_position >= 0:
        current_position = current_position - D*times
    elif current_position < 0:
        current_position = current_position + D*times
K = K - times
for k in range(K):
    if current_position >=0:
        current_position = current_position - D
    elif current_position< 0 :
        current_position = current_position + D

    if abs(current_position) <= D:
        if (K - k + 1) % 2 == 0:
            break
        elif (K - k + 1) % 2 == 1:
            if current_position >= 0:
                current_position = current_position - D
            elif current_position < 0 :
                current_position = current_position + D
            break

print(current_position)

Submission status: Wrong Answer

Solution code:
#C
from decimal import Decimal
X,K,D= map(Decimal, input().split())

if X>=0:
    if K*D<=X:
        ans=X-K*D
    else:
        tmp = X%D
        cnt = K-X//D
        if cnt==0:
            ans=tmp
        else:
            if cnt%2==0:
                ans=tmp
            else:
                ans=abs(tmp-D)
else:
    if K*D<=abs(X):
        ans = X+K*D
    else:
        tmp = -1*abs(X)%D
        cnt = K-abs(X)//D
        if cnt==0:
            ans = tmp
        else:
            if cnt%2==0:
                ans = tmp
            else:
                ans=abs(tmp+D)
    
print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = abs(x) - abs(k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = abs(x) - abs(a * d)
        x = abs(x) - abs(d)
    else:
        x = abs(x) - abs(a * d)
        x = abs(x) - abs(d)

print(abs(x))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
while k!=0:
    if x-(k*d)>0:
        x=x-(k*d)
        break
    if x-d==0:
        print (x)
        break
    if x-d < 0 :
        if abs(x-d)<x:
            x=abs(x-d)
            break
        else:
            break
    else :
        x=x-d
        k=k-1

print (x)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split(' '))
time=0
while X>0:
    X-=D
    time+=1
    if K-time==0:
        break
if (K-time)%2==0:
    print(abs(X))
else:
    print(abs(X+D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X==K==D:
    print(X)
    exit()
x = X
# vmin = abs(X)
cnt = 0
# for k in range(K):
#     if X>0:
#         x -= D
#     else:
#         x += D
#     if vmin>abs(x):
#         vmin = abs(x)
#         cnt += 1
#     else:
#         break
if abs(X)>=(K*D):
    cnt = K
else:
    cnt = int(math.floor(abs(X)/D))
if X>0:
    print(abs(X - (cnt * D)))
else:
    print(abs(X + (cnt * D)))

Submission status: Wrong Answer

Solution code:
import random
# x, k, d = 7, 4, 3
x, k, d = map(int, input().split())

# 正方向へn回動くとすると
# abs(x + 2dn - kd)を最小とするようなnを求める

n = d//2
min = x + 2*d*n - d*k

for _ in range(100):
    m = random.randint(0, k)
    v = x + 2*d*m - d*k
    if abs(v) < abs(min):
        min = v
        n = m

for _ in range(10):
    if min < 0:
        n += 1
    else:
        n -= 1
    if n < 0 or n > d:
        break
    v = x + 2*d*n - d*k
    if abs(v) < abs(min):
        min = v
    else:
        break
print(abs(min))

Submission status: Wrong Answer

Solution code:
X, K, D = [int(n) for n in input().split()]
x = abs(X)
d = (x+D-1)//D
r = x % D
ans = 0
if d<=K:
    if X>0:
        X -= d*D
    else:
        X += d*D
    K -= d
if K%2==0:
    ans = X
else:
    if X>0:
        ans = X-D
    else:
        ans = X+D
print(abs(ans))

Submission status: Wrong Answer

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

X = abs(X)

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

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split())
if(a<0):
    aa=-a
else:
    aa=a
if(aa>b*c):
    ans=aa-b*c
    print(ans)
    exit()
else:
    if(aa<c):
        if(b%2==0):
            print(aa)
            exit()
        else:
            print(c-aa)
            exit()
    if(aa==0):
        if(b%2==0):
            print(aa)
            exit()
        else:
            print(c)
            exit()
    else:
        amari=aa%c
        amarioruta=abs(amari-c)
        if(amari==0):
            hantei=aa/c
            if(hantei%2==0):       
                bb=b%2
                if(bb==0):
                    print(0)
                    exit()
                else:
                    print(c)
                    exit()
            else:
                bb=b%2
                if(bb==0):
                    print(c)
                    exit()
                else:
                    print(0)
                    exit()
        else:
            if(aa<b*c<aa+c):
                muri1=b*c-aa
                print(muri1)
                exit()
            else:
                if(amari>amarioruta):
                    print(amarioruta)
                    exit()
                else:
                    print(amari)
                    exit()

Submission status: Wrong Answer

Solution code:
X, K ,D = input().split()
X = int(X)
K = int(K)
D = int(D)
if (K == 1):
  if (X >= 0):
    print(abs(X -D))
  if (int(X) < 0):
      print(abs(X + D))
  exit()
if (X == 0):
  print(abs( - (K % 2) *D))
elif (X > 0):
  while ((X > 0) and (K != 0)):
         X = X - D
         K -= 1
  if (K==0):
    print(X)
    exit()
  print(abs(abs(X) - (K % 2) *D))
elif (X < 0):
  while ((X < 0) and (K != 0)):
         X = X + D
         K -= 1
  if (K==0):
    print(X)
    exit()
  print(abs(abs(X) - (K % 2) *D))

Submission status: Wrong Answer

Solution code:

li = list(map(int ,input().split()))

x = abs(li[0])
k = li[1]
d = li[2]

if x == d and k % 2 == 0:
    print(x)
    exit()
if x == d and k & 2 == 1:
    print(0)
    exit()

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

for _ in range(k):
    sum_result = x + d
    minus_result = x - d
    if abs(sum_result) < abs(minus_result):
        x = sum_result
    elif abs(sum_result) > abs(minus_result):
        x = minus_result


print(abs(x))

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 999999999
tmp = 0
if X == 0:
  if K % 2 == 0:
    print(0)
  else:
    print(D)
  sys.exit()
if X >= K*D:
  print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  if X >= 0:
    b = a - D*2
  else:
    b = a + D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  if X >= 0:
    b = a - D*2
  else:
    b = a + D*2
print(tmp,a,b)
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

Solution code:
X,  K,  D = list(map(int, input().split()))
L = X + D * K
 
if L >= 0:
   if L / (2 * D) - int(L / (2 * D)) > 0.5:
      a = int(L / (2 * D)) + 1
   else:
      a = int(L / (2 * D))
 
elif L < 0:
   if L / (2 * D) - int(L / (2 * D)) < -0.5:
      a = int(L / (2 * D)) - 1
   else:
      a = int(L / (2 * D))
print(abs(X-2*D*a+D*K))

Submission status: Wrong Answer

Solution code:
import sys

X, K, D = [int(x) for x in input().strip().split(" ")]

X = abs(X)
k, x = divmod(X, D)

if K <= k:
    print(abs(X - D * K))
    sys.exit(0)

X = x
K -= k

if k % 2 == 0:
    print(abs(X))
    sys.exit(0)

print(abs(X - D))

Submission status: Wrong Answer

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

for k in range(move):
    move_plus = x + d
    move_minus = x - d

    if abs(move_plus) > abs(move_minus):
        x = move_minus
    else:
        x = move_plus

    if abs(x) <= d/2 and move-(k+1)%2==0:
        break
    else:
        x = x+d
        break

print(abs(x))

Submission status: Wrong Answer

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

for i in range(k):
    if x < 0:
        if i % 2 == 1:
            print(abs(x + d))
            exit()
        else:
            print(abs(x))
            exit()
    x -= d
print(abs(x))

Submission status: Wrong Answer

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

  if X != D:
    k_lim = X // D
    X_pulas = X - D * k_lim
    ans = pulas(X_pulas, k_lim, D)
  else:
    if K % 2 == 0:
      ans = X
    else:
      ans = 0
  print(ans)

def pulas(x, k, D):
  if k % 2 == 0:
    return x
  else:
    return min([np.abs(x + D), np.abs(x - D)])


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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

X = abs(X)	

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

mov = X 

if abs(X-mov*D) > abs(X-(mov+1)*D):	
    mov += 1	

rem = abs(X-mov*D)	
mov = K - mov  
mov = mov % 2	

print(abs(rem-mov*D))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().strip().split())
p=x
hold=x
for i in range(k):
	if p<=0 and x<=0:
		x+=d

	elif p<=0 and x>=0:
		break

	if p>=0 and x>=0:
		x-=d

	elif p>=0 and x<=0:
		break

	if hold>=0:
		hold=hold-d

	if hold<0:
		hold=hold+d



if i+1!=k:
	k=k-(i)

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

	else:
		print(min(abs(x-d),abs(x+d)))
else:
	print(hold)

Submission status: Wrong Answer

Solution code:
s=input()
s=s.split()
x=int(s[0])
k=int(s[1])
d=int(s[2])

n=x
for i in range(k):
    if i == 0:
        n=x-d
        continue
    if abs(n-d) < abs(n+d):
        n=n-d
    else:
        n=n+d

print(abs(n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
i=0
count=0
if X<0:
	if X+K*D<=0:
		print(abs(X+K*D))
		count+=1
	else:
		L=X+i*D
		while L<=0:
			i+=1
			L=X+i*D
		if abs(L)>abs(X+(i+1)*D):
			i+=1
			L=abs(X+i*D)
else:
	if X-K*D>=0:
		print(abs(X-K*D))
		count+=1
	else:
		L=X-i*D
		while L>=0:
			i+=1
			L=X-i*D
		if abs(L)>abs(X-(i-1)*D):
			i-=1
			L=abs(X-i*D)
if count==0:
	kaisuu=K-i
	if kaisuu%2==0:
		print(abs(L))

	else:
		a=abs(L+D)
		b=abs(L-D)
		if a>=b:
			print(a)
		else:
			print(b)

Submission status: Wrong Answer

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

if x-k*d >= 0:
    print(x-k*d)
    exit
elif x+k*d <= 0:
    print(x+k*d)
    exit
else:
    m = 2 * math.ceil(((k-2)*d-x)/(2*d))- k
    print(min(abs(x+m*d), abs(x+(m+2)*d)))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ischange = bool()
ischanging = bool()
bef_x = 0

if x <= 0:
    ischange = False
    ischanging = False
else:
    ischange = True
    ischanging = True

if abs(x) < abs(k * d):
    for i in range(k):
        if ischange == ischanging:
            if x <= 0:
                x = x + d
                ischanging = False
            else:   
                x = x - d
                ischanging = True
        else:
            if x <= 0:
                bef_x = x + d
            else:   
                bef_x = x - d
      
            break
else:
    x = abs(x) - abs(k * d) 
    
if bef_x == 0:
    bef_x = x

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8

X, K, D = map(int, input().split())
# X: 現在地
# K: 移動回数
# D: 移動距離の絶対値
# ans: 移動後の座標の絶対値が最も小さくなるように動け！
mem = []

for _ in range(K):
    p_move = X + D
    n_move = X - D

    if abs(p_move) <= abs(n_move):
        X = p_move
    else:
        X = n_move

    mem.append(X)
    if len(mem) >= 8 and mem[-1] == mem[-3]:
        break

print(abs(X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
L = [int(i) for i in input().rstrip().split()]
X = L[0]
K = L[1]
Y = L[2]

if X>K*Y:
  print(X-(K*Y))
else:
  p = X//Y
  q = (K-p)%2
  if q == 0:
    pos = X-p*Y
  else:
    pos = (p+1)*Y - X
  print(pos)

Submission status: Wrong Answer

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

    a = 0

    for i in range(k//2):
        if abs(x-d) < abs(x+d):
            a -= 1
        else:
            a += 1
        if abs(x-d) < abs(x+d):
            a -= 1
        else:
            a += 1

        if a == 0:
            break

        x = x + a * d 


    if k % 2 == 1:
        if abs(x-d) < abs(x+d):
            x = x - d
        else:
            x = x + d
    print(abs(x))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
def make_most_min(X, K, D):
    mokuhyou = X
    best = 0
    if X >= 0:
        for i in range(K + 1):
            if abs(X - i * D) < mokuhyou:
                mokuhyou = abs(X - i * D)
                best = i
        if best <= K:
            best_posi = abs(X - best * D)
            return best_posi
        if best % 2 == K % 2:
            return abs(X - best * D)
        else:
            return min(abs(X - best * (D+1)), abs(X - best * (D-1)))
if X >= 0:
    a = make_most_min(X, K, D)
else:
    a = make_most_min(-X, K, D)
print(a)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
import math
x, k, d = map(int, input().split())

num = k*d

x = abs(x)

if x >= num:
    print(x - num)
else:
    if x > d:
        j = int(x / d)
    else:
        if x*2 > d:
            j = 1
        else:
            j = 0

    if j != 0 and j != 1:
        b = x % (d*j)
    elif j == 1:
        b = d - x
    else:
        b = x
    min = min(b, d - b)
    max = max(b, d - b)
    if min == d - b:
        j += 1

    if (k - j) % 2 == 0:
        print(min)
    else:
        print(max)

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
a = X / D
A = math.floor(a)
if A < K:
  if A + 2 < K:
  	print(min(abs(X - D * A), abs(X - D * (A + 2))))
  else:
    print(abs(X - D * A))
else:
  print(abs(X - D * K))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
flag = True
if abs(X)>K*D:
  ans = abs(abs(X)-K*D)
  flag = False
else:
  ans = X

res = abs(X)%D
r = X//D
if flag:
  if r%2 == K%2:
    ans = min(ans, res)
  else:
    ans = min(ans, abs(res-D))
print(ans)

Submission status: Wrong Answer

Solution code:
arr=list(map(int,input().rstrip().split()))
X=arr[0]
K=arr[1]
D=arr[2]

ab=X/D

if ab.is_integer():
    if ab<=K:
        ab=int(ab)
    else:
        ab=K
        
else:
    if ab<=K:
        ab=int(ab)+1
    else:
        ab=K
X=X-ab*D

cc=X
cc1=X

K=K-ab
K1=K
if K>1:
    asa=K//2
    cc=cc+(asa)*(D)
    K=K-asa
    cc=cc-K*D
    aw=abs(cc)
else:
    cc=cc+K*D
    aw=abs(cc)

if K1>1:
    
    asa1=K1//2
    cc1=cc1-(asa1)*(D)
    K1=K1-asa1
    cc1=cc1+K1*D
    aw1=abs(cc1)
else:
    cc1=cc1-K1*D
    aw1=abs(cc1)
    



print(min(aw1,aw))

Submission status: Wrong Answer

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

t = abs(X) // D
now = X - t*D

if K <= t: 
    ans = X - D*K
else:
    if (K - t) % 2 == 0:
        ans = now
    else:
        ans = abs(now - D)

print(ans)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

if x-k*d >= 0:
    print(x-k*d)
    exit
elif x+k+d <= 0:
    print(x+k*d)
    exit
else:
    m = 2 * math.ceil(((k-2)*d-x)/(2*d))- k
    print(min(abs(x+m*d), abs(x+(m+2)*d)))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
ans=0
KK=K-X//D
XX=X%D

if KK%2==0:
    ans=abs(XX)
else:
    if abs(XX+D)>=abs(XX-D):
        ans=abs(XX-D)
    else:
        ans=abs(XX+D)
        
anss=0
KKK=K-X//D-1
XXX=abs(X%D-D)

if KKK%2==0:
    anss=abs(XXX)
else:
    if abs(XXX+D)>=abs(XXX-D):
        anss=abs(XXX-D)
    else:
        anss=abs(XXX+D)
        
if ans>=anss:
    print(anss)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
def mini(X, K, D):
    if X > 0:
        for i in range(K):
            X -= D
            if X <= 0:
                K -= i
                break
            if i == K-1:
                return abs(X)
    elif X < 0:
        for i in range(K):
            X += D
            if X >= 0:
                K -= i
                break
            if i == K-1:
                return abs(X)
    if K%2 == 0:
        if X <= 0:
            return abs(X + D)
        if X > 0:
            return abs(X - D)
    else:
        return abs(X)

if __name__ == '__main__':
    X, K, D = map(int, input().split())
    print(mini(X, K, D))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
Xa, K, D = map(int, input().split())
X = abs(Xa)
Xs = X // D
Xm = X % D
if K < Xs:
#     print("A")
    print(abs(X-K*D))
elif Xm == 0:
    if Xa != 0:
        if K % 2 == 0:
            print(D)
        else:
            print(0)
    else:
        if K % 2 == 0:
            print(0)
        else:
            print(D)
elif Xm / 2 > K:
#     print("B")
    print(abs(D-Xm))
#     if (Xa >= 0):
#         print(abs(D-Xm))
#     else:
#         print(abs(D-K))
else:
    print(abs(D-K))
#     if (Xa >= 0):
#         print(abs(D-K))
#     else:
#         print(abs(D-Xm))

Submission status: Wrong Answer

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

X = abs(X)

a = X % D

X -= min(a, K) * D
K -= min(a, K)

if K > 0 and abs(X) > abs(X - D):
    X -= D
    K -= 1

K %= 2

if K == 1:
    if abs(X + D) < abs(X - D):
        X += D
    else:
        X -= D

result = abs(X)

print(result)

Submission status: Wrong Answer

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

if(X-D*K)>=0:
  print(X-D*K)
else:
  x = int((D*K+X) // (2*D))
  ans0 = abs(-2*D*(x-1)+(D*K+X))
  ans1 = abs(-2*D*x+(D*K+X))
  ans2 = abs(-2*D*(x+1)+(D*K+X))
  ans3 = abs(-2*D*(x+2)+(D*K+X))
  print(min(ans0, ans1, ans2, ans3))

Submission status: Wrong Answer

Solution code:
l=list(map(int,input().split()))
x=l[0]
k=l[1]
d=l[2]
x1=abs(x)
cou=0
while x1>0:
  x1-=d
  cou+=1
if k<cou:
  x2=x1-k*d
  print(x2)
  # if x<=0:
  #   x3=x2*(-1)
  #   print(x3)
  # else:
  #   print(x2)

else:
  a=cou%2
  b=k%2
  x4=x1*(-1)
  if a==b:
    print(abs(x1))
  if a!=b:
    print(x1+d)

Submission status: Wrong Answer

Solution code:
lst = input().split()

X = int(lst[0])
K = int(lst[1])
D = int(lst[2])

a = int((D * K - X) / (2 * D))

if a < 0:
   a = 0

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

result = min([abs(f(a)), abs(f(a+1)), abs(f(a-1))])

print(result)

Submission status: Wrong Answer

Solution code:
import random
# x, k, d = 7, 4, 3
x, k, d = map(int, input().split())

# 正方向へn回動くとすると
# abs(x + 2dn - kd)を最小とするようなnを求める

n = d//2
min = x + 2*d*n - d*k

for _ in range(100):
    m = random.randint(0, k)
    v = x + 2*d*m - d*k
    if abs(v) < abs(min):
        min = v
        n = m

for _ in range(10):
    if min < 0:
        n += 1
    else:
        n -= 1
    if n < 0 or n > d:
        break
    v = y = x + 2*d*n - d*k
    if abs(v) < abs(min):
        min = v
    else:
        break
print(abs(min))

Submission status: Wrong Answer

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

if X==K==D:
  print(X)
  exit()

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

Submission status: Wrong Answer

Solution code:
from math import ceil, floor
X, K, D = map(int, input().split())
ans = abs(X)
fapp = (K*D - X)/(2*D)

if fapp < 0:
    list = [0]
elif fapp > K:
    list = [K]

else:
    list = [ceil(fapp), floor(fapp)]
for f in list:
    abso = abs(X + f*D - (K-f)*D)
    if abso < ans:
        ans = abso

print(ans)

Submission status: Wrong Answer

Solution code:
l = list(map(int,input().split()))
x = l[0]
k = l[1]
d = l[2]
prev = 0

n = x//d

if k%2 == 0 and n%2 == 1:
  n += 1
elif k%2 == 1 and n%2 == 0:
  n += 1

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

Submission status: Wrong Answer

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


def solve(X: int, K: int, D: int):
    X=abs(X)

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

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

    N = X // (D*2)
    ans = min(ans, abs(X - D * 2 * (N - 1)))
    ans = min(ans, abs(X - D * 2 * N))
    if N < Kh:
        ans = min(ans, abs(X - D * 2 * (N+1)))

    print(ans)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(X, K, D)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x,k,d = [int(x) for x in input().split()]
t = abs(int(x/d))
if x < 0:
    d = -d
if t >= k:
    print(x-d*k)
elif t == 0:
    if k % 2 == 0:
        print(abs(x))
    else:
        print(min(abs(x-d),abs(x+d)))
elif t < k:
    if abs(x-d*t) < abs(x-d*t-d):
        if (t-k)%2==0:
            print(abs(x-d*t))
        else:
            print(min(abs(x-d*t+d),abs(x-d*t-d)))
    elif abs(x-d*t) == abs(x-d*t-d):
        print(abs(x-d*t))
    else:
        if (t-k-1)%2==0:
            print(abs(x-d*t-d))
        else:
            print(min(abs(x-d*t),abs(x-d*t-2*d)))

Submission status: Wrong Answer

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

x=abs(x)

t=x

if k%2==1:
    t-=d
    k-=1

min=abs(t)

if t-k*d>0:
    print(t-k*d)
    exit(0)

if t-k*d<0:
    s=int(t/(d*2))
    print(s)
    k-=s*2
    t-=d*s*2
    min=abs(t)

while(-x<t and k>0):
    t-=d*2
    k-=2
    if min < abs(t):
        break
    else:
        min=abs(t)

print(min)

Submission status: Wrong Answer

Solution code:
# from quantize import Decimal
X, K, D = list(map(int, input().split()))

dist= (X/D-K)/2
# print(dist)

if -dist > K or dist > 0 :
    i = K-1
else :
    i = -round(dist)

# print(i)
print(abs(X + i*D - ((K-i)*D)))

Submission status: Wrong Answer

Solution code:
#C - Walking Takahashi
X,K,D= map(int,input().split())

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    l = 0
    r = k+1
    tmp_res = 0
    while(l+1!=r):
        mid = (l+r)//2
        tmp_res = copy.deepcopy(res)
        if((k-mid)%2==1):
            res = min(res, abs(abs(x)-(mid+1)*d))
            res = min(res, abs(abs(x)-(mid-1)*d))
            mid+=1
        if((k-mid)%2==0):
            res = min(res, abs(abs(x)-mid*d))
            res = abs(res)
        if(abs(abs(x)-mid*d) > x):
            r = mid
        else:
            l = mid
        if tmp_res == res:
            break

    print(res)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

x = abs(x)
s = float("inf")
t = 0
for i in range(k):
  x -= d
  if abs(x) <= s:
    s = abs(x)
    t = i+1
  if x < 0: break

if (k-t)%2 == 0: print(s)
else: print(s+d)

Submission status: Wrong Answer

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

a = abs(X) // D
if a <= K:
  b = abs(X) - (D * a)
  c = abs(b - D)
else:
  b = abs(X) - (D * K)
  c = 100000000000000000

if ((K - a)%2 == 0) and b <= c:
  print(b)
elif ((K - a)%2 == 1) and b <= c:
  print(abs(b - D))
elif ((K - a)%2 == 0) and c < b:
  print(c)
elif ((K - a)%2 == 1) and c < b:
  print(abs(c - D))

Submission status: Wrong Answer

Solution code:
import sys

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

p = X
_p = 0
k = 0

if abs(p) > K*D:
    print(abs(p)-K*D)
    sys.exit()

if p < 0:
    while 1:
        _p = p
        p += D
        k += 1
        if p > 0:
            break
    if K-k % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
elif p > 0:
    while 1:
        _p = p
        p -= D
        k += 1
        if p < 0:
            break
    if K-k % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)