Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

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

Sample Input 1

6 2 4

Sample Output 1

2

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

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

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

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

Sample Input 2

7 4 3

Sample Output 2

1

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

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

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

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Accepted

Solution code:
x,k,d=[int(i) for i in input().strip().split(" ")]
x=abs(x) if x<0 else x
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: Accepted

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

Submission status: Accepted

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

X = abs(X)

if X // D >= K:
    print(X - D*K)
else:
    e = X // D
    K -= e
    X -= D*e

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

    print(X)

Submission status: Accepted

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

value = abs(X)

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

Submission status: Accepted

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

x=abs(x)

a=x//d

if a>=k:
    ans=x-k*d

elif (k-a)%2==0:
    ans=x-d*a

else:
    ans=x-d*a-d

print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
if X/D >= K:
  print(X-D*K)
  exit()
s = X//D
X -= s*D
K -= s
if K%2 == 0:
  print(X)
else:
  print(abs(X-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)
elif (k-x//d)%2 ==1:
    print(abs(x%d-d))
else:
    print(x%d)

Submission status: Accepted

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

Solution code:

import glob
import os

bn = os.path.basename(__file__).split('.')[0]
dn = os.path.dirname(__file__).split('/')

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        X, K, D = map(int, self.input_data().split())
        if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

        print(str(result))


solver = Solver(REL_PATH)
solver.exec_resolve()

Submission status: Accepted

Solution code:
X,K,D = 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: Accepted

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

Submission status: Accepted

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

x = abs(x)

m = x // d

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

elif (k-m) % 2 == 0:
    ans = x - (d*m)

else:
    ans = x - (d*m) - d

print(abs(ans))

Submission status: Accepted

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

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

n_moveto0=0

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


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

Submission status: Accepted

Solution code:
# walking takahashi

def walk(X, K, D):
  abX = abs(X)
  if abX >= K*D:
    return abX - K*D
  itr = round(abX/D)
  ret = K - itr
  res = abs(abX - D*itr)
  if (ret % 2) == 0:
    return res
  return abs(res - D)

if __name__ == "__main__":
  inputs = list(map(int, input().split()))
  print(walk(inputs[0], inputs[1], inputs[2]))

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if X < 0:
    X = -X
step = X//D
X -= D*step
K -= step

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

Submission status: Accepted

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

Solution code:
x,k,d=[int(i) for i in input().split()]

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

Submission status: Accepted

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 = (absX - 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: Accepted

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

x = abs(x)

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

print(ans)

Submission status: Accepted

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

x = abs(x)
# 絶対値が最小になる場所
# つまり x から 0 に向かう
# 移動距離(dマス * k回) >= 0 以上であればそれが答え 
#  x - k*d  が答え
#  Python では問題ながい、型付の言語ではオーバフローを注意する必要あり
#  ストレートにやるといかになるが k*d がオーバフローするため式を変形させる
#  x - k*d = 0 -> x >= k*d -> x/k >= d -> d <= x/k
# if (x - k*d) >= 0:
if d <= x/k:
    print(x -k*d)
    exit()

# 1. そうでない場合は、一旦 0 に近いところまでの回数を計算する（つまり x/d回）
#    また、0に近い位置とは x/d のあまり（x%d）になる
# 2. 元の回数から1.の回数を引いた値が偶数の場合往復ができるため実質0
# 3. 2.と同様に奇数の場合 1. の位置から d を引いた絶対値が答えになる 
p = x%d
rem = k - x//d
if rem%2 == 0:
   print(p)
else:
    print(abs(p -d))

Submission status: Accepted

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

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 = cur - d

ans = abs(cur)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
position,moves,distance=[int(x) for x in input().split()]
position=abs(position)
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: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

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

    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(tmp+D))    

else:

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

t = min(abs(x)//d, k)
u = abs(x)-d*t
print(abs(u-d*((k-t)%2)))

Submission status: Accepted

Solution code:
import numpy as np

inpt = input()
inpt_list = inpt.split(" ")
X, K, D = [int(x) for x in inpt_list]

X_abs = np.abs(X)
num = X_abs // D
rem = K - num
if rem < 0:
    out = X_abs - D*K
else:
    if rem%2==0:
        out = X_abs - num*D
    else:
        out = np.abs(X_abs - num*D - D)
print(out)

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
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: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(string):
    x, k, d = map(int, string.split())
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    x, k, d = abs(x), k // 2, d * 2
    return str(min(x % d, abs((x % d) - d))) if x < k * d else str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def solve(string):
    x, k, d = map(int, string.split())
    x = abs(x)
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    k, d = k // 2, d * 2
    return str(min(x % d, abs((x % d) - d))) if x < k * d else str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def solve(string):
    x, k, d = map(int, string.split())
    x = abs(x)
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    k, d = k // 2, d * 2
    if x < k * d:
        return str(min(x % d, abs((x % d) - d)))
    else:
        return str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def solve(string):
    x, k, d = map(int, string.split())
    x = abs(x)
    if k % 2:
        x = min(x - d, x + d, key=abs)
        k -= 1
    if x < k * d:
        return str(min(x % (2 * d), abs((x % (2 * d)) - 2 * d)))
    else:
        return str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

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

ans_min = X % D

if K <= X_div_D:
  print(X - K * D)
else:
  if (K % 2) ^ (X_div_D % 2) == 0:
    print(ans_min)
  else:
    print(abs(ans_min - D))

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
straight=min(K,X//D)
K-=straight
X-=straight*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)
s = x//d
if x-k*d>0:
  print(x-k*d)
else:
    if (k-s)%2==0:
        print(x-s*d)
    else:
        print(-x+(s+1)*d)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# string
# a, b, c = input().split()
# str_list = list(input().split())

# number
# a, b, c = map(int, input().split())
# num_list = list(map(int, input().split()))

# lows
# str_list = [input() for _ in range(n)]

# many inputs
# num_list = []
# for i in range(n): num_list.append(list(map(int, input().split())))

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

if x < 0:
    x = -x

l = int(x/d)

if l < k:
    print(abs(x - ((k-l)%2 + l) * d))
else:
    print(abs(x - k*d))

Submission status: Accepted

Solution code:
def solve():
    X,K,D = [int(i) for i in input().split()]
    abs_X = abs(X)
    if D*K <= abs_X:
        print(abs_X - D*K)
    else:
        steps2pivot = abs_X // D
        abs_X -= steps2pivot * D
        rest = K - steps2pivot
        if rest % 2 == 0:
            print(abs_X)
        else:
            print(abs(abs_X-D))

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

# Xは0以上として一般性を失わない
X = abs(X)
ans = 0
# 「X-KD>=0」であれば、その地点で終了
# 「X-KD<0」であれば、原点周りを振動する
if X - K*D >= 0:
  ans = X - K*D
else:
  count = X // D
  K = K - (X // D) # math.floor() を使うより簡潔
  if K % 2 == 0: 
    ans = X - count*D
  else: 
    ans = D - (X - count*D)
    
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

if x >= k*d:
    print(x - d*k)
else:
    e = x//d
    k -= e
    x %= 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)
straight = min(k, x // d)
k -= straight
x -= straight * 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)
count = 0

if x - k*d > 0:
    print(x - k*d)

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X,K,D = map(int, input().split())
X=abs(X)
if X>=K*D:
  print(X-K*D)
else:
  y=X//D
  z=K-y
  j=y*D
  if z%2==0:
    print(X-j)
  else:
    print(abs(X-j-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:
  y=X//D
  z=K-y
  j=y*D
  if z%2==0:
    print(abs(X-j))
  else:
    print(abs(X-j-D))

Submission status: Accepted

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


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

Submission status: Accepted

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

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

print(g)

Submission status: Accepted

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

X = abs(X)

cuts = X//D

if cuts>=K:
    print(X-(K*D))
else:
    remainingcuts = K-cuts
    r = X-cuts*D

    if remainingcuts%2 == 1:
        print(abs(r-D))
    else:
        print(r)

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)
elif x >= 0:
    print(min([abs(x - (k - 2 * i) * d) for i in range((k * d - x) // (2 * d) - 100, (k * d - x) // (2 * d) + 100)]))
else:
    print(min([abs(x + (k - 2 * i) * d) for i in range((k * d + x) // (2 * d) - 100, (k * d + x) // (2 * d) + 100)]))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

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

if __name__=='__main__':
  main()

Submission status: Accepted

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

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
    if K % 2 == 1:
        print(D-X)
    else:
        print(X)

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

Solution code:
a, b, c = map(int, input().split())
d = abs(a)
count = min(d//c, b)
d -= c*count
b -= count
if b % 2 == 1:
    d -= c
print(abs(d))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

X = abs(X)

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

Submission status: Accepted

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

straight = min(k, math.floor(x / d))

k -= straight
x -= straight * d

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

Submission status: Accepted

Solution code:
x,k,d=list(map(int,input().split()))
#print(x,k,d)
result=abs(x)-k*d
swithn=0

if result<0:
    if k%2==1:
        x-=d
    result=min(x%(2*d),-x%(2*d))
print(result)

Submission status: Accepted

Solution code:
#移動の最小値(高速化問題)
x,k,d = map(int,input().split())
x = abs(x)
if (x//d) > k:
    ans = x - d*k
elif (k-(x//d))%2 == 0:
    y = x-(x//d)*d
    ans = y
else:
    y = x-(x//d)*d
    ans = y-d
print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

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

kyori = abs(X)
kaisu = math.ceil(kyori / D)

if(K < kaisu):
    ans = kyori - (K * D)
    #if(X < 0):
    #    print(-ans)
    #else:
    #    print(ans)
else:
    if((K - kaisu) % 2 == 0):
        ans = kyori - (kaisu * D)
    else:
        ans = kyori - (kaisu * D) + D

    #if(X < 0):
    #    print(-ans)
    #else:
    #    print(ans)

print(abs(ans))

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
q, m = divmod(X, D)
if q >= K:
    print(X-K*D)
elif (q+K)%2==0:
    print(m)
else:
    print(-(m-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:
    cnt = (X - X%D) // D
    if (K-cnt)%2==0:
        print(X%D)
    else:
        print(D - X%D)

Submission status: Accepted

Solution code:
x,k,d = list(map(int, input().split()))
x = abs(x)
a,b = divmod(x, d)
a = min(a, k)
x -= a * d
#print('a=',a)
#print('x=',x)
#print('ka=',k-a)
x -= d * ((k-a) % 2)
print(abs(x))

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:
  if K%2==0:
    s=X-2*D*int(X/(2*D))
    t=-(s-2*D)
    print(min(abs(s),t))
    
  else:
    s=X-2*D*int((X-D)/(2*D))-D
    t=-(s-2*D)
    
    print(min(abs(s),t))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    x, k, d = map(int, input().split())
    ans = abs(x) - (d * min(k, abs(x)//d))
    t = k - abs(x)//d
    if t%2 != 0 and t > 0:
        ans = min(abs(ans-d), abs(ans+d))
    print(ans)

main()

Submission status: Accepted

Solution code:
"""
AtCoder :: Beginner Contest 175 :: C - Walking Takahashi 
https://atcoder.jp/contests/abc175/tasks/abc175_c
"""
import sys


def solve(X, K, D):
    """Solve puzzle."""
    # print('solve X', X, 'K', K, 'D', D)
    if D == 0:
        return X

    X = abs(X)
    soln = abs(X - (K * D))
    steps_to_zero = abs(X) // D
    # print('steps to zero', steps_to_zero, K - steps_to_zero)
    # print('to zero', abs(X - (steps_to_zero * D)))
    # Undershoot or get directly on to zero.
    if steps_to_zero <= K and ((K - steps_to_zero) % 2 == 0):
        soln = min(soln, abs(X - (steps_to_zero * D)))

    # Overshoot by one step
    steps_past_zero = steps_to_zero + 1
    # print('steps past zero', steps_past_zero, K - steps_past_zero)
    # print('past zero', abs(X - (steps_past_zero * D))) 
    if steps_past_zero <= K and ((K - steps_past_zero) % 2 == 0):
        soln = min(soln, abs(X - (steps_past_zero * D)))

    # Overshoot and return by one
    steps_back_zero = steps_past_zero + 1
    # print('steps back zero', steps_back_zero, K - steps_back_zero)
    # print('back zero', abs(X - (steps_back_zero * D)))
    if steps_back_zero <= K and ((K - steps_back_zero) % 2 == 0):
        soln = min(soln, abs(X - (steps_back_zero * D)))

    return soln


def main():
    """Main program."""
    X, K, D = (int(i) for i in sys.stdin.readline().split())
    print(solve(X, K, D))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#ABC 175 C
x, k, d = map(int, input().split())

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

if k <= syou:
    ans = x - (d * k)

else:
    if (k - syou) % 2 == 0: #残りの動ける数が偶数
        ans = amari

    else:#残りの動ける数が奇数
        ans = abs(amari - d)

print(ans)

Submission status: Accepted

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

if a < 0:
    x = -a
else:
    x = a

y = x % d
l = x // d
m = k - l

if m < 0:
    ans = x - (k * d)
elif m % 2 ==0:
    ans = y
else :
    ans = y - d

print(abs(ans))

Submission status: Accepted

Solution code:
# C - Walking Takahashi

X, K, D = map(int, input().split())
X = abs(X)
d = X // D
m = X % D

if K <= d:
    ans = X - (D * K)
elif (K - d) % 2 == 0:
    ans = X - (D * d)
else:
    ans = X - (D * (d+1))
print(abs(ans))

Submission status: Accepted

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

zan = X%D


K2 = K-X//D
if K2 < 0:
  print(X-D*K)
else:
  if K2%2 == 0:
  	print(zan)
  else:
  	print(D-zan)

Submission status: Accepted

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

Submission status: Accepted

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