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:
X,K,D = map(int,input().split())
tmp = X -K*D
if tmp >= 0:
    ans = tmp
else:
    if X < 0:
        X *= (-1)
    dif  = X//D
    buf = X - D*dif
    if (K-dif) % 2  == 0:
        ans = abs(buf)
    else:
        ans = abs(buf - D)
print(ans)

Submission status: Wrong Answer

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

if a >= k:
    for i in range(k):
        if x > 0:
            x = x - d;
        elif x <= 0:
            x = x + d;

elif a < k:    
    if x > 0:
        for i in range(a):
            x = x - d;
        if (k-a) % 2 != 0:
            x = x - d;
    elif x <= 0:    
        for i in range(a):
            x = x + d;
        if (k-a) % 2 != 0:
            x = x + d;

# xが負の数の場合正の数に編集する
if x < 0:
    x = x * (-1);

print(x);

Submission status: Wrong Answer

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

k_min=abs(x//d)
if k_min>=k:
  k_min=k
if x>0:
  d_min=x-((k_min)*d)
else:
  d_min=x+((k_min)*d)

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

Submission status: Wrong Answer

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

k1 = X // D
if k1 >= K:
    print(X - D * K)
    exit()
k = K - k1
x = X % D

while k > 0:
    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()
    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

Submission status: Wrong Answer

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

d = int(abs(X)/D)
if X > 0:
    r = X - d*D
    if abs(r-D) < abs(r):
        r = r-D
    min_count = int((X-r)/D)
else:
    r = X + d*D
    if abs(r+D) < abs(r):
        r = r+D
    min_count = int(abs(X-r)/D)

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

Submission status: Wrong Answer

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

for p in range(k+1):
  ima = abs(x+(d*p-d*(k-p)))
  if p == 0:
    mae = ima
    continue
  if mae < ima:
    print(mae)
    exit()
  mae = ima

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = int(x)
k = int(k)
d = int(d)
req = x//d

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
while k!=0:
    if x-d==0:
        if k%2 ==0:
            break
        else:
            x=x-d
            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:
if __name__ == '__main__':
    x, k, d = list(map(int, input().split()))

    if k > 3000:
        if k * d > x:
            rest = (k - int(x / d)) % 2
            print(x % d if rest == 0 else abs(x % d - d))
        else:
            print(x - k * d)
    else:
        x_dict = {0: [x]}

        for i in range(1, k + 1):
            x_dict[i] = []
            for previous_x in x_dict[i - 1]:
                x_dict[i].append(previous_x - d)
                x_dict[i].append(previous_x + d)
            x_dict[i] = set(x_dict[i])
        print(min(map(abs, x_dict[k])))

Submission status: Wrong Answer

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

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 abs(x-k*d)<min:
    print(x-k*d)
    exit(0)

if x-k*d<0:
    k-=(x/(d*2))*2


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:

x,k,d = list(map(int,input().split()))
#偶数回前後に移動したら0
#偶数回一方向に移動したら2D

if x >= 0:
  capa_iter = x // d
  if capa_iter < k:
    if (k - capa_iter) % 2 == 0:
      print(x - capa_iter*d)
    else:
      print(min([abs(x - capa_iter*d-d),x - capa_iter*d]))
    exit()

  ans =x-d*k
  print(ans)

if x < 0:
  capa_iter = abs(-x // d)
  if capa_iter < k:
    if (k - capa_iter) % 2 == 0:
      print(x + capa_iter*d)
    else:
      print(min([abs(x + capa_iter*d+d),abs(x + capa_iter*d)]))
    exit()

  ans =abs(x+d*k)
  print(ans)

Submission status: Wrong Answer

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

if X > 0:
    tmp = abs(X) % D
    if tmp  <= K:
        ans = X - D*K
    elif tmp > K:
        nK = K - tmp
        X1 = X- D*tmp
        X2 = X1 - D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
elif X < 0:
    tmp = abs(X) % D
    if tmp  <= K:
        ans = X + D*K
    elif tmp > K:
        nK = K - tmp
        X1 = X + D*tmp
        X2 = X1 + D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
else:
    if K %2 == 0:
        ans = 0
    else :
        ans = D
print(abs(ans))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if abs(x)/d>=k:
  print(abs(x-k*d))
else:
  x,k=x-d*(abs(x)//d),k-abs(x)//d
  m=0
  p=0
  eo=k
  ans=x
  for i in range(1,k+1):
    m,p=abs(ans-d),abs(ans+d)
    ans=min(m,p)
    if ans<=d:
      eo=i
      break
  if (k-eo)%2==0 and eo<k:
    print(ans)
  elif (k-eo)%2==1 and eo<k:
    print(abs(ans-d))
  else:
    print(ans)

Submission status: Wrong Answer

Solution code:
a, b, c = map(int, input().split())
result = a
for i in range(b):
    if abs(a + c) < abs(a - c):
        a += c
        if abs(result) < abs(a):
            break
        result = a
    else:
        a -= c
        if abs(result) < abs(a):
            break
        result = a
print(abs(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    x, k, d = map(int, input().split())
    abs_x = abs(x)
    if abs_x == d:
        if k % 2 == 0:
            print(abs_x)
            return 0
        else:
            print(0)    
            return 0

    if d > abs_x:
        if k % 2 == 0:
            print(abs_x)
            return 0
        else:
            print(d-abs_x)    
            return 0
    
    s = abs_x // d

    if s <= k:
        if abs(abs_x - s*d) <= abs(abs_x - (s+1)*d):
            if (k-s) % 2 == 0:
                print(abs(abs_x - s*d))
            else:
                print(min(abs(abs_x - (s+1)*d + d), abs(abs_x - (s+1)*d - d)))

        elif abs(abs_x - s*d) > abs(abs_x - (s+1)*d):
            if (k-(s+1)) % 2 == 0:
                print(abs(abs_x - (s+1)*d))
            else:
                print(min(abs(abs_x - (s+1)*d + d), abs(abs_x - (s+1)*d - d)))

    else:
        print(min(abs(abs_x + k*d), abs(abs_x - k*d)))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
c,K,M=list(map(int,input().split()))
d=10000000000000000000
for i in range(K):
  c=abs(c-M)
  if c<d:
    d=c
    if d<M and (K+1-i)%2==0:
      print(d)
      exit()
    elif d<M and (K+1-i)%2==1:
      print(min(abs(d+M),abs(d-M)))
      exit()
print(d)

Submission status: Wrong Answer

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

# print(x,k,d)

x = abs(x)
c = min(x//d, k)
x = abs(x-d*c)
k = k-c
# print(k,c,x)
if x == 0:
    if k%2==0: # even
        print(0)
    else: # odd
        print(d)

else: # +
    print(x)

Submission status: Wrong Answer

Solution code:
import math

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

a = X - K * D
if a > 0:
    print(a)
if a + 2 * K * D < 0:
    print(abs(a + 2 * K * D))
else:
    b = a / (-2 * D)
    p = math.floor(b)
    q = math.ceil(b)
    r = -(a + 2 * D * p)
    s = a + 2 * D * q
    print(min(r,s))

Submission status: Wrong Answer

Solution code:
import math;

# input 現在座標ｘ, 移動回数k, 移動距離d
x, k, d = map(int, input().split())


a = math.floor(x / d);

if a >= k:
    for i in range(k):
        if x > 0:
            x = x - d;
        elif x <= 0:
            x = x + d;

elif a < k:
    
    if x > 0:
        for i in range(a):
            x = x - d;

        if (k-a) % 2 != 0:
            x = x - d;

    elif x <= 0:    
        for i in range(a):
            x = x + d;

        if (k-a) % 2 != 0:
            x = x + d;

            

# xが負の数の場合正の数に編集する
if x < 0:
    x = x * (-1);

print(x);

Submission status: Wrong Answer

Solution code:
def Qc():
    x, k, d = map(int, input().split())
    x = abs(x)
    for v in range(k):
        x -= d
        ans = x
        if x <= 0:
            ans = min(abs(x-d), abs(x+d))
            break
    print(ans)


if __name__ == '__main__':
    Qc()

Submission status: Wrong Answer

Solution code:
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()
if abs(x)/d >= k:
    print(abs(x-k*d))
else:
    if (k - math.floor(abs(x)/d))%2 == 0:
        print(abs(x%d))
    else:
        print(abs(x%d-d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x < 0 and x + (k * d) < 0:
    print(x + (k * d))
    exit()
elif x >= 0 and x - (k * d) >= 0:
    print(x - (k * d))
    exit()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import math
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    tmp = 2
    # res = min(res, x-i*d)でk-iが偶数となるようなiを二分探索(0 < i < k)

    if k%2==1:
        tmp-=1
    dlis = [x]
    i = 0
    while tmp<=k:
        dlis.append(abs(abs(dlis[i])-tmp*d))
        i+=1
        tmp += tmp
    print(min(dlis))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def function(X, K, D):
    kai = int(abs(X)/D)
    if (abs(X) % D == 0):
        if abs(X) > D:
            ans = D
        else:
            ans = X
    elif kai > K:
        if X > 0:
            ans = X-K*D
        else:
            ans = X+K*D
    else:
        K = K-kai
        if X > 0:
            X = X-kai*D
        else:
            X = X+kai*D
        if K % 2 == 0:
            ans = X
        else:
            if X > 0:
                if abs(X-D) < X:
                    ans = X-D
                else:
                    ans = X
            else:
                if abs(X+D) < abs(X):
                    ans = X+D
                else:
                    ans = X
    return ans


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

Submission status: Wrong Answer

Solution code:
import sys

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

p = X
_p = 0
k = 0
if p < 0:
    while 1:
        _p = p
        p += D
        k += 1
        if p >= 0:
            break
        if k == K:
            print(abs(p))
            sys.exit()
    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:
            print(abs(p))
            sys.exit()
    if K-k % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Wrong Answer

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

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

s = X//D

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

k = K - s
x = X - D*s
if k%2 == 0:
  print(x)
else:
  print(D-x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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)
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)
            else:
                print(c)
        else:
            bb=b%2
            if(bb==0):
                print(c)
            else:
                print(0)
    else:
        if(aa<b*c<aa+c):
            muri1=b*c-aa
            print(muri1)
        else:
            if(amari>amarioruta):
                print(amarioruta)
            else:
                print(amari)

Submission status: Wrong Answer

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

for i in range(k):
    if abs((x + d)) > abs((x - d)):
        x = abs(x - d)
    elif abs((x - d)) > abs((x + d)):
        x = abs(x + d)
    if mina > x:
        mina = x
        cnt = 0
    else:
        cnt += 1
    if cnt >= 5:
        break

print(x)

Submission status: Wrong Answer

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

d = X // D

if X < 0:
    X *= -1

if d <= K+1:
    s.add(X - D*d)
    s.add(D*(d+1) - X)
elif d == K:
    s.add(X - D*d)
else:
    s.add(X - D*K)

print(min(s))

Submission status: Wrong Answer

Solution code:
def zet(num):
    if num<0:
        return -num
    else:
        return num

x,k,d = map(int, input().split())
x = zet(x)
amari = x%d
syou = x//d
if syou > k:
    syou = k

sa = k-syou
genzai = zet(x) - syou * d
 
if sa % 2 == 0:
    if amari*2 < d:
        print(genzai)
    elif sa == 0:
        print(genzai)
    else:
        print(zet(genzai - d))
else:
    if sa == 0:
        print(zet(genzai))
    else:
        print(zet(genzai - d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

k = int(X / D)
k = min(k, K)
if X > 0:
  x = X - k * D
else:
  x = X + k * D

diff = K - k
if diff % 2 == 0:
	print(x)
else:
  x_plus = x + D
  x_minus = x - D
  print(min(abs(x_plus), abs(x_minus)))

Submission status: Wrong Answer

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

x = abs(x)

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

else:
    if x > k*d:
        print(x - k*d)
    else:
        print(min(x%d,abs(x%d-d)))

Submission status: Wrong Answer

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

if x < 0:
    a = abs(x)//d
    if k < a:
        x += k*d
        k = 0
    else:
        x += a*d
        k -= a
else:
    a = x//d
    if k < a:
        x -= k*d
        k = 0
    else:
        x -= a*d
        k -= a

b = abs(x-d)
c = abs(x+d)

if 0 < k and k % 2 == 1:
    x = min(b,c,abs(x))

print(x)

Submission status: Wrong Answer

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

k1 = X // D
if k1 >= K:
    print(X - D * K)
    exit()
k = K - k1
x = X % D

while True:
    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()

Submission status: Wrong Answer

Solution code:
import math
a=input().split(" ")
c=int(a[0])
if (int(a[0])==int(a[2])):
    print(int(a[0]))
else:
    for i in range(int(a[1])):
        if(c>=0):
            c=c-int(a[2])
        if(c<0):
            c=c+int(a[2])
    print(c)

Submission status: Wrong Answer

Solution code:
X,D,C = map(int,input().split())
count=abs(X)
a = X//C
b = X%C
if a>D:
  print(count-D*C)
elif a==D:
  print(b)
elif a<D:
  D = D-a
  if D%2==0:
    print(b)
  else:
    print(min(abs(b-C),abs(b+C)))

Submission status: Wrong Answer

Solution code:

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

x = li[0]
k = li[1]
d = li[2]

for _ in range(k):
    sum_result = x + d
    minus_result = x - d
    if x == d:
        break
    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:
def main():
    x,k,d = list(map(int,input().split()))
    ln = abs(x)//d
    if abs(x)%d > d//2:
        ln += 1

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

    print(ans)

main()

Submission status: Wrong Answer

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

c = X

for i in range(K):
  if c >= 0:
    if abs(c-D) <= abs(c):
      c = c - D
    else:
      c = c + D
  if c < 0:
    if abs(c+D) <= abs(c):
      c = c + D
    else:
      c = c - D
  i += 1
  if K > 10**6:
    print(X)
    exit()

print(abs(c))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

x,k,d=MI()
y=abs(x)//d
ans=abs(x)
for i in range(-2,3):
    z=y+i
    if z>k or z<0:
        continue
    if z%2!=k%2:
        continue
    ans=min(ans,abs(x-z*d))
    ans=min(ans,abs(x+z*d))
ans=min(ans,abs(x-k*d))
ans=min(ans,abs(x+k*d))
print(ans)

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 >= K*D:
  if K % 2 == 0:
    print(X - K*D)
  else:
    print(X - K*D + D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  #print(tmp)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  #print(tmp)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

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 = (D*K+X)//(2*D)
  ans1 = abs(-2*D*x+(D*K+X))
  ans2 = abs(-2*D*(x+1)+(D*K+X))
  print(min(ans1, ans2))

Submission status: Wrong Answer

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

d = int(abs(X)/D)
if X > 0:
    r = X - d*D
    if abs(r-D) < abs(r):
        r = r-D
    min_count = int((X-r)/D)
else:
    r = X + d*D
    if abs(r+D) < abs(r):
        r = r+D
    min_count = int(abs(X-r)/D)

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
# x > 0のとき
left = abs(x - d * (x // d + 1))
right = abs(x - d * (x // d))

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

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

Submission status: Wrong Answer

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
for i in range(K):
    if X<=D:
        break
    X-=D

_K = K-i
if X<=D and  _K>=2 and _K%2==0:
    print(X)
elif X<=D and _K>=3 and _K%2!=0:
    print(X)
elif X<=D and _K ==1:
    print(abs(X-D))
else:
    print(X)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
Y = 0
p = 0
for i in range(K):
    if X > 0:
        Y = X - D
        if Y < 0:
            print((min(X,-Y)))
            p = 1
            break
        X = Y
    elif X < 0:
        X = X + D
        if Y > 0:
            print((min(-X,Y)))
            p = 1
            break
        X = Y
    else:
        print(X)
        p = 1
        break
if p == 0:
    print(X)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
key=abs(X)
while key>=D and K>0:
  key-=D
  K-=1 

if K==0:
  print(key)
  
else:
  Key=key-D
  if K%2==0:
    ans=min(abs(key),abs(Key))
  else:
    ans=max(abs(key),abs(Key))
  print(ans)

Submission status: Wrong Answer

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

    '''
    for i in range(K):
        plusX = X + D
        minusX = X - D

        if abs(plusX) > abs(minusX):
            X = minusX
        else:
            X = plusX

    print(abs(X))
    '''
    if X == 0:
        minCount = 0
    else:
        minCount = abs(X) // D
    
    if minCount >= K:
        print(X - D * K)
        return

    X = X - D * minCount
    K = K - minCount

    plusX = X + D
    minusX = X - D

    if min(X, abs(plusX), abs(minusX)) == X:
        if K % 2 == 0:
            print(X)
        else:
            print(min(abs(plusX), abs(minusX)))

    else:
        if (K - 1) % 2 == 0:
            print(min(abs(plusX), abs(minusX)))
        else:
            print(X)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    x, k, d = i_map()

    amari = x % d
    shou = x // d

    if k % 2 == 0:
        if amari == 0:
            if shou % 2 == 0:
                if shou <= k:
                    print(0)
                    exit()
                else:
                    print(x-k*d)
                    exit()

        if shou % 2 == 1:
            if shou+1 <= k:
                print(abs(x-(shou+1)*d))
                exit()
    else:
        if amari == 0:
            if shou % 2 == 1:
                if shou <= k:
                    print(0)
                    exit()
                else:
                    print(x-k*d)
                    exit()

        if shou % 2 == 0:
            if shou+1 <= k:
                print(abs(x-(shou+1)*d))
                exit()

    if shou <= k:
            print(amari)
    else:
        print(x-k*d)  


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

sho, amari = abs(x)//d, abs(x)%d
if k <= sho:
  print(abs(x-(k*d)))
  sys.exit()
else:
  x = amari
  k = k-sho
  
if x == abs(x-d):
  print(x)
elif x < abs(x-d):
  if k%2==0:
    print(x)
  else:
    print(abs(x-d))
else:
  if k%2==1:
    print(x)
  else:
    print(abs(x-d))

Submission status: Wrong Answer

Solution code:
xx, kk, dd= list(map(int,input().split()))

xx = abs(xx)

if dd * kk <= xx:
    ans = xx - dd*kk
else:
    aa_int = int(xx/dd)
    remaining_move = kk - aa_int
    if (aa_int % 2) == 0:
        ans = xx - dd*aa_int
    else:
        ans = xx - dd*(aa_int+1)

print(abs(ans))

Submission status: Wrong Answer

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

if(jud == 0):
  if((k)%2 == 1):
    print(abs(abs(x)-d))
  else:
    print(abs(x))
    
else:
  jud += 1
  if(k<=jud):
    print(abs(abs(x)-d*k))
  else:
    if((k-jud)%2 == 1):
      print(abs(abs(x)-(jud-1)*d))
    else:
      print(abs(abs(x)-(jud)*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(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:
#!/usr/bin/env python3
import sys


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

    mostl = X - K * D
    mostr = X + K * D
    if mostl*mostr > 0:
        print(min(mostl, mostr))
        exit()

    ans=X
    if K%2 == 1:
        X -= D
        K -= 1
        ans = min(ans, X)

    N = X // (D*2)
    ans = min(ans, abs(X - D * 2 * (N - 1)))
    ans = min(ans, abs(X - D * 2 * N))
    if N < K//2:
        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 = map(int, input().split())  
if X > 0 :
    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))
else:
    if -X // D >= K:
        print(X + K*D)
    else:
        K -= -X // D
        X = X % D - D
        if K % 2 == 0:
            print(-X)
        else:
            print(abs(X+D))

Submission status: Wrong Answer

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

if d * k >= abs(x) and x != 0:
  # 0超える1つ手前は？
  near_0 = x // d
  if x < 0:
    near_0 += 1
  if (k - abs(near_0)) %2 == 0:
    print(abs(x - near_0 * d))
  else:
    hoge = x - near_0 * d + d
    huga = x - near_0 * d - d
    if abs(hoge) < abs(huga):
      print(abs(hoge))
    else:
      print(abs(huga))
else:
  if x == 0:
    if k%2 == 0:
      print(d)
    else:
      print(0)
  elif x > 0:
    print(x-(d*k))
  else:
    print(x+(d*k))

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())

if abs(a)>=abs(b*c):
    print(abs(a-b*c))
    exit()

n=abs(a)
m=n//c
if abs(a-c*m)>abs(a-c*(m+1)):
    m=m+1
if (b-m)%2==0:
    print(abs(a)-abs(c*m))
else:
    print(min(abs(abs(a)-abs(c*(m+1))),abs(abs(a)-abs(c*(m-1)))))