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:
if __name__ == '__main__':

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

    #マイナス座標はプラスに変換する
    x = abs(x)

    #総移動回数でどこまで行くか？
    x = x - k * d

    ans = 0
    if 0 <= x:
        #残りがないのでその地点が最終値
        ans = x
    else:
        #残りを正確に計算
        tmp = x // d
        #残り距離
        x = x - (tmp * d)
        
        #残り回数
        k -= tmp
        
        if k%2 == 1:
            ans = abs(x-d)
        else:
            ans = x
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# ABC175
# C
# 座標XからK回正または負にD移動
# できない
x, k, d = map(int, input().split())
c = x // d
c

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

semin=x//d #最小に行くための個数
if semin%2==0:
  minb=0 #最小に行くための個数が偶数
else:
  minb=1 #最小に行くための個数が奇数

if semin>k:
  print(abs(x-k*d))
else:
  k=k-semin
  if k%2==0:
    ans=0 #偶数
  else:
    ans=1 #奇数
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-d*semin))
    else:
      print(min(abs((x-(d*semin))-d),abs(x-d*semin)))
  else:#ｋが奇数の時
    if minb==0:
      print(abs((x-(d*semin))-d))
    else:
      print(abs(x-d*semin))

Submission status: Wrong Answer

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

semin=x//d #最小に行くための個数
if semin%2==0:
  minb=0 #最小に行くための個数が偶数
else:
  minb=1 #最小に行くための個数が奇数

if semin>k:
  print(abs(x-k*d))
else:
  k=k-semin
  if k%2==0:
    ans=0 #偶数
  else:
    ans=1 #奇数
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-d*semin))
    else:
      print(min(abs((x-(d*semin))-d),abs(x-d*semin)))
  else:#ｋが奇数の時
    if minb==0:
      print(abs((x-(d*semin))-d))
    else:
      print(abs(x-d*semin))

Submission status: Wrong Answer

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

semin=x//d #最小に行くための個数
if semin%2==0:
  minb=0 #最小に行くための個数が偶数
else:
  minb=1 #最小に行くための個数が奇数

if semin>k:
  print(abs(x-k*d))
else:
  k=k-semin*d
  if k%2==0:
    ans=0 #偶数
  else:
    ans=1 #奇数
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-d*semin))
    else:
      print(min(abs((x-(d*semin))-d),abs(x-d*semin)))
  else:#ｋが奇数の時
    if minb==0:
      print(abs((x-(d*semin))-d))
    else:
      print(abs(x-d*semin))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
pX = 0
mX = 0
if X < 0:  X = -1*X

if X >= K*D:
  print(X - K*D)
  exit()
elif D <= X:
  K = X//D
  X = X%D
  while K > 0:
    if X >=0:
      X -= D
      K -= 1
      if pX == X:
        print(min(abs(mX),abs(pX)))
      if X < 0:
        pX = X 
    else:
      X += D
      K -= 1
      if mX == X:
        print(min(abs(mX),abs(pX)))
      if X > 0:
        mX = X

else:
    while K > 0:
      if X >=0:
        X -= D
        K -= 1
        if pX == X:
          print(min(abs(mX),abs(pX)))
          exit()
        if X < 0:
          pX = X 
      else:
        X += D
        K -= 1
        if mX == X:
          print(min(abs(mX),abs(pX)))
          exit()
        if X > 0:
          mX = X


print(abs(X))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if k*d <= x:
    ans = x-k*d
else:
    # 残り移動回数
    modCount = k - x//d
    # 残り距離
    modDis = x%d
    if modCount%2 == 0:
        ans = abs(x%d)
    else:
        ans = min(abs(x%d - d), abs(x%d + d))
print(ans)

Submission status: Wrong Answer

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

    # print(modDis - d*modCount)
print(ans)

Submission status: Wrong Answer

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


if X == 0:
    if K % 2 == 0:
        print(0)

    elif K % 2 != 0:
        print(D)

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

    elif X - D * K > 0:
        print(abs(X - D * K))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
if X < 0:  X = -1*X

if X >= K*D:
  print(X - K*D)
  exit()
else:
  K = X//D
  X = X%D
  while K > 0:
    if X >=0:
      X -= D
      K -= 1
    else:
      X += D
      K -= 1
print(abs(X))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
time = X // D
time = min(K, time)
X = X - (D * time)
K = K - time

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

Submission status: Wrong Answer

Solution code:
from math import *
x, k, d = map(int, input().split())
z = ceil(x/d)
if x-k*d >= 0:
    print(x-k*d)
else:
    if (k-z)%2 == 0:
        print(abs(x-z*d))
    else:
        print(x-z*d+d)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

p = x//d
m = k - p
ans = 0
if k<p:
    ans = x - d*k
else:
    ans = x - d*p
    if (m%2 == 1) or (k%2 ==1):
        ans = abs(ans) - d

print(abs(ans))

Submission status: Wrong Answer

Solution code:

def walking():
    x, k, d = map(int, input().split())
    x = abs(x)
    r = 0
    if x > k * d:
        return x - k * d
    else:
        r = x//d
        if (k - r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d
    
loc = walking()
print(loc)

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
syo = X // D
amari = X % D
#print(syo, amari)
if syo <= D:
    another = amari - D
    #print(amari, another)
    if (K - syo) % 2 == 0:
        print(amari)
    else:
        print(abs(another))
else:
    print(X - K * D)

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(abs(Xa-D*K))
elif (K-Xs) % 2 == 0:
    print(abs(Xm))
else:
    print(abs(D-Xm))

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif Xm == 0 and X != 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif Xm == 0:
    if (K % 2 == 0):
        print(D)
    else:
        print(0)
    
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif X == 0:
    if (K % 2 == 0):
        print(0)
    else:
        print(D)
    
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif Xm == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
if X<0:
  X = -X

a = X//D

A = abs(X-D*a)
k = abs(K - a)

if k%2 == 0:
  print(A)
else:
  print(abs(A-D))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
if X<0:
  X = -X
if D<0:
  D = -D

a = X//D

A = abs(X-D*a)
k = K - a

if k%2 == 0:
  print(A)
else:
  print(abs(A-D))

Submission status: Wrong Answer

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

a = abs(X//D)

A = abs(X)-abs(D*a)
k = K - a

if k%2 == 0:
  print(abs(A))
else:
  print(abs(A-D))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

cnt = x // d
if k < cnt:
    ans = abs(x) - d * k
else:
    k -= cnt
    if k % 2:
        ans = d * (cnt + 1) - abs(x)
    else:
        ans = abs(x) - d * cnt

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

X = abs(X)

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

Submission status: Wrong Answer

Solution code:
from math import floor, ceil

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

if abs(X) >= K*D:  
    print(abs(X) - K*D)
else:
    lower_steps_needed = floor(abs(X)/D)
    upper_steps_needed = ceil(abs(X)/D)
    lower_min_dist = abs(abs(X) - lower_steps_needed * D)
    upper_min_dist = abs(abs(X) - upper_steps_needed * D)

    if (upper_min_dist < lower_min_dist):
        steps_needed = upper_steps_needed
        alt_steps_needed = lower_steps_needed
    elif (lower_min_dist < upper_min_dist):
        steps_needed = lower_steps_needed
        alt_steps_needed = upper_steps_needed
    else:
        steps_needed = lower_steps_needed
        alt_steps_needed = lower_steps_needed
    
    if (K - steps_needed)%2 == 0:
        print(abs(abs(X) - steps_needed * D))
    else:
        print(abs(abs(X) - (steps_needed+1)*D))

Submission status: Wrong Answer

Solution code:
from math import floor, ceil

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

if abs(X) >= K*D:  
    print(abs(X) - K*D)
else:
    lower_steps_needed = floor(X/D)
    upper_steps_needed = ceil(X/D)
    lower_min_dist = abs(abs(X) - lower_steps_needed * D)
    upper_min_dist = abs(abs(X) - upper_steps_needed * D)

    if (upper_min_dist < lower_min_dist):
        steps_needed = upper_steps_needed
        alt_steps_needed = lower_steps_needed
    elif (lower_min_dist < upper_min_dist):
        steps_needed = lower_steps_needed
        alt_steps_needed = upper_steps_needed
    else:
        steps_needed = lower_steps_needed
        alt_steps_needed = lower_steps_needed
    
    if (K - steps_needed)%2 == 0:
        print(abs(abs(X) - steps_needed * D))
    else:
        print(abs(abs(X) - (steps_needed+1)*D))

Submission status: Wrong Answer

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

X = abs(X)

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

Submission status: Wrong Answer

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 abs(x) % d == 0:
        if(k - (abs(x) // d)) % 2 == 0:
            print(0)
            return
        print(d)
        return
    b = abs(x) - (abs(x) // d) * d
    print(min(b, abs(d - b)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 abs(x) % d == 0:
        if(k - (abs(x) // d)) % 2 == 0:
            print(0)
            return
        print(d)
        return
    print(min(abs(x) % d, abs(d - abs(x) % d)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 abs(x) % d == 0:
        if(k - (abs(x) // d)) % 2 == 0:
            print(0)
            return
        print(d)
        return
    print(min(abs(x) % d, d - abs(x) % d))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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))
    else:
        print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 (k*(abs(x)//d))%2==1:
        if k%2==0:
            print(abs(abs(x)%d-d))

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

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

Submission status: Wrong Answer

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

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

elif x/d==1:
    print(x)

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

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

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

Submission status: Wrong Answer

Solution code:

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

    #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()
    if x > 0:
        x_r = x - d * r
        x_l = x_r - d

    else:
        x_l = x + d * r
        x_r = x + 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: Wrong Answer

Solution code:

def main():
    x, k, d = map(int, input().split())
    # x >= 0
    r = (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 abs(k_rest) % 2 == 0:
        print(abs(x_r))
    else:
        print(abs(x_l))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Solution code:
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

x,k,d = rm()
if k==1:
    print(min(abs(x-d),abs(x+d)))
else:
    pos=(True if x>0 else False)
    x=abs(x)
    movesToSmall=x//d
    if movesToSmall==0:
        movesToSmall+=1
    if pos:
        small=x-d*movesToSmall
        if small==0:
            if k>=movesToSmall:
                if k%2==movesToSmall%2:
                    print(abs(small))
                else:
                    print(d)
            else:
                print(x-d*k)
        else:
            if small<0:
                anothersmall=small+d
            else:
                anothersmall=small-d
            if abs(small)<abs(anothersmall):
                if k>=movesToSmall:
                    if k%2==movesToSmall%2:
                        print(abs(small))
                    elif k>=movesToSmall+1:
                        print(abs(anothersmall))
                    else:
                        print(abs(x-d*(movesToSmall-1)))
                else:
                    print(x-d*k)
            else:
                if k>=movesToSmall+1 and k%2==movesToSmall%2:
                    print(abs(anothersmall))
                else:
                    print(abs(small))
    else:
        small=-x+d*movesToSmall
        if small==0:
            if k>=movesToSmall:
                if k%2==movesToSmall%2:
                    print(abs(small))
                else:
                    print(d)
            else:
                print(-x+d*k)
        else:
            if small<0:
                anothersmall=small+d
            else:
                anothersmall=small-d
            if abs(small)<abs(anothersmall):
                if k>=movesToSmall:
                    if k%2==movesToSmall%2:
                        print(abs(small))
                    elif k>=movesToSmall+1:
                        print(abs(anothersmall))
                    else:
                        print(-x+d*(movesToSmall-1))
                else:
                    print(-x+d*k)
            else:
                if k>=movesToSmall+1 and k%2==movesToSmall%2:
                    print(abs(anothersmall))
                else:
                    print(abs(small))

Submission status: Wrong Answer

Solution code:
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

x,k,d = rm()
if k==1:
    print(min(abs(x-d),abs(x+d)))
else:
    pos=(True if x>0 else False)
    x=abs(x)
    movesToSmall=x//d
    if movesToSmall==0:
        movesToSmall+=1
    if pos:
        small=x-d*movesToSmall
        if small==0:
            if k>=movesToSmall:
                if k%2==movesToSmall%2:
                    print(abs(small))
                else:
                    print(d)
            else:
                print(x-d*k)
        else:
            if small<0:
                anothersmall=small+d
            else:
                anothersmall=small-d
            if abs(small)<abs(anothersmall):
                if k>=movesToSmall:
                    if k%2==movesToSmall%2:
                        print(abs(small))
                    elif k>=movesToSmall+1:
                        print(abs(anothersmall))
                    else:
                        print(x-d*k)
                else:
                    print(x-d*k)
            else:
                if k>=movesToSmall+1 and k%2==movesToSmall%2:
                    print(abs(anothersmall))
                else:
                    print(abs(small))
    else:
        small=-x+d*movesToSmall
        if small==0:
            if k>=movesToSmall:
                if k%2==movesToSmall%2:
                    print(abs(small))
                else:
                    print(d)
            else:
                print(-x+d*k)
        else:
            if small<0:
                anothersmall=small+d
            else:
                anothersmall=small-d
            if abs(small)<abs(anothersmall):
                if k>=movesToSmall:
                    if k%2==movesToSmall%2:
                        print(abs(small))
                    elif k>=movesToSmall+1:
                        print(abs(anothersmall))
                    else:
                        print(-x+d*k)
                else:
                    print(-x+d*k)
            else:
                if k>=movesToSmall+1 and k%2==movesToSmall%2:
                    print(abs(anothersmall))
                else:
                    print(abs(small))

Submission status: Wrong Answer

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

else:
    ans = x - k*d

print(abs(ans))

Submission status: Wrong Answer

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

else:
    ans = x + di*k*d

print(abs(ans))

Submission status: Wrong Answer

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

else:
    ans = x + di*k*d

print(abs(ans))

Submission status: Wrong Answer

Solution code:
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

x,k,d = rm()
if k==1:
    print(min(abs(x-d),abs(x+d)))
else:
    pos=(True if x>0 else False)
    x=abs(x)
    movesToSmall=x//d
    if pos:
        small=x-d*movesToSmall
        if small==0:
            if k>=movesToSmall:
                if k%2==movesToSmall%2:
                    print(small)
                else:
                    print(d)
            else:
                print(x-d*k)
        else:
            anothersmall=small-d
            if abs(small)<abs(anothersmall):
                if k>=movesToSmall and k%2==movesToSmall%2:
                    print(abs(small))
                else:
                    print(x-d*k)
            else:
                if k>=movesToSmall+1 and k%2==movesToSmall%2:
                    print(abs(anothersmall))
                else:
                    print(small)
    else:
        small=-x+d*movesToSmall
        if small==0:
            if k>=movesToSmall:
                if k%2==movesToSmall%2:
                    print(small)
                else:
                    print(d)
            else:
                print(-x+d*k)
        else:
            anothersmall=small+d
            if abs(small)<abs(anothersmall):
                if k>=movesToSmall and k%2==movesToSmall%2:
                    print(abs(small))
                else:
                    print(-x+d*k)
            else:
                if k>=movesToSmall+1 and k%2==movesToSmall%2:
                    print(abs(anothersmall))
                else:
                    print(small)

Submission status: Wrong Answer

Solution code:
# # Make IO faster
# import sys
# input = sys.stdin.readline

# # get single (or) multiple str
# X = input()

# # get single int
# N = int(input())
# # get multiple int (e.g., 2)
# X, Y = map(int, input().split())
# # get multiple int (e.g., 2) for N lines
# XY = [list(map(int, input().split())) for _ in range(N)]

# from IPython import embed; embed(); exit();

# 全部入り
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
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()
N = X // D

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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
    else:
        l = n

print(min_distance)

Submission status: Wrong Answer

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