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())
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 (check + 1)%2==0):
        print(abs(ans2))
    elif(ans1 + ans2 > 0 and (check + 1)%2==1):
        print(ans1)
    elif(ans1 + ans2 < 0 and check%2==0):
        print(ans1)
    else:
        print(abs(ans2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	X,K,D = inputMap()
	X = abs(X)

	koko = abs(X-D)
	if koko == 0:
		if K % 2 == 0:
			print(abs(D))
			sys.exit()
		else:
			print(0)
			sys.exit()


	ima = abs(X-0)
	if ima == 0:
		if K % 2 == 0:
			print(0)
			sys.exit()
		else:
			print(abs(D))
			sys.exit()

	imaima = abs(X-(K*D))
	if imaima == 0:
		print(0)
		sys.exit()

	if X - K*D > 0:
		ans = X - K*D
		print(ans)
		sys.exit()
	else:
		tmp = X // D
		nokori = K - tmp
		if nokori % 2 == 0:
			ans = X % D
			print(ans)
			sys.exit()
		else:
			o000 = X % D
			assd = abs(D - abs(D-o000))
			print(assd)
			sys.exit()





#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
def walk(place,D):
    A=place-D
    B=place+D
    if abs(A)>abs(B):
        return B
    else: 
        return A

M=-(-abs(X) // D)
M=min(2*M,K)
for i in range(M):
    X=walk(X,D)
print(abs(X))

Submission status: Wrong Answer

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

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

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

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

print(x)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().strip().split())
p=x
a={}
if k!=1:
	for i in range(k):
	
		x=min(abs(x-d),abs(x+d))
		if x in a:
			break

		else:
			a[x]=1

	k=k%i

x=p
for i in range(k):
	x=min(abs(x-d),abs(x+d))



print(abs(x))

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 lowest_value == lst_of_values[-1]:
            break
        else:
            lst_of_values.append(lowest_value)
    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
        if lowest_value == lst_of_values[-1]:
            break
        else:
            lst_of_values.append(lowest_value)
    else:
        break
print(lowest_value)

Submission status: Wrong Answer

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

# print(x,k,d)

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

else: # +
    print(x)

Submission status: Wrong Answer

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

ideal_k = x // d
ideal_result = x - (ideal_k * d)

ideal_result_list = [ideal_result-d, ideal_result, ideal_result+d]
diff = [abs(each) for each in ideal_result_list]
i =  diff.index(min(diff))

ideal_result -= ((-i+1) * d)
ideal_k = (x - ideal_result) // d

# print('ideal_k', ideal_k)
# print('ideal_result', ideal_result)

if k - ideal_k < 0:
    result = x - (k * d)
    result = x - (k * d)
elif (k - ideal_k) % 2 == 0:
    result = ideal_result
else:
    if ideal_result < 0:
        result = ideal_result + d
    else:
        result = ideal_result - d

print(abs(result))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        if x >= 0:
            x = x - (a * d)
        else:
            x = x + (a * d)
    else:
        x = x - (a * d)
        if x >= 0:
            x = x - abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

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

b = x - k * d
b_ = x + k * d


if x >= 0:
    a = -(-x // d)
    if a >= k:
        if b >= 0:
            print(b)
        else:
            print(-b)
    elif a < k:
        b = x - a * d
        c = k - a
        if c % 2 == 0:
            print(-b)
        else:
            print(b + d)
if x < 0:
    a = x // d
    if -a >= k:
        if b_ >= 0:
            print(b_)
        else:
            print(-b_)
    elif -a < k:
        b = x - a * d
        c = k + a
        if c % 2 == 0:
            print(b)
        else:
            print(b - d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

start, kai, step = map(int, input().split(" "))

if 0 <= start - (kai * step):
    print(abs(start - (kai * step)))
    exit()

ans1 = start % step
ans2 = abs(ans1 - step)
if (kai - abs(start // step)) % 2 == 0:
    print(abs(ans1))
else:
    print(abs(ans2))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
best = min(abs(x%d+d),abs(x%d-d),abs(x)%d)
if best==abs(x)%d:
    c=abs(x)//d
else:
    c=abs(x)//d+1
if c < k:
    #best
    if (k-c) % 2 == 0:
        print(min(abs(x%d+d),abs(x%d-d),abs(x)%d))
    #second-best
    else:
        print(min(abs(x%d+d),abs(x%d-d)))
# cannot reach the best/second-best
else:
    print(abs(x)-d*k)

Submission status: Wrong Answer

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

else:
    print(x)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
ans=0
KK=K-X%D
XX=X-(X%D)*D

if KK%2==0:
    ans=abs(XX)
    
else:
    if abs(XX+D)>=abs(XX-D):
        print(abs(XX-D))
    else:
        print(abs(XX+D))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
#  何回で0をまたぐか、その時いる場所
times = X // D + 1
place = X - D * (times)
if times <= K:
    #  残った移動回数
    K = K - times
    if K % 2 == 0:
        place = place
    else:
        place = place + D

else:
    place = X - D * K

if place < 0:
    place = place * (-1)
print(place)

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:
  ans = min(abs(mx),abs(mn))
print (ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
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 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:
                ans=min(-ans,ans+D)
                break
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:
                ans=min(ans,D-ans)
                break
print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
if X<=D and D%2==0:
  print(X)
  exit()
elif X<=D and D%2==1:
  print(abs(X-D))
  exit()
s = X
for i in range(K):
  s = min(X,abs(K-D))
print(s)

Submission status: Wrong Answer

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


if d>=abs(x):
    if k%2==0:
        p=abs(x)
    else:
        if x>=0:
            p=abs(x-d)
        else:
            p=abs(x+d)
    print(p)
    exit(0)
else:
    cnt=abs(x)//d

    if cnt>=k:
        cnt=k
        if x>=0:
            p=x-d*cnt
        else:
            p=x+d*cnt
        print(p)
        exit(0)
    else:
        if x>=0:
            p=x-d*cnt
            k-=cnt
            if k%2==0:
                print(p)
            else:
                print(abs(p-d))
        else:
            p=x+d*cnt

            k-=cnt

            if k%2==0:
                print(p)
            else:
                print(abs(p+d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = [int(a) for a in input().split()]
X0=X
u=X
l=X
now=0
for i in range(K):
    if X>=0:X-=D
    elif X<0:X+=D
    if X>0 and X<X0:
        u=X
        now=1
    if X<0 and X>-X0:
        l=X
        now=-1
    if X==abs(X0):
        u=X
        if i!=K-1:l=0
        now=1
    if X==-abs(X0):
        l=X
        if i!=K-1:u=0
        now=-1
    if X==0 and i==K-1:
        print("0")
        break
    if (u>0 and u<X0 and l<0 and l>-X0) or (u==0 or l==0):
        if (K-1-i)%2:
            if now:
                print(u)
            else:
                print(abs(l))
        else:
            if now:
                print(abs(l))
            else:
                print(u)
        break
    if i==K-1:
        print(abs(X))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
a=divmod(abs(x),d)
if a[0]<k:
  if a[0]%2!=0:
    if a[1]<=abs(d-a[1]):
      print(a[1])
    else:
      print(abs(d-a[1]))
  else:
    print(a[1])
else:
  print(abs(x)-d*k)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if x == d:
    if k % 2 == 0:
      print(x)
    else:
      print(0)
else:
  for i in range(k):
    if x > d:
      x = x - d
    else:
      x = d - x 
  print(x)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
if abs(X) >= D:
    minX = abs(X) % D

if abs(X) / D <= K and abs(X) >= D:
    if (K - (abs(X) / D)) % 2 == 0:
        print(minX)
    else:
        if abs(minX - D) < abs(minX + D):
            print(abs(minX - D))
        else:
            print(abs(minX + D))
elif abs(X) / D > K and abs(X) >= D:
    print(abs(X) - K*D)
elif abs(X) < D and K % 2 == 0:
    print(abs(X))
elif abs(X) < D and K % 2 == 1:
    print(D - 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))
else:
    print(abs(X  - K*(1+int(abs(X)/abs(K)))))

Submission status: Wrong Answer

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

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

print(abs(x))

Submission status: Wrong Answer

Solution code:
import math;

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


a = math.floor(x / d);

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

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

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

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

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

            

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

print(x);

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import *
import sys,bisect,copyreg,copy,statistics,os
def inp(): return sys.stdin.readline().strip()
def IIX(): return (int(x) for x in sys.stdin.readline().split())
def II(): return (int(inp()))
def LI(): return list(map(int, inp().split()))
def LS(): return list(map(str, inp().split()))
def L(x):return list(x)
def out(var): return sys.stdout.write(str(var))
def binary_search(arr, x): 
    left = 0
    right = len(arr) - 1
    mid = 0
    while left <= right:   
        mid = (left + left) // 2
        if arr[mid] < x: 
            left = mid + 1
        elif arr[mid] > x: 
            right = mid - 1
        else: 
            return mid
    return -1

x,k,d=LI()

x=abs(x)
currPos=x
minPos=currPos
while k>0:
    currPos=(currPos-d)
    minPos=min(abs(minPos),abs(currPos))
    k-=1
out(minPos)

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 = x % d

print(ans)

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np

X,K,D = map(int,input().split())
if np.sign(X)== -1:
  X=-X
a = X//K
b = X%K
if D-a < 0:
  print(X-K*D)
  exit()

if b > K-b:
  b = K-b

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

Submission status: Wrong Answer

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

if y > 0:
    print(y)
    exit()

while k > 0:
    if x >= 0:
        x -= d
    else:
        x += d

    if abs(x) >= abs(x_pre):
        if k % 2 == 0:
            print(abs(x_pre))
            exit()
        else:
            print(abs(abs(x_pre) - d))
            exit()
    else:
        x_pre = x
        k -= 1

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
flag=10
x,k,d=map(int,input().split())
for i in range(k):
    if x>0:
        x-=d
        if x<=0:
            flag=-1
            break
    elif x<0:
        x+=d
        if x>=0:
            flag=1
            break
    else:
        flag=0
        break

if flag==0:
    print(d)
elif flag==-1:
    print(min(abs(x),x+d))
elif flag==1:
    print(min(abs(x-d),x))
else:
    print(abs(x))

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:
    print(min(abs(x),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())
jud = x//d

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

for i in range(k):
  tmp=x
  x-=d
  if abs(x) >= abs(tmp):
    if (k-i)%2==0:
      print(abs(tmp))
      exit()
    else:
      print(abs(x))
      exit()
else:
  print(abs(x))

Submission status: Wrong Answer

Solution code:
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
if abs(X)>K*D:
    print(min(abs(X-K*D), abs(X+K*D)))
else:
    N=X//D
    Min=10**16
    n=0
    X=abs(X)
    for i in range(N-1, N+2):
        M=abs(X-i*D)
        if M<Min:
            Min=M
            n=i
    if (K-n)%2==0:
        print (abs(Min))
    else:
        print(min(abs(Min+D), abs(Min-D)))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
X, K, D = list(map(int, input().split()))
x = int(X)
D = int(D)
j = 0
j = X//D

if j > K:
    j = K


if x > 0:
    x = x - (D*j)
else:
    x = x + (D*j)

j = (K-j) % 2

if j==0:
    print(abs(x))
else:
    if x > 0:
        x = x - D
    else:
        x = x + D
    print(abs(x))

Submission status: Wrong Answer

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

init = x

for i in range(round(abs(x) // abs(d))):
    if (round(abs(x) // abs(d)))>>10**6:
        print(abs(x)-abs(d)*k)
        break
    tempx = abs(x) + i*abs(d) - (k-i)*abs(d)
    init = min(init, abs(tempx))

print(init)

Submission status: Wrong Answer

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

move_count = 0
D = abs(D)
X = abs(X)

q = X // D
mod = X % D
vi = X //2
if q > K:
  print(X - D*K)
else:
  if (K-q) // 2 == 0:
    print(X- D*q)
  else:
    print(abs(X-D*(q+1)))

Submission status: Wrong Answer

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

seki = K * D
amari = X % D

if 0 < (X-seki) or (X+seki) < 0 :
    result = min(abs(X-seki),abs(X+seki))
else :
    if X >= 0 :
        if K%2 == 0 :
            if ((abs(X)-amari)/D) % 2 == 0 :
                #print("a")
                result = amari
            else :
                #print("b")
                result = abs(amari-D)
        else :
            if ((abs(X)-amari)/D) % 2 == 1 :
                #print("c")
                result = amari
            else :
                #print("d")
                result = abs(amari-D)
    else :
        if K%2 == 0 :
            if ((abs(X)-amari)/D) % 2 == 0 :
                #print("e")
                result = amari
            else :
                #print("f")
                result = abs(-amari+D)
        else :
            if ((abs(X)-amari)/D) % 2 == 1 :
                #print("g")
                result = amari
            else :
                #print("h")
                result = abs(-amari+D)
    
print(result)

Submission status: Wrong Answer

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

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

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

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((x-d)%(2*d)),abs((x-d)%(2*d)-2*d))
    else:
      b = min(abs(abs(x+d)%(2*d)),abs((x+d)%(2*d)-2*d))
  ans = min(abs(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())

R = X // D
Q = X - D * K

if K <= R:
  result = Q
else:
  K -= R
  K %= 2
  P = X - D * R
  if K == 0:
    result = P
  else:
    result = abs(P - D)

print(result)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cnt = x//d
mod = x%d
min = 0
if mod > abs(mod-d):
    min = abs(mod-d)
    cnt += 1
else:
    min = mod

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

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

x = X
for k in range(1, K+1):
  tmp_down, tmp_up = abs(x - D), abs(x + D)
  if tmp_down > tmp_up:
    x = tmp_up
  elif tmp_down < tmp_up:
    x = tmp_down
  elif tmp_down == tmp_up:
    x = tmp_down
    break
print(abs(x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

mod1 = abs(x) % d
mod2 = mod1 - d
mod = min(mod1, abs(mod2))
modx = max(mod1, abs(mod2))
x1 = abs(x)

a = (x1-mod)/d

if mod1+k*d <= x1:
    print(x-k*d)
elif (k-a) % 2 == 0:
    print(mod)
else:
    print(modx)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if k%2==0:
  ans=0 #偶数
else:
  ans=1 #奇数

semin=x//d #偶数か奇数
if semin%2==0:
  minb=0 #偶数
else:
  minb=1 #奇数
humin=semin+1

if semin>=k:
  print(abs(x-k*d))
  exit()
else:
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-k*semin))
    else:
      print(abs(x-k*humin))
  else:#ｋが奇数の時
    if minb==0:
      print(abs(x-k*humin))
    else:
      print(abs(x-k*semin))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

first=X//D
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))