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 io
# import sys

# _INPUT = """\
# -100 12 9
# """
# sys.stdin = io.StringIO(_INPUT)
####################################################################
####################################################################
import sys
def _p(*_a):
  #return
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
####################################################################
def _pa(_aa):
  #return
  for _a in _aa: _p(_a)
####################################################################
X,K,D = map(int, input().split())

X = abs(X)

# X=0の場合
if X==0:
  if K%2==0:
    ans = 0
  else:
    ans = X-D
  print(ans)
  exit()


# K回移動しても、0に届かない場合
if K * D < abs(X):
  ans = abs(X) - (K*D)
  if X < 0: ans *= -1
  print(ans)
  exit()


i = abs(X) % D

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(x)<d :
    if k%2==0 :
        ans=x
    else :
        x1,x2=x-d,x+d
        ans=x2
        if abs(x1)<abs(x2) :
            ans=x1

else :
    if x>0 :
        X=x%d
        K=k-(x//d)
        if K%2==0 and K>0 :
            ans=X
        elif K%2==1 and K>0 :
            x3,x4=X-d,X+d
            ans=x4
            if abs(x3)<x4 :
                ans=x3
        else :
            ans=x-k*d

    else :
        Y=abs(x)%d
        K=k-(abs(x)//d)
        if K%2==0 and K>0 :
            ans=-Y
        elif K%2==1 and K>0 :
            y1,y2=-Y+d,-Y-d
            ans=y1
            if abs(y2)<y1 :
                ans=y2
        else :
            ans=x+k*d

print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
#0またがない
if X > K*D:
    ans = X - K*D
#0またぐ
else:
    while X > 0:
        X = X - D
    ans = min(abs(X),X+D)

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x_=x
if x>=0:
  za=[0]
  while x>=0:
    za.append(x)
    x=x-d
else:
  x<0
  za=[0]
  while x<=0:
    za.append(x)
    x=x+d
if abs(x_)//d>=k:
  if x_>=0:
    print(x_-k*d)
    
  else:
    print(-x_+k*d)
else:
  if len(za)==2:
    if k%2==0:
      print(abs(x_))
    else:
      print(abs(d-x_))
  else:
    
    if (k-len(za)+2)%2==0:
      print(abs(x_)%d)
    else:
      print(d-abs(x_)%d)

Submission status: Wrong Answer

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

res = abs(X)%D
r = abs(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:
X, K, D = map(int, input().split())
X_list = []

for num in range(K):
    plus = num * D
    minus = -(K - num) * D

    x = X + plus + minus
    X_list.append(x)

X_list.sort()
if X_list[0] >= 0:
    print(X_list[0])
else:
    print(-X_list[0])

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
 
A = abs(X)
while A > 10000000*D:
    A -= 10000000*D
    K -= 10000000
    if K == 0:
        break
 
while A > D:
    A -= D
    K -= 1
    if K == 0:
        break

if K > 0:
    if K%2 == 1:
        A = A - D
 
print(abs(A))

Submission status: Wrong Answer

Solution code:
import sys
import math
X,K,D = map(int, input().split()) 
X1=abs(X)
if X<D:
    print(abs(X-D))
    sys.exit()
syou=math.floor(X/D)
amari=math.floor(K/syou)
ans=0

if amari==0:
    ans=X-D*K
else:
    ya=math.floor(K/amari)
    ans=X-ya*D
    if ya%2==0:
        print(ans)
        sys.exit()
    if abs(ans+D)>abs(ans-D):
        ans=ans-D
    else:
        ans=ans+D
print(abs(ans))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = 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)
    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())

X = abs(X)

n = X // D
m = K - n

A1 = abs(X - n*D)
A2 = abs(X - (n+1)*D)

Ans = A2

if m > 1:
    if A1 <= A2:
        if m % 2 == 0:
            Ans = A1
    else:
        if m-1 % 2 != 0:
            Ans = A1
else:
    Ans = abs(X - K*D)

print(Ans)

Submission status: Wrong Answer

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

a1 = X // D
a2 = (X // D)+1
if min((abs(X)-(a1*D)),(abs(X)-(a2*D))) == (abs(X)-(a1*D)):
    a = a1
else:
    a = a2

if a > K :
    print(abs(abs(X) - abs(K*D)))
elif (K - a) % 2 == 0 :
    print(min((abs(abs(X) - abs(a*D))),(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:
    a = -(-x // d)
    if a >= k:
        print(abs(x - k*d))
    else:
        b = x - a*d
        c = b + d
        if abs(d) > abs(c):
            if (k - d) % 2 == 0:
                print(abs(d))
            else:
                print(abs(c))
        else:
            if (k - d) % 2 == 0:
                print(abs(c))
            else:
                print(abs(d))

if x < 0:
    a = x // d
    if -a >= k:
        print(abs(x + k*d))
    else:
        b = x + a*d
        c = b - d
        if abs(d) > abs(c):
            if (k - d) % 2 == 0:
                print(abs(d))
            else:
                print(abs(c))
        else:
            if (k - d) % 2 == 0:
                print(abs(c))
            else:
                print(abs(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 Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
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())
if x>=0:
    dummy=x
    for i in range(int(k/2)):
        if dummy>=0 and dummy-2*d<0:
            print(min(dummy,2*d-dummy))
            break
        dummy=dummy-2*d
    else:
        print(dummy-k*d)
else:
    dummy=x
    for i in range(int(k/2)):
        if dummy<=0 and dummy+2*d>0:
            print(min(-dummy,dummy+2*d))
            break
        dummy=dummy+2*d
    else:
        print(dummy+k*d)

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 == 0 and K % 2 == 0:
    if A + 2 <= K:
  	  print(min(abs(X - D * A), abs(X - D * (A + 2))))
    else:
      print(abs(X - D * A))
  if A % 2 == 1 and K % 2 == 0:
    if A + 1 <= K:
      print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 0 and K % 2 == 1:
    if A + 1 <= K:
      print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 1 and K % 2 == 1:
    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())

X = abs(X)

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

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
ans = X
minc_floor = math.floor(abs(X)/D)
minc_ceil = math.ceil(abs(X)/D)
min_kaisuu = K
# print(minc_ceil, minc_floor)
if minc_ceil <= K:
    for k in range(minc_floor, minc_ceil+1):
        for flug in [-1, 1]:
            if abs(X + flug*k*D) < abs(ans):
                ans = abs(X + flug*k*D)

                # print("ans" + str(ans))
                min_kaisuu = k
                # print(min_kaisuu)
    if (K-min_kaisuu) % 2 != 0:
        ans = min(abs(ans+D), abs(ans-D))
else:
    # ans = min(ans+K*D), abs(ans-K*D))
    if abs(ans+K*D) > abs(ans-K*D):
        ans = ans-K*D
    else:
        ans = ans+K*D
# print(min_kaisuu)
print(ans)

Submission status: Wrong Answer

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

for i in range(hikukazu):
    X = X - D

    # print(X)

print(abs(X))

Submission status: Wrong Answer

Solution code:
print(-1 // 2)

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

ans = 0
count = X // D



if X >= 0:
    if K <= count:
        ans = X - D * K
    else:
        ans = X - D * count
        if ((K - count) % 2 == 1):
            ans = ans - D
else:
    if K <= (-1) * count:
        ans = X + D * K
    elif X % D == 0:
        ans = X - D * count 
        if ((K + count) % 2 == 1):
            ans = ans + D
    else:
        ans = X - D * (count + 1) 
        if ((K + count + 1) % 2 == 1):
            ans = ans + D

if ans < 0:
    ans = ans * (-1)

print(ans)

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):
  if (X -K*D > 0):
    print(abs(X - K*D))
  while ((X > 0) and (K != 0)):
         X = X - D
         K -= 1
  if (K==0):
    print(abs(X))
    exit()
  print(abs(abs(X) - (K % 2) *D))
elif (X < 0):
  if (X +K*D < 0):
    print(abs(X + K*D))
  while ((X < 0) and (K != 0)):
         X = X + D
         K -= 1
  if (K==0):
    print(abs(X))
    exit()
  print(abs(abs(X) - (K % 2) *D))

Submission status: Wrong Answer

Solution code:
a = input().split()
X = int(a[0])
K = int(a[1])
D = int(a[2])

i = 0

while X - D * i > 0 :
  i = i + 1
#  print('i', i)

if i-1 < K :
  X = X - D * (i-1)
  #print('X', X)
  K = K - (i-1)
  #print('K', K)

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

else :
  print(X-K*D)

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 9999999999999999999
tmp = 0
if X == 0:
  if K % 2 == 0:
    print(0)
  else:
    print(D)
  sys.exit()
if abs(X) >= K*D:
  if X >= 0:
    print(X - K*D)  
  else:
    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(min(abs(a),abs(b)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

n = X // D

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if k%2==0:
    ans=min(abs(x),abs(x+2*d),abs(x-2*d),abs(x+4*d),abs(x-4*d),abs(x+k*d),abs(x-k*d),abs(x+(k-2)*d),abs(x-(k-2)*d),abs(x+(k-4)*d),abs(x-(k-4)*d))
else:
    if k==1:
        ans=min(abs(x+d),abs(x-d))
    else:
        ans=min(abs(x+d),abs(x-d),abs(x+3*d),abs(x-3*d),abs(x+k*d),abs(x-k*d),abs(x+(k-2)*d),abs(x-(k-2)*d),abs(x+(k-4)*d),abs(x-(k-4)*d))
if k>=abs(x)//d:
    if (k-(abs(x)//d))%2==0:
        ans=min(ans,abs(x+(abs(x)//d)*d),abs(x-(abs(x)//d)*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:
        i = (a - 1)*d
        j = (a + 1)*d
    else:
        i = a * d
        j = (a+2) * d
else:
    if a % 2 != 0:
        i = a * d
        j = (a + 2) * d
    else:
        i = (a - 1)*d
        j = (a + 1)*d
# a *= d
# print(a)
if x < 0:
    print(min(abs(x+i), abs(x+j)))
else:
    print(min(abs(x-i), abs(x-j)))

Submission status: Wrong Answer

Solution code:
x, k , d = map(int, input().split())
y = abs(x)
count = x // d
e = x % d
f = abs(e-d)
if e > f:
    min1 = f
    min2 = e
    count += 1
else:
    min1 = e
    min2 = f
if count > k:
    print(y - d * k)
else:
    if (k - count)%2 == 0:
        print(min1)
    else:
        print(min2)

Submission status: Wrong Answer

Solution code:
[_x, _k, _d] = list(map(int, input().split(" ")))
_x = abs(_x)

if _x >= _d:
  _y = _x//_d
  _x = abs(_x - _d * _y)
  _k -= _y
else:
  if _k % 2 == 1:
    _x = abs(_x - _d)

print(_x)

Submission status: Wrong Answer

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

    else:
        print(x)
        
else:
    for i in range(k):
        if i == 0 and n!=0:
            x += n*d
            k -= n
        if abs(x+d) < min:
            min = abs(x+d)
            x+=d
            cnt += 1
        else:
            if abs(x-d)>abs(x+d):
                x = abs(x+d)
            else:
                x = abs(x-d)
            break
    if cnt==0:
        cnt += 1
    k -= cnt
    if k%2 == 0:
        print(min)


    else:
        print(x)

Submission status: Wrong Answer

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

    if K > 0:
        if K%2 == 1:
            A = A - D

print(abs(A))

Submission status: Wrong Answer

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

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

q, mod = divmod(abs(X), abs(D))

ans = 0
if q < K:
    amari = K - q
    l = amari % 2
    if l == 0:
        ans = mod
    else:
        ans = D + mod
else:
    div = q - K
    ans = div * D + mod

print(ans)

Submission status: Wrong Answer

Solution code:
def isEven(num):
  if num%2 == 0:
  	return True
  return False

def isOdd(num):
  if num%2 == 1:
    return True
  return False

if __name__ == "__main__":
  x,k,d = map(int, input().split())
  div = x//d
  cmp = x
  if isOdd(k):
    if x > d:
      div = min(div+1,k)
      for i in range(div):
      	for j in [d,-d]:
          cmp = min(cmp,abs(cmp+(2*i+1)*j))
      print(cmp)
    else:
      for i in [d,-d]:
        cmp = min(cmp,abs(cmp+d))
      print(cmp)
  if isEven(k):
    if x > d:
      div = min(div+1,k)
      for i in range(1,div):
      	for j in [d,-d]:
          cmp = min(cmp,abs(cmp+(2*i)*j))
      print(cmp)
    else:
      print(cmp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n = X // D
m = K - n

A1 = abs(X - n*D)
A2 = abs(X - (n+1)*D)

Ans = A2

if m > 1:
    if A1 <= A2:
        if m % 2 == 0:
            Ans = A1
    else:
        if m-1 % 2 != 0:
            Ans = A2
else:
    Ans = X - K*D

print(Ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# case0: 絶対値を超える範囲まで届かない場合
if x > k*d:
    print (x - k*d)
# case1: 絶対値を超える範囲に届く場合: 0の両隣のどちらかを選択
elif x == k*d:
    print (0)
else:
    #wall = (x // d) + 1 # 0を超えるのに必要な移動回数
    #print("wall=",wall)
    #if wall % 2 == 0: # Xが偶数時: どちらも選択可能
    #    print (abs(x - wall*d))
    #else: # Xが奇数時: どちらかのみとなるので注意
    #    print (abs(x - (wall-1)*d))

    div = x//d

    a1 = abs(x - div*d)
    a2 = abs(x - (div+1)*d)
    if div % 2 == 1:
        print (a2)
    else:
        print (a1)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
s = X//D
a = X%D
z = K-s
if z % 2 == 0:
    print(a)
else:
    if D<=a:
        print(a-D)
    elif a<=D:
        print(K-D)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x = list(map(int, input().split()))
c = 0
x1 = 0
while(x[0] >= 1):
    if (x[0] > x[2] and c != x[1]):
        x1 = x[0] - x[2]
        x[0] = x1
        c += 1
    else:
        break
print(x1)

Submission status: Wrong Answer

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

tmp = int(X / D)
if K > tmp:
    X = X - D*tmp
    K = K - tmp

pre1 = 0
pre2 = 0
now = X

for k in range(K):
    pre2 = pre1
    pre1 = now
    sei = now + D
    hu = now - D
    if(abs(sei) < abs(hu)):
        now = sei
    elif(abs(sei) > abs(hu)):
        now = hu
    else:
        print(abs(sei))
        exit(0)
    if(pre2 == now):
        if((K+1-k) % 2 == 0):
            print(abs(now))
            exit(0)
        else:
            print(abs(pre1))
            exit(0)
print(abs(now))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if x>=0:
    dummy=x
    for i in range(int(k/2)):
        print(i,dummy)
        if dummy>=0 and dummy-2*d<0:
            print(min(dummy,2*d-dummy))
            break
        dummy=dummy-2*d
    else:
        print(dummy-k*d)
else:
    dummy=x
    for i in range(int(k/2)):
        if dummy<=0 and dummy+2*d>0:
            br=True
            print(min(-dummy,dummy+2*d))
            break
        dummy=dummy+2*d
    else:
        print(dummy+k*d)

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)
elif x > 0:
    if (k - math.floor(x/d))%2 == 0:
        print(abs(x%d))
    else:
        print(abs(x%d-d))
elif x <= 0:
    if (k - abs(math.ceil(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())

X = abs(X)

if X == D :
  if K == 0:
    print (X)
  elif K % 2 == 0:
    print(0)
  else:
    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:
X, K, D = map(int, input().split())
X = abs(X) % D
K -= 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())
current_position = X

times = abs(X) // D

if times < K:
    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
else:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
print(current_position)

Submission status: Wrong Answer

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

q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    if (K-q)%2 == 1:
        ans = D - mod
    else:
        ans = mod



print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
Y,Z = X+1,X+2

for i in range(K):
    Z = Y
    Y = X
    if X >= 0:
        X = X - D
    else:
        X = X + D
    if X == Z: #roop
        if (K-i)%2 == 0: #even
            ans = Y
        else: #odd
            ans = X
        break
    if i == K-1:#last
        ans = X
print(abs(ans))

Submission status: Wrong Answer

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

ii=int(X/D)
if K<=ii and X>0:
    X-=K*D
elif K<=ii and X<0:
    X+=K*D
elif X>0:
    X-=ii*D
elif X<0:
    X+=ii*D
if (K-ii)%2==1 and X>0:
    X-=D
elif (K-ii)%2==1 and X<0:
    X+=D

print(abs(X))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
 
A = abs(X)
while A > 10000*D:
    A -= 10000*D
    K -= 10000
    if K == 0:
        break
 
while A > D:
    A -= D
    K -= 1
    if K == 0:
        break

if K > 0:
    if K%2 == 1:
        A = A - D
 
print(abs(A))

Submission status: Wrong Answer

Solution code:
import math

kouho = []
dt = input()
l = dt.split()
dt1 = [0]*3
for i in range(3):
    dt1[i] = int(l[i])

    
for i in range(dt1[1]):
    m1 = dt1[0]+2*(i+1)*dt1[2]
    m2 = dt1[0]-2*(i+1)*dt1[2]
    kouho.append(m1*m1)
    kouho.append(m2*m2)

print(round(math.sqrt(min(kouho))))

Submission status: Wrong Answer

Solution code:
p = input().split()
count = 0
sp = 0
sp1 = 0
sp2 = 0

x = int(p[0])
num = int(p[1])
D = int(p[2])

if(x != D):
  for i in range(num):
    if(x > 0):
      x = x -D
    elif(x < 0):
      x = x + D
    elif(x==0):
      x = x + D  
    sp = x
    sp1 = sp
    sp2 = sp1
    
    if(abs(sp)==abs(sp1)):
      break
    elif(sp == sp2):
      break
      
  print(abs(x))
  
elif(x == D and num%2 == 0):
  print(x)
elif(x == D and num%2 == 1):
  print(0)

Submission status: Wrong Answer

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

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

    else:
        if K- X//D >= 2:
            print(Y)
        else:

            a = abs(D)+Y
            b = abs(Y-D)
            print(min(a,b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if temp > K:
    temp = K

temp = D * temp

print(abs(X - temp))

Submission status: 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)
    else:
        x = abs(x) - abs(a * d)
        x = abs(x) - abs(d)

print(abs(x))

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
        x = x + a * d
        
        if abs(x-d) < abs(x+d):
            a -= 1
        else:
            a += 1
        x = x + a * d

        if a == 0:
            break

        a = 0
        
    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:
import sys
x, k, d = map(int, input().split())

#偶数、奇数
isEven = (k%2 == 0)

#一番いい回数
div = x//d

ans = 0
if k < div:
    if x > 0:
        print(abs(x-d*k))
    else:
        print(abs(x+d*k))
else:
    if x > 0:
        if (k-div) % 2 == 0:
            print(abs(x-d*div))
        else:
            hoge = x-d*div
            if hoge+d < abs(hoge-d):
                print(abs(hoge+d))
            else:
                print(abs(hoge-d))
    else:
        if (k-div) % 2 == 0:
            print(abs(x+d*div))
        else:
            hoge = x+d*div
            if hoge+d < abs(hoge-d):
                print(abs(hoge+d))
            else:
                print(abs(hoge-d))

Submission status: Wrong Answer

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

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

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