Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

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

Sample Input 1

6 2 4

Sample Output 1

2

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

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

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

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

Sample Input 2

7 4 3

Sample Output 2

1

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

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

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

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
x = abs(x)
# math.floor(x/d)+1回!absの方向に行く
# k>x/dのときx-math.floor(x/d)*dとx-(math.floor(x/d)+1)*dのうち小さいほうが答え
if k > x/d:
    if x-math.floor(x/d)*d < abs(x-(math.floor(x/d)+1)*d):
        print(x-math.floor(x/d)*d)
    else:
        print(abs(x-(math.floor(x/d)+1)*d))
else:
    print(x-k*d)

Submission status: Wrong Answer

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

if x == 0:
    if k % 2 == 0:
        print(0)
    else:
        print(d)
elif x > 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        r = abs(mod - d)
        k -= q
        if k == 0:
            print(abs(x - k * d))
        if k % 2 == 0:
            print(mod)
        else:
            print(r)
elif x < 0:
    if abs(x) >= k * d:
        print(x + k * d)
    else:
        q, mod = divmod(x, d)
        r = abs(mod - d)
        k -= q
        if k == 0:
            print(abs(x + k * d))
        elif k % 2 == 0:
            print(mod)
        else:
            print(r)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      elif bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    if ans==mbk:
      ans=mbk if (k-sa-i)%2==0 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Wrong Answer

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

if(x>0):
	while (x>0 and k>0):
		x=x-d
		k-=1
	ans = abs(x)
	if (k>0):
		if(ans==0):
			ans = abs(d)
		else:
			ans = min(abs(x), abs(x+d))
else:
	while (x<0 and k>0):
		x+=d
		k-=1
	ans = abs(x)
	if (k>0):
		if(ans==0):
			ans = abs(d)
		else:
			ans = min(abs(x), abs(x-d))

print(ans)

Submission status: Wrong Answer

Solution code:
def questionC():
  X,K,D = map(int, input().split())
  tmpX = X - K *D
  if tmpX > 0:
    return tmpX
  ans = 10**15
  plusKCnt = 0
  for i in range(K+1):
      dummyX = abs(X + D*i - D*(K-i))
      if ans > dummyX:
          ans = dummyX
      else:
          return ans
  return ans
print(questionC())

Submission status: Wrong Answer

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

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

x = abs(x)

if x - k*d <= 0 <= x + k*d:
    p = x%d
    q = p-d
    print(x,min(abs(p),abs(q)))
else:
    print(min(x,abs(x-k*d),abs(x+k*d)))

Submission status: Wrong Answer

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

if x == 0:
    if k % 2:
        print(0)
    else:
        print(d)
elif x > 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        r = abs(mod - d)
        k -= q
        if k == 0:
            print(abs(x - k * d))
        if k % 2 == 0:
            print(mod)
        else:
            print(r)
elif x < 0:
    if abs(x) >= k * d:
        print(x + k * d)
    else:
        q, mod = divmod(x, d)
        r = abs(mod - d)
        k -= q
        if k == 0:
            print(abs(x + k * d))
        elif k % 2 == 0:
            print(mod)
        else:
            print(r)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if ans==mbk:
      ans=mbk if (k-sa-i)%2==1 else d
      break
    elif abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      if bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy

e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
t=0
if k%2==0:
  t=1
koua=numpy.gcd(x,k)
koub=numpy.gcd(k,d)
kou=numpy.gcd(koua,koub)
x=int(x/kou)
k=int(k/kou)
d=int(d/kou)
if t==1 and kou!=1:
  k+=1
if d>100 and d%2==0:
  d=100
if d>100 and d%2==1:
  d=1001
for i in range(k):
  if abs(x+d)<abs(x-d):
    x+=d
  else:
    x-=d
print(abs(x)*kou)

Submission status: Wrong Answer

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

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

x = abs(x)

if x - k*d <= 0 <= x + k*d:
    p = x%d
    q = p-d
    print(min(abs(p),abs(q)))
else:
    print(min(abs(x-k*d),abs(x+k*d)))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if ans==mbk:
      ans=mbk if (k-sa-i)%2==1 else d
      break
    if abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      if bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

if x - k*d <= 0 <= x + k*d:
    p = x%d
    q = p-d
    print(min(p,-q))
else:
    print(min(abs(x-k*d),abs(x+k*d)))

Submission status: Wrong Answer

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

if x == 0:
    if k % 2:
        print(0)
    else:
        print(d)
elif x > 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        r = abs(mod - d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(r)
elif x < 0:
    if abs(x) >= k * d:
        print(x + k * d)
    else:
        q, mod = divmod(x, d)
        r = abs(mod - d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(r)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
def resolve():
	import math
	X, K, D = map(int, input().split())
	n = math.floor(X/D)
	A = X - n*D
	k = K - n
	if k < 0:
		print(X - (K*D))
	elif k%2 == 0:
		print(A)
	else:
		print(abs(A-D))
resolve()

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      if bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    if ans==mbk:
      ans=mbk if (k-sa-i)%2==0 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 22:44:46 2020

@author: Evans
"""
import math

if __name__ == '__main__':
    l = list(map(int, input().split()))
    X = int(l[0])
    K = int(l[1])
    D = int(l[2])
    
    if D*K <= abs(X):
        minVal=abs(X) - D*K
    else:
        n=math.floor(X/D)+1
        # while abs(X) - D*n > 0:
        #    n=n+1
        
        if (K-n)%2==0:
            minVal = abs(abs(X) - D*n)
        else:
            minVal = abs(abs(X) - D*(n-1))
        
    print(minVal)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

result = 0

firstNearestIterNum = x // d

abs_x = abs(x)

if k <= firstNearestIterNum:
	result = abs_x - (d * k)
else:
	if (k - firstNearestIterNum) % 2 == 1:
		result = abs_x - (d * (firstNearestIterNum + 1))
	else:
		result = abs_x - d * firstNearestIterNum

print(abs(result))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


if abs(x)==d:
    if k&1:
        print(0)
    else:
        print(k)
else:

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

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

Submission status: Wrong Answer

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

x = abs(x)
y = k * d
ans = x
if(x > y):
    ans = min(x , x - y)
else:
    mar = x % d
    if (x // d) % 2 == k % 2:
        ans = min(ans , mar)
    else:
        ans = min(ans , abs(d - mar))
print(ans)

Submission status: Wrong Answer

Solution code:
# Walking Takahashi

def main(x, k, d):
    x = abs(x)
    q, r = divmod(x, d)

    h = 0
    if -(r - d) < r:
        h = d - r
    else:
        h = -r
    q = (x + h) // d

    if k < q:
        return x - k * d
    kr = k - q
    xr = x - q * d
    if kr % 2 == 0:
        return abs(h)
    else:
        if 0 < h:
            return h - d
        else:
            return h + d


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
ans = 10**15
plusKCnt = 0
for i in range(K+1):
    dummyX = abs(X + D*i - D*(K-i))
    if ans > dummyX:
        ans = dummyX
    else:
        break
print(ans)

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

result = 0

firstNearestIterNum = x // d

abs_x = abs(x)

if k < firstNearestIterNum:
	result = abs_x - (d * k)
else:
	if (k - firstNearestIterNum) % 2 == 1:
		result = abs_x - (d * (firstNearestIterNum + 1))
	else:
		result = abs_x - d * firstNearestIterNum

print(abs(result))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      if bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    if ans==mbk:
      ans=mbk if (k-sa-i)%2==0 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
meta = [int(s) for s in input().split(' ')]

now = meta[0]
times = meta[1]
distance = meta[2]

out = 0
i = 0
while i < times:
    time = min([int(abs(now) / distance), times])
    i += time
    if time == 0:
        if i % 2 == 0:
            print(abs(now))
            exit()
        else:
            time = 1
            i = times
#            print('kisuu')
    
    if now > 0:
        now -= distance * time
    else:
        now += distance * time
    
#    print('i: ' + str(i) + ', now: ' + str(now))

print(abs(now))

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:
x, k, d = map(int, input().split())

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

elif x > k * d:
    print(x - k * d)
else:
    q = x // d
    mod = x % d
    r = abs(mod - d)
    k -= q
    # if x > 0:
    #     x -= q * d
    # elif x < 0:
    #     x += q * d
    if k == 0:
        print(abs(x))
    elif k % 2 == 0:
        print(mod)
    else:
        print(r)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      if bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    if ans==mbk:
      ans=mbk if (k-sa-i)%2==1 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
print(X)
time = X // D
X = X - (D * time)
K = K - time
print(X)
print(K)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

result = 0

firstNearestIterNum = x // d

if k < firstNearestIterNum:
	result = (abs(x) - abs(d * k))
else:
	if (k - firstNearestIterNum) % 2 == 1:
		result = (abs(x) - abs(d * (firstNearestIterNum + 1)))
	else:
		result = (abs(x) - abs(d * firstNearestIterNum))

print(abs(result))

Submission status: Wrong Answer

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

if abs(x) > k * d:
    if x > 0:
        print(x - k * d)
    elif x < 0:
        print(x + k * d)
else:
    q = x // d
    mod = x % d
    k -= q
    if x > 0:
        x -= q * d
    elif x < 0:
        x += q * d
    r = abs(mod - d)
    if k == 0:
        print(abs(x))
    elif k % 2 == 0:
        print(mod)
    else:
        print(r)

Submission status: Wrong Answer

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

c = X

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


if abs(x)==d:
    if k&1:
        print(0)
    else:
        print(k)
else:

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

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

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 % 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:
X, K, D = map(int, input().split())
x = abs(X)
cnt = x // D

if K < cnt:
    ans = x - (K * D)
else:
    ans = x - (cnt * D)
    if cnt % 2 != 0:
        ans -= D
print(abs(ans))

Submission status: Wrong Answer

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

if X==K==D:
    print(X)
    exit()
x = X
# vmin = abs(X)
cnt = 0
# for k in range(K):
#     if X>0:
#         x -= D
#     else:
#         x += D
#     if vmin>abs(x):
#         vmin = abs(x)
#         cnt += 1
#     else:
#         break
if abs(X)>=(K*D):
    cnt = K
else:
    cnt = int(math.floor(abs(X)/D))
# if X>0:
#     print(abs(X - (cnt * D)))
# else:
#     print(abs(X + (cnt * D)))
out = abs(abs(X) - (cnt * D))
if K%2==0:
    print(out)
else:
    print(abs(out - 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:
    ans=min(X,K*D-X)%D
    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:
X, K, D = map(int, input().split())
X = abs(X)
X -= (X//D) * D
K -= 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())
o =x//d
if x <0:
    if k%2==0:
        print(min(abs(x+1*d*(o+1)),abs(x+1*d*(o-1))))
        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 k%2==0:
        print(min(abs(x-1*d*(o+1)),abs(x-1*d*(o-1))))
        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:
import math
import sys
x, k, d = map(int, input().split())

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin


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

    absX = abs(x)
    tmp = absX / d

    if tmp > k:
        return absX - d * k
    else:
        absX = absX - d * tmp
        k = k - tmp
        if k % 2 == 0:
            return absX
        else:
            return absX - d


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

Submission status: Wrong Answer

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

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

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())
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 % D)
        else:
            print(abs(X % K - K))
    else:
        print(X - K * D)

Submission status: Wrong Answer

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


if abs(x)==d:
    if k&1:
        print(0)
    else:
        print(k)
else:

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

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

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==1:
        print(d)
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:
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:  
  a=-hajime
  ayumi=a//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 x > k * d:
    print(x - k * d)
else:
    q = x // d
    mod = x % d
    k -= q
    x -= q * d

    r = abs(mod - d)
    if k == 0:
        print(abs(x))
    elif k == 1:
        print(abs(x-d))
    elif k % 2 == 0:
        print(mod)
    else:
        print(r)

Submission status: Wrong Answer

Solution code:

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

ans = x

if x < 0 :
    x = x * -1

# 理想の数字を追求
if abs(x - (x // d * d)) < abs(x - (((x // d) + 1) * d)) :
    i_ans = abs(x - (x // d * d))
    s_ans = abs(x - (((x // d) + 1) * d))
else :
    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))
# print("最適な点に到達後動かす回数 :  {0}".format(k - x // d))

if k <= (x // d) :
    ans = abs(x - (k * d))
else : 
    # 動かせる回数が偶数
    if (x // d - k) % 2 == 0 :
        ans = i_ans
    else :
        ans = s_ans

print(ans)

Submission status: Wrong Answer

Solution code:
X, k, d = map(int, input().split())
x = abs(X)
res = 0
if x <= (k*d):
    m1 = x % d
    c1 = x // d
    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 abs(x) > k * d:
        print(x + k * d)
    elif abs(x) == k * d:
        print(0)
    else:
        q = x // d
        mod = x % d
        k -= q
        x += q * d

        r = abs(mod - d)
        if k == 0:
            print(abs(x))
        elif k == 1:
            print(abs(x+d))
        elif k % 2 == 0:
            print(mod)
        else:
            print(r)

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


else:
    if x > k * d:
        print(x - k * d)
    elif x == k * d:
        print(0)
    else:
        q = x // d
        mod = x % d
        k -= q
        x -= q * d

        r = abs(mod - d)
        if k == 0:
            print(abs(x))
        elif k == 1:
            print(abs(x-d))
        elif k % 2 == 0:
            print(mod)
        else:
            print(r)

Submission status: Wrong Answer

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


if abs(x)==d:
    if k&1:
        print(0)
    else:
        print(k)
else:

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

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

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

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 = int((abs(X) - A1)/D)
    if (K - n) % 2 == 0:
        print(A1)
    else:
        print(A2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if(x < 0):
  i = int(abs(x)/d)
  if(k > i):
    k = k - i
    x = x + d*i
  else:
    k = 0
    x = x + d*k
  if(abs(x) > abs(x - d)):
    k += 1
    x = x - d
elif(x > 0):
  i = int(abs(x)/d)
  if(k > i):
    k = k - i
    x = x - d*i
  else:
    k = 0
    x = x - d*k
  if(abs(x) > abs(x + d)):
    k += 1
    x = x + d

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

Submission status: Wrong Answer

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

X = abs(X)

k = round(abs(X) / D) // 2

st = max(0, k - 10)
en = min(K, k + 10)

ans = 10 ** 9

for i in range(st, en):
    ans = min(ans, abs(X - i * D))

print(ans)