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

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

ans = 0
if x == 0:
    if k%2 == 0:
        ans = 0
    elif k%2 == 0:
        ans = abs(d)
else :
    if abs(x) - k*d >= 0:
        ans = x - k*d
        #if abs(x) - k*d == 0:
            #if k - (abs(x) / (k * d)) % 2 == 0:
                #ans = 0
            #else:
                #ans = abs(x)
    elif abs(x) - k*d < 0:
        if  abs(x) - d*(abs(x) // d) != 0:
            if (k - (math.ceil(abs(x) / d) )) % 2 == 1:
                ans = abs(abs(x) - d*(abs(x) // d))
            else:
                ans = (abs(x) - d*(abs(x) // d + 1))
        else:
            if (k - (math.ceil(abs(x) // d) ) ) % 2 == 1:
                ans = d
            else:
                ans = 0
print(ans)

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 n < 1000:
    n=0
if x > 0:
    for i in range(k):
        if i == 0 and n!=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 and n!=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())
import math
if x>=0:
    print(int(min(math.fabs(x-k*d),math.fabs(x - (k-1)*d + d))))
else:
    print(int(min(math.fabs(x+k*d),math.fabs(x + (k-1)*d - d))))

Submission status: Wrong Answer

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

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

alpha = X%D
count = X//D
if(alpha>abs(alpha-D)):
  count +=1
  alpha = abs(alpha-D)
  
beta = count%2
if beta==1:
  print(abs(alpha-D))
else:
  print(alpha)

Submission status: Wrong Answer

Solution code:
nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]


current = abs(current)
if num%2 == 1:
    current -= move

res = current / move
if res < num:
    tmp = current - res*move
    if abs(tmp) > abs(tmp-move):
        print(abs(tmp-move))
    else:
        print(tmp)
else:
    print(current - num*move)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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 x == d and k % 2 == 1:
    print(0)
elif k % 2 == 0:
    print(min(abs(tmp1), abs(tmp2)))
else:
    print(max(abs(tmp1), abs(tmp2)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 21:49:56 2020

@author: naoki
"""

X,K,D = map(int, input().split()) 
Xmin = abs(X) # x座標の絶対値の最小値(後に更新する)
X2 = 0 
# K 回後のXの座標の値は、 X_last = X + D(i - (K -i))
if X > 0:
    i = 0 
    while abs(X-D) < Xmin and i <= K:
        X = X - D
        Xmin = abs(X)
        i+=1
    if (K-i)%2 == 0:
        print(Xmin)
    else:
        print(min(Xmin, abs(X+D), abs(X-D)))
elif X < 0:
    i = 0 
    while abs(X+D) < Xmin and i <= K:
        X = X + D
        Xmin = abs(X)
        i+=1
    if (K-i)%2 == 0:
        print(Xmin)
    else:
        print(min(Xmin, abs(X+D), abs(X-D)))
else:
    if K%2 == 0 :
        print(0)
    else:
        print(D)

Submission status: Wrong Answer

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

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

genzaichi = abs(user2[0])
kaisu = user2[1]
idoryo = user2[2]

# 0になる距離を測る
soKaisu = math.floor(genzaichi / idoryo)

if soKaisu >= kaisu:
    result = genzaichi - kaisu * idoryo
else:
    niKaisu = kaisu - soKaisu
    # 偶数か奇数かで加算・減算する
    if niKaisu % 2 == 0:
        # 始めに右へ一歩進むケースと比較する
        result = genzaichi - soKaisu * idoryo
        result2 = genzaichi - (soKaisu-1) * idoryo
        if abs(result) >= abs(result2):
            result = result2

    elif niKaisu >= 2:
        result = genzaichi - (soKaisu-1) * idoryo
    else:
        result = genzaichi - (soKaisu+1) * idoryo

print(abs(result))

Submission status: Wrong Answer

Solution code:
list = list(map(int, input().split())) 
hajime=list[0]
kaisuu=list[1]
hohaba=list[2]
if hajime>=0:
  ayumi=hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime-hohaba*kaisuu
  kouho_2=hajime-hohaba*ayumi
  kouho_3=-hajime+hohaba*(ayumi+1)

  
  
  if kaisuu <= ayumi:
    print(kouho_1)
  else:
    if nokori%2==0:
      print(kouho_2)
    else:
      print(kouho_3)
      
if hajime<0:  
  ayumi=-hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime+hohaba*kaisuu
  kouho_2=hajime+hohaba*ayumi
  kouho_3=-hajime-hohaba*(ayumi+1)
  if kaisuu<=hohaba:
    
    print(kouho_1)  
  else:
    if nokori%2==0:
      print(kouho_2)
    else:
      print(kouho_3)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
i=0
count=0
if X<0:
	if X+K*D<=0:
		print(abs(X+K*D))
		count+=1

	else:
		s=int(X/D)
		if abs(X+s*D)>abs(X+(s+1)*D):
			s+=1
			L=abs(X+s*D)
		else:
			L=abs(X+s*D)
else:
	if X-K*D>=0:
		print(abs(X-K*D))
		count+=1

	else:
		s=int(X/D)
		if abs(X-s*D)>abs(X-(s+1)*D):
			s+=1
			L=abs(X-s*D)
		else:
			L=abs(X-s*D)
            
if count==0:
	kaisuu=K-s
	if kaisuu%2==0:
		print(abs(L))

	else:
		a=abs(L+D)
		b=abs(L-D)
		if a<=b:
			print(a)
		else:
			print(b)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

init = 10**16
count =0
if abs(x)%abs(d)>k:
    print(abs(x)-abs(d)*k)
    exit()

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ans_1 = x%d #プラスの最小
ans_2 = abs(ans_1-d) #マイナスの最小

if x//d > k:
    print(x-(d*k))
    exit()
    
if ans_1 > ans_2:
    if (k-(x//d)-1)%2 == 0:
        print(ans_2)
    else:
        print(ans_1)

elif ans_1 < ans_2:
    if (k-(x//d))%2 == 0:
        print(int(ans_1))
    else:
        print(int(ans_2))

elif ans_1 == ans_2:
    print(ans_1)

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 k*d <= x:
    print(x - k*d)
  else:
    a = x // d
    x -= a*d
    if x == 0:
      if (k-a)%2 ==0:
        print(0)
      else:
        print(d)
    else:
      x1 = x
      x2 = x - d
      if (k-a-1)%2 ==0:
        print(abs(x2))
      else:
        print(abs(x1))
elif x < 0:
  if -k*d <= x:
    print(x + k*d)
  else:
    a = x // -d
    x += a*d
    if x == 0:
      if (k-a)%2 ==0:
        print(0)
      else:
        print(d)
    else:
      x1 = x
      x2 = x + d
      if (k-a-1)%2 ==0:
        print(abs(x2))
      else:
        print(abs(x1))

Submission status: Wrong Answer

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 10
    else :
        limit = 11
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(X + K * D)
    elif X == K * D :
        print("0")
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            if (val[i+1] == val[i-1]) and i > limit :
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
ma = X + D*K
mi = X - D*K
 
if X > 0:
  ans = min(abs(mi),abs(X))
  dk = D*K
  ok = X
  ng = mi
  while dk > 1:
    dk //= 2
    mid = X - dk
    if ans < abs(mid):
      pass
    else:
      ans = abs(mid)
  print(ans%(10**9+7))
elif X < 0:
  ans = min(abs(ma),abs(X))
  dk = 0
  ok = ma
  ng = X
  while dk > 1:
    dk += (D*K)
    mid = X + dk
    if ans < abs(mid):
      pass
    else:
      ans = abs(mid)
  print(ans%(10**9+7))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    x, k, d = i_map()

    amari = x % d
    shou = x // d

    if k % 2 == 0:
        if amari == 0:
            if shou % 2 == 0:
                if shou <= k:
                    print(0)
                    exit()
                else:
                    print(abs(x-k*d))
                    exit()

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

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

    if shou <= k:
            print(abs(x-shou*d))
            # print('a')
    else:
        print(abs(x-k*d))  
        # print('b')



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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 = X
        else:
            ans = abs(X - D * is_pos)
            div + 1
        if div % 2 != K % 2:
          ans = abs(ans - D)
    print(abs(ans))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

k = round(X / D)

if k < K:
    if k == 1:
        print(abs(abs(X) - K * D))
    else:
        print(min(abs(abs(X) - K * D), abs(abs(X) - (K - 1) * D)))

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

    else:
        print(min(abs(abs(X) - (k + 1) * D), abs(abs(X) - (k - 1) * D), abs(abs(X) - (k + 3) * D), abs(abs(X) - (k - 3) * D)))

Submission status: Wrong Answer

Solution code:
def resolve():
	import itertools
	X, K, D = map(int, input().split())
	if X == D:
		if K%2 == 0:
			res = K
		else:
			res = 0
	else:
		res = X*10
		for i in range(2**K):
			W = [-1] * K
			for j in range(K):
				if ((i << j) & 1):
					W[K-1-j] = 1
				res = min(res, abs(X + sum(W)*D))
	print(res)
resolve()

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 rest == 0 and K % 2 == 0:
    tmp = X
  elif quotient == 1 and rest == 0 and K % 2 != 0:
    tmp = X - D
  elif rest == X and K % 2 == 0:
    tmp = X
  elif rest == X 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 = list(map(int, input().split()))
if x==d:
  pass
else:
  for i in range(k):
    if abs((x-k))<=abs((x+k)):
      x=x-d
    else:
      x=x+d
print(abs(x))

Submission status: Wrong Answer

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

x = abs(x)
ans = 0
syo = x//d
amari = x%d

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def calc(X, K, D):

    while(abs(X) >= D):
        X -= D
        K -= 1
        if K == 0:
            return(abs(X))

    retry = K % 2
    if retry == 1:

        minu = X - D
        plus = X + D
        if abs(minu) <= abs(plus):
            return abs(minu)
        else:
            return abs(plus)
    else:
        return abs(X)


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

    print(end)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

genzaichi = abs(user2[0])
kaisu = user2[1]
idoryo = user2[2]

# 0になる距離を測る
soKaisu = math.floor(genzaichi / idoryo)

if soKaisu >= kaisu:
    result = genzaichi - kaisu * idoryo
else:
    niKaisu = kaisu - soKaisu
    # 偶数か奇数かで加算・減算する
    if niKaisu % 2 == 0:
        # 始めに右へ一歩進むケースと比較する
        result = genzaichi - soKaisu * idoryo
        result2 = genzaichi - (soKaisu-1) * idoryo
        if abs(result) > abs(result2):
            result = result2

    elif niKaisu >= 2:
        result = genzaichi - (soKaisu-1) * idoryo
    else:
        result = genzaichi - (soKaisu+1) * idoryo

print(abs(result))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
kk=(abs(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 = 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))))
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:
def isEven(num):
  if num%2 == 0:
  	return True
  return False

def isOdd(num):
  if num%2 == 1:
    return True
  return False

if __name__ == "__main__":
  x,k,d = map(int, input().split())
  div = x//d
  cmp = x
  defo = x
  if isOdd(k):
    if x > d:
      if k%div != 0:
        div = min(div,k%div)
      for i in [d,-d]:
        cmp = min(cmp,abs(defo+div*i))
      print(cmp)
    else:
      for i in [d,-d]:
        cmp = min(cmp,abs(defo+i))
      print(cmp)
  if isEven(k):
    if x > d:
      if k%div != 0:
        div = min(div,k%div)
      for i in [d,-d]:
        cmp = min(cmp,abs(defo+div*i))
      print(cmp)
    else:
      print(cmp)

Submission status: Wrong Answer

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

num = X - K*D
if 0<num:
  print(num)
  sys.exit(0)

num = X - (X//D)*D
print(min(-(num-D), num))

Submission status: Wrong Answer

Solution code:
def Qc():
    x, k, d = map(int, input().split())
    x = abs(x)
    all_minus = abs(x) - (k * d)
    if 0 <= all_minus:
        ans = all_minus
    else:
        point = abs(all_minus) // d
        k2 = k - point
        all_minus += d * point
        ans = abs(all_minus)
        if k2 % 2 != 0:
            ans = min(abs(all_minus - d), abs(all_minus + d))
    print(ans)


if __name__ == '__main__':
    Qc()

Submission status: Wrong Answer

Solution code:
a = [int(i) for i in input().split()]
b = abs(a[0] // a[2])
c = a[1] - b
l = []
if c < 0:
  if a[0] >= 0:
    print(a[0] - a[1] * a[2])
  else:
    print(a[0] + a[1] * a[2])
else:
  if c % 2 == 0:
    if a[0] >= 0:
      l = [abs(a[0] - b * a[2]), abs(a[0] - (b + 2) * a[2]), abs(a[0] - (b - 2) * a[2])]
      print(min(l))
    else:
      l = [abs(a[0] + b * a[2]), abs(a[0] + (b + 2) * a[2]), abs(a[0] + (b - 2) * a[2])]
      print(min(l))
  else:
    if a[0] >= 0:
      if abs(a[0] - (b + 1) * a[2]) < abs(a[0] - (b - 1) * a[2]):
        print(abs(a[0] - (b + 1) * a[2]))
      else:
        print(abs(a[0] - (b - 1) * a[2]))
    else:
      if abs(a[0] + (b + 1) * a[2]) < abs(a[0] + (b - 1) * a[2]):
        print(abs(a[0] + (b + 1) * a[2]))
      else:
        print(abs(a[0] + (b - 1) * a[2]))

Submission status: Wrong Answer

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

times_in = abs(X)//D
times_out = (abs(X)//D) + 1

if X>0:
    if X - (K * D) < 0:
        if times_in%2==0:
            if K%2 == 0:
                ans = min(abs(X - (times_in * D)),abs(X - ((times_in + 2) * D)))
            else:
                ans = min(abs(X - (times_out * D)), abs(X - ((times_out - 2) * D)))
        else:
            if K%2 == 0:
                ans = min(abs(X - (times_out * D)),abs(X - ((times_out - 2) * D)))
            else:
                ans = min(abs(X - (times_in * D)), abs(X - ((times_in + 2) * D)))
    else:
        ans = X - (K * D)

if X<0:
    if X + (K * D) > 0:
        if times_in%2==0:
            if K%2 == 0:
                ans = min(abs(X + (times_in * D)),abs(X + ((times_in + 2) * D)))
            else:
                ans = min(abs(X + (times_out * D)), abs(X + ((times_out - 2) * D)))
        else:
            if K%2 == 0:
                ans = min(abs(X + (times_out * D)),abs(X + ((times_out - 2) * D)))
            else:
                ans = min(abs(X + (times_in * D)), abs(X + ((times_in + 2) * D)))
    else:
        ans = X + (K * D)

if X==0:
    if K%2==0:
        ans = 0
    else:
        ans = D

print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
distance = k * d
distance_list = []
result_list = []

while distance >= 0:
  distance_list.append(distance)
  distance -= d * 2
  
for i in distance_list:
  result_list.append(abs(x - i))

result_list.sort()

print(distance_list)
print(result_list[0])

Submission status: Wrong Answer

Solution code:
now2,kaisu,power = map(int,input().split())
now=abs(now2)
if abs(now)-kaisu*power>=0:
    print(abs(now)-kaisu*power)
else:
    n=now/power
    sein=round(n)
    if (kaisu+sein)%2==0:
        print(abs(now-power*sein))
    if (kaisu+sein)%2!=0:
        Q=(min(abs(now-power*(sein-1)),abs(now-power*(sein+1))))

Submission status: Wrong Answer

Solution code:
x, y, z = list(map(int, input().split()))
count=0
if(z<0):
    z=-z
for i in range(0,y):
    if(x>0):
        x=x-z
    count+=1
    if(x<0):
        x=-x
    if(x<=z/2):
        break
print(x)

Submission status: Wrong Answer

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

c = X

if c > 0:
  if c//D > K:
    c = c - D * K
    print(c)
    exit()
  else:
    c = c - D * (X//D)
    if (K - X//D) % 2 == 0:
      print(c)
      exit()
    else:
      print(min(abs(c+D),abs(c-D)))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
flag = True
res = abs(X)%D
r = abs(X)//D

if abs(X)-K*D>=D:
  ans = abs(abs(X)-K*D)
  flag = False
else:
  ans = X

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

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

print(abs(x))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
kk=(abs(X)//D)
#print(kk)
l=abs(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=map(int,input().split())

ans=X

a=abs(X)//D

"""
for i in range(a):
    ans=min(abs(ans-D),abs(ans+D))
    
"""

if (K>a>0 or -K<a<0) and (K-abs(a))%2==0:
    ans=min(abs(X-(a)*D),abs(X+(a)*D))
    
elif (K>a>0 or -K<a<0) and (K-abs(a))%2==1:
    ans=min(abs(X-(a+1)*D),abs(X+(a+1)*D))
elif a>=K:
    ans=abs(X-K*D)
else:
    ans=abs(X+K*D)
    


print(ans)

Submission status: Wrong Answer

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

isReturn = False
i = 0
while i < k:
    if(x >= 0):
        x -= d
    else:
        x += d
        isReturn = True

    if i+1 < k:
        if isReturn:
            if((k-i) % 2 == 0):
                i = k-2
            else:
                i = k-1

    i += 1

print(abs(x))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if k<=x//d:
    print(x-k*d)
else:
    k=k-x//d
    x=x%d
    cand=[x]
    if k==0:
        print(x)
        exit()
    #print(x,x-d)
    if abs(x)<=abs(x-d):
        par=k%2
        if par==0:
            print(x)
        else:
            print(abs(x-d))
    else:
        k-=1
        par=k%2
        if par==0:
            print(abs(x-d))
        else:
            print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
abs_x = abs(X)
hikukazu = abs(X) // D
count = K - (hikukazu)
if K > hikukazu:
    if count % 2 == 0:
        hikukazu = hikukazu
    else:
        hikukazu = hikukazu + 1
else:
    hikukazu = K


for i in range(hikukazu):
    
    abs_x = abs_x - D

    # print(X)

print(abs_x)

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:
#  print("koko")
  L = L % 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(L + D)
    exit()
  else:
    print(abs(L))
    exit()

Submission status: Wrong Answer

Solution code:
import math;

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


a = math.floor(x / d);
if x < 0:
    a = a * (-1);

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

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

if abs(temp) > K:
    temp = K

temp = D * temp

print(abs(X - temp))