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:
#n = int(input()) #入力
#a, b = (int(x) for x in input().split())

#a = [int(x) for x in input().split()]
#a = list(map(int, input().split()))
#a = [list(map(int,input().split(" "))) for i in range(N)]   #2次元


#a.sort() # C++ の sort(a.begin(), a.end());
#a.append(3) # C++ の a.push_back(3);


#exit(0) # return 0 的な。終了できる。

import math
import copy
#math.gcd(a, b) で、gcd 計算できる。
#math.pi は円周率。

INF = 10**18 #べき乗 C++ の pow(10, 18) or 1e18;
pi = math.pi

MOD = 1000000007
#MOD = 1000000009
#MOD = 998244353




#繰り返し2乗法
#N^aの、Mで割った余りを求める。
def my_pow(N, a, M):
    if(a == 0):
        return 1
    else:
        if(a % 2 == 0):
            tempo = my_pow(N, a/2, M)
            return (tempo * tempo) % M
        else:
            tempo = my_pow(N, a - 1, M)
            return (tempo * N) % M




#N_C_a を M で割った余り
def my_combination(N, a, M):
    res = 1

    for i in range(0, a):
        res *= N - i
        res %= M

    for i in range(0, a):
        res *= my_pow(i + 1, M - 2, M)
        res %= M

    return res




#N_C_i を M で割った余りを、v[i] に代入する。
def my_combination_table(N, M, v):
    if(len(v) < N + 1):
        l = N + 1 - len(v)
        tempo = [1] * l
        v.extend(tempo)

    for i in range(1, N + 1):
        v[i] = v[i - 1] * (N - (i - 1))
        v[i] %= M

        v[i] *= my_pow(i, M - 2, M)
        v[i] %= M

    return




#math.factorial で階乗は計算できる。
#math.gcd で gcd は計算できる。

a1, a2, a3 = input().split()
X = int(a1)
K = int(a2)
D  = int (a3)


if (X - K * D >= 0):
   print(X - K * D)
   exit(0)
elif (X + K * D <= 0):
   print(abs(X + K * D))
   exit(0)
else:
    i = (X + K * D) // (2 * D)
    res = abs(X - K * D)
    
    if (0 <= i and i <= K):
        res = min(res, abs(X + K * D - 2 * i * D))   

    if (0 <= i + 1 and i + 1 <= K):
        res = min(res, abs(X + K * D - 2 * (i + 1) * D))

    if (0 <= i - 1 and i - 1 <= K):
        res = min(res, abs(X + K * D - 2 * (i - 1) * D))

    print(res)
    exit(0)
   
   
    






print(res)

Submission status: Accepted

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

M = int(abs(X)/D)

if abs(abs(X) - D*M) <  abs(abs(X) - D*(M+1)): cand = M
else: cand = M + 1

if cand == M:
    if M <= K and (K-M)%2 == 0: print(abs(abs(X) - D*M))
    elif M <= K and (K-M)%2 == 1: 
        if M + 1 <= K: print(min(abs(abs(X) - D*(M-1)), abs(abs(X) - D*(M+1))))
        else: print(abs(abs(X) - D*(M-1)))
    else: print(abs(abs(X) - D*K))

if cand == M + 1:
    if M + 1 <= K and (K-(M + 1))%2 == 0: print(abs(abs(X) - D*(M+1)))
    elif M + 1 <= K and (K-(M+1))%2 == 1: 
        if M + 2 <= K: print(min(abs(abs(X) - D*(M)), abs(abs(X) - D*(M+2))))
        else: print(abs(abs(X) - D*(M)))
    else: print(abs(abs(X) - D*K))

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

x,k,d = I()
a = abs(x) % d
b = abs(x) //d
if x == d:
    if k % 2 ==0:
        print(x)
        sys.exit()
    else:
        print(0)
        sys.exit()

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

Submission status: Accepted

Solution code:
# def chkzero(x,k,d):
#     if(x == 0):
#         if(k%2 ==1):
            
#             print(d)
#         else:
#             print(0)
#         k=0
#     return k
    

x, k, d = map(int, input().split())
#origin reach check
minans=abs(x)+d
while(k>0):
    if(x==0):
        if(k%2==1):
            minans=d
        else:
            minans=0
        break
    if(x<0):
        x=x*(-1)
    steps= x//d
    if(steps<=k):
        k=k-steps
        x=x-d*steps
        #minans=min(minans,x)
        if(k%2==1):
            minans=min(abs(x-d),minans)
        else:
            minans=min(x,abs(x-2*d),minans)
        break
    else:
        x=x-k*d
        k=0
        minans=min(x,minans)
        break
print(minans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline  #文字列では使わない
X,K,D = map(int, input().split())
q1,min1 =divmod(X,D)
q1 =abs(q1)
if min1 >=0:
    min2 =min1 -D
else:
    min2 =min1 +D

ans =0
if K <= q1:
    ans =abs(X) -K *D
elif K %2 ==0 and q1 %2 ==0:
    ans =abs(min1)
elif K %2 ==1 and q1 %2 ==1:
    ans =abs(min1)
elif K %2 ==0 and q1 %2 ==1:
    ans =abs(min2)
elif K %2 ==1 and q1 %2 ==0:
    ans =abs(min2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a, b, c = map(int, input().split())
a = abs(a)
if b*c <= a:
    print(a-b*c)
else:
    kaisuu = a//c
    saisyo = a % c
    b = b-kaisuu
    if b % 2 == 1:
        print(abs(c-saisyo))
    else:
        print(abs(saisyo))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = input()

n_list = n.split()

x = int(n_list[0])
k = int(n_list[1])
d = int(n_list[2])

def count(x, k, d):
	
	# 絶対値しか気にしないのでサッサと正の話に持っていく
	x = abs(x)
	
	# まずベスポジエリア(abs(d)の範囲)までいけるかを確認
	if x / d > k:
		# いけない
		return x - d * k
	
	# いける
	
	# ベスポジエリアまで何回掛かるか？
	cnt = int(x / d)
	
	c = k - cnt # 残りの移動回数
#	print('残り{}回'.format(c))
	
	x = x - d * cnt
#	print('xは{}'.format(x))
	
	# 残りの移動回数が偶数なら現時点がベストになる
	if c % 2 == 0:
		return x
	
	# 奇数の場合（つまりまだ後1回移動できるとき）
	return min(abs(x + d), abs(x - d))
	

print('{}'.format(count(x,k,d)))

Submission status: Accepted

Solution code:
import math

start, kai, step = map(int, input().split(" "))

if 0 <= abs(start) - (kai * step):
    print(abs(start) - (kai * step))
    exit()

ans1 = start % step
ans2 = abs(ans1 - step)
if (kai - abs(start // step)) % 2 == 0:
    print(abs(ans1))
else:
    print(abs(ans2))

Submission status: Accepted

Solution code:
[X,K,D]=list(map(int, input().split()))
abx=round(abs(X))
n=round(abx//D)
if n>K:
    ans=abx-K*D
else:
    x2=abx-n*D
    x1=(n+1)*D-abx
    if x2>=x1:
        if (K-n-1)%2==0:
            ans=x1
        else:
            ans=x2
    else:
        if (K-n)%2==0:
            ans=x2
        else:
            ans=x1
print(ans)

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
#  何回で0をまたぐか、その時いる場所
if X < 0:
    X = X * (-1)
times = X // D + 1
place = X - D * (times)
if times <= K:
    #  残った移動回数
    K = K - times
    if K % 2 == 0:
        place = place
    else:
        place = place + D
else:
    place = X - D * K

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

print(place)

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if X < 0:
  X = -X
a = X // D
b = X % D
#回数が足りない場合
if a > K:
  print(X - (K * D))
#回数が足りる場合
else:
  if (K - a) % 2 == 1:
    print(abs(b - D))
  else:
    print(b)

Submission status: Accepted

Solution code:

def solve():
    X = abs(x)
    a = X//D
    if a <= K:
        if (K-a)%2 == 0:
            ans = X%D
        else:
            ans = min(abs((X%D)+D), abs((X%D)-D))
    else:
        ans = min(abs(X-K*D), abs(X+K*D))
    print(ans)

if __name__ == "__main__":
    x,K,D = list(map(int, input().split()))
    solve()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import math


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    X, K, D = LI()
    X = abs(X)

    a = math.ceil(X/D)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
X, K, D = inpl()
X = abs(X)
if X + D >= K * D:
    print(abs(X-K*D))
else:
    if K % 2:
        K += 1
        X += D
    E = D*2
    R = X % E
    print(min(R, E-R))

Submission status: Accepted

Solution code:
import math
x,k,d = (int(x) for x in input().split())
if x < 0:
    x *= -1

if x/d >= k:
    print(x-d*k)
else:
    if (k-math.ceil(x/d)) % 2 == 0:
        a = x - math.ceil(x/d)*d
        print(a * -1)
    else:
        a = x - math.ceil(x/d)*d
        print(a + d)

Submission status: Accepted

Solution code:
from collections import deque
import collections

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

X,K,D = MI()
flag = 1
if(X<0):
  flag = -1

n = int(X/D)
if(abs(n) < K):
  X = X - n*D
  K = K - n
else:
  X = X - K*D*flag
  print(abs(X))
  exit()

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

Submission status: Accepted

Solution code:
import sys

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

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

Appr = X // D
Near = X - (X // D) * D

if (K - Appr) % 2 != 0:
    print(abs(Near - D))
else:
    print(abs(Near))

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
p = (x + d * k) // (2 * d)

def f(a):
  return abs(x + d * (k - 2 * a))

if p > k or p < 0:
  print(min(f(0), f(k)))
else:
  print(min(f(p), f(p + 1)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

    if cnt>=k:
        cnt=k

    if x>=0:
        p=x-d*cnt
    else:
        p=x+d*cnt

    k-=cnt

    if k==0:
        print(abs(p))
        exit(0)

    if k%2==0:
        print(abs(p))
    else:
        if p>=0:
            p=abs(p-d)
        else:
            p=abs(p+d)

        print(p)

Submission status: Accepted

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

tmp = abs(x) % d
cal = abs(x) // d
if tmp > abs(tmp - d):
    cal += 1
    tmp = abs(tmp - d)

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

Submission status: Accepted

Solution code:
S = list(map(int, input().split()))
X = S[0];
K = S[1];
D = S[2];
if (K % 2) == 1:
	X1 = X - D
else:
	X1 = X
L1 = X1 % (2*D)
L2 = L1 - 2*D
ans = 1000000000000000000
if abs(X - L1) < K * D:
	ans = min(ans, L1)
if abs(X - L2) < K * D:
	ans = min(ans, -L2)
ans = min(ans, abs(X - K*D))
ans = min(ans, abs(X + K*D))
print(ans)

Submission status: Accepted

Solution code:
import math
X, K ,D =map(int, input().split())
ansp =ansm = float('inf')
n = math.ceil(abs(X)/D)
r = K - n

# X>0だったとき
if r>0:
    if r%2 ==0:
        ansp = abs(X-D*n)
    else:
        ansp = abs(X-D*n+D)
else:
    ansp = abs(X-K*D)

# X<0だったとき
if r>0:
    if r%2 ==0:
        ansm = abs(X+D*n)
    else:
        ansm = abs(X+D*n-D)
else:
    ansm = abs(X+K*D)

print(min(ansp,ansm))

Submission status: Accepted

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

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

x = abs(x_org)
ans = 0 

def a(n):
    global x
    global d
    ret = x-d*(n-1)
    return ret 

n = x//d
l = math.floor(n)
r = l+1 

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

if (x//d)%2 == k%2:
    ans = abs(x-d*l)
    print(ans)
    exit()
else:
    ans = abs(x-d*r)
    print(ans)
    exit()

Submission status: Accepted

Solution code:

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
r=0
if x<0:
  y=-x
else:
  y=x
w=y/d
w=int(w)
w=k-w
if w>0:
  a=y%d
  b=d-a
  if w%2==0:
    r=a
  else:
    r=b
else:
  r=k*d
  r=y-r
print(r)

Submission status: Accepted

Solution code:

def main():
    x,k,d = map(int,input().split())
    x = abs(x)
    temp1 = x // d
    temp2 = x // d + 1
    if abs(x - d * temp1) < abs(x - d * temp2):
        temp = temp1
    else:
        temp = temp2

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

    left = k - temp
    if left % 2 == 0:
        print(abs(x - d * temp))
        return
    else:
        answer = min(abs(x -d * temp + d),abs(x - d * temp - d ))
        print(answer)
        return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
# x > 0のとき
left = abs(x - d * (x // d + 1))
right = abs(x - d * (x // d))

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

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

Submission status: Accepted

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

final = -1

q, n = X % D, X // D

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

Submission status: Accepted

Solution code:
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
	exit(0)	
with decimal.localcontext() as ctx:
    ctx.prec = 150
    t=floor(decimal.Decimal(abs(x))/decimal.Decimal(d))
    

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

abs_x = abs(X)
num = int(abs_x / D)


def calc_min():
    if K < num:
        # マイナス方面or+方面に向かうだけで回数の上限
        if X < 0:
            return X + D * K
        return X - D * K

    # これより先は距離Dを行ったり来たりするだけ
    base_pos = X - D * num if X > 0 else X + D * num

    if (K - num) % 2 == 0:
        return base_pos
    else:
        return base_pos - D if base_pos > 0 else base_pos + D


print(abs(calc_min()))

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
if X<0:
    X=-X
if X >= K*D :
    print(X-K*D)
else: #通りすぎる
    C=X-D*(X//D) #最寄りかつ通り越していない１回は残っている
    A=K-(X//D) #残りの移動回数
    if A%2==0:
        print(C)
    else:
        print(D-C)

Submission status: Accepted

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

if( x == 0 ):
    if( k % 2 == 0 ):
        print("{}".format(0))
        exit()
    else:
        print("{}".format(abs(d)))
        exit()

if( x > 0 ) :
    if( x - k * d >= 0 ) :
        ret = x - k * d
        print("{}".format(ret))
        exit()
    rem = x % d
    cost = x // d
    rem2 = abs(rem - d)
    remc = k - cost
    if( remc % 2 == 0 ) : 
        ret = rem
        print("{}".format(ret))
        exit()
    else:
        ret = rem2
        print("{}".format(ret))
        exit()
elif( x < 0 ) :
    if( x + k * d <= 0 ) :
        ret = abs( x + k * d )
        print("{}".format(ret))
        exit()
    rem = (-x) % d
    cost = (-x) // d
    rem *= -1
    rem2 = rem + d
    remc = k - cost
    if( remc % 2 == 0 ):
        ret = abs(rem)
        print("{}".format(ret))
        exit()
    else :
        ret = rem2
        print("{}".format(ret))
        exit()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

b = abs(X)//D
if b <= K:
    K -= b
else:
    b = K
    K = 0
    
    
if X > 0:
    X -= D*b
elif X < 0:
    X += D*b
    
if K == 0:
    print(abs(X))
else:
    if X == 0:
        if K % 2 == 0:
            print(X)
        else:
            print(D)
    elif X > 0:
        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)))
    elif X < 0:
        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:
X, K, D=map(int,input().split())
if D*K<abs(X):
    ans=abs(X)-D*K
else:
    p=abs(X)//D
    q=abs(X)%D
    if (K-p)%2==1:
        ans=abs(D-q)
    else:
        ans=q
print(ans)

Submission status: Accepted

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

if k * d <= abs(x):
    if x > 0:
        ans = x - (k*d)
    elif x < 0:
        ans = x + (k*d)
elif k % 2 == 0:
    t = x // d
    if t % 2 == 1:
        t += 1
    ans = abs(x - t * d)
elif k % 2 == 1:
    t = x // d
    if t % 2 == 0:
        t += 1
    ans = abs(x - t * d)

print(abs(ans))

Submission status: Accepted

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

x=abs(x)

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

Submission status: Accepted

Solution code:
x, k,d = map(int, input().split(" "))
m = abs(x) // d
p,q= k%2, m%2
if k<=m or p==q:
    if x < 0:
        x += d * min(m,k)
    else:
        x -= d * min(m,k)
else:
    if x < 0:
        l = x + d * m - d
        r = x + d * m + d

        x = min(abs(l), abs(r))

    else:
        l = x - d * m - d
        r = x - d * m + d
        x=min(abs(l),abs(r))


print(abs(x))

Submission status: Accepted

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

low = 0
high = K

while low <= high:
    mid = (low + high) //2
    i = mid
    j = K-mid
    ans_pre = (X + D*i - D*j)
    if ans_pre > 0:
        high = mid-1
    else:
        low = mid+1
        

if low > K :
    low = high
if high < 0:
    high = low
    
ans_h = min(abs((X + D*high - D*(K-high))), abs(X + D*(K-high)-D*high))
ans_l = min(abs((X + D*low - D*(K-low))), abs(X + D*(K-low)-D*low))
    
print(min(ans_l, ans_h))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import io, sys


def main():
    X,K,D = list(map(int, sys.stdin.readline().split()))

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
x,k,d=(map(int,input().split()))
a=abs(x)//d # bを実現するために必要な最小移動回数
b=abs(x)%d # 無限回移動できるとした時の最小の位置
if a<=k:
  if (k-a)%2==0:
    print(b)
  else:
    print(abs(b-d))
else:
  print(abs(x)-(k*d))

Submission status: Accepted

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

d = int(abs(X)/D)
if X > 0:
    r = X - d*D
    if abs(r-D) < abs(r):
        r = r-D
    min_count = int((X-r)/D)
else:
    r = X + d*D
    if abs(r+D) < abs(r):
        r = r+D
    min_count = int(abs(X-r)/D)

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

Submission status: Accepted

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

g = abs(X) % D

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

Submission status: Accepted

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

sho, amari = abs(x)//d, abs(x)%d
if k <= sho:
  print((abs(x)-(k*d)))
  sys.exit()
else:
  x = amari
  k = k-sho
  
if x == abs(x-d):
  print(x)
elif x < abs(x-d):
  if k%2==0:
    print(x)
  else:
    print(abs(x-d))
else:
  if k%2==1:
    print(abs(x-d))
  else:
    print(x)

Submission status: Accepted

Solution code:

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

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



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

Submission status: Accepted

Solution code:
def zet(num):
    if num<0:
        return -num
    else:
        return num

x,k,d = map(int, input().split())
x = zet(x)
amari = x%d
syou = x//d
if syou > k:
    syou = k

sa = k-syou
genzai = zet(x) - syou * d
if sa == 0:
    print(genzai)
    exit()
    
if sa % 2 == 0:
    print(genzai)
else:
    print(zet(genzai - d))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

else:
    tempx=x-(x//d)*d
    nokorik=k-(x//d)

    migi=tempx+d
    hidari=abs(tempx-d)

    ans1=tempx
    ans2=min(migi,hidari)

    if(nokorik %2 ==0):
        print(ans1)
    else:
        print(ans2)

Submission status: Accepted

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

if x < 0:
    a = abs(x)//d
    if k < a:
        x += k*d
        k = 0
    else:
        x += a*d
        k -= a
else:
    a = x//d
    if k < a:
        x -= k*d
        k = 0
    else:
        x -= a*d
        k -= a

if k != 0 and k % 2 == 1:
    b = abs(x-d)
    c = abs(x+d)
    x = min(b,c)

print(abs(x))

Submission status: Accepted

Solution code:

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

ans = 0
if abs(K * D) < abs(X):
    ans = (abs(X) - abs(K * D))
else:
    num = X//D # 距離がD未満になるまでに必要な最小の移動回数
    tmp = (abs(X) - abs(num * D))
    if X < 0:
        tmp *= (-1)
    # 残った回数が奇数の場合
    if (K - num) % 2 == 1:  
        ans = min([abs(tmp - D), abs(tmp + D)])
    else:
        ans = abs(tmp)
        
print(ans)

Submission status: Accepted

Solution code:
a = list(input().split(" "))
x = int(a[0])
if x < 0:
    x *= -1
k = int(a[1])
d = int(a[2])
c = x // d

if c >= k:
    x -= d * k
else:
    x -= d * c
    if (k - c) % 2 == 1:
        x = (x - d) * -1

print(x)