Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

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

Sample Input 1

6 2 4

Sample Output 1

2

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

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

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

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

Sample Input 2

7 4 3

Sample Output 2

1

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

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

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

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

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

def func(a):
	return  X + (a * D) - (K-a)*D
  
x = []
for a in range(K):
  x.append(abs(func(a)))

print(min(x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#C
x,k,d=map(int,input().split())
if abs(x)-k*d<d:
    print(min(abs(x)%d,d-abs(x)%d))
else:
    print(abs(x)-k*d)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int,input().split())
x = abs(x)
if x>k*d:
    ans = x-k*d
else:
    cnt = x//d#xに最も近づくための移動回数
    if (k-cnt)%2==0:
        ans = x-cnt*d
    else:
        if x > 0:
            ans = x-cnt*d-d
        else:
            ans = x-cnt*d+d
print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int,input().split())
x = x
if x>k*d:
    ans = x-k*d
else:
    cnt = x//d#xに最も近づくための移動回数
    if (k-cnt)%2==0:
        ans = x-cnt*d
    else:
        if x > 0:
            ans = x-cnt*d-d
        else:
            ans = x-cnt*d+d
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def resolve():
    x, k, d = list(map(int, input().split()))
    ans = 10 * 15
    cnt = 0
    ans = abs(x)

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

resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    x, k, d = list(map(int, input().split()))
    ans = 10 * 15
    cnt = 0
    now = x
    mem = set()
    while True:
        if now > 0:
            now -= d
        else:
            now += d

        ans = min(ans, abs(now))

        cnt += 1
        if cnt >= k:
            break
    print(ans)

resolve()

Submission status: Wrong Answer

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

x = abs(x)
c = x // d
# 必要な移動回数
k_r = k - c
# 移動した後の残りの移動回数
ans_2 = x - d * c
#残り移動回数が偶数のときのｘからの距離

if c >= k:
    print(x - k * c )
    exit()

x_l = x - d * c
#ｘまでの残りの距離

if k_r % 2 == 0:
    print(ans_2)
    exit()
else :
    x_l -= d
    x_l =abs(x_l)
    print(x_l)

Submission status: Wrong Answer

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

x = abs(x)

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

Submission status: Wrong Answer

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

        
N=int(X/D)

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

Submission status: Wrong Answer

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

"""
if K % 2 == 0:  # 偶数回の移動ならabs(X)より、奇数回の移動ならabs(X)+abs(D)よりは小さくできる
    ans = abs(X)
elif X > 0:
    ans = abs(X - D)
else:
    ans = abs(X + D)
"""

ans = 10**15  # abs(X)の最大値

for i in range(K+1):
    goal_hyp = X + i * D - (K - i) * D
    if abs(goal_hyp) < ans:
        ans = abs(goal_hyp)
    else:
        break  # 一端増加に転じたらもう減りることはない

print(ans)

Submission status: Wrong Answer

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

if K % 2 == 0:  # 偶数回の移動ならabs(X)より、奇数回の移動ならabs(X)+abs(D)よりは小さくできる
    ans = abs(X)
elif X > 0:
    ans = abs(X - D)
else:
    ans = abs(X + D)

for i in range(K+1):
    goal_hyp = X + i * D - (K - i) * D
    if abs(goal_hyp) < ans:
        ans = abs(goal_hyp)
    else:
        break  # 一端増加に転じたらもう減りることはない

print(ans)

Submission status: Wrong Answer

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

ans = abs(X) + abs(D)  # 偶数回の移動ならabs(X)より、奇数回の移動ならabs(X)+abs(D)よりは小さくできる

if K % 2 == 0:
    th = max(0,int(K/2 - int(X/D + 1)))
    for i in range(th, int(K/2)):
        goal_hyp = X + i * D - (K - i) * D
        if abs(goal_hyp) < ans:
            ans = abs(goal_hyp)

else:
    if D > abs(X):
        ans = X
    else:
        for i in range(0, min(K, abs(X) // D + 1)):
            goal_hyp = X + i * D - (K - i) * D
            if abs(goal_hyp) < ans:
                ans = abs(goal_hyp)

print(ans)

Submission status: Wrong Answer

Solution code:
a,b,c = list(map(int, input().split()))

a = abs(a)
c = abs(c)

if b*c <= a:
  print(a - (b*c))
else:
  c -= a//b
  a = a%b
  #print(a,b,c)
  if c%2==0:
    print(a)
  else:
    print(abs(a-b))

Submission status: Wrong Answer

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

X = abs(X)

# Dだけ原点に向かって移動したとき，最も原点に近い座標
r = X%D
# 残り移動可能回数
q = X//D

# rに到達後は残りの移動回数が偶数か奇数かで場合分け
# K回を超えている場合
if K <= q:
    print(X-D*K)
# 残り移動可能回数
rem = K - q

# 残り移動可能回数が偶数なら，その場所にとどまることできる
if rem%2 == 0:
    print(r)
# 奇数なら，絶対に負の側でゴールすることになる．よってr-Dの絶対値であるD-rが解
else:
    print(D-r)

Submission status: Wrong Answer

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

X = abs(X)

# Dだけ原点に向かって移動したとき，最も原点に近い座標
r = X%D
# 残り移動可能回数
q = X//D

# rに到達後は残りの移動回数が偶数か奇数かで場合分け
# K回を超えている場合
if K <= q:
    print(X-D*K)
# 残り回数
rem = K - q

# 偶数なら，その場所にとどまることできる
if q%2 == 0:
    print(r)
# 奇数なら，絶対に負の側でゴールすることになる．よってr-Dの絶対値であるD-rが解
else:
    print(D-r)

Submission status: Wrong Answer

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

if X >= K * D:
  ans = X - K * D

else:
  n, d = divmod(X, D)
  ans = d
  if (K - n) % 2:
    ans = D - d


print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X >= K * D:
  ans = X - K * D

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


print(ans)

Submission status: Wrong Answer

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

n = X // D

if K <= n:
  ans = abs(X - K * D)

else:
  if (K - n) % 2:
    ans = abs((n+1) * D - X)
  else:
    ans = abs(X - n * D)

print(ans)

Submission status: Wrong Answer

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

n = X // D

if K <= n:
  ans = X - K * D

else:
  if (K - n) % 2:
    ans = (n+1) * D - X
  else:
    ans = X - n * D

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split(' '))
b = x + k * d;
if b > 0:
    a = int(b / (2 * d))
    if a <= k:
        print(max(b-a*2*d, -(b-(a+1)*2*d)))
    else:
        print(b-k*2*d)
else:
    print(-b)

Submission status: Wrong Answer

Solution code:
# x, k, d = map(int, input().split(' '))
x, k, d = map(int, input().split(' '))
b = x + k * d;
if b > 0:
    a = int(b / (2 * d))
    if a <= k:
        print(max(b-a*2*d, b-(a+1)*2*d))
    else:
        print(b-k*2*d)
else:
    print(-b)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=[int(i) for i in input().strip().split(" ")]
if x//d>=k:
    print(x-d*k)
else:
    if (k-x//d)%2==0:
        print(x%d)
    else:
        print(d-(x%d))

Submission status: Wrong Answer

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

n = X // D

if K <= n:
  ans = X - K * D

elif (K - n) % 2:
  ans = (n+1) * D - X
else:
  ans = X - n * D

print(ans)

Submission status: Wrong Answer

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

n = X // D

if K < n:
  ans = X - K * D

elif (K - n) % 2:
  ans = (n+1) * D - X
else:
  ans = X - n * D

print(ans)

Submission status: Wrong Answer

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

value = abs(X)
for i in range(K):
  pre_value = value - D
  if abs(pre_value) <= value:
    if value == pre_value:
      print(abs(value))
      exit()
    elif (i + 1) == K:
      print(abs(pre_value))
    value = pre_value
  else:
    if (K - i +2 ) % 2 == 0:
      print(abs(value))
      exit()
    else:
      print(value + D)
      exit()

Submission status: Wrong Answer

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

value = X
for i in range(K):
  pre_value = value - D
  if abs(pre_value) <= value:
    if value == pre_value:
      print(abs(value))
      exit()
    elif (i + 1) == K:
      print(abs(pre_value))
    value = pre_value
  else:
    if (K - i +2 ) % 2 == 0:
      print(abs(value))
      exit()
    else:
      print(value + D)
      exit()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()


def main():
    x, k, d = map(int, input().split())
    desired = abs(x) // d
    if k <= desired:
        if x < 0:
            x += k * d
        else:
            x -= k * d
        print(abs(x))
        return
    if x < 0:
        x += desired * d
    else:
        x -= desired * d
    if k - desired % 2 == 0:
        print(abs(x))
        return
    if x == 0:
        print(d)
    elif x > 0:
        x -= d
        print(abs(x))
    elif x < 0:
        x += d
        print(abs(x))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

n=math.ceil(X/K)
m=math.floor(X/K)

n_moveto0=0

if abs(X-n*D) < abs(X-m*D):
  n_moveto0=n
  x_min=X-n*D
else: 
  n_moveto0=m
  x_min=X-m*D


if n_moveto0>=K:
  print(abs(abs(X)-abs(K*D)))
else:
  if (K-n_moveto0)%2==0:
    print(abs(x_min))
  else:
    print(min([abs(x_min+D),abs(x_min-D)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from bisect import bisect_left
from itertools import product
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def LF(): return list(map(float, input().split()))
def LC(): return [c for c in input().split()]
def LLI(n): return [LI() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]

def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)]

def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret


INF = float("inf")
MOD = 10**9 + 7


def main():
    X, K, D = MAP()

    x = abs(X)
    d, m = divmod(X, D)
    cand = (m, D-m)

    if K <= d:
        print(x-K*D)
    else:
        rest = (K-d)
        if rest % 2 == 0:
            print(cand[0])
            return
        else:
            print(cand[1])
            return
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from bisect import bisect_left
from itertools import product
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def LF(): return list(map(float, input().split()))
def LC(): return [c for c in input().split()]
def LLI(n): return [LI() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]

def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)]

def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret


INF = float("inf")
MOD = 10**9 + 7


def main():
    X, K, D = MAP()

    # 到達候補は0に近い正と負の二つの数
    d, m = divmod(X, D)
    cand = (m, m-D)

    if X >= 0:
        if K <= d:
            print(X-K*D)
            return
        else:
            rest = (K-d)
            if rest % 2 == 0:
                print(cand[0])
                return
            else:
                print(-cand[1])
                return
    else:
        if K <= -d-1:
            print(X+K*D)
            return
        else:
            rest = K-(-d-1)
            if rest % 2 == 0:
                print(-cand[1])
                return
            else:
                print(cand[0])
                return
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

absX = abs(X)
a = absX - K * D
minX = absX % D

if a > 0:
    print(a)

elif a <= 0:

    Kmin = (X - minX) / D
    Krem = K - Kmin

    if Krem % 2 == 0:
        print(min(abs(minX), abs(minX - 2 * D)))
    else:
        print(min(abs(minX + D), abs(minX - D)))

Submission status: Wrong Answer

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

a = X - K * D
minX = X % D

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

Submission status: Wrong Answer

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

a = X - K * D
minX = X % D

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ans_1 = x%d #プラスの最小
ans_2 = abs(ans_1-d) #マイナスの最小
x = abs(x)
if x//d >= k:
    print(x-(d*k))
    exit()
    
if ans_1 > ans_2:
    if (k-(x//d)-1)%2 == 0:
        print(ans_2)
    else:
        print(ans_1)

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

elif ans_1 == ans_2:
    print(ans_1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
position,moves,distance=[int(x) for x in input().split()]
if position>=distance*moves:
	print(abs(position-distance*moves))
else:
	variable=moves-position//distance
	if variable%2==0:
		print(abs((position-distance*(position//distance))))
	else:
		print(abs(position-distance*((position//distance) + 1)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X == 0:
    print(0 if K % 2 == 0 else D)
elif X < 0:

    if K % 2 == 0 and abs(X) == D:
        print(abs(X))
        exit()
    div = abs(X) // D
    tmp = X + D*div

    if div >= K:
        print(abs(X + D*K))
    elif div < K:
        K -= div
        print(abs(tmp) if K % 2 == 0 else abs(X+D))    

else:
    if K % 2 == 0 and abs(X) == D:
        print(abs(X))
        exit()
    div = abs(X) // D
    tmp = X - D*div

    if div >= K:
        print(abs(X - D*K))
    elif div < K:
        K -= div
        print(abs(tmp) if K % 2 == 0 else abs(X-D))

Submission status: Wrong Answer

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

move=min(abs(X)//D,K)
ans=X-move*D

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

Submission status: Wrong Answer

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

move=min(X//D,K)
ans=X-move*D

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

Submission status: Wrong Answer

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

move=X//D
A=X-move*D
B=X-(move+1)*D


if move+1<=K:
    if (K-(move+1))%2==0:
        print(abs(B))
    else:
        print(abs(A))
else:
    print(X-K*D)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
history = []
for i in range(1, k+1):
    if x <= 0:
        x += d
    else:
        x -= d
    if x in history:
        if i%2 == 0:
            break
        else:
            if x <= 0:
                x += d
            else:
                x -= d
            break
    history.append(x)
print(abs(x))

Submission status: Wrong Answer

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

y = abs(x)
count = y // d
amari = y % d

if k >= d:
    if (k - d) % 2 == 1:
        amari -= d
else:
    amari = y - k * d

print(abs(amari))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

def solve(x, k, d):
    x = math.fabs(x)
    if x - k * d >= 0:
        return x - k * d
    else:
        cur = x - x // d * d
        k = k - x // d
        if k % 2 == 0:
            return cur
        else:
            return abs(cur - d)

if __name__ == "__main__":
    x, k, d = map(int, input().split(' '))
    print(solve(x, k, d))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
dist = abs(X)
rem = K
count = min(X//D,K)
dist = dist - count*D
rem = K - count
if rem % 2 == 1:
  dist = dist - D
ans = abs(dist) 
print(ans)

Submission status: Wrong Answer

Solution code:
import math
X,K,D = map(int,input().split())
dist = abs(X)
rem = K
D = abs(D)
count = min(math.floor(X/D),K)
dist = dist - count*D
rem = K - count
if rem % 2 == 1:
  dist = dist - D
ans = abs(dist) 
print(ans)

Submission status: Wrong Answer

Solution code:
import math
X,K,D = map(int,input().split())
dist = abs(X)
rem = K
count = min(math.floor(X/D),K)
dist = dist - abs(count*D)
rem = K - count
if rem % 2 == 1:
  dist = dist - D
ans = abs(dist) 
print(ans)

Submission status: Wrong Answer

Solution code:
import math
X,K,D = map(int,input().split())
dist = abs(X)
rem = K
count = min(math.floor(X/D),K)
dist = dist - count*D
rem = K - count
if rem > 0:
  if rem % 2 == 1:
    dist = dist - D
ans = abs(dist) 
print(ans)

Submission status: Wrong Answer

Solution code:
import math
X,K,D = map(int,input().split())
dist = abs(X)
rem = K
count = min(math.floor(X/D),K)
dist = dist - count*D
rem = K - count
if rem % 2 == 1:
  dist = dist - D
ans = abs(dist) 
print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
dist = abs(X)
rem = K
count = min(int(X/D),K)
dist = dist - count*D
rem = K - count
if rem % 2 == 1:
  dist = dist - D
ans = abs(dist) 
print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ans = 0
if abs(x) <= k * d:
  ans = abs(x) % d
  ans = min([ans, d-ans])
else:
  ans = abs(x) - k * d
print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ans = 0
if abs(x) <= k * d:
  ans = abs(x) % k
  ans = min([ans, k-ans])
else:
  ans = abs(x) - k * d
print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D =map(int, input().split())
x=X
if X>0:
    for i in range(K):
        x -= D
        if abs(x)<D:
            if K-(i+1)%2==0:
                break
            else:
                x += D
    
if X<0:
    for i in range(K):
        x += D
        if abs(x)<D:
            if K-(i+1)%2==0:
                break
            else:
                x -= D

print(x)