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

Submission status: Wrong Answer

Solution code:
flag=10
x,k,d=map(int,input().split())
for i in range(k):
    if x>0:
        x-=d
        if x<=0:
            flag=-1
            break
    elif x<0:
        x+=d
        if x>=0:
            flag=1
            break
    else:
        flag=0
        break

if flag==0:
    print(0)
elif flag==-1:
    print(min(abs(x),x+d))
elif flag==1:
    print(min(abs(x-d),x))
else:
    print(abs(x))

Submission status: Wrong Answer

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

if x == 0:
    print(0)
else :
    if abs(x) > k * d:
        if x > 0:
            print(x - k * d)
        else:
            print(abs(- x + k * d))
    else:
        if x > 0:
            aaa = x // d
            a = x - aaa * d
            b = x - (aaa + 1) * d
            if abs(a) >= abs(b):
                if (k - (aaa + 1))%2 == 0:
                    print(abs(b))
                else:
                    print(abs(a))
            else:
                if (k - aaa)%2 == 0:
                    print(abs(a))
                else:
                    print(abs(b))    
        else:
            aaa = x // d
            a = - x + aaa * d
            b = - x + (aaa + 1) * d
            if abs(a) >= abs(b):
                if (k - (aaa + 1))%2 == 0:
                    print(abs(b))
                else:
                    print(abs(a))
            else:
                if (k - aaa)%2 == 0:
                    print(abs(a))
                else:
                    print(abs(b))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def minimum_abs(X, K, D):
    # initial value
    current_value = X
    for _ in range(0, K):
        if current_value > 0: # positive
            current_value -= D
        elif current_value < 0: # negative
            current_value += D
    return abs(current_value)

input_arr = list(map(int, input().rstrip().split()))
X = input_arr[0]
K = input_arr[1]
D = input_arr[2]
print(minimum_abs(X, K, D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
a = 0
b = k + 1
mid = 0
if x < 0:
    x *= -1
while b - a > 1:
    mid = (a + b) >> 1
    if x - d * mid >= 1:
        a = mid
    else:
        b = mid

ans = x - a * d
if a < k:
    y = abs(ans - d)
    if y < ans:
        ans = y
if x == 0:
    ans = d
print(ans)

Submission status: Wrong Answer

Solution code:
n,k,m = map(int,input().split())
#print(k*m,n)
if n>=0:
    if k*m > n:
        #print(min(n%m, n-(n%m)))
        x = n//m;
        left = n - x*m;
        right = abs(left - m);
        k = k - x;
        if k&1:
            print(right)
        else:
            print(left)
        
    else:
        print(n - k*m)
else:
    if k*m > n:
        #print(min(n%m, n-(n%m)))
        x = n//m;
        left = n - x*m;
        right = abs(left - m);
        k = k - x;
        if k&1:
            print(left)
        else:
            print(right)
        
    else:
        print(n + k*m)

Submission status: Wrong Answer

Solution code:
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
with decimal.localcontext() as ctx:
    ctx.prec = 150
    t=decimal.Decimal(abs(x))//decimal.Decimal(d)
    

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

k = K - (X // D)
x = X % D
while True:
    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()

Submission status: Wrong Answer

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

if x2 - k * d > 0:
    print(x2 - k * d)
    exit()

for i in range(k + 1):
    if x < 0:
        ans = min(abs(x),abs(x + d))
        i2 = i
        if i2 % 2 != k % 2:
            #print("a")
            print(abs(x))
            exit()
        else:
            #print("b")
            print(x + d)
            exit()
    x = x - d

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


x, k, d = map(int, readline().split())
if x < 0 and x + (k * d) < 0:
    print(x + (k * d))
    exit()
elif x >= 0 and x - (k * d) >= 0:
    print(x - (k * d))
    exit()

a = (x // d)
if k % 2 == 0:
    if a % 2 != 0:
        a += 1
else:
    if a % 2 == 0:
        a += 1
a *= d

if x < 0:
    print(abs(x+a))
else:
    print(abs(x-a))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
 
#偶数の場合
if K %2 == 0:
  if X == D:
    print(X)
  elif X//D < K:
    print(X%D)
  else:
    print(X%D + (X//D -K)*D)
else:
  if X == D:
    print(abs(abs(X)-abs(D)))
  if X//D < K:
    if abs(X%D-D) < abs(X%D +D):
      print(abs(X%D-D))
    else:
      print(abs(X%D +D))
  else:
    print(X%D + (X//D -K)*D)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X=abs(X)
q=X//D
mod=X%D

if q <= K:
    if (K-q)%2==0:
        ans=mod
        print("A")
    else:
        ans=D-mod
        print("B")
else:
    ans=X-K*D
print(ans)

Submission status: Wrong Answer

Solution code:
#n = int(input())
x,k,d = map(int,input().split())
#l = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
v=abs(x)//d
if abs(x)>= d:
    if abs(x)//d >= k:
        if x >= 0:
            ans=x-d*k
        else:
            ans=x+d*k
    else:
        move=v*d
        dif=k-v
        if x >0:
            #l=[]
            #l.append(x-v*d)
            x2=x-v*d
            #l.append(abs(x2-d))
            #ans=min(l)
            if dif%2==0:
                ans=x-v*d
            else:
                ans=abs(x2-d)
        else:
            x2=x+v*d
            if dif%2==0:
                ans=x+v*d
            else:
                ans=abs(x2+d)
else:
    if x>=0:
        x2=x-d
        dif=k-1
        if dif==0:
            ans=abs(x2)
        elif dif%2==0:
            ans=abs(x2)
        else:
            ans=abs(x2+d)
    else:
        x2=x+d
        dif=k-1
        if dif==0:
            ans=abs(x2)
        elif dif%2==0:
            ans=abs(x2)
        else:
            ans=abs(x2-d)
print(ans)

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 (k-d)%2 == 0:
    print(ans_2)
else:
    print(ans_1)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
count = int(X / D)#移動回数内で中央をまたげるか
if D > abs(X):
    if K % 2 == 1:
        last_place = abs(abs(X) - D)#Kが奇数の場合(済)
    else:
        last_place = abs(X)#(済)
else:
    if count <= K:#移動回数内で中央をまたげる場合
        if (K - count) % 2 == 1:#移動後残り移動回数が奇数の場合(済)
            last_place = abs(abs(X) - (count + 1) * D)
        else:
            last_place = abs(abs(X) - count * D)#(済)
    else:#(済)
        last_place = abs(abs(X) - D * K)
print(last_place)

Submission status: Wrong Answer

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

x=min(K,abs(X)//D)

if X>0:
    X_=X-(D*x)
else:
    X_=X+(D*x)

k=K-x
if k==0:
    ans=X_
elif k>1 and k%2==0:
    ans=min(X_,abs(X_-D),abs(X_+D))
else:
    ans=min(abs(X_-D),abs(X_+D))
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

import numpy as np

if X > 0 and X > K*D:
    print(X-K*D)
elif X < 0 and X < K*D:
    print(np.abs(X+K*D))
else:
    a = X // D
    b = a + 1
    if a % 2 != K % 2:
        print(np.abs(b*D - X))
    else:
        print(np.abs(X - a*D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if (x//d)<=k:
  k-=x//d
  x-=(x//d)*d

for i in range(k):
  tmp=x
  x-=d
  if abs(x) >= abs(tmp):
    if (k-i)%2==0:
      print(abs(tmp))
      exit()
    else:
      print(abs(x))
      exit()
  else:
    pass
else:
  print(abs(x))

Submission status: Wrong Answer

Solution code:
x,k,d = [int(p) for p in input().split()]
x=abs(x)
ans = min(x%d,d-x%d);
if(ans==0):
    p = (x+d-1)//d
    t = k;
    k-=p;
    if(k<0):
        print((x-t*d))
    elif(k%2==0):
        print(0)
    else:
        print(d)
elif(k*d>x):
    p = (x+d-1)//d
    t = k
    k-= p
    rem = abs(x-p*d)
    if(rem==ans):
        if(k%2==0):
            print(ans)
        else:
            print(d-rem)
    else:
        k-=1
        if(k%2==0):
            print(ans)
        else:
            print(d-rem)
else:
    print((x-k*d))

Submission status: Wrong Answer

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

t = abs(X) // D

if X < 0: sign = -1
else: sign = 1

if K < t:
    x = X - sign*K*D
else:
    x = X - sign*t*D

if (K - t) % 2 == 1:
    x = D - abs(x)

print(abs(x))

Submission status: Wrong Answer

Solution code:
import math

start, kai, step = map(int, input().split(" "))

if step < start - (kai * step):
    print(start - (kai * step))
    exit()

ans1 = start % step
ans2 = abs(ans1 - step)
if (kai - (start // step)) % 2 == 0:
    print(ans1)
else:
    print(ans2)

Submission status: Wrong Answer

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

q, mod = divmod(abs(X), abs(D))

ans = 0
if q < K:
    if mod == 0:
        if q % 2 == 1:
            ans = D
        else:
            ans = mod
    else:
        ans = mod
else:
    div = q - K
    ans = div * D + mod

print(ans)

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split())
if(a<0):
    aa=-a
else:
    aa=a
if(a>b*c):
    ans=a-b*c
    print(ans)
else:
    amari=aa%c
    amarioruta=abs(amari-c)
    if(amari==0):
        hantei=a/c
        if(hantei%2==0):       
            bb=b%2
            if(bb==0):
                print(0)
            else:
                print(c)
        else:
            bb=b%2
            if(bb==0):
                print(c)
            else:
                print(0)
    else:
        if(aa<=b*c<=aa+b):
            muri1=b*c-a
            print(muri1)
        else:
            if(amari>amarioruta):
                print(amarioruta)
            else:
                print(amari)

Submission status: Wrong Answer

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

for i in range(k):
    x=x-d
    if x>0:
        x=x-d
    elif x<0:
        x=x+d

print(abs(x))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
while k:
	prev_x, x = x, min(abs(x+d),abs(x-d))
	k -= 1
	if k and abs(prev_x-x) >= d:
		print(prev_x)
		exit()
print(abs(x))

Submission status: Wrong Answer

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

def main():
    X, K, D = map(int, input().rstrip().split())
    c = abs(X) // D
    ans = 0
    if c >= K:
        if X >= 0:
            ans = X - (D * K)
        else:
            ans = X + (D * K)
    else:
        a = (X % D)
        k = (K - c) % 2
        if a >= 0:
            ans = a - (D * k)
        else:
            ans = a + (D * k) 
    print(abs(ans))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


if abs(X) > K * D: # 移動しきれない場合
    #print('short')
    #print(abs(X) // D, K)
    print(abs(abs(X) - (D * K)))
else: # 移動しきれる場合
    #print('enough')
    step = K - (X // D)
    if step % 2 == 0: # case even
        #print('even')
        print(abs(abs(X) - (D * (X // D))))
    else:
        #print('odd')
        print(abs(abs(abs(X) - (D * (X //D))) - D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = x - (a * d)
    else:
        x = x - (a * d)
        if x > 0:
            x = x - abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

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

if X > 0:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X - D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X- D*tmp
        X2 = X1 - D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
elif X < 0:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X + D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X + D*tmp
        X2 = X1 + D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
else:
    if K %2 == 0:
        ans = 0
    else :
        ans = D
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

num_moved = min(x // d, k)
num_remained = k - num_moved
if num_moved > k:
  now = x % d
else:
  now = x - d*num_moved

if num_remained % 2 == 0:
  result = abs(now)
else:
  tmpa = now + d
  tmpb = now - d
  if abs(tmpa) > abs(tmpb):
    result = abs(tmpb)
  else:
    result = abs(tmpa)
print(result)

Submission status: Wrong Answer

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

mod1 = x % d
mod2 = mod1 - d
mod = min(mod1, abs(mod2))
modx = max(mod1, abs(mod2))
x1 = abs(x)

a = (x-mod)/d

if mod1+k*d <= x1:
    print(x-k*d)
elif (k-a) % 2 == 0:
    print(mod)
else:
    print(modx)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:

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

for i in range(k):
    if abs((x + d)) > abs((x - d)):
        x = abs(x - d)
    elif abs((x - d)) > abs((x + d)):
        x = abs(x + d)

print(x)

Submission status: Wrong Answer

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

init = x

for i in range(round(abs(x) // abs(d))):
    tempx = x + i*d - (k-i)*d
    init = min(init, abs(tempx))

print(init)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

import numpy as np

if X > 0 and X > K*D:
    print(X-K*D)
elif X < 0 and X < K*D:
    print(X+K*D)
else:
    a = X // D
    b = a + 1
    if a % 2 != K % 2:
        print(np.abs(b*D - X))
    else:
        print(np.abs(X - a*D))

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
min_X = abs(X) // D
if min_X >= K:
    ans = abs(X) - K * D
else:
    if abs(X) % D == 0:
        res_D = K - min_X
        if res_D % 2 == 0:
            ans = 0
        else:
            ans = D
    else:
        res_D = K - min_X
        if res_D == 1:
            ans =  K * D - abs(X)
        else:
            if (res_D - 1) % 2 == 0:
                ans =  K * D - abs(X)
            else:
                res = abs(X) % D
                ans = min(res, abs(X) - res)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

else:
  if K%2==1:
    print(D)
  else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
X, K, D = list(map(int, input().split()))
x = int(X)
D = int(D)
j = 0

for i in range(K):
    if abs(x) < int(D/2)-1:
        j = i
        break

    if x > 0:
        x = x - D
    else:
        x = x + D

j = j % 2

if j==0:
    print(abs(x))
else:
    if x > 0:
        x = x - D
    else:
        x = x + D
    print(abs(x))

Submission status: Wrong Answer

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

timesToMove = x//d

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x2 - k * d > 0:
    print(x2 - k * d)
    exit()

for i in range(k + 1):
    if x < 0:
        ans = min(abs(x),abs(x + d))
        i2 = i
        if i2 % 2 == k % 2:
            #print("a")
            print(abs(x))
            exit()
        else:
            #print("b")
            print(x + d)
            exit()
    x = x - d

Submission status: Wrong Answer

Solution code:
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
if X>K*D:
    print(X-K*D)
else:
    N=abs(X)//D
    Min=10**16
    n=0
    for i in range(N-1, N+2):
        M=abs(X-i*D)
        if M<Min:
            Min=M
            n=i
    if (K-n)%2==0:
        print (abs(Min))
    else:
        print(min(abs(Min+D), abs(Min-D)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	X,K,D = inputMap()
	X = abs(X)

	koko = abs(X-D)
	if koko == 0:
		if K % 2 == 0:
			print(abs(D))
			sys.exit()
		else:
			print(0)
			sys.exit()


	ima = abs(X-0)
	if ima == 0:
		if K % 2 == 0:
			print(0)
			sys.exit()
		else:
			print(abs(D))
			sys.exit()

	imaima = abs(X-(K*D))
	if imaima == 0:
		print(0)
		sys.exit()

	if X - K*D > 0:
		ans = X - K*D
		print(ans)
		sys.exit()
	else:
		tmp = X // D
		nokori = K - tmp
		if nokori % 2 == 0:
			ans = X+D % D
			print(ans)
			sys.exit()
		else:
			ans = X % D
			print(ans)
			sys.exit()





#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X = abs(X)

if X > K * D:
  print (X - K * D)
elif X < D :
  print (X)
else:
  for i in range(K):
    if X - D > 0:
      X = X-D
    else:
      print(X)
      break

Submission status: Wrong Answer

Solution code:

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

if abs(X) > K*D:
    print(X - K*D)
else:
    mod = X % D

    min_step = int((X - mod) / D)

    if K % 2 == min_step % 2:
        print(mod)
    else:
        print(D - mod)

Submission status: Wrong Answer

Solution code:
import math
y, k, d = map(int, input().split())
if y >= 0:
    x = y
else:
    x = -y
i = math.floor(x / d)
if i >= k:
    p = x - k * d
elif i == x / d:
    if i + k == 2 * int(i + k):
        p = 0
    else:
        p = d
else:
    if (2 * i + 1) * d == 2 * x:
        p = x - i * d
    else:
        if i + k == 2 * int((i + k) / 2):
            p = x - i * d
        else:
            p = (i + 1) * d - x
print(int(p))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
count=0
import math
c=abs(x)//d
amari=abs(x)%d
if c<=k:
    if (k-c)%2==0:
        print(amari)
    else:
        amari2=abs(amari-d)
        print(max(amari,amari2))
else:
    print(abs(x)-d*k)

Submission status: Wrong Answer

Solution code:

x,k,d = list(map(int,input().split()))
#偶数回前後に移動したら0
#偶数回一方向に移動したら2D

capa_iter = abs(x) // abs(d)
if capa_iter < k:
  if (k - capa_iter) % 2 == 0:
    print(x - capa_iter*d)
  else:
    print(max([abs(x - capa_iter*d-d),abs(x - capa_iter*d+d)]))
  exit()


ans = min([abs(x-d),abs(x+d)])

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ch = k%2
ans = abs(x)
cnt = 0
while True:
    if x == 0:
        if (cnt%2) == ch:
            print(0)
            break
        else:
            print(abs(d))
            break
    if x//d > k:
        if x > 0:
            print(abs(x-d*k))
        elif x < 0:
            print(abs(x+d*k))
        break
    if x > 0:
        cnt += x//d
        x -= d*(x//d)
        xx = x - d
        if cnt%2 == ch:
            print(abs(x))
        else:
            print(abs(xx))
        break
    elif x < 0:
        cnt += abs(x)//d
        x += d*(abs(x)//d)
        xx = x + d
        if cnt%2 == ch:
            print(abs(x))
        else:
            print(abs(xx))
        break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x == d:
    print(x)
    exit()

ans = 100000000000000000000
for i in range(k):
    if abs(x + d*i - d*(k-i)) < ans:
        ans = abs(x + d*i - d*(k-i))

print(ans)