Problem:


Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

Solution code:
import math

kouho = []
dt = input()
l = dt.split()
dt1 = [0]*3
for i in range(3):
    dt1[i] = int(l[i])

    
for i in range(-(-dt1[1]//2)):
    m1 = dt1[0]+2*(i+1)*dt1[2]
    m2 = dt1[0]-2*(i+1)*dt1[2]
    kouho.append(m1*m1)
    kouho.append(m2*m2)

print(round(math.sqrt(min(kouho))))

Submission status: Wrong Answer

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

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

min_score = abs(X)
if abs(X-D)>abs(X+D):
    traverse = True
else:
    traverse = False

if traverse:
    for i in range(K):
        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
else:
    for i in range(K):
        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:

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

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

lowest_value = X

lst_of_values = [1232141]

if X == K == D:
    if K // 2 != 0:
        K = 0
        lowest_value = X
    else:
        K = 0
        lowest_value = 0



while K != 0:

    if len(lst_of_values) > 2:
        if abs(lst_of_values[-1]) == abs(lst_of_values[-2]):
            break
    if len(lst_of_values) > 4:
        if abs(lst_of_values[-1]) == abs(lst_of_values[-3]):
            break

    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1
        lst_of_values.append(lowest_value)

    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
        lst_of_values.append(lowest_value)
    else:
        value1 = abs(lowest_value + D)
        value2 = abs(lowest_value - D)
        if value1 < value2:
            lowest_value = value1
            K -= 1
            lst_of_values.append(lowest_value)
        else:
            lowest_value = value2
            K -= 1
            lst_of_values.append(lowest_value)


print(abs(lowest_value))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = input().split()
X, K, D = int(X), int(K), int(D)
count = 0
if X < 0:
  X = -X
  L = X
else:
  L = X
if K < L // D:
  L = L - K * D
  K = 0
elif K == L // D:
  L = L % D - D
  K = 0
else:
#  print("C")
  temp = L
  L = L % D - D
  K = K - temp // D - 1
if L == 0:
  if K % 2 == 0:
    print(0)
    exit()
  else:
    print(D)
    exit()
if K == 0:
  print(abs(L))
  exit()
if abs(L) >= L + D:
  goal = L + D
  if K % 2 != 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()
elif abs(L) < L + D:
  goal = abs(L)
  if K % 2 == 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 0
tmp = 0
if X >= K*D:
  print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

Solution code:

X, K, D = map(int, input().split())
K2 = K
#X=１に近いものにおきかえる
if X >= 0:
  a= 1
else:
  a = -1
#何回引けるか
h = min(K, int(a*X/D))
K = K-h
X = X - a*D*h

print(X, K, D)
      


flg = 0
minx = abs(X)
Xnow = X
Xprepre = 0
for i in range(K):
  if i != 0:
    Xprepre = Xpre
  Xpre = Xnow #前回
  X1 = Xnow -D
  X2 = Xnow +D
  if abs(X1) < abs(X2):
    Xnow = X1
  else:
    Xnow = X2
  #繰り返したらおわり
  if Xprepre == Xnow:
    flg = 1
    if (K-i+1)%2 == 0:
      print(abs(Xnow))
    else:
      print(abs(Xpre))
    break
if flg == 0:
  print(abs(Xnow))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X = abs(X)

quotient = X // D
rest = X % D

if quotient <= K:
  if quotient == 1 and K % 2 == 0:
    tmp = X
  elif quotient == 1 and K % 2 != 0:
    tmp = X - D
  else:
    tmp = X - quotient * D
else:
  tmp = X - K * D 

print(abs(tmp))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def abs_min(current, move):
    f = current+move
    b = current-move
    if abs(f) <= abs(b):
        return f
    else:
        return b
    
nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]

for i in range(num):
    tmp = abs_min(current, move)
    if i%2==0 and abs(tmp) >= abs(current):
        break;
    current = tmp

print(abs(current))

Submission status: Wrong Answer

Solution code:
def main():
    X, K, D = list(map(int, input().split()))
    ans = 0
    is_pos = 1
    if X < 0:
        is_pos = -1
    if K * D <= X * is_pos:
        ans = abs(X - K * D * is_pos)
    else:
        div = X // D
        X = X % D
        if X < abs(X - D * is_pos):
            ans = abs(X)
        else:
            ans = abs(X - D * is_pos)
            div + 1
        if div % 2 != K % 2:
          ans = abs(X - D * is_pos)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,  K,  D = list(map(int, input().split()))
L = X + D * K
 
if L >= 0:
   if K == 1 or D == 1:
      print(abs(X - D*K))
      exit()
   if L / (2 * D) - int(L / (2 * D)) > 0.5:
      a = L // (2 * D) + 1
   else:
      a = L // (2 * D)
 
elif L < 0:
   if K == 1 or D == 1:
      print(abs(X + D*K))
      exit()
   if L / (2 * D) - int(L / (2 * D)) < -0.5:
      a = int(L / (2 * D)) - 1
   else:
      a = int(L / (2 * D)) 
print(abs(X-2*D*a+D*K))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
raw = (input().strip()).split(" ")
X = int(raw[0])
K = int(raw[1])
D = int(raw[2])

#print(X, K, D)


if abs(X) >= K * D:
    print(abs(X) - K * D)
    #print("A")

else:
    AMARI = abs(X) % D
    SHO = abs(X) // D
    if (K - SHO) == 1:
        print(abs(AMARI - D))
        #print("B")
    elif AMARI >= (D / 2):
        if (K - SHO) // 2 != 0:
            print(abs(AMARI - D))
            #print("C")
        else:
            print(AMARI)
            #print("D")

    else:
        print(AMARI)
        #print("E")

Submission status: Wrong Answer

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

if K > 0:
    if K%2 == 1:
        A = A - D

print(abs(A))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
m = 0
s = 0
if abs(X) > K * D:
    # for i in range(K):
    if X > 0:
        m = X - (K * abs(D))
    else:
        m = X + (K * abs(D))
else:
    if X == 0:
        if X % 2 == 1:
            m = D
        else:
            m = 0
        print(m)
        exit()
    else:
        if abs(X) > D:
            s = K * D % abs(X)
            if s > D:
                if K % 2 != 1:
                    m = abs(X) % abs(D)
                else:
                    m = abs(s)
            else:
                m = abs(s)     
        else:
            s = D - abs(X)
            if K % 2 != 1:
                m = abs(X)
            else:
                m = abs(s)


       

print(m)

Submission status: Wrong Answer

Solution code:
A = list(map(int, input().split()))

#print(A)

def hantei(list_1):
    x = list_1[0]
    k = list_1[1]
    d = list_1[2]
    hantei_0 = x
    hantei_1 = x
    for a in range(k):
        if hantei_0 * hantei_1 <= 0:
            break
        """
            if d % 2 == 0:
                hantei_1 = hantei_0
                break
            elif d % 2 == 1:
                break"""
        if hantei_0 > 0:
            hantei_0 = hantei_0 - d
            if abs(hantei_0) < abs(hantei_1):
                hantei_1 = hantei_0
        elif hantei_0 <= 0:
            hantei_0 = hantei_0 + d
            if abs(hantei_0) < abs(hantei_1):
                hantei_1 = hantei_0
    return hantei_1

print(hantei(A))

Submission status: Wrong Answer

Solution code:
x,k,d=list(map(int,input().split()))
syou, amari = divmod(x,d)
if x>0:
    if x-k*d==0:
        print(0)
    elif x-k*d<0:
        if amari==0:
            newk=k-syou
            if newk%2==0:
                print(0)
            else:
                print(d)
        elif amari!=0:
            newk=k-syou+1
            if newk%2==0:
                print(abs(x-d*(syou+1)))
            else:
                print(abs(x-d*syou))
    elif x-k*d>0:
        print(abs(x-k+d))
if x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 0
tmp = 0
if abs(X) >= K*D:
  if X > 0:
    print(X - K*D)  
  else:
    print(X + K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

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

if k%2 == 0:
    i = 2
    while 1:
        ans1 = min(abs(x+i*d), abs(x-i*d))
        ans2 = min(abs(x+2*i*d), abs(x-2*i*d))
        if ans1 < ans2:
            break
        i += 2
    print(ans1)
else:
    ans = min(abs(x+d), abs(x-d))
    print(ans)

Submission status: Wrong Answer

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

X = abs(X)

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

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
ans = X
minc_floor = math.floor(abs(X)/D)
minc_ceil = math.ceil(abs(X)/D)
min_kaisuu = K
# print(minc_ceil, minc_floor)
if minc_ceil <= K:
    for k in range(minc_floor, minc_ceil+1):
        for flug in [-1, 1]:
            if abs(X + flug*k*D) < abs(ans):
                ans = abs(X + flug*k*D)

                # print("ans" + str(ans))
                min_kaisuu = k
                # print(min_kaisuu)
    if (K-min_kaisuu) % 2 != 0:
        ans = min(abs(ans+D), abs(ans-D))
else:
    # ans = min(ans+K*D), abs(ans-K*D))
    if abs(ans+K*D) > abs(ans-K*D):
        ans = ans-K*D
    else:
        ans = ans+K*D
# print(min_kaisuu)
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

while k > 0 and x > 0:
    k -= 1
    x -= d

if k == 0:
    print(abs(x))
elif k == 1:
    x += d
    print(abs(x))
else:
    xd = abs(x - d)
    xdd = abs(xd - d)
    if k % 2 != 0:
        print(xd)
    else:
        print(xdd)

Submission status: Wrong Answer

Solution code:
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()
if x/d >= k:
    print(x-k*d)
else:
    if (k - int(x/d))%2 == 0:
        print(abs(x%d))
    else:
        print(abs(x%d-d))

Submission status: Wrong Answer

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

ii=int(X/D)
if K<=ii and X>0:
    X-=K*D
elif K<=ii and X<0:
    X+=K*D
elif X>0:
    X-=ii*D
elif X<0:
    X+=ii*D
if (K-ii)%2==1 and X>=0:
    X-=D
elif (K-ii)%2==1 and X<0:
    X+=D

print(abs(X))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if K <= 0:
    list_ = [abs((X - K * D) + (2* i * D)) for i in list(range(0,K+1))]
    result = min(list_)
    print(result)
    

else:
    
    it = int(K /2)
    tmp_result = abs((X - K * D) + (2* (it +1) * D))
    
    flag =True
    
    while flag:
        
        if it == 0:
            result = abs((X - K * D) + (2* 0 * D))
            flag = False
            
        elif it == (K+1):
            result = abs((X - K * D) + (2* (K+1) * D))
            flag = False
        
        
        
        result = abs((X - K * D) + (2* it * D)) 
        result_m = abs((X - K * D) + (2* (it -1) * D))
        result_p = abs((X - K * D) + (2* (it +1) * D))
        
        if result <= result_m and result <= result_p:
            flag =False
            
        if result > result_m:
            if it == 4:
                it = 3
            elif it == 3:
                it = 2
            elif it == 2:
                it =1
            elif it ==1:
                it = 0
            else:
                it = int(it/2)
                
        elif result > result_p:
            if (K+1- it) == 4:
                it = K-2
            elif (K+1- it) == 3:
                it = K-1
            elif (K+1- it) == 2:
                it =K
            elif (K+1- it) ==1:
                it = K+1
            else:
                it = it +(int((K+1- it)/2))
    print(result)

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//2)
		else:
			l -= max(1,l//2)

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10


    x,k,d = LMIIS()
    sign = -1 if x < 0 else 1
    absX = abs(x)
    if k*d <= abs(x):
        print(sign*(absX-k*d))
        return
    


    a = ceil(absX / d)
    b = floor(absX / d)
    dbg(a,b)
    best = a if  abs(absX - a*d) < abs(absX- b*d) else b
    if ((k & 1) and not (best & 1)) or not(k&1) and best & 1:
        a = best + 1
        b = best - 1
        dbg(a,b)
        best = a if  abs(absX - a*d) < abs(absX- b*d) else b

    
    print(sign*abs(absX-best*d))
     
    



    
main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

temp = abs(X) // D
if K%2 != temp%2:
    temp = temp + 1

if temp > K:
    temp = K

temp = D * temp

print(abs(X - temp))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
if X >= K * D:
  print(X - K * 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:
x, k, d = map(int, input().split())

x = abs(x)
ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
# cook your dish here
# your code goes here
a, b, c = input().split()
x=int(a)
k=int(b)
d=int(c)
val=(k*d-x)/(2*d)
fs=int(val)
# print(fs)
ans1=1000000000000000000
ans2=1000000000000000000
ans3=1000000000000000000
if fs<0:
	ans=abs(x-k*d)
elif fs==0:
	ans1=abs(x-k*d)
	ans2=abs(x-k*d+2*d)
	ans=min(ans1,ans2)
else:
    if fs-1>=0:
        ans1=abs(x-k*d+2*(fs-1)*d)
    if fs<=k:
        ans2=abs(x-k*d+2*fs*d)
    if fs+1<=k:
        ans3=abs(x-k*d+2*(fs+1)*d)
    ans=min(ans1,ans2,ans3,x)
print(ans)

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 X > D:
        span = int(X / D)
        if (K - span) % 2 == 0:
            print(int(X - span * D))
        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:
X,K,D = map(int,input().split())
if X>0:
    if X%D==0:
        if (X//D)>K:
            print(X-K*D)
        else:
            if (K-X//D)%2==0:
                print(0)
            else:
                print(D)
    else:
        L1 = abs(X-K*(X//D))
        L2 = abs(X-K*((X//D)+1))
        print(min(L1,L2))
elif X<0:
    if (-X)%D==0:
        if ((-X)//D)>K:
            print((-X)-K*D)
        else:
            if (K-(-X)//D)%2==0:
                print(0)
            else:
                print(D)
    else:
        L1 = abs((-X)-K*(X//D))
        L2 = abs((-X)-K*((X//D)+1))
        print(min(L1,L2))
else:
    if K%2==0:
        print(0)
    else:
        print(D)

Submission status: Wrong Answer

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

if x < 0:
  x = -x
print(x)

Submission status: Wrong Answer

Solution code:
X,  K,  D = list(map(int, input().split()))
L = X + D * K
 
if L >= 0:
   if K == 1 or D == 1:
      print(abs(X - D*K))
      exit()
   if L / (2 * D) - int(L / (2 * D)) > 0.5:
      a = L // (2 * D) + 1
   else:
      a = L // (2 * D)
 
elif L < 0:
   if L / (2 * D) - int(L / (2 * D)) < -0.5:
      a = int(L / (2 * D)) - 1
   else:
      a = int(L / (2 * D)) 
print(abs(X-2*D*a+D*K))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
flag = True
if abs(X)-K*D>D:
  ans = abs(abs(X)-K*D)
  flag = False
else:
  ans = X

res = abs(X)%D
r = abs(X)//D
if flag:
  if r%2 == K%2:
    ans = min(ans, res)
  else:
    ans = min(ans, abs(res-D))
  
print(ans)

Submission status: Wrong Answer

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]


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

tmp = abs(x//d)
if (tmp >= k):
    if (x>=0):
        print(abs(x-k*d))
    else:
        print(abs(x+k*d))
    exit()

if (tmp % 2 != 0 and k %2 == 0):
    tmp+=1
elif (tmp % 2== 0 and k%2 != 0):
    tmp+1

#print(tmp)

if (x>=0):
    print(abs(x-tmp*d))
else:
    print(abs(x+tmp*d))

Submission status: Wrong Answer

Solution code:
from decimal import *
x, k, d = map(Decimal, input().split())

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

Submission status: Wrong Answer

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

aX = abs(X)
if aX // D >= K:
    ans = aX - K * D
else:
    if (aX // D ) % 2 == 0:
        ans = aX % D
    else:
        ans = (aX % D) - D
    
print(abs(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys

X, K, D = [int(x) for x in input().strip().split(" ")]

X = abs(X)
k, x = divmod(X, D)

if K <= k:
    print(x)
    sys.exit(0)

X = x
K -= k

if K % 2 == 0:
    print(abs(X))
    sys.exit(0)

print(abs(X - D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 99999999999
tmp = 0
if X == 0:
  if K % 2 == 0:
    print(0)
  else:
    print(D)
  sys.exit()
if abs(X) >= K*D:
  if X > 0:
    print(X - K*D)  
  else:
    print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  if X >= 0:
    b = a - D*2
  else:
    b = a + D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  if X >= 0:
    b = a - D*2
  else:
    b = a + D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

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

print(x)

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

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

		k=k-num

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

Submission status: Wrong Answer

Solution code:
def function(X, K, D):
    kai = int(abs(X)/D)
    if (abs(X) % D == 0) and (abs(X)/D <= K):
        if K % 2 == 1:
            ans = D
        else:
            ans = X
    elif kai >= K:
        if X > 0:
            ans = X-K*D
        else:
            ans = X+K*D
    else:
        K = K-kai
        if X > 0:
            X = X-kai*D
        else:
            X = X+kai*D
        if K % 2 == 1:
            if X > 0:
                ans = X-D
            else:
                ans = X+D
        else:
            ans = X
    return abs(ans)


if __name__ == '__main__':
    X, K, D = input().split()
    X = int(X)
    K = int(K)
    D = int(D)
    print(function(X, K, D))

Submission status: Wrong Answer

Solution code:
X,  K,  D = list(map(int, input().split()))
L = X + D * K
 
if L >= 0:
   if K == 1 or D == 1:
      print(abs(X - D*K))
      exit()
   if L / (2 * D) - int(L / (2 * D)) > 0.5:
      a = L // (2 * D) + 1
   else:
      a = L // (2 * D)
 
elif L < 0:
   if L / (2 * D) - int(L / (2 * D)) < -0.5:
      a = int(L / (2 * D)) 
   else:
      a = int(L / (2 * D)) + 1
print(abs(X-2*D*a+D*K))

Submission status: Wrong Answer

Solution code:
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()
if abs(x)/d >= k:
    print(abs(x)-k*d)
else:
    if (k - int(x/d))%2 == 0:
        print(abs(x%d))
    else:
        print(abs(x%d-d))

Submission status: Wrong Answer

Solution code:
[_x, _k, _d] = list(map(int, input().split(" ")))
_x = abs(_x)

while _k > 0:
  if _x >= _d:
    _y = _x//_d
    _x = abs(_x - _d * _y)
    _k -= _y
  else:
    if _k % 2 == 1:
      _x = abs(_x - _d)
    _k = 0

print(_x)