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 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(-3,4):
    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:
x,k,d = map(int,input().split())
flag = False
x -= k*d
# print(x)
if x>=0:
    print(x)
else:
    while x<0:
        if len(str(x))>6:
            x = x+(k*d)
        else:
            x+=d
        #print(x)
    # if x == 1000000000000000:
    #     print('ok')
    print(x)

Submission status: Wrong Answer

Solution code:
x, k ,d = map(int,input().split())
q = x // d
mod = x % d
time = k - q
if time >= 2:
  if time % 2 == 0:
    ans = abs(mod)
  else:
    ans = abs(mod) - d
else:
  for i in range(k):
    if x >= 0:
      x = x -d
    else:
      x = x + d
  ans = x
print(abs(ans))

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:
import sys
input=lambda: sys.stdin.readline().rstrip()
x,k,d=map(int,input().split())
if k%2==1:
  if x>0:
    x-=d
  else:
    x+=d
  k-=1

a=x%(2*d)
b=a-2*d
ans=abs(x)
if abs(x-a)//d<=k:
  ans=min(ans,abs(a))
if abs(x-b)//d<=d:
  ans=min(ans,abs(b))
print(ans)

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())
m=x//d
r=x%d
if x<0:
    if m>k:
        ans=x+k*d
    elif (k-m)%2==1:
        ans=d-r
    else:
        ans=r
else:
    if m>=k:
        ans=x-k*d
    elif (k-m)%2==1:
        ans=d-r
    else:
        ans=r
print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
mx = x + d*k
mn = x - d*k

if (x < 0 and mx > 0) or (x > 0 and mn < 0):
  if k%2 == 0:
    if x < 0:
      b = min(abs(abs(x)%(2*d)),abs(x%(2*d)-2*d))
    else:
      b = min(abs(x%(2*d)),abs(x%(2*d)-2*d))
    b = min(abs(b),abs(mx),abs(mn))
  else:
    if x > 0:
      b = min(abs(abs(x-d)%(2*d)),abs(abs(x-d)%(2*d)-2*d))
    else:
      b = min(abs(abs(x+d)%(2*d)),abs(abs(x+d)%(2*d)-2*d))
  ans = min(abs(b),abs(mx),abs(mn))
else:
  if x == 0 and k%2 == 0:
    ans = 0
  else:
    ans = min(abs(mx),abs(mn))
print (ans)

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)
else:
    if sa == 0:
        print(zet(genzai))
    else:
        print(zet(genzai - d))

Submission status: Wrong Answer

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

k = abs(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())
t = x // d
a = []
a.append(t - 2)
a.append(t - 1)
a.append(t)
a.append(t + 1)
a.append(t + 2)
a.append(k)
a.append(k - 1)
a.append(-k)
a.append(-k + 1)

res = abs(x)
for i in a:
    if i % 2 == k % 2 and abs(i) <= k:
        res = min(res, abs(x - i * d))
print(res)

Submission status: Wrong Answer

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

X = abs(X)

q, mod = divmod(X, D)

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

else:
  cnt = K-q
  if cnt%2 == 0:
    print(mod)
  if cnt%2 == 1:
    print(D-mod)

Submission status: Wrong Answer

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

first=X//D
if D//2<first:
    first=first+1
else:
    pass
if K>first:
    if X>0:
        X=X-first*D
        if (K-first)%2==0:
            pass
        else:
            if X>0:
                X=X-D
            else:
                X=X+D
    else:
        X=X+first*D
else:
    if X>0:
        X=X-K*D
    else:
        X=X+K*D

print(abs(X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import math
N=input().split()
X=int(math.fabs(int(N[0])))
K=int(N[1])
D=int(N[2])
if X-K*D>=0:
    print(X-K*D)
else:
    x=X%D
    y=X-X%D
    ans_pre=min(x,y)
    i=int((X-ans_pre)/D)
    if i%2==K%2:
        print(ans_pre)
    else:
        print(max(x,y))

Submission status: Wrong Answer

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

g = abs(X) % D

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

Submission status: Wrong Answer

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

all_dis = K * D
ans = 0
if X >= all_dis:
    ans = X - all_dis
else:
    amari_X = X % D # 最初０に動きたい時に最終的に到着した位置
    sho_X = X // D #　何回Kを使ったか

    all_dis -= sho_X * D # 到着した位置までの変量を減らす
    amari_a = all_dis % D # 余る数 
    sho_a = all_dis // D # 残りの動ける変量から何回動けるか
    ans = amari_X + amari_a
    if sho_a % 2 != 0:
        ans += D
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if X < 0:
    X *= -1

d = X // D

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:
X, K, D = map(int,input().split())
absX = abs(X)
t = X // D
remain_K = K - t
l = X % D
if remain_K < 0:
    a = abs(X - (K * D))
    b = abs(X + (K * D))
else:
    if remain_K % 2 == 0:
        a = abs(l - (D * 2))
        b = abs(l + (D * 2))
        if l < a:
            a = l
        if l < b:
            b = l
    else:
        a = abs(l - D)
        b = abs(l + D)
if a < b:
    print(a)
else:
    print(b)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
ans=float('inf')
for _ in range(K):
  X-=D
  K-=1
  if abs(X)>ans and K%2!=0:
    break
  if abs(X)>ans and K%2==0:
    ans=abs(X)
    break
  ans=min(ans,abs(X))
print(ans)

Submission status: Wrong Answer

Solution code:
import math;

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


a = math.ceil(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())

low = 0
high = K

while low <= high:
    mid = (low + high) //2
    i = mid
    j = K-mid
    ans_pre = (X + D*i - D*j)
    if ans_pre > 0:
        high = mid-1
    else:
        low = mid+1
        

if high < 0:
    high = 0

#ans_h = min(abs((X + D*high - D*(K-high))), abs(X + D*(K-high)-D*high))
ans_l = min(abs((X + D*low - D*(K-low))), abs(X + D*(K-low)-D*low))
    
print(ans_l)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

if x < 0:
    x = -1 * x

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

    if neibor_1 > 0:
        neibor_2 = x - d * (p + 1)
    else:
        neibor_2 = neibor_1 + d

    if abs(neibor_2) > abs(neibor_1) and (k - p) % 2 == 0:
        print(abs(neibor_1))
    else:
        print(abs(neibor_2))

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+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 -  d)]))
    exit()

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

Submission status: Wrong Answer

Solution code:
[X,K,D]=list(map(int, input().split()))
abx=int(abs(X))
n=abx//D
if n>K:
    if X>0:
        ans=X-K*D
    else:
        ans=X+K*D
else:
    x2=abx-n*D
    x1=(n+1)*D-abx
    if x2>=x1:
        if (K-n-1)%2==0:
            ans=x1
        else:
            ans=x2
    else:
        if (K-n)%2==0:
            ans=x2
        else:
            ans=x1
print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import math
 
X,K,D = map(int, input().split())
tm = round(X/D)
if K <= tm:
  print(abs(X-D*K))
else:
  if (K-tm)%2==1:
    print(abs(X-D*(tm+1)) if abs(X-D*(tm+1)) < abs(X-D*(tm-1))else abs(X-D*(tm-1)))
  else:
    print(abs(X-D*tm))

Submission status: Wrong Answer

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

    if k >= abs(x) // d:
        a = abs(x) - abs(d) * (x // d)
        b = abs(a - d)
        left_number = k - (abs(x) // d)
        if left_number % 2 == 0:
            minimum = a
        else:
            minimum = b

    else:
        minimum = abs(x) - k * d

    print(minimum)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys


def get_ints():
    return map(int, sys.stdin.readline().strip().split())


def main():
    x, k, d = get_ints()
    ax = abs(x)
    tf = k % 2
    if ax < abs(ax-d) and tf == 0:
        print(x)
    elif ax < abs(ax-d):
        print(abs(x-d))
    else:
        tmp_k, mod_d = divmod(ax, d)
        if tmp_k < k:
            ax -= d * tmp_k
            k -= tmp_k
            tf = k % 2
            if tf == 1:
                ax = abs(ax - d)
            print(ax)
        else:
            ax -= d * k
            k = tmp_k - k
            tf = k % 2
            if tf == 1:
                ax = abs(ax - d)
            print(ax)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

print(abs(ans))

Submission status: Wrong Answer

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

temp = x // d
if temp < k:
    if x == k:
        print(x)
    else:
        print(abs(x - max(temp, 1) * d))
else:
    print(x - k * d)

Submission status: Wrong Answer

Solution code:
import itertools

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

if K % 2 == 0:
  q, mod = divmod(abs(X), D*2)
  if mod <= abs(mod - D*2):
    if q*2 <= K:
      print(mod)
    else:
      print(X-(K*D))
  else:
    if q*2 <= K:
      print(mod-D*2)
    else:
      print(X-(K*D))   
else:
  q, mod = divmod(abs(X), D)
  if mod <= abs(mod - D):
    if q <= K:
      print(mod)
    else:
      print(X-(K*D))
  else:
    if q <= K:
      print(mod-D)
    else:
      print(X-(K*D))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import io, sys


def main():
    X,K,D = list(map(int, sys.stdin.readline().split()))

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

playerinput = input()
lst = playerinput.split(" ")

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

lowest_value = X

lst_of_values = ["empty"]

if X == K == D:
    K = 0
    lowest_value = X
    
while K != 0:
    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1
        if abs(lowest_value) == lst_of_values[-1]:
            break
        else:
            lst_of_values.append(abs(lowest_value))
    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
        if abs(lowest_value) == lst_of_values[-1]:
            break
        else:
            lst_of_values.append(abs(lowest_value))
    else:
        break

Submission status: Wrong Answer

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

if k%2 == 0:
  start = 0
else:
  start = 1

for i in range(start,k+1,2):
  step = i*d
  ans = x-step
  #print(ans)
  if ans<0 and prev>0:
    ans = min(abs(ans),abs(prev))
    break
  prev = ans

ans = abs(ans)
print(ans)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
X=abs(X)
if X==0 and K%2==0:
    ans=0
elif X==0 and K%2==1:
    ans=D
elif X>0 and D>=X and K%2==0:
    ans=X
elif X>0 and D>=X and K%2==1:
    ans=D-X
elif X>0 and D<X :
    if K*D<X:
        ans=X-K*D
    else:
        ans=X
        for i in range(0,K):
            ans=ans-D
            if ans<0:
                if (K-i)%2==0:
                    ans=-ans
                    break
                else:
                    ans=ans+D
                    break
print(ans)

Submission status: Wrong Answer

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

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

n=abs(a)
m=n//c
if abs(a)-abs(c*m)>abs(a)-abs(c*(m+1)):
    m=m+1
print(m)
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)))))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x,k,d=map(int,input().split())
x+=d*k
m=abs(x)
for i in range(k+1):
  x-=d*2
  if i==k:
    print(m)
  else:
    if abs(x)<m:
      m=abs(x)
    else:
      print(m)

Submission status: Wrong Answer

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

if( x == 0 ):
    if( k % 2 == 0 ):
        printf("{}".format(0))
else:
    print("{}".format(abs(d)))

if( x > 0 ) :
    if( x - k * d >= 0 ) :
        ret = x - k * d
        print("{}".format(ret))
    rem = x % d
    cost = x // d
    rem2 = abs(rem - d)
    remc = k - cost
    if( remc % 2 == 0 ) : 
        ret = rem
        print("{}".format(ret))
    else:
        ret = rem2
        print("{}".format(ret))
elif( x < 0 ) :
    if( x + k * d <= 0 ) :
        ret = abs( x + k * d )
        print("{}".format(ret))
    rem = (-x) % d
    cost = (-x) / d
    rem *= -1
    rem2 = rem + d
    remc = k - cost
    if( remc % 2 == 0 ):
        ret = abs(rem)
        print("{}".format(ret))
    else :
        ret = rem2
        print("{}".format(ret))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    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(abs(X), abs(plusX), abs(minusX)) == abs(X):
        if K % 2 == 0:
            print(abs(X))
        else:
            print(min(abs(plusX), abs(minusX)))

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

x = abs(x)

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

if K >= 10**6:
    K = 10**5
ans = 10**15+1
for i in range(K):
    if X >= 0:
        X -= D
    else:
        X += D
    if X <= abs(ans):
        ans = X

print(abs(ans))

Submission status: Wrong Answer

Solution code:

def main():
    x, k, d = map(int, input().split())
    num = x
    ans_1 = 0
    ans_2 = 0
    if k == d:
        print(abs(num))
        return
    if k % 2 == 0:
        k = int(k / 2)
    for i in range(0, k):
        ans_1 = num - d
        ans_2 = num + d
        if abs(ans_2) >= abs(ans_1):
            num = ans_1
        else:
            num = ans_2
    print(abs(num))
    return

main()

Submission status: Wrong Answer

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

if X < 0:
    X *= -1

d = X // D

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:
X, K, D = [int(x) for x in input().strip().split(" ")]

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

Submission status: Wrong Answer

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

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+k*d),abs(x-k*d),abs(x+(k-2)*d),abs(x-(k-2)*d))
else:
    if k==1:
        ans=min(abs(x),abs(x+d),abs(x-d))
    else:
        ans=min(abs(x+d),abs(x-d),abs(x+k*d),abs(x-k*d),abs(x+(k-2)*d),abs(x-(k-2)*d))
print(ans)

Submission status: Wrong Answer

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

b1 = X
counter = 0

while (counter < K):
  b1 = X
  if (X > 0):
    X = X - D
  else:
    X = X + D
  if (X == 0 or abs(X) < D):
    break
  print(X)
  counter += 1

print(abs(X))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def function(X, K, D):
    kai = int(abs(X)/D)
    if (abs(X) % D == 0) and (abs(X)/D <= K):
        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:
X,K,D=map(int,input().split())
if K<int(X/D):
    print(abs(X-K*D))
else:
    if abs(X-(int(X/D)+1)*D)<abs(X-int(X/D)*D):
        X=X-(int(X/D)+1)*D
        if (K-(int(X/D)+1))%2==0:
            print(abs(X))
        else:
            if abs(X+D)<abs(X-D):
                print(abs(X+D))
            else:
                print(abs(X-D))
    else:
        X=X-(int(X/D))*D
        if (K-(int(X/D)))%2==0:
            print(abs(X))
        else:
            if abs(X+D)<abs(X-D):
                print(abs(X+D))
            else:
                print(abs(X-D))

Submission status: Wrong Answer

Solution code:
# 初期入力
import sys
input = sys.stdin.readline  #文字列では使わない
X,K,D = map(int, input().split())
q1,min1 =divmod(X,D)
q1 =abs(q1)
if min1 >=0:
    min2 =min1 -D
else:
    min2 =min1 +D

ans =0
if K <= q1:
    ans =abs(X) -K *D
elif K %2 ==0 and q1 %2 ==0:
    ans =abs(min1)
elif K %2 ==1 and q1 %2 ==1:
    ans =abs(min2)
elif K %2 ==0 and q1 %2 ==1:
    ans =abs(min2)
elif K %2 ==1 and q1 %2 ==0:
    ans =abs(min2)
print(ans)

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 x-k*d>0 and 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 = map(int,input().split())

n = x//d
#print(k-n,(abs(x-d*k)),(abs(x-(x//d)*d-d)),abs(x-(x//d)*d))
if n >= k:
    print(abs(x-d*k))
elif n < k:
    if (k-n)%2 == 1:
        print((abs(x-(x//d)*d-d)))#,abs(x-(x//d)*d)))
    else:
        print(abs(x-(x//d)*d))
        #print(min(abs(x%d),abs(x%d-d)))

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

Submission status: Wrong Answer

Solution code:
import math

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

div, mod = divmod(abs(x), d) 

if k < div:
    print(abs(x) - k*d)
else:
    if x == d and x % 2 == 0:
        print(mod+d)
    else:
        print(mod)

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(abs(x-d))
  else:
    print(x)

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
if k % 2 == 0:
  for i in range(0, k//2):
    if (x - 2 * (i-1) * d)**2 >= (x - 2 * (i) * d)**2:
      print(abs(x - 2 * (i + 1) * d))
      
else:
  for i in range(0, k//2):
    if (x - (2 * i -1)* d)**2 >= (x - (2 * i + 1) * d)**2:
      print(abs(x - (2 * i + 3) * d))

Submission status: Wrong Answer

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

direction = -1 if x > 0 else 1

move_counter = 1 if (k % 2) == 1 else 2

min_abs = abs(x)

while True:
    moved_x = x + (direction * d * move_counter)

    if abs(moved_x) < min_abs:
        min_abs = abs(moved_x)
    else:
        break

print(min_abs)

Submission status: Wrong Answer

Solution code:
import numpy as np


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

diff = abs(K * D - X)

ans = 0
if abs(K * D) < abs(X):
    ans = (abs(X) - abs(K * D)) * (X > 0)
else:
    num = X//D # 距離がD未満になるまでに必要な最小の移動回数
    ans = (abs(X) - abs(num * D)) * (1 if X > 0 else -1)
    # 残った回数が奇数の場合
    if (K - num) % 2 == 1:  
        ans = min([abs(ans - D), abs(ans + D)])
        
print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cnt = x//d
mod = x%d
min = 0

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

if mod > abs(mod-d):
    min = abs(mod-d)
    cnt += 1
else:
    min = mod

if k%2 == 1:
    if cnt%2 == 1:
        print(min)
    else:
        print(abs(min-d))
else:
    if cnt%2 == 0:
        print(min)
    else:
        print(abs(min-d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
 
X,K,D = map(int, input().split())
tm = round(X/D)
if K <= tm:
  print(abs(X-D*K))
else:
  if (K-tm)%2==1:
    if math.modf(X/D)[0]<0.5:
      print(abs(X-D*(tm+1)))
    else:
      print(abs(X-D*(tm-1)))
  else:
    print(abs(X-D*tm))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
num = X//D
X = abs(X)
if num >= K:
    print(X-K*D)
elif num < K:
    rest_X = X - num*D
    rest_K = K - num
    if rest_K%2 == 0:
        print(rest_X)
    else:
        print(abs(rest_X - D))