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

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
if d*k<x:
    exit(print(x-d*k))
l=0
r=k
while r-l>1:
    m=l+r>>1
    if d*m>=x:
        r=m
    else:
        l=m
if(k-r)%2:
    print(min(abs(d*~-r-x),abs(d*-~r-x)))
else:
    a=d*r-x
    if r<k:
        a=min(a,abs(d*(r-2)-x),abs(d*(r+2)-x))
    print(a)

Submission status: Accepted

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

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

K2= K - (abs(X) // D)


X= abs(X) - abs(((K-K2)*D))

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


print(abs(X))

Submission status: Accepted

Solution code:
def point(x):
    return (abs(x), x)

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

    if X < 0:
        k = abs(X) // D
        k = min(k, K)
        ans = X + D*k
        K -= k
        # print(ans, K)
        if K >= 1 and point(ans + D) < point(ans):
            ans += D
            K -= 1
        # print(ans, K)
        if K % 2 == 1:
            ans = ans - D if ans > 0 else ans + D

    else:
        k = X // D
        k = min(k, K)
        ans = X - D*k
        K -= k

        # print(ans, K)
        if K >= 1 and point(ans - D) < point(ans):
            ans -= D
            K -= 1
        # print(ans, K)
        if K % 2 == 1:
            ans = ans - D if ans > 0 else ans + D

    print(abs(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

l=0#右に進む回数
r=K

if D*K + X <=0:
    print(abs(D*K+X))
    exit()
if -D*K +X >=0:
    print(abs(-D*K+X))
    exit()

while r-l > 1:
    k = (l+r)//2

    if D*(2*k-K)+X > 0:
        r=(l+r)//2
    else:
        l=(l+r)//2
    
    if D*(2*k-K)+X ==0:
        print(0)
        exit()
k=l
l=abs(D*(2*k-K)+X)
k=r
r=abs(D*(2*k-K)+X)

print(min(r,l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
flag = False
if x < 0:
    x = -x
    flag = True

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

Submission status: Accepted

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

X = abs(X)

d, r = divmod(X, D)
if K >= d:
    if (K - abs(d)) % 2 == 0:
        print(min(r, abs(X - D*K)))
    else:
        print(min(abs(r - D), abs(X - D*K)))
else:
     print(abs(X - D*K))

Submission status: Accepted

Solution code:
#Problem C

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

T_min = abs(X)//D
ans = 0

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


if (T_min-K)%2 ==0:
    ans = abs(abs(X) - (T_min*D))
else:
    ans1 = abs(abs(X) - (T_min*D) + D)
    ans2 = abs(abs(X) - (T_min*D) - D)
    ans = min(ans1,ans2)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

x = abs(x)

a = x // d

if k <= a:
    ans = x - k * d
    print(abs(ans))
    exit()

x -= a * d
x = abs(x)
k -= a


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

Submission status: Accepted

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


def solve(X: int, K: int, D: int):
    if X<0:
        X *= -1
    a = X % D
    b = X // D
    if K<b:
        ans = X - K * D
    else:
        if ((K-b) % 2)==0:
            ans = a
        else:
            ans = -(a-D)
    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:
X, K, D = map(int, input().split())

if X < 0:
    X = -1 * X

x = X % D
N = X // D

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    x, k, d= map(int, input().split())
    if x == 0:
        if k%2 == 0:
            print(0)
            return
        else:
            print(d)
            return
    else:
        if x < 0:
            x = -1*x
        if k*d <= x:
            print(x-k*d)
            return
        else:
            y = x//d
            p = x-d*y
            nokori = k - y
            if nokori%2 == 0:
                print(p)
            else:
                print(d-p)
        

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    if X >= K * D:
        return X - K * D
    overzero_K = math.ceil(X/D)
    overzero_X1 = X - overzero_K * D
    overzero_X2 = overzero_X1 + D
    left_K = K - overzero_K
    if left_K % 2 == 1: return overzero_X2
    else: return overzero_X1

print(abs(main()))

Submission status: Accepted

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

n_to_move = X // D
  
if K >= n_to_move:
  pre_pos = X % D
  n_move = X // D
    
  n_nokori_move = K - n_move
    
  if n_nokori_move % 2 == 0:
    last_pos = pre_pos
      
  else:
    last_pos = min(abs(pre_pos + D), abs(pre_pos - D))
	
else:
  if X >= 0:
    last_pos = X - K * D
    

print(last_pos)

Submission status: Accepted

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

a,b = divmod(x,d)
if a <= k:
    if (k-a) % 2 == 0:
        print(b)
    else:
        print(min(abs(b-d),abs(b+d)))
else:
    print(x - k*d)

Submission status: Accepted

Solution code:
import sys
x, k, d = map(int, input().split())
if x>0 and x-k*d>=0:
  print(x-k*d)
  sys.exit()
if x<0 and x+k*d<=0:
  print(-x-k*d)
  sys.exit()
a=0
b=0
for i in range(1,k+1):
  if abs(x)-i*d<=0:
    a=i
    b=i-1
    break
if (k-a)%2 ==0:
  print(-abs(x)+a*d)
else:
  print(abs(x)-b*d)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    X, K, D = map(int, input().split())
    M = abs(X)%D
    S = abs(X)//D
    C = K - S 
    if C < 0:
        ans = abs(X) - K*D
    else:
        if M <= abs(M-D):
            if C%2 == 0:
                ans = M
            else:
                ans = abs(M - D)
        else:
            C -= 1
            if C%2 != 0:
                ans = M
            else:
                ans = abs(M - D)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

x,k,d=vfunc(int)(reader()[0])

x=abs(x)
if d*k<x:print(x-d*k)
else:
    k0 = int(x/d)+1
    x0 = abs(x-d*k0)
    print(x0 if (k-k0)%2==0 else (d-x0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def li():
    return [int(x) for x in input().split()]


X, K, D = li()

if X < 0:
    X *= -1

ans = 0
if K * D < X:
    #print("A")
    ans = X - K * D
else:
    over_K = X // D + 1
    #print(over_K)
    if (K - over_K) % 2 == 1:
        #print("B")
        ans = X % D
    else:
        #print("C")
        ans = D - (X % D)

print(ans)

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
ax = abs(x)
ad = abs(d)
import sys
if x+d*k<0:
    print(-(x+d*k))
elif 0<x-d*k:
    print(x-d*k)
else:
    m1 = ax%ad
    m2 = d-m1
    mm = min(ax%ad,d-ax%ad)
    if mm == m1:
        if (ax//ad)%2==k%2:
            print(m1)
        else:
            print(m2)
        sys.exit()
    if mm == m2:
        if (ax//ad)%2==(k+1)%2:
            print(m2)
        else:
            print(m1)
        sys.exit()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve():
    x, k, d = map(int, input().split())
    l = x - k * d
    r = x + k * d
    if l > 0:
        print(l)
        return
    elif r < 0:
        print(-r)
        return
    a1 = r - r // (2 * d) * (2 * d)
    a2 = a1 - (2 * d)
    ans = min(abs(a1), abs(a2))
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

count = abs(x) // d

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

dates = [int(i) for i in input().split(" ")]
X = dates[0]
K = dates[1]
D = dates[2]
numDiv = math.ceil(abs(X) / D)

if numDiv < K:
    if X >= 0:
        X -= numDiv * D
        K -= numDiv
    else:
        X += numDiv * D
        K -= numDiv
else:
    if X >= 0:
        X -= K * D
        K = 0
    else:
        X += K * D
        K = 0

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

Submission status: Accepted

Solution code:
import sys
x,k,d = map(int, sys.stdin.readline().rstrip("\n").split())
t,d_large = divmod(x, d)
t = abs(t)
if k <= t:
    if x >= 0:
        print(abs(x-d*k))
    else:
        print(abs(x+d*k))
else:
    d_small = abs(d_large - d)
    dd = t-k
    if dd % 2 == 0:
        print(d_large)
    else:
        print(d_small)

Submission status: Accepted

Solution code:
from math import ceil, floor

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

X = abs(X)

r = floor(X / D)

if K <= r:
    print(X - K * D)
    exit()

l = r + 1

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

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
    # combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    x,k,d = readInt()
    count = abs(x) // d
    mn = 0
    if count>k:
        mn = abs(x) - (d * k)
        print(mn)
        return
    else:
        mn = abs(x) % d
    ans = INF
    if (k-count)%2==0:
        ans = mn
    else:
        ans = abs(mn-d)
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Accepted

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

if x < 0:
    x = -x

a = x // d

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if n%2==0:
  if k%2==0:
    ans = x - n*d
  else:
    ans = (n+1)*d - x
else:
  if k%2==1:
    ans = x - n*d
  else:
    ans = (n+1)*d - x
    
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import math
#input=stdin.readline
#def print(x):stdout.write(str(x)+'\n')
x,n,d=map(int,input().split())
ans=x
if x>0:
    if x-n*d<0:
        c=x//d
        n=n-c
        ans=x-c*d
        a=abs(ans-d)
        b=ans
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x-n*d)
elif x==0:
    if n%2==0:
        print(x)
    else:
        print(x+d)
else:
    if x+n*d>0:
        c=abs(x)//d
        n=n-c
        ans=x+c*d
        a=ans+d
        b=abs(ans)
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(abs(x+n*d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# print(q, r)

if k <= q:
    print(x - k * d)
else:
    k -= q
    # print(f'{k=}')
    
    if k % 2 == 0:
        print(r)
    else:
        print(abs(r - d))

Submission status: Accepted

Solution code:

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

    if X < 0:
        X = -X
    #Xは正の数
    #min abs(X-kD) ただし k = K-2n

    def f(n: int):
        k = K - 2*n
        return X-k*D

    def is_ok(arg):
        #print("n=", arg)
    # 条件を満たすかどうか？問題ごとに定義
        if f(arg) >= 0:
            return True
        else:
            return False

    def meguru_bisect(ng, ok):
        '''
        初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
        まずis_okを定義すべし
        ng ok は  とり得る最小の値-1 とり得る最大の値+1
        最大最小が逆の場合はよしなにひっくり返す
        '''
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok

    n = meguru_bisect(0, K//2)
    
    ans = min(abs(f(max(0, n-1))),
        abs(f(n)),
        abs(f(min(K//2+1, n+1)))
    )
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# print('input >>')
X, K, D = map(int,(input().split()))

# 0を横切らない場合
if K * D <= abs(X):
    print(abs(X) - K * D)
# 0を横切る場合
else:
    sho = abs(X) // D
    # 偶奇一致
    if sho % 2 == K % 2:
        print(abs(X) % D)
    else:
        print(abs(abs(X) % D - D))

# print('-----output-----')

Submission status: Accepted

Solution code:
import os

x,k,d = (int(x) for x in input().split())

x = abs(x)

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

Submission status: Accepted

Solution code:
import math

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

KL = (X+K*D)/(2*D)

if 0 <= KL < K:
    ans1 = X - math.floor(KL)*D + (K-math.floor(KL))*D
    ans2 = X - math.ceil(KL)*D + (K-math.ceil(KL))*D
    if abs(ans1) < abs(ans2):
        print(abs(ans1))
    else:
        print(abs(ans2))
elif 0 > KL:
    print(abs(X+K*D))
else:
    print(abs(X-K*D))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

print(abs(ans))

Submission status: Accepted

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

#正の処理
if x > 0:
    neededmove = x//d
    amari = x%d
    if abs(amari - d) < abs(amari):
        amari = abs(amari - d)
        neededmove += 1
    if k < neededmove:
        print(abs(x-k*d))
    elif k == neededmove:
        print(abs(amari))
    else:
        hoge = k - neededmove
        if hoge % 2 == 0:
            print(abs(amari))
        else:
            if amari <= 0:
                print(abs(amari + d))
            else:
                print(abs(amari-d))
            
#負の処理
elif x < 0:
    neededmove = abs(x//d)
    amari = x%d
    if abs(amari + d) < abs(amari):
        amari = abs(amari + d)
        neededmove += 1
    if k < neededmove:
        print(abs(x+k*d))
    elif k == neededmove:
        print(abs(amari))
    else:
        hoge = k - neededmove
        if hoge % 2 == 0:
            print(abs(amari))
        else:
            if amari <= 0:
                print(abs(amari + d))
            else:
                print(abs(amari-d))
#0の処理
else:
    if k % 2 == 0:
        print(0)
    else:
        print(d)

Submission status: Accepted

Solution code:
import sys

x,k,d=map(int,input().split())
x=abs(x)
if(abs(x%d)<abs(x%d-d)):
  min_num=x%d
  min_num2=x%d-d
else:
  min_num=x%d-d
  min_num2=x%d

kaisuu=(x-min_num)/d

if(kaisuu>k):
  print(abs(x-d*k))
  sys.exit()
if((k%2==0 and kaisuu%2==0)or(k%2==1 and kaisuu%2==1)):
  print(abs(min_num))
else:
  print(abs(min_num2))

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

count = abs(X) // D


if K >= count:
    if count % 2 == 0:
        if K % 2 == 0:
            print(abs(X) % D)
            exit()
        else:
            print(abs((abs(X) % D) - D))
            exit()
    else:
        if K % 2 == 1:
            print(abs(X) % D)
            exit()
        else:
            print(abs((abs(X) % D) - D))
            exit()

if X < 0:
    print(abs(X + (K * D)))
else:
    print(X - (K * D))

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)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

left = k - (abs(x) // d)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

sho = abs(X)//D
amari = abs(X) % D

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

if sho % 2 == K % 2:
    print(amari)
else:
    print(D - amari)

Submission status: Accepted

Solution code:
import sys
import bisect as bi
import math
from collections import defaultdict as dd
input=sys.stdin.readline
##sys.setrecursionlimit(10**7)
def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
for _ in range(1):
    x,k,d=cin()
    x=abs(x)
    jumps=x//d
    des1=x%d
    if(k<=jumps):
        ans=x-k*d
    else:
        if((k-jumps)%2==0):
            ans=des1
        else:
            ans=d-des1
    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)
  exit()

K -= X//D

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

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

elif X - K*D >= 0:
  print(X - K*D)
    
elif X - K*D < 0:
  a = X // D
  b = a + 1
  if (K - a) % 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 K*D<=X:
    print(X-(K*D))
    exit()
else:
    div=X//D
    if (K-div)%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*d>=abs(x):
    n=abs(x)//d
    if (k-n)%2==0:
        print(abs(x)-n*d)
    else:
        print(min(abs(x)-n*d+d, abs(abs(x)-n*d-d)))
else:
    print(abs(x)-(k*d))

Submission status: Accepted

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

X = abs(X)

tmp = int(X/D)

if tmp <= K:
    X -= tmp*D
    K -= tmp

    tmp = K % 2

    for i in range(tmp):
        if abs(X-D) < X + D:
            X = abs(X-D)
        else:
            X = X + D
else:
    X -= K*D

print(X)

Submission status: Accepted

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

N = abs(X)//D

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

Submission status: Accepted

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

x=abs(x)

saisho=x//d

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
#print(x,k,d)
x=abs(x)
a = x//d
b = x%d

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

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

Submission status: Accepted

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

X = abs(X)

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

mov = X // D    # 0～Xの間で移動する回数

if abs(X-mov*D) > abs(X-(mov+1)*D):
    mov += 1

rem = abs(X-mov*D)
mov = K - mov   # 残回数（あとは地点0付近を行ったり来たりするだけ）
mov = mov % 2

print(abs(rem-mov*D))