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())
a_list = []
if K <= 1000000:
  for i in range(K+1):
    if X >= 0:
      a_list.append(abs(X - i*D))
    else:
      a_list.append(abs(X + i*D))
  if K % 2 == 0:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
else:
  q = X // K
  for j in range(-100000+q, 100000+q):
    if q >= 0:
      a_list.append(abs(X - j*D))
    else:
      a_list.append(abs(X + j*D))
  if K % 2:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
print(ans)

Submission status: Wrong Answer

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


if x > k * d:
    print(x - k * d)
else:
    q = x // d
    mod = x % d
    x2 = x - q * d
    k2 = k - q
    r = abs(mod - d)
    if k2 % 2 == 0:
        print(mod)
    else:
        print(r)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

#if abs(diff-d) == abs(diff-2*d):

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x0 = x
if k%2 == 0 and abs(x)==abs(d):
  print(abs(x))
  exit()

for i in range(k):
  """if abs(x - d) <= abs(x + d):
      x = x - d
  else:
      x = x + d"""
  if abs(x) > abs(x - (d*i) + d*(k-i)):
    x = x - (d*i) + d*(k-i)
    #print(x)
if abs(x0) > abs(x - d*k):
    x = x - (d*k)
print(abs(x))

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import sys

x,k,d=map(int, input().split())
ans = x
i = 0
  
for i in range(k):
  if ans>0:
    ans = ans-d
  else:
    ans = ans+d

  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools

if __name__ == '__main__':
    # 初期位置、移動回数、一回の移動量
    X, K, D = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split()))
    origX = X
    # oldX = X
    # lasti = 0
    # 7-3=4
    # 4-3=1
    # 1-3=-2

    # for i in range(K):
    #     X -= D
    #     if abs(X) > abs(oldX):
    #         X = oldX
    #         lasti = i
    #         break
    #     oldX = X
    # else:
    #     print(abs(X))
    #     exit()

    # 6 3 2
    # 9 3 4
    lasti = X // D
    X = X - D*lasti

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
x, k, d =map(int, input().split())
ans=[]
if x > 0:
    for i in range(k):
        if x > 0:
            ans.append(x - d)
        elif x < 0:
            if (i)%2 == 0:
                print(ans[i-1])
            else:
                ans.append(x + d)
                print(ans[i])
        elif x == 0 and (i+1)%2 == 0:
            print(0)
            sys.exit()
elif x < 0:
    for i in range(k):
        if x < 0:
            ans.append(x + d)
        elif x > 0:
            if (i)%2 == 0:
                print(ans[i-1])
            else:
                ans.append(x - d)
                print(ans[i])
        elif x == 0 and (i+1)%2 == 0:
            print(0)
            sys.exit()
else:
    if k%2==0:
        print(0)
        sys.exit()
    else:
        print(x-d)
        sys.exit()
print(ans[-1])

Submission status: Wrong Answer

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

if K%2 == 0:
    ans = abs(X-(D*2))
    print(ans)
    exit()
else:
    ans = abs(X-D)
    for i in range(2, K+1):
        num = abs(X-(D*i))
        if ans >= num:
            ans = num
        else:
            break
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
x_min = abs(x)
if x < 0:
    for j in range(16):
        if x + d * int(k/(10**j)) < 0:
            x += d * int(k/(10**j))
            x_min = abs(x)
            break
else:
    for j in range(16):
        if x - d * int(k/(10**j)) >= 0:
            x -= d * int(k/(10**j))
            x_min = abs(x)
            break
for i in range(k - int(k/(10**j))):
    if x < 0:
        x += d
        if abs(x) < x_min:
            x_min = abs(x)
        else:#xが0以上
            if (k - i - 1) % 2 == 1:
                break
            else:
                x_min = d - x_min
                break
    else:
        x -= d
        if abs(x) < x_min:
            x_min = abs(x)
        else:#xが負
            if (k - i - 1) % 2 == 1:
                break
            else:
                x_min = d - x_min
                break
print(x_min)

Submission status: Wrong Answer

Solution code:
# Walking Takahashi

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

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

    if k < q:
        return x - k * d
    kr = k - q
    xr = x - q * d
    if kr % 2 == 0:
        return -h
    else:
        if 0 < h:
            return h - d
        else:
            return h + d


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

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
ans = X
res = []
for _ in range(K):
    X = ans
    ans = abs(X - D)
    res.append(ans)
print(min(res))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
l = 0
if x > 0:
    x -= k*d
    if x < 0:
        while True:
            x += d
            l += 1
            if x >= 0 or l == k:
                print(min((abs(x), abs(x -d))))
                exit()
    else:
        print(abs(x))
else:
    x += k*d
    if x > 0:
        while True:
            x -= d
            l += 1
            if x <= 0 and l == k:
                print(min(abs(x), abs(x +d)))
                exit()
    else:
        print(abs(x))

Submission status: Wrong Answer

Solution code:
def abso(n):
    if n < 0:
        n *= -1
    return n


def main():
    X, K, D = map(int, input().split())
    ans = X
    X= abso(X)

    if X//D>K:
        ans = X-K*D
    else:
        ans = X- X//D*K
        if (K - X//D)%2==1:
            if abso(X- X//D*D+D) > abso(X- X//D*D-D):
                ans = X- X//D*D-D
            else:
                ans = X- X//D*D+D


    return abso(ans)

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# atcoder 
# Türkler var mı?
# Herkese memnün oldum
import sys

readline = sys.stdin.buffer.readline
iimr = lambda: map(int, sys.stdin.buffer.readline().rstrip().split())
#sys.setrecursionlimit(10 ** 8)

def debug(*x):
    print(*x, file=sys.stderr)


class atcoder():
    def __init__(self):
        pass

    def çözmek(self):
        
        N, K, D = map(int, input().split())
        res = []
        res.append(self.関数(N, K, D))
        print(*res, "\n")	
    
    def 関数(self, n, k, d):
       
        if n == 0:
            return n
        if (n % d == 0) and (n // d == k):
            return 0
        else:
            karpayi = n // d
            if karpayi < k:
                ost1  = n % d
                if ((karpayi - k) % 2) == 0:
                    return ost1 
                else:
                    return(abs(ost1 - d))

            else:
                return int(n - (k * d))
            
        return -1

if __name__ == "__main__":
    ima = atcoder()
    ima.çözmek()

Submission status: Wrong Answer

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

if x == 0:
  if k%2 ==0:
    print(0)
  else:
    print(d)
elif x > 0:
  if k*d <= x:
    print(x - k*d)
  else:
    a = x // d
    x -= a*d
    if x == 0:
      if (k-a)%2 ==0:
        print(0)
      else:
        print(d)
    else:
      x1 = x
      x2 = x - d
      if (k-a-1)%2 ==0:
        print(x2)
      else:
        print(x1)
elif x < 0:
  if -k*d <= x:
    print(x + k*d)
  else:
    a = x // -d
    x += a*d
    if x == 0:
      if (k-a)%2 ==0:
        print(0)
      else:
        print(d)
    else:
      x1 = x
      x2 = x + d
      if (k-a-1)%2 ==0:
        print(x2)
      else:
        print(x1)

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split())) 
X =l[0]
K =l[1]
D =l[2]
abs_X=abs(X)
tmp_int = int(abs_X/D)
# if abs_X%D!=0:
#     increase_tmp_int=tmp_int+1
nearly = abs_X-tmp_int*D

flag =0
if nearly >= abs(abs_X-(tmp_int+1)*D):
    nearly = abs_X-(tmp_int+1)*D
    flag=1
if flag==0 and tmp_int%2==1:
    nearly=0+D
if tmp_int > K:
    nearly = abs_X-(K)*D

print(str(abs(nearly)))

Submission status: Wrong Answer

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

if x > k*d:
    # 届かない場合は極力近づく
    print(x-(k*d))
elif x < k*d and x > (k-2)*d:
    # ぎりぎり０をまたげるパターン
    x1 = x - k*d
    x2 = x - (k-2)*d
    # またぐか、2つ前か、の2択
    if abs(x1) > abs(x2):
        print(abs(x2))
    else:
        print(abs(x1))
else:
    # それ以降
    # 目的地を求める
    step1 = int(x/d) # 目的地までのステップ数
    a1 = x - step1*d # 目的地
#    print("step1:{} a1:{}".format(step1,a1))
    # 0を突き抜けたほうが目的地のケース
    if abs(a1) > abs(a1-d):
        a1 = a1-d
        step1 +=1
#        print("step1:{} a1:{}".format(step1,a1))
    if (k-step1)%2 == 0:
        # 残ステップが偶数ならぴったり
        a2 = a1
    else:
        # 残ステップが奇数なら1個余分に移動
        if a1 > 0:
            a2 = a1-d
        else:
            a2 = a1+d
    print(abs(a2))

Submission status: Wrong Answer

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

tmp1 = x - d * (x // d)
tmp2 = tmp1 + d

if x >= k * d:
    print(x - k * d)
elif x == d and k % 2 == 0:
    print(x)
elif x == d and k % 2 == 1:
    print(0)
elif k % 2 == 0:
    print(min(abs(tmp1), abs(tmp2)))
else:
    print(max(abs(tmp1), abs(tmp2)))

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
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
else:
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
print(abs(current_position))

Submission status: Wrong Answer

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

N = X // D

res = X - min(K, N) * D
if N < K:
    res = min(abs(res - D), res + D)

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

init = 10**16
count =0
while True:
    if count == k:
        break
    count += 1
    next1 = abs(x)+d
    next2 = abs(x)-d
    next = min(abs(next1),abs(next2))
    if abs(x) <= abs(next):
        break
    else:
        x=next

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

Submission status: Wrong Answer

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

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

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

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

    print(ans)

    return


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools

if __name__ == '__main__':
    # 初期位置、移動回数、一回の移動量
    X, K, D = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split()))
    origX = X
    # oldX = X
    # lasti = 0
    # 7-3=4
    # 4-3=1
    # 1-3=-2

    # for i in range(K):
    #     X -= D
    #     if abs(X) > abs(oldX):
    #         X = oldX
    #         lasti = i
    #         break
    #     oldX = X
    # else:
    #     print(abs(X))
    #     exit()

    # 6 3 2
    # 9 3 4
    lasti = X // D
    X = X - D*lasti

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if k%2 == 0 and abs(x)==abs(d):
  print(abs(x))
  exit()

for i in range(k):
  """if abs(x - d) <= abs(x + d):
      x = x - d
  else:
      x = x + d"""
  if abs(x) > abs(x - (d*i) + d*(k-i)):
    x = x - (d*i) + d*(k-i)
    #print(x)
if abs(x) > abs(x - d*k):
    x = x - (d*k)
print(abs(x))

Submission status: Wrong Answer

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

    if(D > abs(X)):
        ans = abs(X)
        print(ans)
        sys.exit()
    elif(abs(X) - D*K >= 0):
        ans = abs(X) - D*K 
        print(ans)
    elif(abs(X) - D*K < 0):
        if((K - (abs(X) // D)) % 2 == 0):
            ans = abs(X) - ((abs(X) // D) * D)
            print(ans)
        else:
            tmp = abs(X) - ((abs(X) // D) * D)
            ans = min(abs(tmp+D), abs(tmp-D))
            print(ans)



if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
x, y, z = list(map(int, input().split()))
count=0
if(z<0):
    z=-z
for i in range(0,y):
    if(x>0):
        x=x-z
    count+=1
    if(x<0):
        x=-x
    if(x<=z/2):
        if((y-count)%2==1):
            x=z-x
        break
print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cent = x % d
num = x // d

if d / 2 > cent and cent != 0:
    cent = d - cent
    num += 1

if num >= k:
    print(x - k * d)
    exit(0)

rem = k - num

if rem % 2 == 0:
    print(cent)
    exit(0)
else:
    print(d - cent)

Submission status: Wrong Answer

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

if K > 10000000 and K % 2 == 0:
    K = 10000
if K > 10000000 and K % 2 == 1:
    K = 10001

for i in range(K):
    if place == 0 and (K-i)%2==0:
        break

    if place >= 0 and (place - D) >= 0:
        place -= D

    elif place >= 0 and (place - D) <= 0:
        place -= D
  
    else: 
        place += D 
      
if place < 0:
    place = abs(place)
print(place)

Submission status: Wrong Answer

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

ii=abs(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())

c = X

if c > 0:
  if c//D > K:
    c = c - D * K
    print(c)
    exit()
  else:
    c = c - D * (c//D)
    if (K - c//D) % 2 == 0:
      print(c)
      exit()
    else:
      print(max(c,abs(c-D)))

Submission status: Wrong Answer

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

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

    # print(X)

print(abs(X))

Submission status: Wrong Answer

Solution code:

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

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

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

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

k_count = k

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

    if abs(x) < d and k_count % 2 == 0:
        print(abs(x))
        exit()
    if abs(x) < d and k_count % 2 == 1:
        if abs(x) - d < abs(x) + d:
            print(abs(x) - d)
            exit()
        else:
            print(abs(x) + d)
            exit()

print(abs(x))

Submission status: Wrong Answer

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

if abs(x) > abs(k*d):
    print(abs(x) - abs(k*d))
elif abs(x) == abs(k*d):
    print("0")
else:
    a = abs(x) // d
    z1 = abs(x) - a*d
    z2 = abs(x) - (a+1)*d
    if z1 == 0 and (k-a) % 2 == 0:
        print("0")
    elif z1 == 0 and (k-a) % 2 == 1:
        print(abs(z2))
    else:
        b = min(abs(z1), abs(z2))
        print(abs(b))

Submission status: Wrong Answer

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

if (x == d):
    if ((k % 2) == 0):
        print(d)
    else:
        print(0)
    sys.exit()

s = x // d
a = x % d
ans = 0

if (s <= k):
    if (x < 0):
        ans = x + (d * s)
    else:
        ans = x - (d * s)
    '''
    if ((s < k) and ((k % 2) == 0)):
        ans *= -1
    '''
else:
    if (x < 0):
        ans = x + (d * k)
    else:
        ans = x - (d * k)

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int,input().split(' '))
flg = True
hituyou_kaisuu = x//d
saisyou = x%d


if saisyou > d // 2:
    saisyou -= d
    hituyou_kaisuu += 1
    flg = False

amari = k - hituyou_kaisuu

if amari >= 0:
    if amari % 2 == 0:
        print(abs(saisyou))
    else:
        if flg:
            print(abs(saisyou - d))
        else:
            print(abs(saisyou + d))
else:
    print(-1 * amari * d)

Submission status: Wrong Answer

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

#十分に原点から離れている場合
if abs(X) >= K*D:
    ans = abs(X) - K*D
else:
    t = (abs(X) - D*(K//2)) // (2*D)
    t = 2*t + K//2
    if X > 0:
        t = -t
    A = [t-2,t,t+2]
    ans = 3*D
    for a in A:
        if abs(X+D*a)<ans:
            ans = abs(X+D*a)


print(ans)

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)


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


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:
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 shou > k:
            shou = k

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

    else:
        if shou > k:
            shou = k
        
        if shou + 1 > k:
            print(abs(x-shou*d))
        else:
            print(abs(x-(shou+1)*d))
            

    exit()

    if k % 2 == 0:
        if amari == 0:
            if shou % 2 == 0:
                if shou <= k:
                    print(0)
                    exit()
                else:
                    print(abs(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(abs(x-k*d))
                    exit()

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

    if shou <= k:
            print(abs(amari))
            # print('a')
    else:
        print(abs(x-k*d))  
        # print('b')



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split(' ')))
for i in range(K):
    if (abs(X-D)==abs(X+D)):
        newX = X-D
    else:
        newX = min(abs(X-D), abs(X+D))
    newX = abs(newX)
    
    #print('--')
    #print(newX)
    #print(X)
    #print('--')
    if (newX>=X) and ((K-i-1)%2==0):
        if ((K-i)%2==0):
            break
        else:
            X = newX
            break
    X = newX
print(X)

Submission status: Wrong Answer

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

ans=X

a=X//D

"""
for i in range(a):
    ans=min(abs(ans-D),abs(ans+D))
    
"""

if (K>a>0 or -K<a<0) and (K-abs(a))%2==0:
    ans=min(abs(X-(a)*D),abs(X+(a)*D))
    
elif (K>a>0 or -K<a<0) and (K-abs(a))%2==1:
    ans=min(abs(X-(a+1)*D),abs(X+(a+1)*D))
elif a>=K:
    ans=abs(X-K*D)
else:
    ans=abs(X+K*D)
    


print(ans)

Submission status: Wrong Answer

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

if k*d<=x:
    print(x-k*d)
elif -k*d>=x:
    print(x+k*d)
else:
    if x>=0:
        rem = x%(d)
        div = x//d
        if (k-div)%2==0:
            print(rem)
        else:
            print(d-rem)
    else:
        rem = (-x)%(d)
        div = (-x)//d
        if (k-div)%2==0:
            print(rem)
        else:
            print(d-rem)

Submission status: Wrong Answer

Solution code:
x, k, d=map(int, input().split(' '))
if x - k*d >= 0:
  print(x - k*d)
else:
  for i in range(k):
    if i*d > x:
      l=i
      break
  if (k-i) % 2 == 0:
    x = -x + i*d
  else:
    x = -(i+1)*d + x
  if x < 0:
    x = -x
  print(x)

Submission status: Wrong Answer

Solution code:
x, k, d = list(map(int, input().split()))
ans = x
res = []
for _ in range(k):
    x = ans
    ans = abs(x - d)
    res.append(ans)
print(min(res))

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
temp=a//c
if(temp>b):
    temp=temp-b
    if(a%c==0):
        if(temp>b):
            print(a-(b*c))
        else:
            print(temp*c)
    else:
        print(a-(b*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))

Submission status: Wrong Answer

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

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

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:
def main():
    x,k,d = map(int,input().split())

    y = x

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

        if abs(x-d) < abs(x+d):
            x = x - d
        else:
            x = x + d
        if y == x:
            break

        y = x

        if x - 4 * d > 0:
            x = x - 4 * d
            i -= 4

    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:
def C(X, K, D):
    current = X
    mark = {abs(current)}

    for i in range(1, K + 1):
        if current >= 0:
            current -= D
        else:
            current += D

        if current in mark:
            if (K - i) % 2 == 0:
                return min(mark)
            else:
                return sorted(mark)[1]

        mark.add(abs(current))

    return abs(current)

  
IN = [int(n) for n in input().split(' ')]
print(C(*IN))

Submission status: Wrong Answer

Solution code:
a = [int(i) for i in input().split()]
b = abs(a[0] // a[2])
c = a[1] - b
l = []
if c < 0:
  if a[0] >= 0:
    print(a[0] - a[1] * a[2])
  else:
    print(a[0] + a[1] * a[2])
else:
  if c % 2 == 0:
    if a[0] >= 0:
      print(a[0] - b * a[2])
    else:
      print(a[0] + b * a[2])
  else:
    if a[0] >= 0:
      if abs(a[0] - (b + 1) * a[2]) < abs(a[0] - (b - 1) * a[2]):
        print(abs(a[0] - (b + 1) * a[2]))
      else:
        print(abs(a[0] - (b - 1) * a[2]))
    else:
      if abs(a[0] + (b + 1) * a[2]) < abs(a[0] + (b - 1) * a[2]):
        print(abs(a[0] + (b + 1) * a[2]))
      else:
        print(abs(a[0] + (b - 1) * a[2]))

Submission status: Wrong Answer

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

res = 0
if x <= (k*d):
    m1 = x % k
    c1 = x // k
    c2 = abs(k - c1)
    if c2%2 == 0 or c2 < 0:
        res = m1
    else:
        res = abs(m1-k)
else:
    res = abs(x - (k*d))
print(res)