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

# x:位置　k：回数　d:距離

#往復に辿りつく前に終わるパターン
if(abs(x)-d*k >= 0):
    print(abs(abs(x)-d*k))
#0またいで往復するパターン
else:
    #往復直前までにかかる回数
    r_count = abs(x)//d
 
    #残り捜査回数
    rest = k - r_count

    #残り回数の奇遇によって計算を判断
    #残り回数が偶数の時はr_count分動かした時と変わらない
    if(rest %2 == 0):
        print(abs(abs(x) - d*r_count))
    #残り回数が奇数:さらにD移動した分？
    else:
        print(abs(abs(abs(x) - d*r_count)-d))


#通らない.....

Submission status: Accepted

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

walk = min(k,x//d)
k -= walk
x -= walk*d

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

Submission status: Accepted

Solution code:


startX, countK, distanceD = map(int, input().split())
n = int(abs(startX) / distanceD)
ans = abs(startX) % distanceD
if n > countK:
    ans += distanceD * (n - countK)
else:
    if (countK - n) % 2 == 1:
        ans= abs(ans - distanceD)
print(ans)

Submission status: Accepted

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

ax = abs(x)
n = ax // d
if n >= k:
    ans = ax - k*d
else:
    a = ax - n*d
    if (k - n) % 2:
        ans = abs(a - d)
    else:
        ans = abs(a)

print(ans)

Submission status: Accepted

Solution code:
from math import floor, ceil

def solve(X, D, lb, ub):
    # min abs(X + t * D) (lb <= t <= ub) (lb, ub, t are int)
    ts = [
        lb,
        ub,
        max(min(floor(-X / D), ub), lb),
        max(min(ceil(-X / D), ub), lb),
    ]
    return min([abs(X + t * D) for t in ts])

X, K, D = map(int, input().split())
if K % 2 == 0:
    ans = solve(X, D * 2, -(K // 2), +(K // 2))
else:
    ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
    ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
    ans = min(ans1, ans2)
print(ans)

Submission status: Accepted

Solution code:
from math import floor

def solve(X, D, lb, ub):
    # min abs(X + t * D) (lb <= t <= ub) (lb, ub, t are int)
    ts = [
        lb,
        ub,
        max(min(floor(-X / D), ub), lb),
        max(min(floor(-X / D) + 1, ub), lb),
    ]
    return min([abs(X + t * D) for t in ts])

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

if K % 2 == 0:
    ans = solve(X, D * 2, -(K // 2), +(K // 2))
else:
    ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
    ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
    ans = min(ans1, ans2)
print(ans)

Submission status: Accepted

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

X = abs(X)
reach_or_before = X // D


if K <= reach_or_before:
    print(X - D * K)
else:
    #remaining jump
    remain = K - reach_or_before
    before_dist = X % D #(>0)
    after_dist = D - before_dist
    if remain % 2 == 0:
        print(before_dist)
    else:
        print(after_dist)

Submission status: Accepted

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

X = abs(X)
reach_or_before = X // D


if K <= reach_or_before:
    print(X - D * K)
else:
    #remaining jump
    remain = K - reach_or_before
    before_dist = X - D * reach_or_before #(>0)
    after_dist = D * (reach_or_before + 1) - X
    if remain % 2 == 0:
        print(before_dist)
    else:
        print(after_dist)

Submission status: Accepted

Solution code:
from math import floor

def solve(X, D, lb, ub):
    # min abs(X + t * D) (lb <= t <= ub) (lb, ub, t are int)
    ts = [
        lb,
        ub,
        max(min(floor(-X / D), ub), lb),
        max(min(floor(-X / D) + 1, ub), lb),
    ]   
    return min([abs(X + t * D) for t in ts])

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

y1 = X - K * D
y2 = X + K * D
if (y1 > 0 and y2 > 0) or (y1 < 0 and y2 < 0): # same sign
    print(min(abs(y1), abs(y2)))
else:
    if K % 2 == 0:
        ans = solve(X, D * 2, -(K // 2), +(K // 2))
    else:
        ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
        ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
        ans = min(ans1, ans2)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
##C - Walking Takahash
##正負が反転しないなら単調増加、減少
##反転するなら、反転した瞬間で残りの回数が偶数なら、その値、奇数なら、一つ前にの値
X,K,D = map(int,input().split())
##X1の正負フラグがあれば簡潔に書けるかも
if X < 0:
    X = -X
R = K - X // D
if X-K*D >= 0:
    print(X-K*D)
else:
    if R % 2 == 0:
        print(X - D*(X//D))
    else:
        print(abs(X - D*(X//D)-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:
    M = X // D
    K -= M
    X -= M*D

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

Submission status: Accepted

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

if x%d==0:
    minNear  = x%d + d
    minFar = x%d
    minTransitionToNear = (x-minNear)/d
    #print(minNear)
    #print(minFar)
    #print(minTransitionToNear)
    
else:
    minNear  = x%d
    minFar = x%d - d
    minTransitionToNear = (x-minNear)/d
    #print(minNear)
    #print(minFar)
    #print(minTransitionToNear)
    
if k<minTransitionToNear:
    print(x-k*d)
else:
    if (k-minTransitionToNear)%2==0:
        print(abs(minNear))
    else:
        print(abs(minFar))

Submission status: Accepted

Solution code:
# coding=utf-8

if __name__ == '__main__':
    X, K, D = map(int, input().split())

    ABS_X = abs(X)

    if K * D <= ABS_X:
        ans = ABS_X - (K * D)

    else:
        r = ABS_X // D
        l = ABS_X // D + 1
        if r % 2 == K % 2:
            ans = abs(ABS_X - D * r)
        else:
            ans = abs(ABS_X - D * l)

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

# 移動できる距離の max は KD

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split());xx=abs(x)
tmp=xx;tmp=(xx//d);cc=min(tmp,k)
xx-=d*cc;k-=cc;k&=1;xx-=d*k
print(abs(xx))

Submission status: Accepted

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

Submission status: Accepted

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

a = abs(X)//D
b = abs(X)%D
if (K - a)%2 == 1 and a < K:
    ans = D - b
elif (K - a)%2 == 0 and a < K:
    ans = b

print(ans)

Submission status: Accepted

Solution code:
X,K,D=(int(x) for x in input().split())
x=abs(X)
a=x//D
 
b=x-a*D
B=abs(x-(a+1)*D)
 
if b>B:
  a=a+1
 
if a>=K:
  print(abs(x-K*D))
 
else:
  c=K-a
  if c%2 == 0:
    print(abs(x-a*D))
  else:
    d=abs(x-(a-1)*D)
    e=abs(x-(a+1)*D)
    print(min(d,e))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D=(int(x) for x in input().split())
x=abs(X)
a=x//D

b=x-a*D
B=abs(x-(a+1)*D)

if b>B:
  a=a+1

if a>=K:
  print(abs(x-K*D))

else:
  c=K-a
  if c%2 == 0:
    print(abs(x-a*D))
  else:
    d=abs(x-(a-1)*D)
    e=abs(x-(a+1)*D)
    print(min(d,e))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# input
X, K, D = map(int, input().split())
DIV = (X + K * D) // (2 * D)

if X + K * D < 0:
    ans = -(X + K * D)
elif X - K * D > 0:
    ans = X - K * D
else:
    ans = min(X + K * D - DIV * D * 2, -(X + K * D - (1 + DIV) * D * 2))
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k,d=map(int,input().split())
n=abs(n)
q=n//d
r=n%d
if(k<=q):
    print(n-k*d)
else:
    rem=k-q
    if(rem%2==0):
        print(r)
    else:
        print(min(abs(r-d),abs(r+d)))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

x = abs(x)

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

bai = x//d

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

Submission status: Accepted

Solution code:
xkd = input().split()
X = int(xkd[0])
K = int(xkd[1])
D = int(xkd[2])

X = abs(X)
ans = 0
times = int(X/D)
oneMore = False
if X%D > abs(D-X%D):
    times+=1

if times > K:
    ans = X - (D*K)
else:
    if (times%2) == (K%2):
        ans = min(X%D, (D-X%D))
    else:
        ans = max(X%D, (D-X%D))

print(ans)

Submission status: Accepted

Solution code:
def main():
    x,k,d = map(int,input().split())
    x = abs(x)
    if x < k*d:
        # minimum = min(d-(x%d),x % d)
        cnt, amari = divmod(x,d)
        if abs(amari-d) < amari:
            amari = abs(amari - d)
            cnt += 1
        if (k-cnt)%2==0:
            print(amari)
        else:
            print(abs(amari-d))

    else:
        print(x-k*d)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

import math

X=abs(X)
U=min(math.floor(X/D),K)
K=K-U
A=X-U*D

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

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

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

Submission status: Accepted

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

if x < 0 :
  x = -x

l = x // D

if K > l:
  K = K - l
  if (K % 2) == 0:
    print(abs(x-D*l))
  else:
    print(abs(x-D*(l+1)))
else:
  print(abs(x - D*K))

Submission status: Accepted

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

if (X // D) >= K:
    ans = X - (K * D)
elif (X // D) % 2 == 0:
    if K % 2 == 0:
        ans = X % D
    else:
        ans = abs((X % D) - D)
else:
    if K % 2 ==1:
        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)

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

Submission status: Accepted

Solution code:
X, K, D = (int(x) for x in input().split())

if X > 0:
    div, mod = divmod(X, D)
    if div >= K:
        answer = X - K*D
    else:
        if (K - div)%2 == 0:
            answer = X - div*D
        else:
            answer = abs(X - (div+1)*D)
elif X < 0:
    div, mod = divmod(-X, D)
    if div >= K:
        answer = abs(X + K*D)
    else:
        if (K - div)%2 == 0:
            answer = abs(X + div*D)
        else:
            answer = X + (div+1)*D
else:
    if K%2 == 0:
        answer = 0
    else:
        answer = D

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

if X<0:
    X = -X
if X - K*D >= 0:
    print(X-K*D)
else:
    i = 0
    while True:
        i+=1
        X -= D
        if X < 0:
            break
    if (K-i) % 2 == 0:
        print(abs(X))
    else:
        print(D-abs(X))

Submission status: Accepted

Solution code:
first_place, K, Distance = map(int, input().split())
first_place = abs(first_place)
test = first_place
if first_place - Distance*K > 0:
    print(first_place -Distance*K)
else:
    for i in range(1,K+1):
        test -= Distance
        if test <= 0:
            a = i
            #print(a)
            break
    if (K-a)%2 == 0 or K-a == 0:
        print(abs(first_place - Distance*a))
    else:
        print(abs(first_place - Distance*(a-1)))

Submission status: Accepted

Solution code:
from collections import deque

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

# まずは何回目で符号が逆転するかを算出
num_reverse = abs(X) // D + 1
if num_reverse > K:
    print(abs(X) - (K * D))
else:
    K -= num_reverse
    ans = abs(X) - (num_reverse * D)
    if K % 2 == 0:
        print(abs(ans))
    else:
        print(abs(abs(ans) - 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:
    k -= x // d
    if k % 2 == 0:
        print(x % d)
    else:
        print(abs(x % d - d))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if X > 0:
    X -= D*k
    if abs(X-D) < abs(X):
        X -= D
        K -= 1
elif X < 0:
    X += D*k
    if abs(X+D) < abs(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:
def abc175_c_3():
    x, k, d = map(int, input().split())

    def solve(x, k, d):
        """
        https://atcoder.jp/contests/abc175/editorial/52
        """

        x = abs(x)

        straight = int(min(k, x / d))
        k -= straight
        x -= straight * d

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

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_3()

Submission status: Accepted

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

    def solve(x, k, d):
        # Xを絶対値でプラスの値にし、0に近づける問題に置き換える
        x = abs(x)

        # (x // d)は0の手前まで移動するのに必要な回数。kの方が小さい場合はk回移動してしまえばよい
        if k < (x // d):
            return x - (k * d)
        # kのほうが大きい場合はそこから数回移動が必要
        else:
            # xからとりあえずx//d回は移動してOK
            current_x = x - d * (x // d)
            k = k - (x // d)

            # 結局ここをfor文で探索すると時間がかかるので終わらない。(10**30回とか探索しちゃう。)
            if (k % 2) == 0:
                # 残りの移動回数が偶数回なら移動を繰り返してその場所に戻ってくる
                return current_x
            else:
                # 奇数回の場合、偶数回移動を繰り返した後、もう一回だけ移動する。
                # この時、current_xは0以上なので、マイナス方向に動けばOK
                return abs(current_x - d)

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_2()

Submission status: Accepted

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

    def solve(x, k, d):
        # Xを絶対値でプラスの値にし、0に近づける問題に置き換える
        x = abs(x)

        # (x // d)は0の手前まで移動するのに必要な回数。kの方が小さい場合はk回移動してしまえばよい
        if k < (x // d):
            return x - (k * d)
        # kのほうが大きい場合はそこから数回移動が必要
        else:
            # xからとりあえずx//d回は移動してOK
            current_x = x - d * (x // d)
            k = k - (x // d)

            # 結局ここが時間がかかるので終わらない。(10**15回とか探索しちゃう。)

            if (k % 2) == 0:
                # 残りの移動回数が偶数回なら移動を繰り返してその場所に戻ってくる
                return current_x
            else:
                # 奇数回の場合、偶数回移動を繰り返した後、もう一回だけ移動する。
                # return min(abs(current_x + d), abs(current_x - d))
                return abs(current_x - d)

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_1()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math

X, K, D = [int(i) for i in input().split()]

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

a = X // D if X > 0 else math.ceil(X / D)
k = K - a
k = k % 2

print(abs(X - D * a - k * D if X > 0 else X - D * a + k * D))

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().rstrip('\n')


def calc(X, K, D):
    X = abs(X)
    n = X // D
    if K <= n:
        return abs(X - K * D)
    if (K - n) % 2 == 0:
        return abs(X - n * D)
    else:
        return abs(X - (n + 1) * D)


(X, K, D) = tuple([int(s) for s in input().split()])
print(calc(X, K, D))

Submission status: Accepted

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

result = 0
if abs(X) > K * D:
    result = abs(X) - K * D
else:
    y = int(round(abs(X) / D))
    resX = abs(abs(X) - D * y)
    resK = K - y
    if resK % 2 == 1:
        result = abs(resX - D)
    else:
        result = resX
print(result)

Submission status: Accepted

Solution code:
def solve():
  X, K, D = map(int, input().split())
  X = abs(X)
  if X>=K*D:
    return X-K*D
  X -= D*(K%2)
  r =  X%(D*2)
  return min(r,2*D-r)
print(solve())

Submission status: Accepted

Solution code:
import math

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

abs_x = abs(x)

max_len = math.ceil(abs_x/d)*d
min_len = max_len-d

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

if (math.ceil(abs_x/d)-1)%2 == k%2:
    print(abs(abs_x-min_len))
else:
    print(abs(abs_x-max_len))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

cur = abs(x)
rem = k

cnt = min(cur // d, k)
cur -= d * cnt
rem -= cnt

if rem > 0:
    if rem % 2 == 1:
        cur -= d

ans = abs(cur)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
import collections
import itertools
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint=====================================
N, K = map(int, input().split())
N, M = map(int, input().split())
A, B = map(int, input().split())

H, W = map(int, input().split())

L, R = map(int, input().split())

1行のstring=================================
S, T = input().split()

1行の整数配列================================
P = list(map(int,input().split()))

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
D = list(map(int,input().split()))

L = list(map(int,input().split()))
R = list(map(int,input().split()))

T = list(map(int,input().split()))

改行あり整数==================================
X = []
Y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    X.append(x1)
    Y.append(y1)

A = []
for i in range(N):
    a=int(input())
    A.append(a)

Pair = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    Pair.append((x1, y1))
    
2次元整数配列=====================================
A = []
for i in range(N):
    tmp = list(map(int,input().split()))
    A.append(tmp)


N行M列の初期化====================================
dp = [[VAL] * RETU for i in range(GYO)]

'''





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

X = abs(X)
amari = K - X // D

if (D * K <= X):
	print(X - D * K)
elif (amari % 2 == 0):
	print( X % D)
else:
	print(abs(D - (X % 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:
    tempK = X//D
    if (K-tempK)%2==0:
        print(X-D*tempK)
    else:
        print(abs(X-D*tempK-D))

Submission status: Accepted

Solution code:
from __future__ import division, print_function
# import threading
# threading.stack_size(2**27)
# import sys
# sys.setrecursionlimit(10**7)
# sys.stdin = open('inpy.txt', 'r')
# sys.stdout = open('outpy.txt', 'w')
from sys import stdin, stdout
import bisect            #c++ upperbound
import math
import heapq
i_m=9223372036854775807
def inin():
    return int(input())
def stin():
    return input()
def spin():
    return map(int,stin().split())
def lin():                           #takes array as input
    return list(map(int,stin().split()))
def matrix(n):
    #matrix input
    return [list(map(int,input().split()))for i in range(n)]

################################################
def calculate_sum(a, N): #sum of a to N
    # Number of multiples 
    m = N / a 
    # sum of first m natural numbers 
    sum = m * (m + 1) / 2
    # sum of multiples 
    ans = a * sum
    return ans
def series(N):
    return (N*(N+1))//2

def count2Dmatrix(i,list):
    return sum(c.count(i) for c in list)

def modinv(n,p):
    return pow(n,p-2,p)

def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x
def LCM (x, y):
    return (x * y) // GCD(x, y)
    
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
def isprime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            # print(f)
    return j
# d = {}
 
"""*******************************************************"""
x, k, d = spin()
x = abs(x)
m = x//d
ans = 0
if m > k:
    ans = x - k * d
else :
    x -= d*m
    k -= m
    if k%2==1:
        x -= d
        ans = abs(x)
    else :
        ans = abs(x)
print(ans)

Submission status: Accepted

Solution code:
#0:initial pos, 1:the number, 2:len of a step
line = list(map(int, input().split( )))
iniabs = abs(line[0])
#原点まであと一歩の回数, その時の原点との距離, 残り歩数の偶奇
lstep = iniabs//line[2]
ldis = iniabs%line[2]
odeven = (line[1] - lstep)%2
if lstep > line[1]:
  print(iniabs - line[2]*line[1])
elif odeven == 0:
  print(ldis)
else:
  print(line[2] - ldis)

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
m = x//d
ans = 0
if m > k:
    ans = x - k * d
else :
    x -= d*m
    k -= m
    if k%2==1:
        x -= d
        ans = abs(x)
    else :
        ans = abs(x)
print(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:
    rest = k - x//d
    if rest%2==0:
        print(x-d*(x//d))
    else:
        print(abs(x-d*(x//d+1)))

Submission status: Accepted

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

x,k,d=input2()
abs_x=abs(x)
count=min(abs_x//d,k) #移動回数
abs_x-=d*count #到達点
rem=k-count


if rem>0:
	if rem%2==1:
		abs_x-=d #abs_x>=0は自明なので，-でok

ans=abs(abs_x)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
it = lambda: list(map(int, input().strip().split()))

def solve():
    X, K, D = it()
    X = abs(X)
    if X - K * D >= 0:
        return X - K * D
    K = K - X // D
    X = X - X // D * D
    assert K >= 0
    assert X >= 0
    return abs(X) if K % 2 == 0 else abs(X - D)


if __name__ == '__main__':
    ans = solve()
    print(ans)

Submission status: Accepted

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

min_p = abs(x)//d
min_n = min_p + 1
rest_p = abs(x)- d*min_p
rest_n = rest_p - d

if k < min_p:
  print(abs(x) - k*d)
elif (k-min_p) % 2 == 0:
  print(rest_p)
else:
  print(abs(rest_n))

Submission status: Accepted

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

x = abs(x)
kk = x // d
amari = x % d

if kk >= k:
    ans = (kk - k) * d + amari
elif (k - kk) % 2 == 1:
    ans = abs(amari - d)
else:
    ans = amari

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

if X > K*D:
    print(X-K*D)
else:
    c=math.floor(X/D)
    print( abs(X-D*c-((K-c)%2)*D) )

Submission status: Accepted

Solution code:
# -*- using:utf-8 -*-

class main:
  def function(x, k, d):
    if x >= k * d:
      ans = x - k * d
    else:
      r = x // d
      n = k - r
      if n % 2 == 0:
        ans = x % d
      else:
        ans = abs(x % d - d)
    return ans

if __name__ == "__main__":
  x, k, d = map(int, input().split())
  x = abs(x)
  ans = main.function(x, k, d)
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#座標はxは負の場合もあるの絶対値をとること
X, K, D = list(map(int, input().split()))
X = abs(X)
#K回繰り返すことを考慮すること
i = min(X//D, K) #移動回数が最小の方をとる
X = X - i*D
K = K - i

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

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline().rstrip("\r\n")
def List(): return list(map(int, input().split()))
def Num(): return int(input())


x, k, d = List()
x = abs(x)
if d * k > x:
    distance = x % d
    do = k - (x // d)
    if do & 1:
        print(d - distance)
    else:
        print(distance)
else:
    print(x - k * d)

Submission status: Accepted

Solution code:
import sys

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

if D * K <= abs(X):
	print(abs(X) - D * K)
else:
	distance = abs(X) % D

	# 残りの移動回数
	count = K - (abs(X) // D)

	if count % 2 == 1:
		print(abs(D - distance))
	else:
		print(distance)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

X = abs(X)

if X > K * D:
    print(X-K*D)
else:
    c=math.floor(X / D)
    print(abs(X - D * c - ((K - c) % 2) *D))