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

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:
#C - Walking Takahashi
X,K,D= map(int,input().split())

a1 = X // D
a2 = (X // D)+1
if min((abs(X)-(a1*D)),(abs(X)-(a2*D))) == (abs(X)-(a1*D)):
    a = a1
else:
    a = a2

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

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
temp=abs(a)//c
if(temp>b):
    temp=temp-b
    if(abs(a)%c==0):
        if(temp>b):
            print(abs(a)-(b*c))
        else:
            print(temp*c)
    else:
        print(abs(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())
ans_temp = x // d

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

Submission status: Wrong Answer

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

ans=X

if X>=0:
    a=X//D
elif X<0:
    a=X//D+1


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

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


print(ans)

Submission status: Wrong Answer

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

abs_x = abs(x)
if x == d:
  if k % 2 == 0:
    print(0)
  else :
    print(d)
else:
  total = k * d
  if abs_x >= total:
    print(abs_x - total)
  else:
    times = int(abs_x / d)
    k -= times
    if k % 2 == 0:
      print(abs_x % d)
    else:
      print(abs_x - d)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
input_line = input().split(' ')
X = int(input_line[0])
K = int(input_line[1])
D = int(input_line[2])

num = X // D
if num < 0:
    num *= -1

if num >= K:
    if X > 0:
        X -= K * D
    else:
        X += K * D
else:
    K -= num
    if X > 0:
        X -= num * D
        if K % 2 == 1:
                X -= D
    else:
        X += num * D
        if K % 2 == 1:
                X += D

if X < 0:
    X *= -1
print(X)

Submission status: Wrong Answer

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

for a in range(K):
  if abs(X-D) > abs(X+D):
    a = abs(X-D)
  if abs(X+D) < abs(X+D):
    a = abs(X+D)

print(a)

Submission status: Wrong Answer

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

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

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


print(ans)

Submission status: Wrong Answer

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

X = abs(X)

quotient = X // D
rest = X % D

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

print(abs(tmp))

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
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())
num=0
if (X//D)<K:
    while abs(X)>D/2:
        nu=num+1
        if X>0:
            X=X-D
        else:
            X=X+D
    if (K-num)%2==0:
        if X>0:
            X=X-D
        else:
            X=X+D
else:
    if X>0:
        X=X-K*D
    else:
        X=X+K*D
print(abs(X))

Submission status: Wrong Answer

Solution code:
import sys

s = input()
a = s.split()

x = int(a[0])
k = int(a[1])
d = int(a[2])

for i in range(k):
    if 0 < x:
        x = x-d
    else :
        if((k-i)%2==0):
            break
        elif ((k-i)%2==1):
            x = x+d
            break

print(str(abs(x)))

Submission status: Wrong Answer

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

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

print(ans)

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)
    while(position > 0):
        position = position - D

    minus_abs = abs(position)
    position = position + D
    plus_abs = abs(position)

    if minus_abs > plus_abs:
        print(plus_abs)
    else:
        print(minus_abs)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X == D:
    print(X)
    exit()

ans = abs(X-D)
for i in range(2, K+1):
    num = abs(X-(D*i))
    if ans >= num:
        ans = num
    else:
        break
print(ans)
exit()

if K%2 == 0:
    ans = abs(X-(D*2))
    print(ans)
    exit()
else:
    ans = abs(X-D)
    for i in range(2, K+1):
        num = abs(X-(D*i))
        if ans >= num:
            ans = num
        else:
            break
    print(ans)
    exit()

Submission status: Wrong Answer

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

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

else:
    cent = x % d
    num = abs(x) // d

if d / 2 > cent and cent != 0:
    cent = d - cent
    num += 1

if num >= k:
    print(abs(x) - k * d)
    exit(0)

rem = k - num

if rem % 2 == 0:
    print(cent)
    exit(0)
else:
    print(d - cent)

Submission status: Wrong Answer

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

X=abs(X)
ii=int(X/D)
if K<=ii:
    X-=K*D
else:
    X-=ii*D
if (K-ii)%2==1:
    X-=D

print(abs(X))

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())
# x, k, d = (10, 31, 3)
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 = abs(x // d)
# print(a)
if k % 2 == 0:
    if a % 2 != 0:
        i = (a - 1)*d
        j = (a + 1)*d
    else:
        i = a * d
        j = 0
else:
    if a % 2 != 0:
        i = a * d
        j = 0
    else:
        i = (a - 1)*d
        j = (a + 1)*d
# a *= d
# print(a)
if x < 0:
    print(min(abs(x+i), abs(x+j)))
else:
    print(min(abs(x-i), abs(x-j)))

Submission status: Wrong Answer

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

if X == K == D:
    print(0)
for num in range(K):
    plus = num * D
    minus = -(K - num) * D

    x = X + plus + minus
    X_list.append(x)

for i in range(len(X_list)):
    if X_list[i] < 0:
        X_list[i] = -X_list[i]

X_list.sort()
print(X_list[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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_ = 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(abs(last), abs(now))
        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:
nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]


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

Submission status: Wrong Answer

Solution code:
#C - Walking Takahashi
X,K,D= map(int,input().split())

a1 = X // D
a2 = (X // D)+1
if min((abs(X)-(a1*D)),(abs(X)-(a2*D))) == (abs(X)-(a1*D)):
    a = a1
else:
    a = a2

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
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 k==0:
                y=x1
                break
            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 k==0:
                y=x1
                break
            if x2>=0:
                if k%2==1:
                    y=x2
                else:
                    y=x1
                break

print(abs(y))

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

if abs(x // d) >= k:
    print(abs(x)-k*d)
else:
    min_steps = [x//d, (x//d)+1]
    min_dis = [abs(x) - d * min_steps[0], d * min_steps[1] - abs(x)]
    if min_dis[0] == min_dis[1]:
        print(min_dis[0])
    elif min_dis[0] < min_dis[1]:
        if (k-min_steps[0]) % 2 == 0:
            print(min_dis[0])
        else:
            print(min_dis[1])
    else:
        if (k-min_steps[1]) % 2 == 0:
            print(min_dis[1])
        else:
            print(min_dis[0])

Submission status: Wrong Answer

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

X = abs(X)

quotient = X // D
rest = X % D

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

print(abs(tmp))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
if(X <= K*D):
    tmp = abs(X) // D
    tmpk = K - tmp
    tmp2 = tmp * D
    X = X - tmp2
    if(tmpk % 2 == 0):
        print(str(abs(X)))
    else:
        print(str(abs(X-D)))
else:
    print(str(min(abs(X-K*D),abs(X+K*D))))

Submission status: Wrong Answer

Solution code:

x, k, d = (int(x) for x in input().split())

ans = x

if 0 < x :

    # 理想の数字を追求
    if abs(x - (x // d * d)) < abs(x - (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x - (x // d * d))
        s_ans = abs(x - (((x // d) + 1) * d))
    else :
        # print('2採用')
        i_ans = abs(x - (((x // d) + 1) * d))
        s_ans = abs(x - (x // d * d))

    # print("回数制限なしの理想の答え :  {0}".format(i_ans))
    # print("回数制限なしの次点の答え :  {0}".format(s_ans))

    # print("最適な答えにたどりつく回数 :  {0}".format(x // d))
    # print("動かせる回数 　　　　　　　:  {0}".format(k))

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

            if (x // d) % 2== 0 :
                # print("aaa")
                ans = i_ans
            else :
                # print("bbb")
                ans = s_ans
        else :
            if (x // d) % 2 == 0:
                ans = s_ans
            else :
                ans == i_ans
else :
    if abs(x + (x // d * d)) < abs(x + (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x + (x // d * d))
        s_ans = abs(x + (((x // d) + 1) * d))
    else :
        # print('2採用')
        i_ans = abs(x + (((x // d) + 1) * d))
        s_ans = abs(x + (x // d * d))

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

            if (x // d) % 2 == 0 :
                ans = s_ans
            else :
                ans = i_ans
        else :
            if (x // d) % 2 == 0:
                ans = i_ans
            else :
                ans == s_ans

print(ans)

Submission status: Wrong Answer

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

i = 0

if(X <= D):
    if(K % 2 == 0):
        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:
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())
ma = X + D*K
mi = X - D*K
if X == 1000000000000000 == K == D:
  print(1000000000000000)
  exit()
if X > 0:
  ans = min(abs(mi),abs(X))
  dk = D*K
  ok = X
  ng = mi
  while dk > 1:
    dk //= 2
    mid = X - dk
    if ans < abs(mid):
      break
    else:
      ans = abs(mid)
  print(ans)
elif X < 0:
  ans = min(abs(ma),abs(X))
  dk = 0
  ok = ma
  ng = X
  while dk > 1:
    dk += (D*K)
    mid = X + dk
    if ans < abs(mid):
      break
    else:
      ans = abs(mid)
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
m=a
if a<=0:
  if b*c+a>=0:
    while m<0:
      m=m+c
    if m>=c-m:
      m=c-m
  else:
    m=-b*c-a
else:
  if b*c-a>=0:
    while m>0:
      m=m-c
    if -m>=m+c:
      m=m+c
  else:
    m=a-b*c
print(m)

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(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(x - d*y)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if x < k*d:
    L = []
    s = x
    for i in range(k):
        L.append(s)
        L = sorted(L)
        s = abs(x) - d
        if s >= L[0] or s == 0:
            break
        x = s
    if k%2 == 0:
        if len(L)>1:
            res = L[1]
        else:
            res = L[0]
    else:
        res = L[0]
elif abs(x) >= k*d:
    res = abs(abs(x)-k*d)
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# atcoder 
# Türkler var mı?
# Herkese memnün oldum
import sys
import math

readline = sys.stdin.buffer.readline
iimr = lambda: map(int, sys.stdin.buffer.readline().rstrip().split())
#sys.setrecursionlimit(10 ** 8)

def debug(*x):
    print(*x, file=sys.stderr)


class atcoder():
    def __init__(self):
        pass

    def çözmek(self):
        
        N, K, D = map(int, input().split())
        res = []
        res.append(self.関数(N, K, D))
        print(*res, "\n")	
    
    def 関数(self, n, k, d):
       
        if n == 0:
            return n
        if (n % d == 0) and (n // d == k):
            return 0
        else:
            karpayi = n // d
            if karpayi <= k:
                ost1  = n % d
                if ((karpayi - k) % 2) == 0:
                    return ost1 
                else:
                    return(abs(ost1 - d))

            else:
                return n - math.floor(k * d)
            
        return -1

if __name__ == "__main__":
    ima = atcoder()
    ima.çözmek()

Submission status: Wrong Answer

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

if k%2==0:
  ans=0 #偶数
else:
  ans=1 #奇数

semin=x//d #最小に行くための個数
if semin%2==0:
  minb=0 #最小に行くための個数が偶数
else:
  minb=1 #最小に行くための個数が奇数

if semin>k:
  print(abs(x-k*d))
else:
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-d*semin))
    else:
      print(min(abs((x-(d*semin))-d),abs(x-d*semin)))
  else:#ｋが奇数の時
    if minb==0:
      print(abs((x-(d*semin))-d))
    else:
      print(abs(x-d*semin))

Submission status: Wrong Answer

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

KOE=K%2

A=abs(X)//D
M=abs(X)%D

# print(A)

if K<=A:
    print(abs(X)-K*D)
    exit()

else :
    if A%2==KOE:
        print(min(M,abs(M-D)))
    else :
        print(min(abs(M+D),abs(M-D)))

Submission status: Wrong Answer

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

if K > 1000000 and K % 2 == 0:
    K = 1000000
if K > 1000000 and K % 2 == 1:
    K = 1000001

for i in range(K):
    if place == 0 and (K-i)%2==0:
        break

    if place >= 0 and (place - D) >= 0:
        place -= D

    elif place >= 0 and (place - D) <= 0:
        place -= D
  
    else: 
        place += D 
      
if place < 0:
    place = abs(place)
print(place)

Submission status: Wrong Answer

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

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

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

elif ans_1 == ans_2:
    print(ans_1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
min = 1000000000000000
cnt = 0
n = x//d
n = abs(n)
if n > k:
    n=k

if n < 1000:
    n=0
if x > 0:
    for i in range(k):
        if i == 0 and n!=0:
            x -= n*d
            k -= n
            if abs(x) < min:
                min = abs(x)
            if n==k:
                break
        elif abs(x-d) < min:
            min = abs(x-d)
            x-=d
            cnt += 1
        else:
            if abs(x-d)>abs(x+d):
                x = abs(x+d)
            else:
                x = abs(x-d)
            break
    if cnt == 0:
        cnt += 1
    k -= cnt
    if k%2 == 0:
        print(min)

    else:
        print(x)
        
else:
    for i in range(k):
        if i == 0 and n!=0:
            x += n*d
            k -= n
            if abs(x) < min:
                min=abs(x)
            if k==n:
                break
        
        if abs(x+d) < min:
            min = abs(x+d)
            x+=d
            cnt += 1
        else:
            if abs(x-d)>abs(x+d):
                x = abs(x+d)
            else:
                x = abs(x-d)
            break
    if cnt==0:
        cnt += 1
    k -= cnt
    if k%2 == 0:
        print(min)


    else:
        print(x)

Submission status: Wrong Answer

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

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

print(abs(A))