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:
def solve():
    X,K,D = map(int,input().split())
    close_zero = None

    if X >= 0:
        close_zero = X // D
        if close_zero > K:
            print(abs(X + (-K) * D))
            return
    else:
        close_zero = - X // D
        if close_zero > K:
            print(abs(X + K * D))
            return
    
    rest_k = K - close_zero
    if rest_k % 2 == 0:
        if X >= 0:
            print(abs(X-close_zero*D))
        else:
            print(abs(X+close_zero*D))
    else:
        if X >= 0:
            print(abs(X-(1+close_zero)*D))
        else:
            print(abs(X+(1+close_zero)*D))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

if X - K*D >= 0:
    print(X - K*D)
    exit()

z_cnt = math.ceil(X/D)
if K%2 == z_cnt%2:
    print(math.ceil(X/D)*D-X)
else:
    print(X - (math.ceil(X / D)-1) * D)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

X, K, D = map(int, sys.stdin.readline().split())

if K*D <= abs(X):
    if 0 <= X:
        print(X - K*D)
    else:
        print(abs(X + K*D))
else:
    dist = abs(X)
    a = dist // D
    # 残りの移動回数
    r = K - a
    if 0 <= X:
        pos1 = X - a * D
        pos2 = pos1 - D
    else:
        pos1 = X + a * D
        pos2 = pos1 + D

    if r % 2 == 1:
        print(abs(pos2))
    else:
        print(abs(pos1))

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)

cnt = x//d
amari = x%d

nokori = k-cnt

if nokori < 0:
  ans = x - d*k
else:
  if nokori%2 == 0:
    ans = amari - d + d
  else:
    ans = amari - d

print(abs(ans))

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
if X-K*D>0:
    print(X-K*D)
else:
    kaisu=int(X/D)
    gi1=X-kaisu*D
    gi4 = abs(gi1)
    if (K - kaisu) % 2 == 0:
        print(gi4)
    else:
        gi2=abs(gi1-D)
        gi3=abs(gi1+D)
        kot=gi2
        if gi3<kot:
            kot=gi3
        print(kot)

Submission status: Accepted

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

ma = D * K
ans = 0
if abs(X) >= ma:
    ans = abs(X) - ma
    print(ans)
else:
    div,mod = divmod(X,D)
    if (div - K) % 2 == 0:
        print(mod)
    else:
        tmp = (div + 1) * D
        print(abs(X - tmp))

Submission status: Accepted

Solution code:
# coding: utf-8

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

main()

Submission status: Accepted

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

kk = k % 2
y = (x + d*kk) % (2*d)

z = abs(x) // d

if z >= k:
    print(abs(x) - (k*d))
else:
    if d >= y:
        print(y)
    else:
        print((2*d)-y)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n = x // d

if abs(n) > k:
    if x < 0:
        print(abs(x+k*d))
        exit()
    elif x > 0:
        print(x-k*d)
        exit()

if abs(x-d*n) > abs(x-d*(n+1)):
    a = abs(x-d*(n+1))
    b = n+1
else:
    a = abs(x-d*n)
    b = n

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

Submission status: Accepted

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

min_abs = 10**15
min_step = X // D
if K <= min_step:
    min_abs = X - D*K
else:
    lowest_plus = X - D*min_step
    highest_minus = abs(lowest_plus - D)
    if (K - min_step) % 2 == 0:
        min_abs = lowest_plus
    else:
        min_abs = highest_minus

print(min_abs)

Submission status: Accepted

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

def f( x, k, d, left):
	return abs(x + left*(-d) + (k-left)*d)


l = 0
r = k
res=10000000000000000;
while(r - l >2):
	
	m1 = l + (r - l) // 3
	m2 = r - (r - l) // 3
	
	f1 = f(x,k,d,m1)
	f2 = f(x,k,d,m2)
	
	res=min(res,f1)
	res=min(res,f2)
	
	if (f1 < f2):
		r = m2
	else:
		l = m1


for _i in range(l,r+1):
	cur = f(x,k,d,_i)
	res=min(res,cur)


print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


x, k, d = MI()

q, r = divmod(abs(x), d)

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

Submission status: Accepted

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

ans = x
if k > 0 and x >= 0:
    if k % 2 != 0:
        ans = x - d
    else:
        ans = x
elif k > 0:
    if k % 2 != 0:
        ans = x + d
    else:
        ans = x

print(abs(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
if x < 0:
    x *= -1
t1 = x % d
t2 = t1 - d
n = (x - t1) // d
if k == n:
    print(t1)
elif k < n:
    print(x - k * d)
else:
    n = k - n
    if n % 2 == 0:
        print(t1)
    else:
        print(-t2)

Submission status: Accepted

Solution code:
X, K, D = [abs(int(i)) for i in input().split()]
ans = 0
if K*D < X:
    ans = X - K * D
elif X//D%2 == 0 and K%2 == 0 or X//D%2 * K%2 == 1:
    ans = X%D
else:
    ans = D - X % 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)
else:
    if (K - X // D) % 2 == 0:
        print(X%D)
    else:
        print(D - X%D)

Submission status: Accepted

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

dist = abs(x)

if dist-k*d <= 0:
    k -= dist//d
    if k%2 == 0:
        print(abs(dist%d))
    else:
        print(abs(dist%d-d))
    # print(min(abs(dist%d),abs(dist%d-d)))
else:
    print(dist-k*d)

Submission status: Accepted

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

if abs(X) - K * D > 0:
    print(abs(X) - K * D)
else:
    i = round((K * D - abs(X)) / (2 * D))
    print(abs(abs(X) - K * D + i * D * 2))

Submission status: Accepted

Solution code:
import sys
X,K,D=map(int,input().split())
Xa=abs(X)
Da=abs(D)
one=Xa//Da

if K < one:
    print(Xa-Da*K)
    sys.exit()


guki=(K-one)%2

if guki==0:
    print(abs(Xa-Da*one))
else:
    print(abs(Xa-Da*(one+1)))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def mp(): return map(int, input().split())
def lmp(): return list(map(int, input().split()))
x,k,d = mp()
x = abs(x)
if x - k*d >= 0:
    print(x-k*d)
else:
    u = x % d
    l = abs(u - d)
    if (k - x//d) % 2 == 0:
        print(u)
    else:
        print(l)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    X, K, D = [int(x) for x in input().split()]

    kcnt = abs(X) // D

    if kcnt >= K:
        print(abs(X) - (K * D))
        return

    ans1 = abs(X) % D
    ans2 = abs(ans1 - D)

    if (K - kcnt) % 2 == 0:
        print(ans1)
    else:
        print(ans2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
x, k, d = map(int, input().split())
mx = x + k*d
mn = x - k*d
ans = min(abs(mx), abs(mn));
p = abs(x)//d
# print(p)
l = max(p-1, 0)
r = min(p+1, k)
for n in range(l, r+1):
	rem = k - n
	mx = x + n*d
	mn = x - n*d
	# print(n, rem, mx, mn)
	if rem % 2 == 0:
		# print("here", mn, " ", mx)
		ans = min(ans, abs(mx), abs(mn))
	else:
		ans = min(ans, abs(mx+d), abs(mn+d));
		ans = min(ans, abs(mx-d), abs(mn+d));
		ans = min(ans, abs(mx+d), abs(mn-d));
		ans = min(ans, abs(mx-d), abs(mn-d));

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

move_num_before = abs(X)//D
move_num_after = move_num_before+1
if (K - move_num_after)%2 == 0:
    print(abs(abs(X)-move_num_after*D))
    exit()
print(abs(X)-move_num_before*D)

Submission status: Accepted

Solution code:
# cook your dish here
x,k,d=map(int,input().split())
l=0
r=k
while (l<r-1):
    m=l+(r-l)//2
    if(x-k*d+2*m*d>0): r=m
    else: l=m
ans1=x-k*d+2*l*d;
ans2=x-k*d+2*r*d;
print(min(abs(ans1),abs(ans2)))

Submission status: Accepted

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

a=abs(x)//d

if x<0:
  d=d*(-1)

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

Submission status: Accepted

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

candidates = []

if abs(X) >= abs(K*D):
    candidates.append(abs(X - K * D))
    candidates.append(abs(X + K * D))
else:
    tmp = X // D  # 何回移動すれば良いのか
    if tmp % 2 == K % 2:
        candidates.append(abs(X - tmp * D))
        candidates.append(abs(X + tmp * D))
    else:
        candidates.append(abs(X - (tmp+1) * D))
        candidates.append(abs(X + (tmp+1) * D))
        candidates.append(abs(X - (tmp-1) * D))
        candidates.append(abs(X + (tmp-1) * D))

print(min(candidates))

Submission status: Accepted

Solution code:
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    if X >= K * D:
        return X - K * D
    else:
        div = X // D
        mod = [X % D, abs(X % D - D)]
        return mod[(K - div) % 2]

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

Submission status: Accepted

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

d = abs(X) // D
if d >= K:
  if X >= 0:
    ans = X - K * D
  else:
    ans = X + K * D
  print(abs(ans))
  exit()

if X >= 0:
  ans = X - d * D
else:
  ans = X + d * D

n = K - d
if n % 2 == 1:
  if ans >= 0:
    ans -= D
  else:
    ans += D

print(abs(ans))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

cnt = int(x/d)

x -= d*min(cnt, k)

if x > d/2 and cnt < k:
  x = abs(x - d)
  cnt += 1

if (k-cnt) % 2 == 1 and cnt < k:
  x -= d

print(abs(x))

Submission status: Accepted

Solution code:

#n=int(input())
x,k,d=[int(c) for c in input().split()]


if k*d< abs(x):
     print(abs(x)-(k*d))

elif (k*d)==abs(x):
    print(0)

else:

    x=abs(x)

    r=(x//d)

    p=x-r*d

    q=abs(p-d)

    k=k-r

    if k%2==1:
        print(q)
    else:
      print(p)

Submission status: Accepted

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

amari = x % d
syou = x // d
a = k - syou
if a % 2 == 0:
    print(amari)
else:
    print(abs(d-amari))

Submission status: Accepted

Solution code:
x,k,d=map(int,input('').split(' '))
if x<0:
    x=x*(-1)

if x>=k*d:
    print(x-k*d)
else:
    e=x//d
    f=x%d
    g=f-d
    if g<0:
        g=g*(-1)
    
    if (k-e)%2==0:
        print(f)
    else:
        print(g)

Submission status: Accepted

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


saidai = X + K * D
saisyou = X - K * D

if(saidai == 0):
    ans = 0
elif(saisyou == 0):
    ans = 0
else:
    if(saidai > 0 and saisyou > 0):
        ans = abs(saisyou)
    elif(saidai < 0 and saisyou < 0):
        ans = abs(saidai)
    else:
        L = X // D
        P = X - L * D
        #print(L)
        if(L % 2 == K % 2):
            l = [abs(P-6*D), abs(P - 4*D), abs(P-2*D), abs(P), abs(P+2*D), abs(P+4*D), abs(P+6*D)]
        else:
            l = [abs(P-5*D), abs(P - 3*D), abs(P-D), abs(P+D), abs(P+3*D), abs(P+5*D)]
        ans = min(l)
            
        '''
        a = X - L * D
        b = a + D
        c = b + D
        d = a - D
        e = d - D
        l = [abs(a), abs(b), abs(c), abs(d), abs(e)]
        ans = min(l)
        '''
        #ans = -1

print(ans)

Submission status: Accepted

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

q = abs(x) // d

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

mod = abs(x) % d


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

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=tf-8
#

"""
"""

from collections import defaultdict
from itertools import combinations
import bisect

import sys
input = sys.stdin.readline

def solve(x,k,d):
    x = abs(x)
    a = x // d
    if k <= a:
        ans = x - k * d
    elif (k - a) % 2 == 0:
        ans = x % d
    else:
        ans = d - x%d
    print(ans)

            

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

s = x // d

if s > k:
  nx = x - k * d
  print(nx)

else:
  nx = x - s * d
  nk = k - s
  if nk % 2 != 0:
    nx = abs(nx - d)
  print(nx)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
"""
def main():
    X, K, D = map(int, input().split())
    X = abs(X)

    answer = int()
    if X > 0:
        if X >= D*K:
            answer = X-D*K
        else:
            if (X//D)%2 == K%2:
                answer = X%D
            else:
                answer = D-X%D
    elif X == 0:
        if D%2 == 0:
            answer = 0
        else:
            answer = D
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

if(abs(x) - k*d >= 0):
    print(abs(x) - k*d)
else:
    num = abs(x) // d
    rest = abs(x) % d

    if(num % 2 == 0):
        if(k%2==0): print(rest)
        else: print(d-rest)
    else:
        if(k%2==1): print(rest)
        else: print(d-rest)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import copy
from collections import deque
import collections
import itertools
stdin = sys.stdin

mod = 10**9+7

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

x, k, d = na()

r = 1
if x < 0:
    r = -1

a = abs(x)//d

if a > k:
    print(r*x-k*d)
else:
    res1 = r*x-a*d
    res2 = res1 - d
    if (k-a)%2==0:
        print(abs(res1))
    else:
        print(abs(res2))

Submission status: Accepted

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

x = abs(x)

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

else:
    if (k - n) % 2 == 0:
        ans = x - (n*d)
    else:
        ans = x - ((n+1)*d)

print(abs(ans))

Submission status: Accepted

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

if abs(X)>=K*D:
    if X>=0:
        print(abs(X-K*D))
    else:
        print(abs(X+K*D))
else:
    a=math.ceil(X/D)
    b=X-a*D
    K-=a
    if K%2==0:
        print(abs(b))
    else:
        print(abs(b+D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os

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

x = abs(x)

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
a = x // d # 必要回数
if k <= a:
  print(x - k * d)
  exit()
k -= a
eveng = x - d * a
oddg = abs(x - d * (a + 1))
if k % 2 == 0:
  print(eveng)
else:
  print(oddg)

Submission status: Accepted

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

if x - k * d >= 0:
    print(x - k * d)
elif x + k * d <= 0:
    print(abs(x + k * d))
else:
    dis = x % d
    dis_p = d - dis
    c = k - x // d

    if c % 2 == 0:
        print(dis)
    else:
        print(dis_p)

Submission status: Accepted

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

cnt = X // D
ans1 = X - cnt * D
ans2 = abs(ans1 - D)

if abs(cnt) < K:
  if (K - abs(cnt))%2 == 0:
    print(ans1)
  else:
    print(ans2)
else:
  if X >= 0:
    print(X - K * D)
  else:
    print(abs(X + K * D))

Submission status: Accepted

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

Submission status: Accepted

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

#最小に要する回数

px = x%d
mx = x%d - d

pc = abs(x - px) // d
mc = abs(x - mx) // d

if min(pc,mc) < k:
  if ( (k-pc) % 2) == 0:
    print( abs(px) )
  else:
    print( abs(mx) )
else:
  print( abs(x) - d * k )

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Y = X

if X < 0:
    X = -X

i = 0
posA = X % D
posB = posA - D
i = X // D + 1

if K >= i:
    if K % 2 == i % 2:
        print(abs(posB))
    else:
        print(abs(posA))
else:
    print(abs(X-D*K))

Submission status: Accepted

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

if K * D < x:
  print(x - K * D)
  exit(0)

v = x // D # ここまでの移動回数
x = x % D  # 現時点でのx

nokori = (K - v) % 2
if nokori:
  print(abs(x - D))
else:
  print(x)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# from typing import *
import sys
import math
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def solve(X, K, D):

    X = abs(X)
    if X/K > D or X/D > K or X > K*D :
        print(X-K*D)
        return
    cnt = X//D
    X %= D
    ans1 = X
    ans2 = D-X
    K -= cnt
    if K % 2 == 1:
        print(ans2)
    else:
        print(ans1)




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

if __name__ == '__main__':
    main()


'''
cd ~/Desktop/Programming/AtCoder/contest/abc175/abc175_c && sh shellscripts/test_py.sh
cd ~/Desktop/Programming/AtCoder/contest/abc175/abc175_c && sh shellscripts/etest_py.sh
cd ~/Desktop/Programming/AtCoder/contest/abc175/abc175_c && sh shellscripts/submit_py.sh
'''

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
if k * d < abs(x):
    if x >= 0:
        ans = x - k*d
    else:
        ans = x + k*d
    print(abs(ans))
    exit()
    
xd = x // (2*d)
if k % 2:
    tmp = x - 2*d*xd
    l = [abs(tmp-d), abs(tmp+d), abs(tmp+3*d)]
    ans = min(l)
else:
    tmp = x - 2*d*xd
    l = [abs(tmp-2*d), abs(tmp), abs(tmp+2*d)]
    ans = min(l)
print(ans)

Submission status: Accepted

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

X = abs(X)
a = X / D
b = round(a)

c = K - b
e = 0

if c < 0:
    print(X-(K*D))
    e = 1
elif c % 2 != 0:
    if a > b:
        b = b + 1
    else:
        b = b - 1
if e == 0:
    d = abs(X - (D*b))
    print(d)

Submission status: Accepted

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

    L = abs(X)

    r = L // D
    c = L % D

    if K < r:
        print(L - K * D)
    elif K == r:
        print(c)
    elif K == r + 1:
        print(D - c)
    else:  # K > r + 1:
        if K % 2 == r % 2:
            print(c)
        else:
            print(D - c)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import ceil

x,k,d = map(int,input().split())
x = abs(x)
steps = ceil(x/d)
if k <= steps:
  x = x-d*k
  print(abs(x))
else:
  k -= steps
  x = x-d*steps
  if k&1:
    print(abs(x+d))
  else:
    print(abs(x))

Submission status: Accepted

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

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

Submission status: Accepted

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

X = abs(X)

if X >= K*D:
    print(X-K*D)
else:
    t,r = divmod(X-D, D)
    if r>0:
        t+=1
    K -= t
    
    if K%2==0:
        print(X - t*D)
    else:
        print(abs(X - (t+1)*D))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# from numba import njit

# input = stdin.readline

# @njit
def solve(x,k,d):
  cand1 = x % d
  cand2 = -(abs(cand1) -  d)
  delta_c1 = abs(cand1 - x)
  delta_c2 = abs(cand2 - x)
  if abs(x//d) > k:
    return abs(x) - k*d
  elif (k-x//d) % 2 == 0:
    return cand1
  else:
    return cand2



def main():
  X,K,D = map(int,input().split())
  # a = list(map(int,input().split()))
  print(solve(X,K,D))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    X, K, D = map(int, input().split())
    if X < 0:
        X *= (-1)
    i = min(X // D, K)
    out = X - i * D
    if (K - i) % 2 == 0 or K < X//D:
        print(out)
    else:
        print(min(abs(out - D), abs(out + D)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if(X<0):
  X = -X
if(X>=K*D):
  print(X-K*D)
else:
  p = X % D
  q = (X-p)//D
  if((K-q) % 2 == 0):
    print(p)
  else:
    print(D - p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    X,K,D=mi()

    if X == 0:
        ans = D if K % 2 == 1 else 0
        print(ans)
        exit()
    
    n = abs(X)//D
    ans = INF
    for n2 in [n-1,n,n+1]:
        m = K-n2

        if X > 0:
            res = X - D*n2
        else:
            res = X + D*n2

        if m <= 0:
            if X > 0:
                res = X - D*K
            else:
                res = X + D*K
            ans = min(ans,abs(res))
            continue


        if m % 2 == 1:
            res = min(abs(res + D), abs(res-D))
        else:
            res = res
        
        ans = min(ans,abs(res))
    
    print(ans)






if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = (int(x) for x in input().split())
if x<0:
    x = -x
if k*d < x:
    print(x-k*d)
else:
    div = x//d
    k-=div
    x-=div*d
    if k%2==0:
        print(x)
    else:
        print(d-x)

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)
m = x//d

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