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 = [int(x) for x in input().split()]

if abs(X) >= K * D:
    print(abs(X) - K * D)
else:
    K = K - int(abs(X) / D)
    print(K)
    X = int(abs(X) % D)
    print(X)
    if X >= int(D / 2):
        if K % 2 == 0:
            print(X)
            print("no")
        else:
            print(D - X)
    else:
        if K % 2 != 0:
            print(D - X)
        else:
            print(X)

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)
	exit(0)
t=abs(x//d)
if (k<=t):
	if (x>0):
		print(x-k*d)
	else:
		print(x+k*d)
	exit(0)
else:
	if (x>0):
		x=x-t*d
		k-=t
		if k%2==0:
			print(x)
		else:
			print(abs(x-d))
	else:
		x=x+t*d
		k-=t
		if k%2==0:
			print(abs(x))
		else:
			print(abs(x+d))

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
    #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:
import sys
def solve():
    X, K, D = list(map(int, input().split()))

    if(D > abs(X)):
        ans = min(abs(X), -abs(X)+D)
        print(ans)
    elif(abs(X) - D*K >= 0):
        ans = abs(X) - D*K 
        print(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 = tmp - D
            print(abs(ans))



if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
ai=list(map(int,input().split()))
s = ai[0]
c = int(0)
n = int(ai[1])
for i in range(n):
  if abs (s + ai[2]) < abs(s - ai[2]):
    s += ai[2]
  else:
    s += -ai[2]
print(s)

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)

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

Submission status: Wrong Answer

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

    if(D > abs(X)):
        ans = min(abs(X), abs(X)-D)
        print(ans)
    elif(abs(X) - D*K >= 0):
        ans = abs(X) - D*K 
        print(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 = tmp - D
            print(abs(ans))



if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
syo = X // D
amari = X % D
#print(syo, amari)
if syo <= D:
    another = abs(amari - D)
    #print(amari, another)
    if (K - syo) % 2 == 0:
        print(amari)
    else:
        print(abs(another))
else:
    print(X - K * D)

Submission status: Wrong Answer

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

if X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(abs(D))
    exit(0)

if X > 0:
    k = X - K * D
    if k >= 0:
        print(k)
    else:
        kouho1 = X % D
        kouho2 = abs(kouho1 - D)
        sho = X // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)
if X < 0:
    k = X + K * D
    if k <= 0:
        print(k)
    else:
        kouho1 = (-X) % D
        kouho2 = abs(kouho1 - D)
        sho = (-X) // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)

Submission status: Wrong Answer

Solution code:
def WalkingTakahashi():
    
    data = list(map(int,input().split()))
    
    position = data[0]

    print(data[1]%2)
    
    if data[0] == data[2] and data[1]%2 == 0:
        print(data[2])
    
    elif data[0] == data[2] and data[1]%2 == 1:
        print(0)
        
    else:
        for i in range(data[1]):
            
            if abs(position + data[2]) < abs(position - data[2]):
                position = position + data[2]
                
            else:
                position = position - data[2]
                
        print(abs(position))
        
if __name__ == '__main__':
    WalkingTakahashi()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect
x,k,d=map(int,input().split())
if x<0 and x+d*k<=0:
    print(abs(x+d*k))
elif x>0 and x-d*k>=0:
    print(x-d*k)
elif x<0:
    i=math.ceil((-1*x)/d)-1
    if k-(i+1)%2==0:
        print(x+d*(i+1))
    else:
        print(abs(x+d*i))
elif x>0:
    i=math.ceil(x/d)-1
    if k-(i+1)%2==0:
        print(abs(x-d*(i+1)))
    else:
        print(x-d*i)
elif x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

Submission status: Wrong Answer

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

    if(D > abs(X)):
        if(D > abs(X)):
            if(abs(abs(X) - D) > abs(X)):
                ans = abs(X)
            else:
                ans = abs(abs(X) - D)
        print(ans)
        sys.exit()
    elif(abs(X) - D*K >= 0):
        ans = abs(X) - D*K 
        print(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 = tmp - D
            print(abs(ans))



if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

m=[]

for i in range(1,k+1):
    n=abs(d*k+x-(2*i*d))
    m.append(n)
    
print(min(m))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
# print(f"{x} {k} {d}")

chi_count, mod = divmod(x, d)
# print(f"{chi_count} {mod}")

if chi_count > k:
    ofuku_count = 0
    chi_count = k
else:
    ofuku_count = k - chi_count
    if ofuku_count % 2 != 0:
        ofuku_count -= 1
        chi_count += 1

# print(ofuku_count)
print(abs(x - (d * chi_count)))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if K <= 10:
    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
    
    c = 0
    while flag:
        
        c += 1
        if c < 1000:
        
            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))
        
        else:
            
            it = it - 500
            for z in range(1000):
                
                if (it + z) > (K+1) or (it + z) < 0:
                    continue
                tmp_result = abs((X - K * D) + (2* (it + z) * D)) 
                
                if tmp_result < result:
                    result = tmp_result
            flag = False
            
                
    print(result)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int,input().split())
if x < 0: x = -x
mv = x // d
if mv >= k:
    # なるべく0に近づけば良い
    print(abs(x)-d*k)
else:
    nearest = x % d
    if mv % 2 == 1:
        print(abs(nearest - d))
    else:
        print(nearest)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
ans=0
u=x//d
v=x%d
if (k-u)%2==0 and (k-u)>0:
  print(v)
else:
  while k>0:
    if x>0:
      x=x-d
      k-=1
    else:
      x=x+d
      k-=1
  print(abs(x))

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int, input().split())
if K == 1:
    print(min(abs(X+D), abs(X-D)))
    sys.exit(0)
if K % 2 == 0:
    for _ in range(2):
        if abs(X+D) > abs(X-D):
            X -= D
        else:
            X += D
    print(abs(X))
else:
    one = min(abs(X+D), abs(X-D))
    for _ in range(3):
        if abs(X+D) > abs(X-D):
            X -= D
        else:
            X += D
    print(min(one, abs(X)))

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

Submission status: Wrong Answer

Solution code:
x,n,d=map(int, input().split())
opt=x//d
if (n-opt)%2==0 and opt<n:
  print(x-(d*opt))
elif opt>=n:
  print(x-(d*n))
else:
  a=x-(d*opt)
  a=d-a
  print(a)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
if K%2==0:
  if abs(2*D)>=abs(X):
    print(X)
  else:
    p=K*D
    q=abs(X-p)
    r=min(q%D,abs(q%D-D))
    print(r)   
else:
  if K==1:
    print(min(X,min(X+D,X-D)))
  else:
    if abs(D)>=abs(X):
      print(X)
    else:
      p=K*D
      q=abs(X-p)
      r=min(q%D,abs(q%D-D))
      print(r)

Submission status: Wrong Answer

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

if x>0 and x-k*d<0:
    tem = int(x/d)
    if (x-tem*d)>abs(x-d*(tem+1)):
        print(abs(x -  d*(tem + 1)))

    else:
        print(x - d*tem)

else:
    print(x-k*d)

Submission status: Wrong Answer

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

if X > K * D:
    position = abs(X)
    print(X - (K * D))

elif X % D == 0:
    print(D)
else:
    position = abs(X)
    count = 0

    while(position > 0):
        count = count + 1
        position = position - D

    remain = K - count
    if remain % 2 == 0:
        print(abs(position))
    else:
        position = position + D
        print(abs(position))

Submission status: Wrong Answer

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

def binary_search(asset):
    last_r = 0
    last = (asset["R"] - asset["L"]) * d + x
    min_ = abs(last)

    mid = asset["R"] + (k - asset["R"]) // 2 + asset["R"] % 2
    asset["R"] = mid
    asset["L"] = k -mid

    now = (asset["R"] - asset["L"]) * d + x

    while abs(last_r - mid) > 0 and abs(last) != abs(now) :
        if min_ > abs(now):
            min_ = abs(now)
        if abs(last_r - mid) == 1:
            return min_
        last = now
        last_r = mid
        assert asset["L"] + asset["R"] == k
        if now == 0:
            return now
        if abs(now) > abs(last):
            mid = asset["R"] - ((k - asset["R"]) // 2 + asset["R"] % 2)
            asset["R"] = mid
            asset["L"] = k -mid

        else:
            mid = asset["R"] + ((k - asset["R"]) // 2 + asset["R"] % 2)
            asset["R"] = mid
            asset["L"] = k -mid
        now = (asset["R"] - asset["L"]) * d + x
        
    return min_

#最初はL が K個
asset = {"L": k, "R": 0}
print(binary_search(asset))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


while distance >= 0:
  distance -= d * 2
  if result > abs(x - distance):
    result = abs(x - distance)
    
print(result)

Submission status: Wrong Answer

Solution code:
def cumsum(x):
    return list(np.cumsum(x))


def cumsum_number(N, K, x):
    ans = [0] * (N - K + 1)
    number_cumsum = cumsum(x)
    number_cumsum.insert(0, 0)
    for i in range(N - K + 1):
        ans[i] = number_cumsum[i + K] - number_cumsum[i]
    return ans

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

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:
                if times_in+2 <= K:
                    ans = min(abs(X - (times_in * D)),abs(X - ((times_in + 2) * D)))
                else:
                    ans = abs(X - (times_in * 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:
                if times_in+2 <= K:
                    ans = min(abs(X - (times_in * D)), abs(X - ((times_in + 2) * D)))
                else:
                    ans = abs(X - (times_in * D))
    else:
        ans = X - (K * D)

if X<0:
    if X + (K * D) > 0:
        if times_in%2==0:
            if K%2 == 0:
                if times_in + 2 <= K:
                    ans = min(abs(X + (times_in * D)),abs(X + ((times_in + 2) * D)))
                else:
                    ans = abs(X + (times_in * 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:
                if times_in + 2 <=K:
                    ans = min(abs(X + (times_in * D)), abs(X + ((times_in + 2) * D)))
                else:
                    ans = abs(X + (times_in * D))
    else:
        ans = X + (K * D)

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

print(ans)











"""
int #整数
float #小数
math.log() 自然対数
math.log(x,y) yを底とするxの対数
#リスト
list0 = [0]*10 #[0,0,0,0,0,0,0,0,0,0]
list1= [i for i in range(10)] #>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
number=[int(input()) for i in range(int(input()))] #複数行の入力をリストに
list1 = list1 + [6, 7, 8] #リストの追加
list2.append(34) #リストの追加　最後に（）内が足される
list.insert(3,10) #リストの追加　任意の位置（追加したい位置,追加したい値）
list5.count(2) #リスト(文字列も可）内の2の数
list1.sort() #並び替え小さい順
list1.sort(reverse=True)  #並び替え大きい順
set(list1) #list1内の重複している要素を削除し、小さい順に並べる

number=Counter(list) #listを値（文字）ごとにカウントし辞書型で返す [1:4,2:5 …]

text1 = text1.replace("34","test") #text1内の"34"を"test"に書き換える(文字列内の文字を入れ替える)
sorted(S) #Sを辞書順にソート
exit(0) #強制終了

cumsum(リスト) #累積和をリストで返す
cumsum_number(N,K,x) #N個の数が含まれるリストx内のK個からなる部分和をリストで返す

alphabet={0:"A",1:"B",2:"C",3:"D",4:"E",5:"F",6:"G",7:"H",8:"I",9:"J",
        10:"K",11:"L",12:"M",13:"N",14:"O",15:"P",16:"Q",17:"R",18:"S",19:"T",
        20:"U",21:"V",22:"W",23:"X",24:"Y",25:"Z",}
print(*S,sep="") #リストSを1つずつ出力

math.floor() #切り捨て
math.ceil() #切り上げ
chr(X) ユニコードXの文字
ord("X") Xのユニコード　A == 65

"""

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

if flag == 1 and X - K * D > 0:
    print(X - K * D)
    sys.exit()
elif flag == -1 and X + K * D < 0:
    print(X + K * D)
    sys.exit()

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

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

elif X > 0 and X - D * K > 0:
    print(X - D * K)

elif X < 0 and X + D * K < 0:
    print(X + D * K)

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

elif X == 0 and K % 2 == 0:
    print(0)

elif X == 0 and K % 2 != 0:
    print(D)

Submission status: Wrong Answer

Solution code:
import sys
import math


def read_input_line():
    return sys.stdin.readline().strip()


def task(X, K, D):
    if_steps = abs(X) / D
    if K <= if_steps:
        return abs(X) - K * D
    a = math.floor(if_steps + 1)
    b = math.ceil(if_steps - 1)
    if (K - a) % 2 == 0:
        return abs(abs(X) - a * D)
    else:
        return abs(abs(X) - b * D)


def main():
    [X, K, D] = [int(s) for s in read_input_line().split(' ')]
    result = task(X, K, D)
    print(result)


main()

Submission status: Wrong Answer

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

ans = 1000000000000007
prev = 1000000000000007
if X + D*K < 0:
  print(X+D*K)
  exit()

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


if K % 2 == 0:

  i = K // 2
  for k in range(0, i+1):
    mn = abs(X + k * D * 2)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

  prev = 1000000000000007
  for k in range(0, i+1):
    mn = abs(X - k * D * 2)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

else:
  for k in range(1, K+1, 2):
    mn = abs(X + k * D)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

  prev = 1000000000000007
  for k in range(1, K+1, 2):
    mn = abs(X - k * D)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

print(ans)

Submission status: Wrong Answer

Solution code:
a=input().strip().split(" ")
x=int(a[0]);
k=int(a[1]);
d=int(a[2]);
while k!=0:
    if abs(x+d)>abs(x-d):
        x=x-d
    else:
        x=x+d
    k=k-1;
print(x)

Submission status: Wrong Answer

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

i = 0

if(X <= D):
    if(K % 2 == 0):
        if(X > 0):
            X = abs(X - D)
        else:
            X = abs(X + D)
    else:
        X = abs(X - D)
else:
    while(i < K):
        if(abs(X + D) < abs(X - D)):
            X = abs(X + D)
        else:
            X = abs(X - D)
    
        i += 1

print(X)

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
x=a//c
temp=abs(x)
if(temp>b):
    temp=temp-b
    if(a%c==0):
        if(temp>b):
            print(a-(b*c))
        else:
            print(temp*c)
    else:
        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())
r = x//d
s = 0
ans = 0
if r>k:
    ans = abs(abs(x)-abs(k*d))
else:
    l = (k-r)%2
    if l==0:
        ans = abs(abs(x)-abs(d*r))
    else:
        ans = abs(abs(x)-abs(d*(r+1)))
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if x>0 and x-k*d<0:
    tem = int(x/k)
    if (x-tem)>abs(x-(tem+1)):
        print(abs(x -  d*(tem + 1)))

    else:
        print(x - d*tem)

else:
    print(x-k*d)

Submission status: Wrong Answer

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

y = x//d

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

isReturn = False
i = 0

l = math.floor(x / d)
k=k-l

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

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

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

if x >= k * d:
    print(x - k * d)
elif x == 0 and k % 2 == 0:
    print(0)
elif x == 0 and k % 2 == 1:
    print(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:
x,k,d=map(int,input().split())
x2=None

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

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

else:
    s=x+(k*d)
    if s<=0:
        y=s
    else:
        x1=x
        x2=x+d
        while 1:
            x1=x2
            x2=x2+d
            k=k-1
            if x2>=0:
                if k%2==1:
                    y=x2
                else:
                    y=x1
                break

print(abs(y))

Submission status: Wrong Answer

Solution code:
from math import floor as f
x, k, d = map(int, input().split())
x = abs(x)
if x-k*d >= 0:
    print(x-k*d)
else:
    if d > x:
        if k == 1:
            print(abs(x-d))
        elif k == 2:
            print(x)
        else:
            print(min(abs(x-d), d))
    else:
        if k % 2 == f(x/d) % 2:
            if x-d*(x//d) > abs(x-d*(x//d))-d:
                print(abs(x-d*f(x/d)-d))
            else:
                print(x-d*f(x/d))
        else:
            print(min(abs(x-d*f(x/d+d)), abs(x-d*f(x/d)-d)))

Submission status: Wrong Answer

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

if abs(x) > k*d:
    print(abs(x) - k*d)
elif abs(x) == k*d:
    print("0")
else:
    a = abs(x) // d
    z1 = abs(x) - a*d
    z2 = abs(x) - (a+1)*d
    if z1 == 0 and (k-a) % 2 == 0:
        print("0")
    elif z1 == 0 and (k-a) % 2 == 1:
        print(abs(z2))
    else:
        b = min(abs(z1), abs(z2))
        print(b)

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(mini)
    else:
        print(abs(mini-d))
else:
    if cnt%2 == 0:
        print(mini)
    else:
        print(abs(mini-d))

Submission status: Wrong Answer

Solution code:
xkd = input('').split()
x = int(xkd[0])
k = int(xkd[1])
d = int(xkd[2])

if x > k*d:
    # 届かない場合は極力近づく
    print(x-(k*d))
elif x < k*d and x > (k-1)*d:
    # ぎりぎり０をまたげるパターン
    x1 = x - k*d
    x2 = x - (k-2)*d
    # またぐか、2つ前か、の2択
    if abs(x1) > abs(x2):
        print(abs(x2))
    else:
        print(abs(x1))
else:
    # それ以降
    # 目的地を求める
    step1 = int(x/d) # 目的地までのステップ数
    a1 = x - step1*d # 目的地
#    print("step1:{} a1:{}".format(step1,a1))
    # 0を突き抜けたほうが目的地のケース
    if abs(a1) > abs(a1-d):
        a1 = a1-d
        step1 +=1
#        print("step1:{} a1:{}".format(step1,a1))
    if (k-step1)%2 == 0:
        # 残ステップが偶数ならぴったり
        a2 = a1
    else:
        # 残ステップが奇数なら1個余分に移動
        if abs(a1-d) > abs(a1+d):
            a2 = a1+d
        else:
            a2 = a1-d
    print(abs(a2))

Submission status: Wrong Answer

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

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

if (x < d):
    if ((k % 2) == 0):
        if (x < 0):
            print(x+d)
        else:
            print(x-d)
    sys.exit()

s = x // d
a = x % d
ans = 0

if (s <= k):
    if (x < 0):
        ans = x + (d * s)
    else:
        ans = x - (d * s)

    '''
    if ((k % 2) == 0):
        if (x < 0):
            ans += d
        else:
            ans -= d
    '''

elif (s > k):
    if (x < 0):
        ans = x + (d * k)
    else:
        ans = x - (d * k)

print(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(k)
elif k == 0 or d == 0:
    print(x)
elif abs(x) - d == 0 and k % 2 == 0:
    print(x)
elif abs(x) - (k * d) == 0:
    print(0)
elif abs(x) - (k * d) >= 0:
    print(abs(x) - (k * d))
elif abs(x) - (k * d) < 0:
    i = 1
    while abs(x) - (k * d) + (d * i) < 0:
        i += 1
    print(min(abs(abs(x) - (k * d) + (d * i)), abs(abs(x) - (k * d) + (d * (i - 1)))))