Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

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

Sample Input 1

6 2 4

Sample Output 1

2

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

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

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

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

Sample Input 2

7 4 3

Sample Output 2

1

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

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

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

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


if X == 0:
    if K % 2 == 0:
        print(0)

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

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

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if K <= 1000000:
    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
    
    tmp_max_it = K+1
    tmp_min_it = 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:
                tmp_max_it = it
                if it == 4:
                    it = 3
                elif it == 3:
                    it = 2
                elif it == 2:
                    it =1
                elif it ==1:
                    it = 0
                else:
                    it = int((it - tmp_min_it)/2)

            elif result > result_p:
                tmp_min_it = it
                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((tmp_max_it- 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())
x = abs(X)
res = 0
if x <= (k*d):
    m1 = x % k
    c1 = x // k
    c2 = abs(k - c1)
    if c2%2 == 0:
        res = m1
    else:
        res = abs(m1-k)
else:
    res = abs(x - (k*d))
print(res)

Submission status: Wrong Answer

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


if X == 0:
    if K % 2 == 0:
        print(0)

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

elif X > 0:
    if 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 - D * K > 0:
        print(X - D * K)

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if K <= 10000:
    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
    
    tmp_max_it = K+1
    tmp_min_it = 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:
                tmp_max_it = it
                if it == 4:
                    it = 3
                elif it == 3:
                    it = 2
                elif it == 2:
                    it =1
                elif it ==1:
                    it = 0
                else:
                    it = int((it - tmp_min_it)/2)

            elif result > result_p:
                tmp_min_it = it
                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((tmp_max_it- 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:
a,b,c=map(int,input().split())
a1=(abs(a)-0)%c
if(a==0):
    if(b%2==0):
        print(0)
    else:
        print(c)
if(a>0):
    a2=a1-c
    ma1=int(a/c)
    ma2=ma1+1
    a3=abs(a2)
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(a-(b*c))
if(a<0):
    a2=abs(a1-c)
    ma1=((a1-(2*a1))-(a1))
    ma1=int(ma1/c)
    ma2=ma1+1
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(abs(a+(b*c)))

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:
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())
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
    
    tmp_max_it = K+1
    tmp_min_it = 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:
                tmp_max_it = it
                if it == 4:
                    it = 3
                elif it == 3:
                    it = 2
                elif it == 2:
                    it =1
                elif it ==1:
                    it = 0
                else:
                    it = int((it - tmp_min_it)/2)

            elif result > result_p:
                tmp_min_it = it
                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((tmp_max_it- 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())
X = abs(X)
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    span = X // D
    if span < K:
        if (K - span) % 2 == 0:
            print(X % K)
        else:
            print(abs(X % K - K))
    else:
        print(X - K * D)

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
x,k,d=map(int,input().split())
if k%2==1:
  if x>0:
    x-=d
  else:
    x+=d
  k-=1

a=x%(2*d)
b=a-2*d
ans=abs(x-k*d)
if abs(x-a)//d<=k:
  ans=min(ans,abs(a))
if abs(x-b)//d<=k:
  ans=min(ans,abs(b))
print(ans)

Submission status: Wrong Answer

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


if X == 0:
    if K % 2 == 0:
        print(0)

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

elif X > 0:
    if 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 - D * K > 0:
        print(X - D * K)

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    if(D > abs(X)):
        if( K % 2 == 0):
            ans = abs(X)
            print(ans)
        else:
            ans = D - abs(X)
    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:
import sys
sys.setrecursionlimit(10**9)
x, k, d=map(int, input().split(' '))
if x - k*d >= 0:
  print(x - k*d)
else:
  l=0
  for i in range(k):
    if i*d > x:
      l=i
      break
  if (k-l) % 2 == 0:
    x = -x + l*d
  else:
    x = -(l-1)*d + x
  if x < 0:
    x = -x
  print(x)

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:
                if times_out-2>=0:
                    ans = min(abs(X - (times_out * D)), abs(X - ((times_out - 2) * D)))
                else:
                    ans = abs(X - (times_out * D))
        else:
            if K%2 == 0:
                if times_out-2>=0:
                    ans = min(abs(X - (times_out * D)),abs(X - ((times_out - 2) * D)))
                else:
                    ans = abs(X - (times_out * 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:
                if times_out-2>=0:
                    ans = min(abs(X + (times_out * D)), abs(X + ((times_out - 2) * D)))
                else:
                    ans = abs(X + (times_out * D))
        else:
            if K%2 == 0:
                if times_out-2>=0:
                    ans = min(abs(X + (times_out * D)),abs(X + ((times_out - 2) * D)))
                else:
                    ans = abs(X + (times_out * 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())
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:
        if (X//D)>K:
            print(X-K*D)
        else:
            L1 = abs(X-D*(X//D))
            L2 = abs(X-D*((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:
        if ((-X)//D)>K:
            print((-X)-K*D)
        else:
            L1 = abs((-X)-D*((-X)//D))
            L2 = abs((-X)-D*(((-X)//D)+1))
            print(min(L1,L2))
else:
    if K%2==0:
        print(0)
    else:
        print(D)

Submission status: Wrong Answer

Solution code:
import math
import sys

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

num=math.floor(x/d)
n=x-(num*d)

if x==k and x==d and k==d:
  print(x)
  sys.exit()

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

Submission status: Wrong Answer

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

isReturn = False
i = 0

l = math.floor(x / d)
if(l > k):
    x -= k*d
else:
    x -= l*d
k = k-l

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

Submission status: Wrong Answer

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

if x>0 and x-k*d<0:
    if abs(x)==d:
        if k&1:
            print(0)
        else:
            print(k)
    else:

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

        else:
            print(abs(x) - d*tem)

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

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:
                if times_out-2>=1:
                    ans = min(abs(X - (times_out * D)), abs(X - ((times_out - 2) * D)))
                else:
                    ans = abs(X - (times_out * D))
        else:
            if K%2 == 0:
                if times_out-2>=1:
                    ans = min(abs(X - (times_out * D)),abs(X - ((times_out - 2) * D)))
                else:
                    ans = abs(X - (times_out * 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:
                if times_out-2>=1:
                    ans = min(abs(X + (times_out * D)), abs(X + ((times_out - 2) * D)))
                else:
                    ans = abs(X + (times_out * D))
        else:
            if K%2 == 0:
                if times_out-2>=1:
                    ans = min(abs(X + (times_out * D)),abs(X + ((times_out - 2) * D)))
                else:
                    ans = abs(X + (times_out * 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())
X = abs(X)
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    span = int(X / D)
    if span < K:
        val = min(X - span * D, abs(X - (span + 1) * D))
        print(val)
    else:
        print(X - K * 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:
X,K,D = list(map(int,input().split()))
count = K
ans = abs(X)
x = X
L = K//2
mx = X//D

if x>0:
    if mx<=K:
        x = X-mx*D
        count -= mx
    else:
        x = X-K*D
        count -= K
    
    ans = abs(x)
    while x>0 and count>0:
        x -= D
        count -= 1
        if abs(x)<ans and count%2==0:
            ans = abs(x)
elif x<0:
    if mx<=K:
        x = X+mx*D
        count -= mx
    else:
        x = X+K*D
        count -= K
    ans = abs(x)
    while x<0 and count>0:
        x += D
        count -= 1
        if abs(x)<ans and count%2==0:
            ans = abs(x)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


if X == 0:
    if K % 2 == 0:
        print(0)

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

elif X > 0:
    if 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 - D * K > 0:
        print(X - D * K)

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import exit, stdin

# import copy
# from collections import deque,Counter
# import numpy as np
input = stdin.readline

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

if abs(X) >= K * D:
    print(abs(X - K * D))
else:
    A1 = abs(X) % D
    A2 = abs(A1 - D)
    n = abs(X) // D
    if (K - n) % 2 == 0:
        print(A1)
    else:
        print(A2)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def f(x, k, d):
    if k == 0:
        return x

    tmp = abs(x) / d
    if x == 0:
        if k % 2 == 0:
            return 0
        elif k % 2 == 1:
            return d

    elif x > 0:
        if tmp > k:
            return x - d * k
        else:
            x = x - d * tmp
            k = k - tmp
            if k % 2 == 0:
                return x
            else:
                return x - d
    elif x < 0:
        if tmp > k:
            return x + d * k
        else:
            x = x + d * tmp
            k = k - tmp
            if k % 2 == 0:
                return x
            else:
                return x + d


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(abs(f(X, K, D)))

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)
        else:
            print(D - X)
    else:
        if K % 2 != 0:
            print(D - X)
        else:
            print(X)

Submission status: Wrong Answer

Solution code:
import sys
def solve():
    X, K, D = list(map(int, input().split()))
 
    if(D > abs(X)):
        ans = abs(X)
        print(ans)
        sys.exit()
    elif(abs(X) - D*K >= 0):
        ans = abs(X) - D*K 
        print(ans)
    elif(abs(X) - D*K < 0):
        if((K - (abs(X) // D)) % 2 == 0):
            ans = abs(X) - ((abs(X) // D) * D)
            print(ans)
        else:
            tmp = abs(X) - ((abs(X) // D) * D)
            ans = min(abs(tmp+D), abs(tmp-D))
            print(ans)
 
 
 
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
x=(abs(i[0]))

if x>=i[1]*i[2]:
    print(x-i[1]*i[2])
else:
    if x%i[2]<=(i[2]-x%i[2]):
        if (i[1]-x//i[2])%2 == 0:
            print(x%i[2])
        else:
            print(i[2]-x%i[2])
    else:
        if (i[1]-x//i[2])%2 == 1:
            print(x%i[2])
        else:
            print(i[2]-x%i[2])

Submission status: Wrong Answer

Solution code:
xkd = input()

xstr,kstr,dstr = xkd.split()

x = int(xstr)
k = int(kstr)
d = int(dstr)

if x >= k*d:
    print(x-(k*d))
else:
    if x <= -(k*d):
        print(x+(k*d))
    else:
        if x <= 0:
            lxl = -x
        else:
            lxl = x
        if x <= 0:
            now_x = x + (d * (int(lxl / d) + 1))
        else:
            now_x = x - (d * (int(lxl / d) + 1))
        if now_x <= 0:
            left_x = -now_x
            right_x = now_x + d
        else:
            left_x = -(now_x - d)
            right_x = now_x
        now_k = k - int(lxl / d) - 1
        if now_x <= 0:
            if now_k % 2 == 0:
                print(left_x)
            else:
                print(right_x)
        else:
            if now_k % 2 == 0:
                print(right_x)
            else:
                print(left_x)