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 = map(int,input().split())

count = abs(x)//d

if x<0:
    before_border = x+d*count
    after_border = x+d*(count+1)
else:
    before_border = x-d*count
    after_border = x-d*(count+1)
    
if count >= k:
    if x<0:
        print(abs(x+d*k))
    else:
        print(abs(x-d*k))
else:
    if (count-k)%2 == 0:
        print(abs(before_border))
    else:
        print(abs(after_border))

Submission status: Accepted

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

Submission status: Accepted

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

x_par=x%d
x_num=int((x-x_par)/d)

s=0
if x_num<=k:
  x_div=x-x_num*d
  k-=x_num
  if k%2==0:
    s=abs(x_div)
  else:
    s=x_div-d
else:
  s=x-d*k
print(abs(s))

Submission status: Accepted

Solution code:
import math
X, K, D = map(int, input().split())
ans = 0
if K * D < abs(X):
    ans = abs(X) - K * D
elif (K - math.floor(abs(X) / D)) % 2 == 0:
    ans = abs(X) - (math.floor(abs(X) / D)) * D
else:
    ans = abs(X) - (math.floor(abs(X) / D)) * D - D

print(abs(int(ans)))

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline
X, K, D = map(int, input().split())
A=int((K-X/D)/2)
if 0<=A and A<=K:print(min(abs(-K*D+2*A*D+X),abs(-K*D+2*(A+1)*D+X)))
else:print(min(abs(X+K*D),abs(X-K*D)))

Submission status: Accepted

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

if x == 0:
    if k % 2 == 1:
        x = d

elif x > 0:
    if x >= k*d:
        x -= k*d
    else:
        n = x//d
        x -= n*d
        k -= n

        if k%2 ==1:
            x -= d
else:
    if x <= -(k*d):
        x += k*d

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

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

Submission status: Accepted

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

X = abs(X)
m = X // D
p = K - m if m <= K else 0

if p == 0:
    print(X - (K*D))
else:
    X -= m * D
    if p & 1:
        print(abs(X-D))
    else:
        print(X)

Submission status: Accepted

Solution code:
import math
X,K,D=map(int,input().split())
count=0
#Xが初期値
#Dが移動量（+or-）
#K回移動後の最小の座標
#X/D＞Kの場合、K*D＋Xが答え

Y = 0
#X>0 D>0 の場合
if X / D > K and X / D > 0:
    Y = abs(X - D * K)
elif abs(X / D) > K and X / D < 0:
    Y = abs(X + D * K)
else:
    #X/D<Kの場合、X mod D
    L = abs(X) % D
    M = math.floor(abs(X) / D)
    if (K - M) % 2 == 0:
        Y = L
    else:
        Y=abs(L-D)

print(Y)

Submission status: Accepted

Solution code:
def execute(X, K, D):
    for i in range(K):
        if X > 0:
            X -= D
        else:
            X += D

    return abs(X)

def execute2(X, K, D):
    T = int(abs(X) / D)
    if X > 0:
        if K < T:
            return X - K * D
        else:
            Y = X - T * D
            if (K - T) % 2 == 0:
                return Y
            else:
                return D - Y
    else:
        if K < T:
            return (X + K * D) * -1
        else:
            Y = X + T * D
            if (K - T) % 2 == 0:
                return -1 * Y
            else:
                return D + Y


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

x = np.abs(x)

if x - k * d > 0:
	print(x - k * d)
elif x == d:
	if k % 2 == 0: print(x)
	else:print(0)
else:
	times = k - x // d
	num1 = x % d
	num2 = num1 - d
	if times % 2 == 0:
		print(np.abs(num1))
	else:
		print(np.abs(num2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    in_list = list(map(int, input().split()))
    X = abs(in_list[0])
    K = in_list[1]
    D = in_list[2]

    max_move = K*D
    if max_move < X:
        print(X-max_move)
    else:
        iter1 = int(X/D)+1
        iter2 = K - iter1
        ans = X - iter1*D
        if iter2%2 == 0:
            print(abs(ans))
        else:
            print(abs(ans+D))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

# k回　dだけ変化

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

print(abs(x))

Submission status: Accepted

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

def judge():
    l = x if x > 0 else -x
    i = l//d
    r = l%d

    if (k - i)%2 == 0:
        print(r)
    else:
        print(d - r)
    '''
    if x > 0:
        for r in range(d):
            if (x - r)/d == (x - r)//d:
                i = (x - r)//d
                if (k - i)%2 == 0:
                    print(r)
                else:
                    print(d-r)
                exit()
    else:
        l = -x
        for r in range(d):
            if (l - r)/d == (l - r)//d:
                i = (l - r)//d
                if (k - i)%2 == 0:
                    print(r)
                else:
                    print(d-r)
                exit()
    '''

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Walking Takahashi

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

x = abs(x)

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

k -= x // d
x %= d

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

Submission status: Accepted

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


main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

moves = abs(x) // d

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

Submission status: Accepted

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

Submission status: Accepted

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

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

min_Y = X - K_ * (2 * D)

if min_Y > 0:
  print(min_Y)
else:
  min_Y_p = X % (2 * D)
  min_Y_m = 2 * D - X % (2 * D)
  print(min(min_Y_p, min_Y_m ))

Submission status: Accepted

Solution code:
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()



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

if X<0:
    X*=-1


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

Submission status: Accepted

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

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

else:
  ans=abs(X)-K*D
    
print(abs(ans))

Submission status: Accepted

Solution code:
import math

value = input().split()
X = int(value[0])
K = int(value[1])
D = int(value[2])

if X != 0:
  if X < 0:
    X = -X
  if K * D <= X:
    print(X - K * D)
  else:
    R = math.ceil(X / D)
    K -= R
    X -= D * R
    if K % 2 == 0:
      print(-X)
    else:
      print(X + D)  
else:
  if K % 2 == 0:
    print("0")
  else:
    print(D)

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if x<0:
    x*=(-1)
z=x//d
ans_p=x-z*d
ans_n=-(ans_p-d)
if k<z:
    ans=x-k*d
elif k==z:
    ans=ans_p
else:
    num=k-z
    if num%2==1:
        ans=ans_n
    else:
        ans=ans_p
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,k,d = [int(x) for x in input().split()]
if x < 0:
  x *= -1
def f():
  c = 0
  if x - (k * d) > 0:
    c = x - (k * d)
    return True
  return False

def check():
  l = 0
  r = k
  while l <= r:
    m = (r + l) // 2
    p = x - d * (2 * m - k)
    #print(l,m,r,p)
    if p > d:
      l = m + 1
    elif p < -1 * d:
      r = m - 1
    else:
      return m
  return m

if f():
  print(x - (k * d))
else:
  m1 = check()
  t = x - d * (2 * m1 - k)
  print(abs(t))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []


def main():
    X,K,D = i_map()
    if X<=0:
        X = -X
    tmp1 = X//D
    tmp2 = X-(tmp1 * D)
    if(tmp1 < K):
        if((K-tmp1)%2 == 0):
            print(tmp2)
        else:
            print(D-tmp2)
    else:
        print(X - K * D)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []


def main():
    X,K,D = i_map()
    if X<=0:
        X = -X
    tmp1 = X//D
    tmp2 = X-(tmp1 * D)
    if(tmp1 < K):
        if((K-tmp1)%2 == 0):
            print(tmp2)
        else:
            print(D-tmp2)
    else:
        print(X - K * D)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

M=X//D
Q=X%D

if M%2 == K%2:
    print(Q)
else:
    print(D-Q)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

q = x // d
r = x % d

if k >= q:
    if k%2 == q%2:
        print(r)
    else:
        print(d - r)
else:
    print(x - d*k)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

X = abs(X)

candidate1 = abs(X - K*D)
candidate2 = abs(X % D)
candidate3 = candidate2 - D

count2 = (X - candidate2)//D
if (K - count2) % 2==0:
    count2ok = True
else:
    count2ok = False


count3 = (X - candidate3)//D
if (K - count3) % 2==0:
    count3ok = True
else:
    count3ok = False

candidate3 = abs(candidate3)

if abs(X) - K*D > 0:
    print(candidate1)
elif count2ok == True and count3ok == True:
    print(min([candidate1, candidate2, candidate3]))
elif count2ok == True:
    print(min([candidate1, candidate2]))
elif count3ok == True:
    print(min([candidate1, candidate3]))
else:
    print(candidate1)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,k,d = list(map(int, input().split()))
amari = abs(x) % d
shou = abs(x) // d

amari_ = abs(amari - d)
if k < shou:
  print(abs(x) - d * k)
else:
  if (k - shou) % 2 == 0:
    print(amari)
  else:
    print(amari_)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
x, k, d = map(int,input().split())
x=abs(x)
t=x-k*d
if t>=0:
    ans=t
else:
    l=x//d#一番近いところまでの回数
    p=x%d#一番近いところ
    if (k-l)%2==0:
        ans=p
    else:
        ans=min(abs(p+d), abs(p-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))
    exit()

if (K-X//D)%2:
    print(D-X%D)
else:
    print(X%D)

Submission status: Accepted

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

def sgn(x):
  if x > 0:
    return 1
  if x < 0:
    return -1
  return 0

if x > 0:
  x1 = x - k * d
else:
  x1 = x + k * d

if sgn(x) == sgn(x1):
  print(abs(x1))
  exit()
  
xright = x % d
xleft = x % d - d
#0目前
xr = (abs(x - xright) // d) % 2
if k % 2 == xr:
  print(abs(xright))
elif k % 2 != xr:
  print(abs(xleft))

Submission status: Accepted

Solution code:
import sys

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

if k <= x // d:
    ans = x - k*d
    print(ans)
    sys.exit()

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().strip().split(" "))

x = abs(x)
g = min(k, x//d)

x -= d*g
k -= g

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//d >= k:
    print(x-k*d)
else:
    k -= x//d
    x -= d*(x//d)
    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())
    x = abs(x)
    if x // d > k:  # 最大長が0まで届くか
        print(x - k * d)
    elif (k - x // d) % 2 == 0:  # 0を反復横跳びする     偶数->X側、奇数->-X側
        print(x % d)
    else:
        print(d - x % d)
        
resolve()

Submission status: Accepted

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

y = abs(X)//D

if K<=y:
    print(abs(abs(X)-K*D))

else  :
    a = (abs(X)-y*D)
    b = (abs(X)-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)


#print(y+1)
#print(1000000000000000)

Submission status: Accepted

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

if (X < 0):
	lo, hi = 0, K
	while (lo < hi):
		mid = int(lo+ (hi - lo) / 2)
		if (X + mid*D >= 0):
			hi = mid;
		else:
			lo = mid + 1;
	if (X + hi*D < 0):
		p1 = hi
		if (abs(X + p1*D) < result):
			result = abs(X + p1*D)
	else:
		p1 = lo
		rem1 = K - p1
		if (rem1%2 == 1):
			result = min((result, abs(X + p1*D + D), abs(X + p1*D - D)))
		p2 = lo - 1
		rem2 = K - p2
		if (rem2%2 == 1):
			result = min((result, abs(X + p2*D + D), abs(X + p2*D - D)))
	print(result)
elif (X > 0):
	lo, hi = 0, K
	while (lo < hi):
		mid = int(lo+ (hi - lo) / 2)
		if (X - mid*D <= 0):
			hi = mid;
		else:
			lo = mid + 1
	if (X - hi*D > 0):
		p1 = hi;
		if (abs(X - p1*D) < result):
			result = abs(X - p1*D)
	else:
		p1 = lo
		rem1 = K - p1
		if (rem1%2 == 1):
			result = min((result, abs(X - p1*D - D), abs(X - p1*D + D)))
		p2 = lo - 1;
		rem2 = K - p2
		if (rem2%2 == 1):
			result = min((result, abs(X - p2*D - D), abs(X - p2*D + D)))
	print(result)
else:
	if (K%2 == 1):
		print(D)
	else:
		print(0)

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 -= e * d
    if (k % 2 == 1):
        x = abs(x - d)
    ans = x
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)
a = min(k, x//d)

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

Submission status: Accepted

Solution code:

def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    a = X // D
    ans = 0
    if a <= K:
        K -= a
        ans = X - D * a
        if K % 2 != 0:
            ans = abs(ans - D)
    else:
        ans = X - D * K
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


resolve()

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

x = abs(x)

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

Submission status: Accepted

Solution code:
def main():
    x, k, d = map(int, input().split())
    l = 0
    r = k
    ans = abs(x + k * d)
    while l <= r:
        mid = int((l + r)/2)
        nw = x - mid*d + (k - mid)*d
        ans = min(abs(nw), ans)
        if nw < 0:
            r = mid - 1
        else:
            l = mid + 1
    print(ans)

main()

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

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

        
N=int(X/D)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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)

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

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

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

Submission status: Accepted

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

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

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


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x-=first_move*d
x=abs(x)
k-=first_move
print(abs(x-d) if k&1 else x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if x >= k*d:
    print(x-k*d)
else:
    t = x//d
    t -= int(t%2 ^ k%2)
    print(min(x-d*t, abs(x-d*(t+2))))

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)))