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:
def main():
    X, K, D = map(int, input().split())
    k = K
    count = 0
    num = set()
    num.add(X)
    while True:
        if abs(X + D) < abs(X - D):
            if abs(X + D) not in num and K > 0:
                X += D
                count += 1
                num.add(X)
                K -= 1
            else:
                break
        elif abs(X - D) < abs(X + D):
            if abs(X - D) not in num and K > 0:                
                X -= D
                count += 1
                num.add(X)
                K -= 1
            else:
                break
        else:
            break

    if (k - count) % 2 == 0:
        print(X)
    else:
        print(min(abs(X-D), abs(X + D)))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#  X+DP-D(K-P)
# =X-DK+2DP
# =C+2DP
# C>=0?
#   0
# C<0?
#   P=~(-C/2D)

i = list(map(int, input().split()))
x = i[0]
k = i[1]
d = i[2]
c=x-d*k
if c>=0:
    print(c)
else:
    p=-c//(2*d)
    ans = 10000000000000000000000000000000000000000
    for i in range(-10000,10000):
        if 0<=p+i and p+i<=k:
            ans=min(ans,abs(c+2*d*(p+i)))
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    x, k, d= map(int, input().split())
    if x == 0:
        if k%2 == 0:
            print(0)
            return
        else:
            print(d)
            return
    else:
        if x < 0:
            x = 1*x
        if k*d <= x:
            print(x-k*d)
            return
        else:
            y = x//d
            p = x-d*y
            nokori = k - y
            if nokori%2 == 0:
                print(p)
            else:
                print(d-p)
        

if __name__ == '__main__':
    main()

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:
    b = abs(x%(2*d))
    c = abs(mn%(2*d))
    e = abs(mx%(2*d))
    b = min(b,c,e)
  else:
    b = abs(mn%(2*d))
    e = abs(mx%(2*d))
    b = min(b,e)
  ans = min(b,abs(mx),abs(mn))
else:
  ans = min(abs(mx),abs(mn))
print (ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = list(map(int , input().split()))
ans = 0
if (X >=0):
    a = K*D
    if (X >= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X -= D
            time += 1
            if X < 0:
                break
        nokori = (K- time) % 2
        ans = X + D*nokori
else:
    a = -(K*D)
    if (X <= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X += D
            time += 1
            if X <0:
                break
        nokori = (K - time) % 2
        ans = X - D*nokori
print(ans)

Submission status: Wrong Answer

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

t = abs(X) // D
if K < t:
    if X < 0:
        x = X + K*D
    else:
        x = X - K*D
else:
    if X < 0:
        x = X + t*D
    else:
        x = X - t*D

    if (K - t) % 2 == 1:
        x = D - abs(x)
print(x)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
for _ in range(1):#int(stdin.readline())):
    # n=int(stdin.readline())
    x,k,d=list(map(int,stdin.readline().split()))
    if x==k==d:
        print(0 if k&1 else x)
        continue
    des=x%d
    op=x//d
    a=[des,des-d]
    if k<=op:#check for eq
        print(x-k*d)
    else:
        print(abs(a[k%2]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
    # combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    x,k,d = readInt()
    count = x // d
    mn = 0
    if count>k:
        mn = abs(x) - (d * k)
    else:
        mn = abs(x) % d
    ans = INF
    if (k-count)%2==0:
        ans = mn
    else:
        ans = abs(mn-d)
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

mv = int(-X / D + 0.5)
if (abs(mv)>K): mv = mv * K / abs(mv)
residual_moves = K - abs(mv)

if residual_moves % 2 == 0:
    print (int(abs(X + D * mv)))
else:
    posa = X+D*(mv+1)
    posb = X+D*(mv-1)
    print(int(abs(min(posa, posb))))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    X, K, D = map(int, input().split())
    M = X%D
    S = X//D
    C = K - S 
    if C < 0:
        ans = abs(X) - K*D
    else:
        if M <= abs(M-D):
            if C%2 == 0:
                ans = M
            else:
                ans = abs(M - D)
        else:
            C -= 1
            if C%2 != 0:
                ans = M
            else:
                ans = abs(M - D)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#Problem C

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

T_min = X//D
ans = 0

if abs(X) > K*D:
    print(abs(X) - K*D)
    exit()
elif T_min > K:
    print(abs(X) - K*D)
    exit()


if (T_min-K)%2 ==0:
    ans = abs(X) - (T_min*D)
else:
    ans1 = abs(abs(X) - (T_min*D) + D)
    ans2 = abs(abs(X) - (T_min*D) - D)
    ans = min(ans1,ans2)
    
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    while k > 0:
        
        if k > 0 and x >= d:
            steps = min(k, x // d)
            k -= steps
            x -= steps * d

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

            return

        if k > 0 and x <= -d:
            steps = min(k, -x // d)
            k -= steps
            x += steps * d

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

            return

        if x == 0:
            if k % 2 == 0:
                print(0)
            else:
                print(d)
            
            return


    print(abs(x))

solve()

Submission status: Wrong Answer

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

if K < X // D:
    print(X - (K * D))
    exit()
elif X // D <= K:
    # 残りが偶数か奇数か
    if (K - X // D) % 2 == 0:
        print(X % D)
        exit()
    elif (K - X // D) % 2 == 1:
        print(min(abs(X % D - D), abs(X % D + D)))
        exit()

Submission status: Wrong Answer

Solution code:
x, k, d = [int(e) for e in input().split()]

if x < 0:
    x = -x

a = x // d

if k <= a:
    print(x - k * d)

b = k - a

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

Submission status: Wrong Answer

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

a=X//D
b=a+1
if b<=K:
    if b%2==K%2:
        s=abs(X-D*b)
    else:
        s=abs(X-D*a)
else:
    s=abs(X-D*K)
print(s)

Submission status: Wrong Answer

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

if ans>0:
  print(ans)
else:
  print((-1)*ans)

Submission status: Wrong Answer

Solution code:

n = input()

n_list = n.split()

x = int(n_list[0])
k = int(n_list[1])
d = int(n_list[2])

def count(x,k,d):
	x_positive = 1
	if x > 0:
		x_positive = -1

	c = 0

	while abs(x) >= d:
		x += x_positive * d
		c += 1
#		print(' -> {}'.format(x))
		
		if c == k:
			return x

#	print('\n nokori {} kai'.format(k - c))
	if (k - c) % 2 == 0:
		return x

	now = abs(x)
	nextPlus  = abs(x + d)
	nextMinus = abs(x - d)
#	print('\n now:{} next+:{}, next-:{}'.format(now, nextPlus, nextMinus))
	
	if nextPlus < nextMinus:
		return nextPlus
	else:
		return nextMinus


print('{}'.format(count(x,k,d)))

Submission status: Wrong Answer

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

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

m = X % D
l = X // D

if l > K:
    print(abs(abs(X) - D * K))
else:
    if (K - l) % 2 == 0:
        print(m)
    else:
        print(D - m)

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import math
#input=stdin.readline
#def print(x):stdout.write(str(x)+'\n')
x,n,d=map(int,input().split())
ans=x
if x>0:
    if x-n*d<0:
        c=x//d
        n=n-c
        ans=x-c*d
        a=abs(ans-d)
        b=ans
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x-n*d)
elif x==0:
    if n%2==0:
        print(x)
    else:
        print(x+d)
else:
    if x+n*d>0:
        c=abs(x)//d
        n=n-c
        ans=x+c*d
        a=ans+d
        b=abs(ans)
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x+n*d)

Submission status: Wrong Answer

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

q, r = divmod(x, d)

# print(q, r)

if k <= q:
    print(x - k * d)
else:
    k -= q
    # print(f'{k=}')
    
    if k % 2 == 0:
        print(r)
    else:
        print(abs(r - d))

Submission status: Wrong Answer

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

a = (K * D - X) / (2*D)
a1, a2 = math.floor(a), math.ceil(a)

left = 0
right = K
def f(a):
    return X - K * D + 2 * a * D

mid = None
flag = 0
while left < right:
    mid = (left + right) // 2

    if f(mid) == 0:
        flag = 1
        break
    elif f(mid) < 0:
        left = mid + 1
    else:
        right = mid - 1

if flag:
    print(abs(X - K * D + 2 * mid * D))
else:
    print(abs(X - K * D + 2 * left * D))

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
a = 0
b = k + 1
mid = 0
if x < 0:
    x *= -1
while b - a > 1:
    mid = (a + b) >> 1
    if x - d * mid >= 1:
        a = mid
    else:
        b = mid
print(x - a * d)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
X,K,D=map(int,input().split())
t=abs(X)//D
if t>=K:
  print(abs(X)-D*K)
  sys.stderr.write("1\n")
elif t+1==K:
  print(D*K-abs(X))
  sys.stderr.write("2\n")
else:
  if abs(abs(X)-D*t)<abs(D*(t+1)-abs(X)):
    if (K-t)%2==0:
      print(abs(X)-D*t)
      sys.stderr.write("3\n")
    else:
      print(D*(t+1)-abs(X))
      sys.stderr.write("4\n")
  else:
    if (K-t)%2==0:
      print(D*(t+1)-abs(X))
      sys.stderr.write("5\n")
    else:
      print(abs(X)-D*t)
      sys.stderr.write("6\n")

Submission status: Wrong Answer

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

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:
    a = round((k*d-x)*1./(2*d))
    ans = 2*d*a-k*d+x
    if ans<=0:
        print(-1*ans)
    else:
        print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

a = (K * D - X) / (2*D)
a1, a2 = math.floor(a), math.ceil(a)

left = 0
right = K
def f(a):
    return X - K * D + 2 * a * D

mid = None
flag = 0
while left < right:
    mid = (left + right) // 2

    if f(mid) == 0:
        flag = 1
        break
    elif f(mid) < 0:
        left = mid + 1
    else:
        right = mid - 1

if flag:
    print(abs(X - K * D + 2 * mid * D))
else:
    print(abs(X - K * D + 2 * mid * D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

K2= K - (X // D)

X= X - abs(((K-K2)*D))

if K2 % 2 == 1:
    X= X -D


print(abs(X))

Submission status: Wrong Answer

Solution code:
x,s,d=map(int,input().split())
x=abs(x)
a=s*d
b=x
c=s//2+1
for i in range(c):
  b=min(b,abs(x-a))
  a-=2*d
print(b)

Submission status: Wrong Answer

Solution code:
import sys
import math
X, K, D = map(int, input().split())
if X == 0 and K % 2 == 0:
    print(X)
    sys.exit()
elif X == 0 and K % 2 == 1:
    print(D)
    sys.exit()
if abs(X // D) >= K:
    ans = X - (D * K) * (X // abs(X))
    ans = abs(ans)
else:
    K -= X//D
    ans = X - (D * (X//D)) * (X // abs(X))
    if K % 2 == 0 and K == 0:
        ans = abs(ans)
    elif K % 2 == 0 and K >= 2:
        ans = min([abs(ans), abs(ans-D), abs(ans-D)])
    else:
        ans = min(abs(ans+D), abs(ans-D))
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys

if __name__ == '__main__':
    # 初期位置、移動回数、一回の移動量
    X, K, D = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split()))
    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(X)
        exit()

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

Submission status: Wrong Answer

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

x = abs(x)
#0に最も近い距離になる。
ans = x % d
#ansへの移動回数分、kから引く
if k- (x//d) < 0:
    print(x - k*d)
    exit()
else:
    k -= x//d

tmp = abs(ans - d)

if k % 2 == 0:
    print(min(ans, tmp))
else:
    print(max(ans, tmp))

Submission status: Wrong Answer

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

count = abs(x) // d

if k > count+1:
    ans = min(abs(abs(x) - count * d),abs(abs(x) - (count + 1) * d))
    if (k - count) % 2 == 1:
        ans = abs(ans - d)
    print(ans)
else:
    print(abs(abs(x) - k * d))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ans = 0
if abs(x) >= k*d:
    ans = min(abs(x-k*d),abs(x+k*d))
else:
    b = abs(x)%d
    c = abs(x)//d
    if (k-c) %2 == 0:
        if x < 0:
            ans = -b
        else:
            ans = b
    else:
        if x < 0:
            ans = abs(b-d)
        else:
            ans = -abs(b-d)
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:
    b = abs(x%(2*d))
    c = abs(mn%(2*d))
    e = abs(mx%(2*d))
    b = min(b,c,e)
  else:
    b = abs(mn%(2*d))
    e = abs(mx%(2*d))
    b = min(b,e)
  #print (b,c,e)
  ans = b
else:
  ans = min(abs(mx),abs(mn))
print (ans)

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
    if x==0:
        if K%2==0:
            print(y)
        else:
            print(x)
    else:
        print(min(x,y))

Submission status: Wrong Answer

Solution code:
import sys
X,K,D = map(int,input().split())
if X > K * D and X > 0:
    print(X - K*D)
    sys.exit()
elif X < K * D and X < 0:
    print(X + K*D)
    sys.exit()
X = abs(X)
if X > 0:
    if (X // D) % 2 == K % 2:
        print(X % D)
    elif (X // D) % 2 != K % 2:
        print(abs(X % D - D))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Wrong Answer

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

result_bef = 10**18
for n in range(D+1):
  result = abs(X + n * D - (K - n) * D)
  if result < result_bef:
    result_bef = result
    continue
  else:
    break

print(result_bef)

Submission status: Wrong Answer

Solution code:
#n = int(input())
x,k,d = map(int,input().split())
#l = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
v=abs(x)//d
if abs(x)//d >= k:
    if x >= 0:
        ans=x-d*k
    else:
        ans=x+d*k
else:
    move=v*d
    dif=k-v
    if x >0:
        #l=[]
        #l.append(x-v*d)
        x2=x-v*d
        #l.append(abs(x2-d))
        #ans=min(l)
        if dif%2==0:
            ans=x-v*d
        else:
            ans=abs(x2-d)
    else:
        x2=x+v*d
        if dif%2==0:
            ans=x+v*d
        else:
            ans=abs(x2+d)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n = int(n)
k = int(k)
d = int(d)

# print(n, k, d)

x = (k*d - n)//(2*d)

# debug1(x);
x = max(0, x-100)
y = min(k, x+100)

ans = abs(n+x*d - (k-x)*d);
for i in range (x, y+1):
    ans = min(ans, abs(n+i*d - (k-i)*d));

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

#正の処理
if x > 0:
    neededmove = x//d
    amari = x%d
    if abs(amari - d) < abs(amari):
        amari = abs(amari - d)
        neededmove += 1
    if k < neededmove:
        print(x-k*d)
    elif k == neededmove:
        print(abs(amari))
    else:
        hoge = k - neededmove
        if hoge % 2 == 0:
            print(abs(amari))
        else:
            if amari <= 0:
                print(abs(amari + d))
            else:
                print(abs(amari-d))
            
#負の処理
elif x < 0:
    neededmove = abs(x//d)
    amari = x%d
    if abs(amari + d) < abs(amari):
        amari = abs(amari + d)
        neededmove += 1
    if k < neededmove:
        print(x+k*d)
    elif k == neededmove:
        print(abs(amari))
    else:
        hoge = k - neededmove
        if hoge % 2 == 0:
            print(abs(amari))
        else:
            if amari <= 0:
                print(abs(amari + d))
            else:
                print(abs(amari-d))
#0の処理
else:
    if k % 2 == 0:
        print(0)
    else:
        print(d)

Submission status: Wrong Answer

Solution code:

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

x = abs(x)

decim = x/d

if (decim > k):
    print(abs(x - k*d))
    exit(0)
else:
    if (x % d == 0 and (x // d)% 2 == 0 ):
        # print('b')
        print(0)
    elif (x % d == 0 and (x // d)% 2 == 1 ):
        # print('b2')
        print(d)
    else:
        # print('c')
        lower = x // d
        if (k % 2 == 1):
            lower -= 1
        upper = lower + 2
        print(min(abs(x - lower*d), abs(x - upper*d)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

r = x%d
l = r-d

lc = abs(x-l)//d
rc = abs(x-r)//d

ans = abs(x)
if(k>=lc):
    if((k-lc)%2): ans = min(ans, min(abs(l+d), abs(l-d)))
    else: ans = min(ans, abs(l))
if(k>=rc):
    if((k-rc)%2): ans = min(ans, min(abs(r+d), abs(r-d)))
    else: ans = min(ans, abs(r))
if(k<lc and k<rc):
    ans = min(ans, abs(x-d*k), abs(x+d*k))
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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


if x >= d * k:
    print(abs(x-d*k))
else:
    if k % 2 == 1:
        print(abs(x - math.ceil(x/d) * d))
    else:
        print(abs(x - (math.ceil(x/d) - 1) * d))

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int,input().rstrip().split()))
X = abs(X)
a = X//D
if D < a:
    print(X-(D*K))
elif a&1 == K&1:
    print(X-(D*a))
else:
    print(D*(a+1)-X)

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

X,K,D = LI()

sign = np.sign(X)
x = abs(X)

if x//D>K:
    ans = sign * (x-K*D)
else:
    # 残り回数が偶数回
    if (K - x//D) % 2 ==0:
        # print('even')
        ans = sign * (x- (x//D)*D)
    else:
        # print('odd')
        ans = -1 * sign * (x - (x//D)*D -D)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
str= str(input())
if len(str.split())==3:
    s=[int(x)  for x in str.split()]
x=s[0]
k=s[1]
d=s[2]
cnt=int(x/d)
if cnt>=k:
    print(int(abs(x)-k*d))
else:
    num=int(k-cnt)
    num=int(num%2)
    if num>0 :
        ans1=abs(x)-cnt*d-d
        ans2=abs(x)-cnt*d+d;
        print(int(min(abs(ans1),abs(ans2))));
    else:
        print(int(abs(x)-cnt*d));

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int, input().split())
a=x-k*d
for i in range(k):
    a+=d
    if a>0:
        print(min(abs(a),abs(a-d)))
        break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cand_n_1 = (x + k * d) // (2 * d)
cand_n_2 = (x + k * d) // (2 * d) + 1

best = 1e40
if cand_n_1 > k:
    best = min(best, abs(x + k * d - 2 * d * k))
else:
    best = min(best, abs(x + k * d - 2 * d * cand_n_1))
    best = min(best, abs(x + k * d - 2 * d * cand_n_2))

print(best)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
res = X
n = X//D
rest = 0
if n >= K:
  res = X-K*D
else:
  res = X-n*D
  rest = K-n
  if rest%2 == 1:
    res = res-D
  else:
    pass
res = abs(res)
print(res)

Submission status: Wrong Answer

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

#最大まで移動するとき
if abs(X) >= K * D:
    print(abs(X) - K*D)
    exit()
if X >= 0:
    bef = X % D
    nex = bef - D
    
    if (K - X//D) % 2 == 0:
        print(bef)
    else:
        print(-nex)
else:
    bef = (-X) % D
    nex = bef + D

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

Submission status: Wrong Answer

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

xdivd=x//d

if xdivd+1>k:
    # print('a')
    print(abs(abs(x)-d*k))
elif (k-abs(xdivd))%2==0:
    # print('b')
    print(abs(x-d*xdivd))
else:
    # print('c',xdivd)
    print(abs(x-(xdivd+1)*d))

Submission status: Wrong Answer

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

count = abs(x)/d
if count.is_integer():
    count = int(count)
else:
    count = int(count)+1
    
if x >= 0:
    if count >= k:
        print(abs(x-k*d))
    else:
        if (k-count)%2 == 0:
            print(abs(x-d*count))
        else:
            print(x-d*count+d)
else:
    if count >= k:
        print(x+k*d)
    else:
        if (k-count)%2 == 0:
            print(abs(x+d*count))
        else:
            print(-x-d*count+d)

Submission status: Wrong Answer

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

import sys
import glob

class InputGenerator(object):
    def __init__(self, input_path=None):
        if input_path != None:
            self.g = self.input_generator(input_path)
        else:
            self.g = None

    def input_generator(self, input_path):
        with open(input_path) as f:
            for line in f:
                yield line

    def get_input(self):
        if self.g == None:
            return input()
        else:
            return next(self.g)

def solve(gen):
    len_list = gen.get_input().rstrip().split()
    [x, k, d] = list(map(int, len_list))
    # print(x, k, d)

    r = abs(x) % d
    l = d - r
    q = x // d

    if abs(x) > k * d:
        print(abs(x) - k * d)
    elif (k - q) % 2 == 0:
        print(r)
    else:
        print(l)

if __name__ == '__main__':
    test_flag = len(sys.argv) > 1

    if test_flag == True:
        file_path_list = sorted(glob.glob('test/*'))
    else:
        file_path_list = [None]

    for file_path in file_path_list:
        gen = InputGenerator(file_path)
        solve(gen)

Submission status: Wrong Answer

Solution code:
X,K,D=input().split()
X=int(X)
K=int(K)
D=int(D)
min=X+K*D
for k in range(K):
  current=X-k*D+(K-k)*D
  if min>current:
    min=current
print(min)