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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
min = 1000000000000000
cnt = 0
n = int(x/d)
n = abs(n)
if n > k:
    n=k

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

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


    else:
        print(x)

Submission status: Wrong Answer

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

count = 0
finish = False
position = X
if X > 0:
    while(position > 0 and count < K):
        count = count + 1
        position = position - D
        if position == 0:
            print(D)
            finish = True

    if finish == False:
        minus_abs = abs(position)
        position = position + D
        plus_abs = abs(position)

        if minus_abs > plus_abs:
            print(plus_abs)
        else:
            print(minus_abs)

else:
    while(position < 0):
        position = position + D

        if position == 0:
            print(D)
            finish = True

    if finish == False:
        minus_abs = abs(position)
        position = position + D
        plus_abs = abs(position)

        if minus_abs > plus_abs:
            print(plus_abs)
        else:
            print(minus_abs)

Submission status: Wrong Answer

Solution code:
import sys
import math

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

if X >= 0:
    flag = 1
else:
    flag = -1

for a in range(K):
    #print(X)
    #print('flag',flag)
    if X >= 0:
        X = X - D
        if flag == -1:
            flag = 0
        else:
            flag = 1
    else:
        X = X + D
        if flag == 1:
            flag = 0
        else:
            flag = -1

    if flag == 0 and (K - (a + 1)) % 2 == 0:
        break
    elif flag == 0 and X >= 0:
        X += D
        break
    elif flag == 0 and X < 0:
        X -= D
        break

X = abs(X)
print(X)

Submission status: Wrong Answer

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

p=x//(2*d) + k//2
pa=p+1
#print(p)
print(min(abs(x+(k-2*p)*d),abs(x+(k-2*pa)*d)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = input().split()
a = []
X = int(X)
K = int(K)
D = int(D)
for i in range(K):
    X = X - D
    if X < 0:
        X = -X
    a.append(X)
b = min(a)
print(b)

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
ans = []
a = math.floor(abs(x) / d)
if a > k:
  a = k
  if x > 0:
    ans = (abs(x - d * k))
  else:
    ans = (abs(x - d * k))
else:
  if x > 0:
    tmp = (abs(x - d * a))
    if (k - a) % 2 == 1:
      ans = (abs(x - d * (a + 1)))
    else:
      ans = tmp
  else:
    tmp = (abs(x + d * a))
    if (k - a) % 2 == 1:
      ans = (abs(x + d * (a + 1)))
    else:
      ans = tmp
print (ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = [int(i) for i in input().split()]
x = abs(x)
if k * d <= x:
    print(x - k*d)
else:
    t = x
    x %= d
    k = (t - x)//d
    if k & 1:
        print(abs(d - x))
    else:
        print(x)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
a_list = []
if K <= 10000000:
  for i in range(K+1):
    if X >= 0:
      a_list.append(abs(X - i*D))
    else:
      a_list.append(abs(X + i*D))
  if K % 2 == 0:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
else:
  q = X // K
  for j in range(-1000000+q, 1000000+q):
    if q >= 0:
      a_list.append(abs(X - j*D))
    else:
      a_list.append(abs(X + j*D))
  if K % 2:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
def func():
#    a=int(input())
    i1=list(map(int,input().split()))
    #print(a)
    #for i in range(a):
    #    print(b[i])
    a=i1[0]
    b=i1[1]
    c=i1[2]
#    print(a,b,c)
    l=math.floor(a/c)
    m=b-l
    n=a-c*l
    if l>=b: z=a-c*b
    else:
        z=a-(c*l)
        if m%2==1:
            z=z-c
    print(abs(z))

func()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

# coding: utf-8
# Your code here!

X,K,D = map(int, input().split())
#Xが場所、Kが回数、Dが移動距離

A=X//D#0に近いやつ
B=K*D#動ける最大
#print(A,B)

if A>K:
    ANS=X-B
    #print("A")
elif A%2==0:
    #print("B")
    ANS=X-(min(abs(A*D),abs(B)))
else:
    #print("C")
    ANS=(min(abs(A*D),abs(X-B)))

print(ANS)

Submission status: Wrong Answer

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

if X==K and X==D and K==D:
    print(X)
else:
    for _ in range(K):
        minN=min(abs(minN-D),abs(minN+D))
    print(minN)

Submission status: Wrong Answer

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

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

min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    else:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, abs(X), K-i)
        break
print(min_score)

Submission status: Wrong Answer

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


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

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


    return abso(ans)

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

Submission status: Wrong Answer

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

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

else:
    cent = x % d
    num = x // d

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

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

rem = k - num

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

Submission status: Wrong Answer

Solution code:

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

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

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

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

k_count = k

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

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

print(abs(x))

Submission status: Wrong Answer

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

times = abs(X) // D

if times < K:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
    K = K - times
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
else:
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D*K
        elif current_position < 0:
            current_position = current_position + D*K
print(abs(current_position))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

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

	if x > 0:
		for i in range(k):
			#-進める
			x = x - d
			if x <= 0:
				#判定
				if i != k - 1:
					x = min(abs(x+d),abs(x-d))
					print(x)
					exit()
				else:
					x = abs(x)
	else:
		for j in range(k):
			#+進める
			x = x + d
			if x >= 0:
				#判定
				if j != k - 1:
					x = min(abs(x+d),abs(x-d))
					print(x)
					exit()
				else:
					x = abs(x)
	#判定
	print(x)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
end = False
plus =True
if x >= 0:
    for i in range(k):
        if x >= 0 and plus == False:
            if (k-(i+1))%2 == 1:
                print(abs(x))
                end = True
                break
            elif (k-(i+1))%2 == 0:
                print(abs(x-d))
                end = True
                break
        elif x >= 0:
            x=x-d
        elif x < 0:
            x=x+d
            plus = False
if x < 0:
    for i in range(k):
        if x < 0 and plus == False:
            if (k-(i+1))%2 == 1:
                print(abs(x))
                end = True
                break
            elif (k-(i+1))%2 == 0:
                print(abs(x-d))
                end = True
                break
        elif x >= 0:
            x=x-d
            plus = False
        elif x < 0:
            x=x+d
if end == False:
    print(x)

Submission status: Wrong Answer

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

for i in range(K):
  if key<D:
    break
else:
  key-=D
  K-=1
  
if K==0:
  print(key)

else:
  Key=key-D
  if K%2==0:
    ans=abs(key)
  else:
    ans=abs(Key)
  print(ans)

Submission status: Wrong Answer

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

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

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

if k%2 == 1:
    if cnt%2 == 1:
        print(x-(d*cnt))
    else:
        print(min(abs(mini-d),mini+d))
else:
    if cnt%2 == 0:
        print(x-(d*cnt))
    else:
        print(min(abs(mini-d),mini+d))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    if K * D < X:
        print(X - K * D)
    else:
        if X > D and K % 2 == 0:
            span = int(X / D)
            val = min(X - span * D, X - (span + 1) * D)
            print(abs(val))
        else:
            for i in range(K):
                if X >= 0:
                    X = X - D
                else:
                    X = X + D
            print(abs(X))

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(-4,5):
    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))
for i in range(-4,5):
    z2=k+i
    if z2>k or z2<0:
        continue
    if z2%2!=k%2:
        continue
    ans=min(ans,abs(x-z2*d))
    ans=min(ans,abs(x+z2*d))
print(ans)

Submission status: Wrong Answer

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

p=x//2*d + k//2
print(p)
pa=p+1
print(min(abs(x+(k-2*p)*d),abs(x+(k-2*pa)*d)))

Submission status: Wrong Answer

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

result=X
for i in range(K):
  oldresult=result
  tmpresult=result
  for j in range(2):
    if abs(tmpresult-D)>abs(tmpresult+D):
      tmpresult=tmpresult+D
    elif abs(tmpresult+D)>=abs(tmpresult-D):
      tmpresult=tmpresult-D
    elif abs(tmpresult)==abs(tmpresult+D) or abs(tmpresult)==abs(tmpresult-D):
      break
  if abs(oldresult)==abs(tmpresult):
    result=tmpresult
    break
  if abs(result-D)>abs(result+D):
    result=result+D
  elif abs(result+D)>=abs(result-D):
    result=result-D
  elif abs(result)==abs(result+D) or abs(result)==abs(result-D):
    break

print(str(abs(result)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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


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

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

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

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

Submission status: Wrong Answer

Solution code:
"""
AtCoder :: Beginner Contest 175 :: C - Walking Takahashi 
https://atcoder.jp/contests/abc175/tasks/abc175_c
"""
import sys


def solve(X, K, D):
    """Solve puzzle."""
    if D == 0:
        return X

    X = abs(X)
    steps_to_zero = abs(X) // D
    if steps_to_zero > K and X < 0:
        return abs(X + (K * D))

    if steps_to_zero > K and X > 0:
        return abs(X - (K * D))

    if steps_to_zero == K:
        return 0

    if steps_to_zero == 0:
        # Move back and forth
        K = K % 2
        return abs(X - (K * D))

    if steps_to_zero < K and X != 0:
        parity_K = K % 2
        under_shoot = X - (steps_to_zero * D)
        over_shoot = X - ((steps_to_zero + 1) * D)
        parity_under = steps_to_zero % 2
        parity_over = (steps_to_zero + 1) % 2
        if parity_under == parity_K:
            return abs(under_shoot)
        else:
            return abs(over_shoot)


def main():
    """Main program."""
    X, K, D = (int(i) for i in sys.stdin.readline().split())
    print(solve(X, K, D))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

if X >= 0:
    flag = 1
else:
    flag = -1

for a in range(K):
    #print(X)
    #print('flag',flag)
    if X >= 0:
        X = X - D
        if flag == -1:
            flag = 0
        else:
            flag = 1
    else:
        X = X + D
        if flag == 1:
            flag = 0
        else:
            flag = -1

    if flag == 0 and (K - (a + 1)) % 2 == 0:
        break
    elif flag == 0 and X >= 0:
        X += D
        break
    elif flag == 0 and X < 0:
        X -= D

X = abs(X)
print(X)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
ans = [X]
for i in range(K):
  if X-(i+1)*D<=0:
    break
  ans.append(X-(i+1)*D)
  
print(min(ans))

Submission status: Wrong Answer

Solution code:
x, k, d = [int(i) for i in input().split()]
x = abs(x)
if k * d <= x:
    print(x - k*d)
else:
    t = x
    x %= d
    k = (t - x)//d
    if k & 1:
        print(d - x)
    else:
        print(x)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
Xa, K, D = map(int, input().split())
X = abs(Xa)
Xs = X // D
Xm = X % D
if K < Xs:
#     print("A")
    print(abs(X-K*D))
elif Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif ((K-Xs) % 2 == 0):
    print(abs(D-Xm))
else:
    print(abs(D-K))

Submission status: Wrong Answer

Solution code:
from sys import stdin
x,k,d = map(int, stdin.readline().rstrip().split())
ret = x
if k%2 == 0:
    if (x // d) <= k:
        if (x // d) % 2 == 0:
            print(int(abs(x - ((x // d) * d))))  #
        else:
            print(int(abs(min(abs(x - ((x // d) - 1) * d), abs(x - ((x // d) + 1) * d)))))

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

        else:
            print(int(abs(x- ((x // k) * d))))  #

    else:
        print(int(abs(x-(d*k))))

Submission status: Wrong Answer

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

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

s = abs(x)
num = 0

while k>0:
    s = x
    s = s-d
    if abs(s)>=abs(x):
        break
    x = abs(s)
    k -= 1


if k == 0:
    print(abs(s))
    exit()

if k%2 == 0:
    print(abs(x))
else:
    print(abs(s))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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


main()

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 #最小に行くための個数が奇数

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
l = max(1,k//2)
if x - k*d >= 0:
	print(x-k*d)
else:
	while True:
		if d*k + x - 2*d*l > 0 and d*k + x - 2*d*(l+1) < 0:
			print(min(d*k + x - 2*d*l,-(d*k + x - 2*d*(l+1))))
			break
		elif d*k + x - 2*d*l < 0 and d*k + x - 2*d*(l-1) >0:
			print(min(-(d*k + x - 2*d*l),d*k + x - 2*d*(l-1)))
			break
		elif d*k + x - 2*d*l>0:
			l += max(1,(l-max(1,k//2))//2)
		else:
			l -= max(1,l//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
data1 = [int(x) for x in input().split()]
x = data1[0]
k = data1[1]
d = data1[2]
a = x//d
if k % 2 == a % 2:
    if k >=abs(a):
        takahashi = [x-d*a,x-d*(a+1)]
        t = np.abs(takahashi)
        t_min = min(t)
    else:
        t_min = abs(x - d*k)
else:
    if k >=max([abs(a-1),abs(a+1)]):
        takahashi = [x-d*(a-1),x-d*(a+1)]
        t = np.abs(takahashi)
        t_min = min(t)
    else:
        t_min = abs(x - d*k)
print(t_min)

Submission status: Wrong Answer

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

if abs(X)>K:
    tmp = abs(X)//D
    if D>K:
        tmp=K
    print(tmp)
    if X < 0:
        X += tmp*D
    else:
        X -= tmp*D
    K -= tmp

min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    else:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, abs(X), K-i)
        break
print(min_score)

Submission status: Wrong Answer

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

a = abs(X) // D
if a <= K:
  b = abs(X) - (D * a)
  c = abs(b - D)
else:
  b = abs(abs(X) - (D * K))
  c = 100000000000000000

if ((K - a)%2 == 0) and b <= c:
  print(b)
elif ((K - a)%2 == 1) and b <= c:
  if b < abs(b - D):
    print(b)
  else:
    print(abs(b - D))
elif ((K - a)%2 == 0) and c < b:
  print(c)
elif ((K - a)%2 == 1) and c < b:
  if c < abs(c - D):
    print(c)
  else:
    print(abs(c - D))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(X: int, K: int, D: int):
    X=abs(X)

    mostl = X - K * D
    if mostl > 0:
        print(mostl)
        exit()

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

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

    print(ans)

    return


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
ans = []
a = math.floor(abs(x) / d)
if a > k:
  a = k
  if x > 0:
    ans = (abs(x - d * k))
  else:
    ans = (abs(x - d * k))
else:
  if x > 0:
    tmp = (abs(x - d * a))
    if (k - a) % 2 == 1:
      ans = (abs(x - d * (a + 1)))
    else:
      ans = tmp
  else:
    tmp = (abs(x + d * a))
    if (k - a) % 2 == 1:
      ans = (abs(x - d * (a + 1)))
    else:
      ans = tmp
print (ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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



if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

if x==0:
    if k%2==1:
        y=d
    else:
        y=0

elif x>0:
    x1=x
    x2=x-d
    while 1:
        x1=x2
        x2=x2-d
        k=k-1
        if k==0:
            y=x1
            break
        if x2<=0:
            if k%2==1:
                y=x2
            else:
                y=x1
            break

else:
    x1=x
    x2=x+d
    while 1:
        x1=x2
        x2=x2+d
        k=k-1
        if k==0:
            y=x1
            break
        if x2<=0:
            if k%2==1:
                y=x2
            else:
                y=x1
            break

print(abs(y))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
for k in range(K):
    if X == D/2 or X == D:
        break
    if X >= 0:
        X = X - D
    else:
        X = X + D
print(abs(X))