Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

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

Sample Input 1

6 2 4

Sample Output 1

2

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

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

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

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

Sample Input 2

7 4 3

Sample Output 2

1

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

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

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

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Accepted

Solution code:
import math

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

ans = 0
if x == 0:
    if k%2 == 0:
        ans = 0
    elif k%2 == 1:
        ans = abs(d)
else :
    if abs(x) - k*d >= 0:
        ans = abs(abs(x) - k*d)
        #if abs(x) - k*d == 0:
            #if k - (abs(x) / (k * d)) % 2 == 0:
                #ans = 0
            #else:
                #ans = abs(x)
    elif abs(x) - k*d < 0:
        if  abs(x) - d*(abs(x) // d) != 0:
            if (k - (math.ceil(abs(x) / d) )) % 2 == 1:
                ans = abs(abs(x) - d*(abs(x) // d))
            else:
                ans = abs(abs(x) - d*(abs(x) // d + 1))
        else:
            if (k - (math.ceil(abs(x) // d) ) ) % 2 == 1:
                ans = d
            else:
                ans = 0
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)
elif X == D:
  if K % 2 == 0:
    print(X)
  else:
    print(abs(X - D)) 
else:
  K -=  X // D
  X = X % D
  if K % 2 == 0:
    print(X)
  else:
    print(abs(X - D))

Submission status: Accepted

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

if abs(x)<d :
    if k%2==0 :
        ans=abs(x)
    else :
        x1,x2=x-d,x+d
        ans=abs(x2)
        if abs(x1)<abs(x2) :
            ans=abs(x1)

else :
    if x>0 :
        X=x%d
        K=k-(x//d)
        if K%2==0 and K>0 :
            ans=abs(X)
        elif K%2==1 and K>0 :
            x3,x4=X-d,X+d
            ans=abs(x4)
            if abs(x3)<x4 :
                ans=abs(x3)
        else :
            ans=abs(x-k*d)

    else :
        Y=abs(x)%d
        K=k-(abs(x)//d)
        if K%2==0 and K>0 :
            ans=abs(-Y)
        elif K%2==1 and K>0 :
            y1,y2=-Y+d,-Y-d
            ans=abs(y1)
            if abs(y2)<y1 :
                ans=abs(y2)
        else :
            ans=abs(x+k*d)

print(ans)

Submission status: Accepted

Solution code:
def main():
    X, K, D = map(int, input().split())
    if abs(X) == D:
        if K % 2 == 0:
            print(abs(X))
        else:
            print(0)
        return

    diff = K * D - abs(X)
    if abs(diff) <= D:
        print(abs(diff))
        return
    elif diff < 0:
        print(abs(diff))
        return
    count_minus = abs(diff // (2 * D))
    if X > 0:
        dest = X - (K - count_minus) * D + count_minus * D
        dest2 = X - (K - count_minus - 1) * D + (count_minus + 1) * D
    else:
        dest = X + (K - count_minus) * D - count_minus * D
        dest2 = X + (K - count_minus - 1) * D - (count_minus + 1) * D

    print(abs(dest) if abs(dest) < abs(dest2) else abs(dest2))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

alpha = X%D
count = K-(X//D)
if(alpha>abs(alpha-D)):
  count +=1
  alpha = abs(alpha-D)
  
beta = count%2
if beta==1:
  print(abs(alpha-D))
else:
  print(alpha)

Submission status: Accepted

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

if d*k < x:
    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())


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

Submission status: Accepted

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


# for i in range(hikukazu):
    
#     abs_x = abs_x - D
abs_x = abs_x - (D*hikukazu)
    # print(X)

print(abs(abs_x))

Submission status: Accepted

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

Submission status: Accepted

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

k = round(abs(X) / D)

if k > K:
    if k == 1:
        print(abs(abs(X) - K * D))
    else:
        print(min(abs(abs(X) - K * D), abs(abs(X) - (K - 2) * D)))

else:
    if k % 2 == K % 2:
        print(min(abs(abs(X) - k * D), abs(abs(X) - (k + 2) * D), abs(abs(X) - (k - 2) * D)))

    else:
        print(min(abs(abs(X) - (k + 1) * D), abs(abs(X) - (k - 1) * D), abs(abs(X) - (k + 3) * D), abs(abs(X) - (k - 3) * D)))

Submission status: Accepted

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

if abs(X) > D:
    d = abs(X) // D
    y = abs(X) % D
    if d > K:
        a1 = abs(X) - (D * K)
        print(abs(a1))
        exit()

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
L = input().split()
LS = [int(i) for i in L]
X = abs(LS[0])
K = LS[1]
D = LS[2]

length = X % D
move = X // D

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

Submission status: Accepted

Solution code:
XKD=list(input().split(' '))
X=int(XKD[0])
K=int(XKD[1])
D=int(XKD[2])
AX=abs(X)

if AX>K*D:
    print(AX-K*D)
else:
    K2=AX//D
    if (K-K2)%2==0:
        P=AX-K2*D
        print(P)
    else:
        P=abs(AX-(K2+1)*D)
        print(P)

Submission status: Accepted

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

X = abs(X)
now = X
over_zero_step = 0
over_zero = 0
not_over_zero = now
flg = 0

if X < abs(K * D):
    q = X // D
    now = X - q * D
    now = abs(now)
#    print(now, q)    
    for i in range(q+1, K+1):
        nxt = now - D
        if nxt < 0:
            over_zero = nxt
            not_over_zero = now
            over_zero_step = i
            break
        now = nxt
else:
    remaining_step = 0
    nearest_point = X - K * D
    flg = 1

if flg == 0:
        
    if abs(over_zero) <= not_over_zero:
        nearest_point = over_zero
        nearest_point_step = over_zero_step
        next_nearest_point = not_over_zero
        remaining_step = K - nearest_point_step
    else:
        nearest_point = not_over_zero
        nearest_point_step = over_zero_step - 1
        next_nearest_point = over_zero
        remaining_step = K - nearest_point_step

#print("rem", remaining_step)
if remaining_step % 2 == 0:
    print(abs(nearest_point))
else:
    print(abs(next_nearest_point))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    Qc()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
param_x, param_k, param_d = map(int, input().split())

param_x_tmp = param_x

if param_x > 0:
  int_times_nega_posi = -1
else:
  int_times_nega_posi =  1
  
if (param_x*int_times_nega_posi*-1) - (param_k*param_d) > 0:
  print((param_x*int_times_nega_posi*-1) - (param_k*param_d))
  
else:
  int_time_before_cross_0 = int((param_x*int_times_nega_posi*-1) / param_d)
  # print(int_time_before_cross_0)
  
  param_x_tmp = param_x + int_time_before_cross_0*param_d*int_times_nega_posi
  # print(param_x_tmp)
  
  int_move = param_k - int_time_before_cross_0 #move left
  int_move_devide_2_no_amari = int_move % 2
  print(abs(param_x_tmp + param_d*int_move_devide_2_no_amari*int_times_nega_posi))
  
  #count = 0
  #while param_x_tmp*param_x > 0:
  #  
  #  param_x_tmp = param_x_tmp + param_d*int_times_nega_posi
  #  
  #  count = count + 1
  #  
  #  if param_k - count == 0:
  #    print(abs(param_x_tmp))
  #
  #if count < param_k:
  #  int_move = param_k - count #move left
  #  int_move_devide_2_no_amari = int_move % 2
  #  print(abs(param_x_tmp + param_d*int_move_devide_2_no_amari*int_times_nega_posi*-1))

Submission status: Accepted

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


def solve(X: int, K: int, D: int):
    X=abs(X)

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

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

    Kh = K // 2

    N = X // (D*2)
    ans = min(ans, abs(X - D * 2 * (N - 1)))
    ans = min(ans, abs(X - D * 2 * N))
    if N < Kh:
        ans = min(ans, abs(X - D * 2 * (N + 1)))

    print(ans)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(X, K, D)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
xx, kk, d = map(int, input().split())


def do_solve(x, k):
    if -d == x == d:
        return 0 if k % 2 != 0 else x
    elif -d < x < d:
        return x if k % 2 == 0 else (x - d if x > 0 else x + d)

    ax = abs(xx) // d
    dd = d * ax
    if x > 0:
        return do_solve(x - dd, k - ax)
    else:
        return do_solve(x + dd, k - ax)

    # if x > 0:
    #     return do_solve(x - d, k - 1)
    # else:
    #     return do_solve(x + d, k - 1)


def solve():
    if xx == 0:
        return d

    ax = abs(xx) // d
    if kk < ax:
        if xx > 0:
            return xx - d * kk
        else:
            return xx + d * kk
        # return xx - (d * ax) if xx > 0 else xx + (d * ax)}

    return do_solve(xx, kk)


print(abs(solve()))
#

Submission status: Accepted

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

if X < 0:
    X *= -1

quotient = X // D

if quotient >= K:
    dis = X - K * D
else:
    remainder = X % D
    if remainder < D - remainder:
        dis = remainder
        if (K - quotient) % 2 != 0:
            dis = D-remainder
    else:
        dis = D - remainder
        if (K - quotient) % 2 == 0:
            dis = remainder

print(dis)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
Xa, K, D = map(int, input().split())
X = abs(Xa)
Xs = X // D
Xm = X % D
if K < Xs:
#     print("A")
    print(abs(X-K*D))
elif Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif ((K-Xs) % 2 == 0):
    print(abs(Xm))
else:
    print(abs(D-Xm))

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
n = (K-X//D)//2
#print(n)
if X - K*D > 0:
    ans = X-K*D
elif X + D*K < 0:
    ans = abs(X+D*K)
else:
    ans = float('inf')
    for i in range(-1, 2):
        res = X + D*(2*(n+i)-K)
        ans = min(ans, abs(res))
        #print(res)

print(ans)

Submission status: Accepted

Solution code:
lst = input().split()

X = int(lst[0])
K = int(lst[1])
D = int(lst[2])

a = int((D * K - X) / (2 * D))

if a < 0:
   a = 0

if a > K:
   a = K

def f(x):
   return X - D * K + 2 * D * x

lst2 = [abs(f(a))]

if a + 1 <= K:
   lst2.append(abs(f(a+1)))

if 0 < a:
   lst2.append(abs(f(a-1)))

result = min(lst2)

print(result)

Submission status: Accepted

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

from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

ans = None
if x > d:
    q, r = divmod(x, d)
    if k <= q:
        ans = x - k * d
    else:
        y = x - q * d
        if (k - q) % 2 == 0:
            ans = y
        else:
            ans = min(abs(y + d), abs(y - d))
else:
    if k % 2 == 0:
        ans = x
    else:
        ans = abs(x - d)

print(ans)

Submission status: Accepted

Solution code:
import math
def func():
#    a=int(input())
    i1=list(map(int,input().split()))
    #print(a)
    #for i in range(a):
    #    print(b[i])
    a=abs(i1[0])
    b=i1[1]
    c=i1[2]
#    print(a,b,c)
    l=math.floor(a/c)
    m=b-l
    n=a-c*l
    if l>=b: z=a-c*b
    else:
        z=a-(c*l)
        if m%2==1:
            z=z-c
    print(abs(z))

func()

Submission status: Accepted

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

r_div = abs(X) // D
r_mod = abs(X) % D

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

print(ans)
# print(X, K, D, r_div, r_mod)

Submission status: Accepted

Solution code:
X, K, D = input().split()
X, K, D = int(X), int(K), int(D)
count = 0
if X < 0:
  X = -X
  L = X
else:
  L = X
if K < L // D:
  L = L - K * D
  K = 0
elif K == L // D:
#  print("koko")
  L = L % D
  K = 0
else:
#  print("C")
  temp = L
  L = L % D - D
  K = K - temp // D - 1
if L == 0:
  if K % 2 == 0:
    print(0)
    exit()
  else:
    print(D)
    exit()
if K == 0:
  print(abs(L))
  exit()
if abs(L) >= L + D:
  goal = L + D
  if K % 2 != 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()
elif abs(L) < L + D:
  goal = abs(L)
  if K % 2 == 0:
    print(abs(L))
    exit()
  else:
    print(L + D)
    exit()

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 == 0:
  print( x%d )
else:
  print(abs(x%d - 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:
    s = X // d
    if (k-s) % 2 == 0:
        print(abs(X-d*s))
    else:
        print(abs(X-d*(s+1)))

Submission status: Accepted

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

times = abs(X) // D

if times < K:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
    K = K - times
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
else:
    if current_position >= 0:
        current_position = current_position - D * K
    elif current_position < 0:
        current_position = current_position + D * K
print(abs(current_position))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = [int(i) for i in input().split()]
x = abs(x)
if k * d <= x:
    print(x - k*d)
else:
    t = x
    x %= d
    k = k - (t - x)//d
    if k & 1:
        print(abs(d - x))
    else:
        print(x)

Submission status: Accepted

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

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

min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    else:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, abs(X), K-i)
        break
print(min_score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def solve():
	arr=[int(i)for i in input().split()]
	n=abs(arr[0]);k=arr[1];d=arr[2]
	diff=n//d
	
	if(diff>k):
		print(n-(d*k))
		return
	else:
		if((k-diff)%2==0):
			print(n-(diff*d))
			
		else:
			print(abs((n-(diff*d))-d))





	


	   
	    
	
	
#test=int(input())
test=1
while test:

	solve()
	test-=1

Submission status: Accepted

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

Submission status: Accepted

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

if abs(X)//D>=K:
  print(abs(X)-D*K)
else:
  for i in range(K):
    buf = X
    if X<0:
      X += D
    else:
      X -= D
    if X == before:
      if (K-i)%2 == 0:
        if X<0:
          X += D
        else:
          X -= D
      else:
        X = before
      break
    before=buf
  print(abs(X))

Submission status: Accepted

Solution code:
# x はいる座標, kは移動できる回数, dが幅
x, k, d = map(int, input().split())
if(x < 0):
    x = abs(x)
# q は商、dが何個　にあるか
q = x // d
# r はあまり
r = x % d

if(k <= q):
    print(x - k*d)
if(q < k):
    k2 = k - q
    # d か -d 奇数回進める
    if(k2 % 2):
        print(min(abs(r -d), r + d))
    # d か -d 偶数回進める
    else:
        print(min(abs(r - 2*d), r ))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#十分に原点から離れている場合
if abs(X) >= K*D:
    ans = abs(X) - K*D
else:
    t = (abs(X) - D*(K%2)) // (2*D)
    t = 2*t + K%2
    if X > 0:
        t = -t
    A = [t-2,t,t+2]
    ans = 3*D
    for a in A:
        if abs(X+D*a)<ans:
            ans = abs(X+D*a)


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

num_min_move = abs(X//D)
tmp = abs(X%D)

if num_min_move == K:
  print(tmp)
elif num_min_move > K:
  print(abs(X)-K*D)
else:
  out = tmp
  if (K - num_min_move) % 2 == 1:
    print(abs(tmp-D))
  else:
    print(tmp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
m,K,D = input().split()
m = int(m)
K = int(K)
D = int(D)
if K < 0 or D < 0:
  print('error')
if m < 0:
  m *= -1
a = (m - m % D) / D
if a > K:
  m -= D*K
  K = 0
else:
  K -= a
  if K % 2 == 0:
    m %= D
  else:
    m = (m % D - D) * -1
print(m)

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
x = abs(x)
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = x - (a * d)
    else:
        x = x - (a * d)
        if x > 0:
            x = x - abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Accepted

Solution code:
import math
X, K, D = map(int, input().split())
ans = 10**16
minc_floor = math.floor(abs(X)/D)
minc_ceil = math.ceil(abs(X)/D)
min_kaisuu = K
# print(minc_ceil, minc_floor)
if minc_ceil <= K:
    for k in range(minc_floor, minc_ceil+1):
        for flug in [-1, 1]:
            if abs(X + flug*k*D) < abs(ans):
                ans = abs(X + flug*k*D)

                # print("ans" + str(ans))
                min_kaisuu = k
                # print(min_kaisuu)
    if (K-min_kaisuu) % 2 != 0:
        ans = min(abs(ans+D), abs(ans-D))
else:
    # ans = min(ans+K*D), abs(ans-K*D))
    if abs(X+K*D) > abs(X-K*D):
        ans = X-K*D
    else:
        ans = X+K*D
# print(min_kaisuu)
print(abs(ans))

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
def resolve_best_i(X, K, D):
    if abs(X - int(X / D) * D) < abs(X - (int(X / D)+1)*D):
        return abs(X - int(X / D) * D), int(X /D)
    else:
        return abs(X - (int(X / D)+1)*D), int(X /D) + 1
def make_most_min(X, K, D):
    mokuhyou = X
    best = 0
    if X >= 0:
        mokuhyou, best = resolve_best_i(X, K, D)
        if best > K:
            best_posi = abs(X - K * D)
            return best_posi
        if best % 2 == K % 2:
            return abs(X - best * D)
        else:
            return min(abs(X - (best+1) * (D)), abs(X - (best-1) * (D)))
if X >= 0:
    a = make_most_min(abs(X), K, abs(D))
else:
    a = make_most_min(-X, K, abs(D))
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

x1 = abs(x)
mod1 = x1 % d
mod2 = mod1 - d
if mod1 > abs(mod2):
    mod = mod2
    modx = mod1
else:
    mod = mod1
    modx = mod2

a = abs(x1-mod) / d 

if k*d < x1-mod:
   print(x1-k*d)
elif (k-a) % 2 == 0:
    print(abs(mod))
else:
    print(abs(modx))

Submission status: Accepted

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

amari = abs(x) % d
shou = (abs(x)-amari)/d

if k <= shou:
    print(int(abs(x)-d*k))
else:
    nokorikaisuu = k-shou
    if nokorikaisuu % 2 == 1:
        print(int(abs(abs(x)-d*shou-d)))
    else:
        print(int(abs(x)-d*shou))

Submission status: Accepted

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

Submission status: Accepted

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

# case0: 絶対値を超える範囲まで届かない場合
if x >= k*d:
    print (x - k*d)
# case1: 絶対値を超える範囲に届く場合: 0の両隣のどちらかを選択
else:
    #wall = (x // d) + 1 # 0を超えるのに必要な移動回数
    #print("wall=",wall)
    #if wall % 2 == 0: # Xが偶数時: どちらも選択可能
    #    print (abs(x - wall*d))
    #else: # Xが奇数時: どちらかのみとなるので注意
    #    print (abs(x - (wall-1)*d))

    div = x//d

    a1 = abs(x - div*d)
    a2 = abs(x - (div+1)*d)
#    print(f"a1={a1}, a2={a2}")
    if (div % 2) != (k % 2):
        print (a2)
    else:
        print (a1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import ceil, floor
X, K, D = map(int, input().split())
ans = abs(X + K*D)
fapp = (K*D - X)/(2*D)

if fapp <= 0:
    list = [0]
elif fapp >= K:
    list = [K]

else:
    list = [ceil(fapp), floor(fapp)]
for f in list:
    abso = abs(X + f*D - (K-f)*D)
    if abso < ans:
        ans = abso

print(ans)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

x, k, d = LI()
if d*k <= abs(x):
    print(abs(x) - d*k)
else:
    y = abs(x) // d
    if (k - y) % 2:
        print(abs(abs(x) - d*(y+1)))
    else:
        print(abs(x) - d*y)