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: Accepted

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

Submission status: Accepted

Solution code:

def main():
    x, k, d = map(int, input().split())
    if x > 0:
        if x - d * k >= 0:
            print(x - d * k)
        else:
            a = abs(x - d * k) % d
            b = abs(x - d * k) // d
            if b % 2 == 0:
                print(a)
            else:
                print(d - a)

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


main()

Submission status: Accepted

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

if K * D < abs(X):
    ans = abs(X) - K * D
else:
    if abs(X) < D:
        if K % 2 == 0:
            ans = abs(X)
        else:
            ans= abs(abs(X) - D)
    else:
        if abs(X) // D % 2 == 0:
            if K % 2 == 0:
                ans = abs(X) % D
            else:
                ans = abs(abs(X) % D - D)
        else:
            if K % 2 == 0:
                ans = abs(abs(X) % D - D)
            else:
                ans = abs(X) % D

print(ans)

Submission status: Accepted

Solution code:
start, k , x = map(int, input().split())
start = abs(start)
y = start//x
if y >= k:
    start -= (x*k)
    print(start)
else:
    start -= (x*y)
    k -= y
    k %= 2
    if k == 1:
        print(min(abs(start+x), abs(start-x)))
    else:
        print(start)

Submission status: Accepted

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

Submission status: Accepted

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

xi = abs(x)
if abs(x) > d*k:
  m = min(abs(x+d*k),abs(x-d*k))
else:
  n = ceil(xi / d)
  if (n+k) % 2 == 0:
    m = abs(xi-d*n)
  else:
    m = abs(xi-d*(n-1))
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
count = X // D



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

if ans < 0:
    ans = ans * (-1)

print(ans)

Submission status: Accepted

Solution code:
def main():
    x, k, d = map(int, input().split())
    if x == 0:
        if k % 2 == 0:
            return 0
        else:
            return d
    else:
        n = int(abs(x)/d)
        if n > k:
            return abs(abs(x)-k*d)
        else:
            if (k-n) % 2 == 0:
                return abs(abs(x)-n*d)
            else:
                return abs(abs(x)-(n+1)*d)


if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#C
x,k,d=map(int, input().split())
 
#直線のみに進行すればいい場合の回数
s = abs(x)//d
 
#最短回数よりも移動可能回数の方が少ない場合
if s >= k:
    print(abs(x) - k*d)
 
#最短回数よりも多く移動する場合
else:
    if (k-s) %2 == 0:
        print(abs(x) - s*d)
    else:
        print(d-(abs(x) - s*d))

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
if abs(X) >= D:
    minX = abs(X) % D

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

Submission status: Accepted

Solution code:
X, K, D = (int(i) for i in input().split())
X = max(X, -X)
k, x = divmod(X, D)
if k >= K:
    print(X - K * D)
else:
    if (K - k) % 2 == 0:
        print(abs(x))
    else:
        print(abs(x-D))

Submission status: Accepted

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

Submission status: Accepted

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

#最大まで移動するとき
if X >= K * D:
    print(X - K*D)
    exit()

if D > X:
    bef = X
    nex = X - D

    if K % 2 == 0:
        print(abs(bef))
    else:
        print(abs(nex))

else: #D<=X
    bef = X % D
    nex = bef - D

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
t = abs(x) // d
a = []
a.append(t - 3)
a.append(t - 2)
a.append(t - 1)
a.append(t)
a.append(t + 1)
a.append(t + 2)
a.append(t + 3)
b = []

for i in a:
    b.append(-i)
for i in b:
    a.append(i)

a.append(k)
a.append(k - 1)
a.append(-k)
a.append(-k + 1)

res = abs(x)
if k % 2:
    res += d
for i in a:
    if i % 2 == k % 2 and abs(i) <= k:
        res = min(res, abs(x - i * d))
print(res)

Submission status: Accepted

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


if X>0:
    S = int(abs(X) / D)
    aX = X -D * S
    if S>=K:
        print(X-D*K)
    elif (K-S)%2==0:
        print(abs(aX))
    else:
        print(abs(aX-D))
else:
    S = int(abs(X) / D)
    aX = X + D * S
    if S>=K:
        print(abs(X+D*K))
    elif (K-S)%2==0:
        print(abs(aX))
    else:
        print(abs(aX+D))

Submission status: Accepted

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

print(X)

Submission status: Accepted

Solution code:
import math

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

X = abs(X)

#n = roundup(X/D)
n = math.floor(X/D)

if K<=n:
    ans = abs(X-K*D)
else:
    tmp = X-D*n
    if (K-n)%2==0:
        ans = abs(tmp)
    else:
        ans = abs(tmp-D)

print(ans)

Submission status: Accepted

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

x = abs(x)
t = min(k, x//d)

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

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	#a = int(input())
	x, k, d = tin()
	#s = input()
	na = abs(x) // d
	#pa(na)
	if na >= k:
		return min(abs(x - d*k), abs(x+d*k))
	else:
		xa = x - d*na if x >= 0 else x+d*na
		ka = k-na
		kb = ka%2
		#pa(xa)
		if kb==1:
			return min(abs(xa+d), abs(xa-d))
		else:
			return min(abs(xa-2*d),abs(xa+2*d),abs(xa))
		
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

if K % 2 == 0:
  q, mod = divmod(abs(X), D*2)
  if mod <= abs(mod - D*2):
    if q*2 <= K:
      print(mod)
    else:
      print(abs(abs(X)-(K*D)))
  else:
    if q*2 <= K:
      print(abs(mod-D*2))
    else:
      print(abs(abs(X)-(K*D)))
else:
  q, mod = divmod(abs(X)-D, D*2)
  if mod <= abs(mod - D*2):
    if q*2 <= K:
      print(mod)
    else:
      print(abs(abs(X)-(K*D)))
  else:
    if q*2 <= K:
      print(abs(mod-D*2))
    else:
      print(abs(abs(X)-(K*D)))

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

Solution code:
def main():
    X, K, D = map(int, input().split())
    answer = solve(X, K, D)
    print(answer)


def solve(X, K, D):
    if abs(X) >= D * K:
        return abs(X) - (D * K)

    least_hops = abs(X) // D

    hops_left = K - least_hops
    dst = abs(X) - (D * (least_hops + hops_left % 2))

    return abs(dst)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
xa=abs(x)
kz=xa//d
xz=xa-kz*d

if xz*2>d:
  xzz=d-xz
  kz+=1
else:
  xzz=xz
rest=k-kz
if rest<0:
  print(xa-k*d)
elif rest%2==0:
  print(xzz)
else:
  print(d-xzz)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import decimal    

data = input().split()
X = decimal.Decimal(data[0])
K = decimal.Decimal(data[1])
D = decimal.Decimal(data[2])
if X < 0:
    X *= -1
if X/D-X//D > 0.5:
    y = X//D + 1
else:
    y = X//D
if y > K:
    y = K
X -= y * D

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
ref = abs(X) // D

if ref >= K:
    ans = abs(X) - K * D
else:
    ans = abs(X) - ref * D
    K = K - ref
    if K % 2 == 1:
        ans = abs(ans - D)
    else:
        pass

print(ans)

Submission status: Accepted

Solution code:
x,k,d = map(int, raw_input().split(' '))
if x>=k*d:
	print x-k*d
elif x<=-k*d:
	print -x-k*d
else:
	x=abs(x)
	r = x%d
	q = x//d
	if r<d-r and (k-q)%2==0:
		print r
	elif r<d-r:
		print d-r
	elif r>d-r and (k-q)%2==1:
		print d-r
	else:
		print r

Submission status: Accepted

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

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

Submission status: Accepted

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

x = abs(x)
# mを得るために必要な最小の移動回数
n = x // d

if k <= n:
    print(x - k * d)
    exit()

k -= n

x = x % d

if x > abs(x - d):
    x = x - d
    k -= 1

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

Submission status: Accepted

Solution code:
# from quantize import Decimal
X, K, D = list(map(int, input().split()))

dist= (X/D-K)/2
# print(dist)

if -dist > K :
    i = K
elif dist > 0 :
    i = 0
else :
    i = -round(dist)

# print(i)
print(abs(X + i*D - ((K-i)*D)))

Submission status: Accepted

Solution code:
X, K, D = [abs(int(i)) for i in input().strip().split(' ')]

tmp = X // D
d1 = X - (D * tmp)
d2 = X - (D * (tmp + 1))

if tmp >= K:
  ans = X - (D * K)
else:
  if (tmp % 2) ^ (K % 2) == 0:
    ans = d1
  else:
    ans = d2

print(abs(ans))

Submission status: Accepted

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
import math
x, k, d = map(int, input().split())

num = k*d

x = abs(x)
flag = 0

if x >= num:
    print(x - num)
else:
    if x > d:
        j = int(x / d)
    else:
        if x*2 > d:
            j = 1
            flag = 1
        else:
            j = 0

    if j == 0:
        b = x
    elif j == 1 and flag == 1:
        b = d - x
    else:
        b = x % (d*j)
    min = min(b, d - b)
    max = max(b, d - b)
    if min == d - b:
        j += 1

    if (k - j) % 2 == 0:
        print(min)
    else:
        print(max)

Submission status: Accepted

Solution code:
import math
N=input().split()
X=int(math.fabs(int(N[0])))
K=int(N[1])
D=int(N[2])
if X-K*D>=0:
    print(X-K*D)
else:
    syou=X//D
    x=X-syou*D
    y=X-(syou+1)*D
    if K%2==0:
        if syou%2==0:
            print(x)
        else:
            print(int(math.fabs(y)))
    else:
        if syou%2==0:
            print(int(math.fabs(y)))
        else:
            print(x)

Submission status: Accepted

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

Submission status: Accepted

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

k = K - k1
x = X % D

while k > 0:
    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()
    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

Submission status: Accepted

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

# print(x,k,d)

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

c = min(x//d, k)
x -= d*c
k -= c
if k%2:
    print(abs(d-x))
else:
    print(x)

Submission status: Accepted

Solution code:
import sys
import math


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

x,k,d = inintm()
x = abs(x)

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
num = X//D
if num >= K:
    print(X-K*D)
elif num < K:
    rest_X = X - num*D # < X
    rest_K = K - num
    if rest_K%2 == 0:
        print(rest_X)
    else:
        print(abs(rest_X - D))

Submission status: Accepted

Solution code:
#C
from decimal import Decimal
X,K,D= map(Decimal, input().split())

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

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys


def get_ints():
    return map(int, sys.stdin.readline().strip().split())


def main():
    x, k, d = get_ints()
    ax = abs(x)
    tf = k % 2
    if ax < abs(ax-d) and tf == 0:
        print(x)
    elif ax < abs(ax-d):
        print(abs(x-d))
    else:
        tmp_k, mod_d = divmod(ax, d)
        if tmp_k < k:
            ax -= d * tmp_k
            k -= tmp_k
            tf = k % 2
            if tf == 1:
                ax = abs(ax - d)
            print(ax)
        else:
            ax -= d * k
            print(ax)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if 0 <= X:
    ans = X-D*K
    if 0 < ans:
        print(ans)
        exit(0)
if X < 0:
    ans = X+D*K
    if ans < 0:
        print(abs(ans))
        exit(0)

for i in range(K):
    if 0 <= X:
      absX = abs(X - D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X - D*(i+1))
              print(ans)
              exit(0)
    if X < 0:
      absX = abs(X + D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X + D*(i+1))
              print(ans)
              exit(0)

Submission status: Accepted

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

if x-k*d > 0:
    print(x-k*d)
    exit
elif x+k*d < 0:
    print(-1*(x+k*d))
    exit
else:
    m = 2 * math.ceil(((k-2)*d-x)/(2*d))- k
    print(min(abs(x+m*d), abs(x+(m+2)*d)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,k,d= [int(y) for y in input().split()]
if x<0:
    x=-x

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

Submission status: Accepted

Solution code:
import math
x, k, d = [int(i) for i in input().split()]
def f(i):
    return int(math.fabs(x + (2 * i - k) * d))
def ts(l, r):
    while l + 3 <= r:
        m1 = l + (r - l) // 3
        m2 = r - (r - l) // 3
        if f(m1) < f(m2):
        	r = m2
        elif f(m1) > f(m2):
        	l = m1
        else:
            l = m1
            r = m2
    sol = f(r)
    for i in range(l, r):
      sol = min(sol, f(i))
    return sol
print(ts(0, k))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# from pprint import pprint
# import math
# import collections

# n = int(input())
x, k, d = map(int, input().split(' '))
# a = list(map(int, input().split(' ')))

# 開始位置の符号は正負どちらでも同じなので正にそろえる
x = abs(x)

# 開始距離 >= 総移動量の場合
if x >= k * d:
    ans = x - k * d
# 開始距離 < 総移動量の場合
else:
    # 距離 < 移動量 となるまで移動
    cnt = x // d
    _k = k - cnt

    _x = x - d * cnt

    # この時点の原点からの距離と残移動回数
    # 現在位置のほうが原点に近くかつ残移動回数が偶数であれば戻ってこれるので答えは現在位置
    if _k % 2 == 0:
        ans = _x
    else:
        ans = abs(_x - d)

print(ans)

Submission status: Accepted

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

X = abs(X)
t = X // D
now = X - t*D

if K <= t: 
    ans = X - D*K
else:
    if (K - t) % 2 == 0:
        ans = now
    else:
        ans = abs(now - D)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if abs(X) >= K*D:
    ans = abs(X) - K*D
else:
    if (abs(X)//D)%2 == K%2:
        ans = abs(X)%D
    else:
        ans = abs(abs(X)%D - D)

print(ans)

Submission status: Accepted

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

minus = X < 0
if minus:
  X = -X

R = X // D

if K <= R:
  result = X - D * K
else:
  n = K - R
  n %= 2
  P = X - D * R
  if n == 0:
    result = P
  else:
    result = abs(P - D)
    

print(result)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

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

x = abs(x)

#連続な二分探索
def func(mid): #ここが関数部分
    if x-d*mid < 0:
        return True
    else:
        return False

def binary_search(): #2分探索
    ok = 10**18
    ng = -1
    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if func(mid):
            ok = mid
        else:
            ng = mid

    if abs(x-d*ok) <= abs(x-d*ng):
        return ok
    else:
        return ng

if x-k*d >= 0:
    print(x-k*d)
else:
    times = binary_search()
    ans = abs(x-d*times)
    if even(k-times):
        print(ans)
    else:
        print(min(abs(ans+d),abs(ans-d)))

Submission status: Accepted

Solution code:
l = list(map(int,input().split()))
x = abs(l[0])
k = l[1]
d = l[2]
prev = 0

n = x//d

if k%2 == 0 and n%2 == 1:
  n += 1
elif k%2 == 1 and n%2 == 0:
  n += 1

if n>k:
  n = k
ans = abs(x-n*d)
print(ans)

Submission status: Accepted

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

X = abs(X)
a = int(X/D)
ans = 0

if a>=K:
    ans = X-(D*K)
else:
    if K%2==0:
        if a%2==0: ans = X-(D*a)
        else: ans = X-(D*a)-D
    else:
        if a%2==0: ans = X-(D*a)-D
        else: ans = X-(D*a)

ans = int(abs(ans))
print(ans)

Submission status: Accepted

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

x = abs(x)

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

else:
    if x >= k*d:
        print(x - k*d)
    else:
        #print(min(x%d,abs(x%d-d)))
        print(abs(x%(2*d)-d))

Submission status: Accepted

Solution code:
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def solve():
    x, k, d = map(int, input().split())
    x = abs(x)

    pmin = x - k * d
    if pmin >= 0:
        return pmin

    pmax = x + k * d
    if pmax <= 0:
        return pmax

    dd, dm = divmod(x, d)
    remains = k - dd
    if remains & 1:
        return abs(dm - d)
    else:
        return dm


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

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

if abs(X) % D == 0:
    cnt = abs(X) // D
else:
    cnt = (abs(X) // D) + 1

if cnt >= K:
    ans = abs(X) - D * K
else:
    re = cnt - K
    if re % 2 == 0:
        ans = abs(X) - D * cnt
    else:
        ans = (abs(X) - D * cnt) + D

print(abs(ans))

Submission status: Accepted

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

if abs(x)//d <= k and k>1000 :
  z = k - (abs(x)//d)
  x = abs(x)%d
  if z % 2 == 0:
    print(x)
    exit()
  else:
    if abs(x) + d <= abs(abs(x) - d):
      print(abs(x)+d)
      exit()
    else:
      print(abs(abs(x)-d))
      exit()
elif abs(x)//d > k and k>1000 :
  x = abs(x) - k*d
  print(abs(x))
  exit()
else:
  for i in range(1,k+1):
    if abs(x) >= abs(abs(x) - d):
      x = abs(x) - d
    else:
      if (k-i-1)%2 == 0:
        print(abs(x))
        exit()
      else:
        if abs(x) + d <= abs(abs(x) - d):
          print(abs(x)+d)
          exit()
        else:
          print(abs(abs(x)-d))
          exit()
print(abs(x))

Submission status: Accepted

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: Accepted

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

X = abs(X)

a = X // D

X -= min(a, K) * D
K -= min(a, K)

if K > 0 and abs(X) > abs(X - D):
    X -= D
    K -= 1

K %= 2

if K == 1:
    if abs(X + D) < abs(X - D):
        X += D
    else:
        X -= D

result = abs(X)

print(result)

Submission status: Accepted

Solution code:
# cook your dish here

x,k,d=map(int,input().split())
x=abs(x)
f = x//d;

ans = 0

if(k<=f):
    ans = abs(x) - abs(k*d)
    
else:
    e =  (k - f)%2
    if(e==0):
        ans = x - f*d;
    else:
        ans = x - (f+1)*d;
            
    
print(abs(ans))