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:
from sys import stdin, stdout,stderr
import time,math
from collections import defaultdict as dd, Counter
 
def pn(ob):stdout.write(str(ob))
def inp():return stdin.readline()
 
def main():
	x, k, d = map(int, inp().split())
	if(x < 0):
		x *= -1

	if(x >= k * d):
		print(x - k * d)
	else:
		n = x // d
		if(n % 2 == k % 2):
			print(x % d)
		else:
			print(abs(x - (n + 1) * d))
		
  
if __name__ == '__main__':
	main()
	
#n,m=map(int,input().split()) 
# for i in range(n):
#     t=int(stdin.readline()) # input using fast i/p method 
#     if t%m == 0:
#         stdout.write(str(t)+'\n') #input using fast o/p method
 
# take line of integers as input
# 	arr = list(map(int, inp().split()))

Submission status: Accepted

Solution code:


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

Submission status: Accepted

Solution code:
x,k,d = map(int, input().split())
def  abs_calc(x):
    if  x <  0:
        xi =  (-1)* x
    else:
        xi  = x
    return xi


xi = abs_calc(x)
a = xi // d
if a >= k:
    ans = xi - d * k
else:
    k  -= a
    ans = xi - d * a
    if  k %  2 != 0:
        ans =  d - ans
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin,stdout
input = stdin.readline
def write(n,sep="\n"):
	stdout.write(str(n))
	stdout.write(sep)
def gil():
	return list(map(int, input().split()))
X, K, D = map(int, input().split())
X = abs(X)
if X >= K * D:
    print(abs(X - K * D))
    exit()
mov = X // D    
if abs(X-mov*D) > abs(X-(mov+1)*D):
    mov += 1
rem = abs(X-mov*D)
mov = K - mov   
mov = mov % 2
print(abs(rem-mov*D))

Submission status: Accepted

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

#step1:0を超えない範囲で移動する
n = int(X/D)
if(n>K):
    n=K
p = X-D*n
if((K-n)%2==0):
    print(p)
else:
    print(abs(p-D))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, K, D = mi()
N = abs(N)

if K * D > N:
    num = N//D
    if not (num-K)%2:
        print(min(abs(N - D * (N//D)), abs(N - D * (N//D) - 2 * D)))
    else:
        print(min(abs(N - D * (N//D) + D), abs(N - D * (N//D) - D)))
else:
    print(N - D * K)

Submission status: Accepted

Solution code:
def main():
    X, K, D = map(int, input().split())
    X = X if X >= 0 else -X
    if X == 0:
        if K % 2 == 0:
            ans = 0
        else:
            ans = D
    elif X > 0:
        if X - K*D < 0:
            a = X % D
            b = a - D
            k = X // D
            if (K - k) % 2 == 0:
                ans = abs(a)
            else:
                ans = min(abs(b), abs(a + D))
        else:
            ans = X - K*D

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
L=min(X+D*K,X-D*K)
R=max(X+D*K,X-D*K)

if 0<=L:
    print(L)
elif R<=0:
    print(abs(R))
else:
    X=L
    start=0
    end=K
    while end-start>1:
        test=(end+start)//2
        if X+test*2*D<=0:
            start=test
        else:
            end=test
    ans1=abs(X+2*D*start)
    ans2=abs(X+2*D*end)
    print(min(ans1,ans2))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,k,d = list(map(int,input().split()))
x = abs(x)
mst = x//d
if mst&1 != k&1:
    mst+=1
print(abs(x-(min(mst,k))*d))

Submission status: Accepted

Solution code:
def main():
    import sys
    X,K,D=list(map(int,sys.stdin.readline().split()))
    X=abs(X)
    if X-K*D>=0:
        print(X-K*D)
    else:
        count=X//D
        now=X%D
        if (K-count)%2==0:
            print(now)
        else:
            print(abs(now-D))

main()

Submission status: Accepted

Solution code:
x, k, d = (int(entry) for entry in input().split())
r = x
moves = k
if x < 0:
    r = -x

steps = r // d
if steps >= moves:
    print(r - moves * d)
    exit(0)

r -= steps * d
if abs(r - d) <= abs(r):
    steps += 1
    r -= d

moves -= steps
if moves % 2 != 0:
    moves -= 1
    if abs(r - d) < abs(r + d):
        r -= d
    else:
        r += d

print(abs(r))

Submission status: Accepted

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

ans = abs(x)%d
k = k - int(abs(x)/d)

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

for i in range(k):
    if abs(ans) <= d and (k-i)%2 == 0:
        print(abs(ans))
        exit()
    elif ans >= 0:
        ans = ans - d
    elif ans < 0:
        ans = ans + d

print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def abc175c_walking_takahashi():
    x, k, d = map(int, input().split())
    x = -x if x < 0 else x
    num = math.ceil(x / d)
    if num > k:
        print(x-k*d)
        return
    else:
        k -= num
        x -= num*d
        if k % 2 == 0:
            print(abs(x))
        else:
            print(x+d)
abc175c_walking_takahashi()

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==0:
    t=x//d
    print(d if (k-t)%2 else 0)
else:
    t=x//d
    m=x%d
    print(abs(m-d) if (k-t)%2 else m)

Submission status: Accepted

Solution code:
def resolve():
    x, k, d = map(int, input().split())
    x = abs(x)
    a = x // d
    b = x % d
    if a > k:
        print(x - k * d)
        return
    k -= a

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

resolve()

Submission status: Accepted

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

if X < 0:
    X = -X

cnt = 0
Q = X//D
R = X%D

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

if x < 0:
    x *= -1

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
dist = k * d
if x > 0:
    rang = x - dist
else:
    rang = x + dist
    
if x > 0 and rang > 0:
    print(rang)
    quit()
elif x <= 0 and rang <= 0:
    print(abs(rang))
    quit()
num1 = list(divmod(x, d))
num2 = [num1[0] + 1, x - d * (num1[0] + 1)]
if num1[0] % 2 == 0:
    num = [num1, num2]
else:
    num = [num2, num1]
    
print(abs(num[k % 2][1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
count = abs(int(X / D))#移動回数内で中央をまたげるか
if D > abs(X):
    if K % 2 == 1:
        last_place = abs(abs(X) - D)#Kが奇数の場合(済)
    else:
        last_place = abs(X)#(済)
else:
    if count <= K:#移動回数内で中央をまたげる場合
        if (K - count) % 2 == 1:#移動後残り移動回数が奇数の場合(済)
            last_place = abs(abs(X) - (count + 1) * D)
        else:
            last_place = abs(abs(X) - count * D)#(済)
    else:#(済)
        last_place = abs(abs(X) - D * K)
print(last_place)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

num_moved = min(abs(x) // d, k)
num_remained = k - num_moved
if num_moved > k:
  now = x % d
else:
  tmpa = x + d*num_moved
  tmpb = x - d*num_moved
  now = min(abs(tmpa), abs(tmpb))
#  now = x - d*num_moved

if num_remained % 2 == 0:
  result = abs(now)
else:
  tmpa = now + d
  tmpb = now - d
  if abs(tmpa) > abs(tmpb):
    result = abs(tmpb)
  else:
    result = abs(tmpa)
print(result)

Submission status: Accepted

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


if x < 0:
  x *= -1

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

tmp = x//d
x -= tmp * d
k -= tmp

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

Submission status: Accepted

Solution code:
#C - Walking Takahashi

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

p = abs(x)

if p/d < k:
  k = k - (p // d)
  p = p % d

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

elif x >= 0:
  print(abs(x - (k * d)))
else:
  print(abs(x + (k * d)))

Submission status: Accepted

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

import numpy as np

if np.abs(X) > K*D:
    print(np.abs(X)-K*D)
else:
    a = X // D
    b = a + 1
    if a % 2 != K % 2:
        print(np.abs(b*D - X))
    else:
        print(np.abs(X - a*D))

Submission status: Accepted

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

x, k, d = map(int, input().split(' '))
m = (k * d - x) / (2 * d)
ans = 0

def score(c):
    return abs(x - k * d + 2 * d * c)

if m > k:
    ans = k
else:
    if m < 0:
        ans = 0
    else:
        ans = int(m)
    if score(ans) > score(ans+1):
        ans += 1

print(score(ans))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#from math import abs
x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
if(x<=0):
    if(x+k*d<=0):print(abs(x+k*d))
    else:
        res=x%d
        k-=abs(res-x)//d
        if(k%2):
            ans1=res+d
            ans2=res-d
            if(abs(ans1)>abs(ans2)):print(abs(ans2))
            else:print(abs(ans1))
        else:print(abs(res))
else:
    if(x-k*d>0):print(abs(x-k*d))
    else:
        res=x%d
        k-=abs(res-x)//d
        if(k%2):
            ans1=res+d
            ans2=res-d
            if(abs(ans1)>abs(ans2)):print(abs(ans2))
            else:print(abs(ans1))
        else:print(abs(res))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    X, K, D = map(int, input().split())

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


main()

Submission status: Accepted

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

s=abs(x)//d
if s>=k:
  if x>=0:
    print(abs(x-k*d))
  else:
    print(abs(x+k*d))
elif k>s and (k-s)%2==0:
  if x>=0:
    print(abs(x-s*d))
  else:
    print(abs(x+s*d))
else:
  if x>=0:
    y=x-s*d
  else:
    y=x+s*d
  print(min(abs(y+d),abs(y-d)))

Submission status: Accepted

Solution code:
n,k,m = map(int,input().split())
#print(k*m,n)
if n>=0:
    if k*m > n:
        #print(min(n%m, n-(n%m)))
        x = n//m;
        left = n - x*m;
        right = abs(left - m);
        k = k - x;
        if k&1:
            print(right)
        else:
            print(left)
        
    else:
        print(n - k*m)
else:
    n = abs(n)
    k = abs(k)
    m = abs(m)
    if k*m > n:
        #print(min(n%m, n-(n%m)))
        x = n//m;
        left = n - x*m;
        right = abs(left - m);
        k = k - x;
        if k&1:
            print(right)
        else:
            print(left)
        
    else:
        print(n - k*m)

Submission status: Accepted

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

if x >= k * d:
    print(x - k * d)
elif -x >= k * d:
    print(abs(x + k * d))
else:
    a = x // d
    b = x % d
    if b <= abs(b-d):
        if a % 2 == k % 2:
            print(b)
        else:
            print(abs(b-d))
    else:
        if (a+1) % 2 == k % 2:
            print(abs(b-d))
        else:
            print(b)

Submission status: Accepted

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

X = abs(X)

# 1. 近づく
k = X // D
k = min(K, k)
X = abs(X - k * D)
K = K - k
if K == 0:
    print(X)
    exit()

# 2. 相殺する
K = K % 2
if K == 0:
    print(X)
    exit()

# 3. 最小の移動
X = abs(X - D)
print(X)

Submission status: Accepted

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

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

m = X % D
if m != 0:
    a = X // D
    if (K - a) % 2 == 0:
        print(m)
    else:
        print(D - m)
else:
    a = X // D
    if (K - a) % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split()) # 6, 2, 4

x = abs(X)
syo = x // D # 1 = 6 // 4
zan = K - syo # 1 = 2 - 1
ima = x - (D * syo) # 2 = 6 - (4 * 1)

if zan <= 0:
    ans = x - (K * D)
elif zan % 2 == 0:
    ans = min(ima, abs(ima - (D * 2)))
elif zan % 2 == 1:
    ans = min(ima + D, abs(ima - D))

print(ans)

Submission status: Accepted

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

new_X = abs(X) % D
new_K = K - abs(X) // D

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

nk = abs(x) // d
if k <= nk:
    if x >= 0:
        x -= d * k
    else:
        x += d * k
    print(abs(x))
    exit(0)
    
if x >= 0:
    x -= d * nk
else:
    x += d * nk
k -= nk

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

Submission status: Accepted

Solution code:
XKD = list(map(int,input().split()))
X = XKD[0]
K = XKD[1]
D = XKD[2]
X_list=[]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

B, A = divmod(abs(X), D)

if B <= K:
    print(abs((A - ((K-B) % 2) * D)))

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

Submission status: Accepted

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

Submission status: Accepted

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

N = abs(X)//D

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

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
if x==0:
	print((k%2)*d)
	exit(0)
l = 1
h = k
ans = float('inf')
much = float('inf')
pos = x
if x>0 : di = -1
elif x<0: di=1
while l<=h:
	# ~ print("l",l,"h",h)
	m = (l+h)//2
	# ~ print("m",m)
	tmp = x+di*m*d
	# ~ print("tmp",tmp)
	if tmp>0:
		if x>0 : l=m+1
		else: h = m-1
		# ~ print("l",l,"h",h)
	elif tmp<0:
		if x>0: h = m-1
		else:l=m+1
		# ~ print("l",l,"h",h)
	else:
		ans = 0
		much = m
		break
	if abs(tmp) < ans:
		ans = abs(tmp)
		much = m
		# ~ print("much",much,"ans",ans)
	# ~ print("\n\n\n\n")
reste = k-much
if reste%2==0: print(ans)
else:print(min(abs(ans+d), abs(ans-d)))

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    X, K, D = il()
    max_dist = K * D
    if max_dist > abs(X):
        c = abs(X) // D
        k = K - c
        if k % 2 == 0:
            print(abs(abs(X) - (c * D)))
        else:
            print(min(abs(abs(X) - (c * D) + D), abs(abs(X) - (c * D) - D)))
    else:
        print(abs(abs(X) - max_dist))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

# X: 今いる座標
# K: 移動回数
# D: 移動する距離
X, K, D = map(int, input().split())

# 0を目指す

# 0からの距離
distance = abs(X)

# ちょうど0に到達できる？
if distance % D == 0 and K >= distance // D:
    # 0に到達した後の移動回数が偶数なら0
    remaining_move_count = K - distance // D
    if remaining_move_count % 2 == 0:
        print(0)
    else:
        # 奇数ならD
        print(D)
    sys.exit()

# 0に届かないとき
if distance > K * D:
    print(distance - K * D)
    sys.exit()

# 以降は0を超えるとき

# 0手前に行く移動回数
before_zero_count = distance // D
# 0手前の座標
before_zero_point = distance - before_zero_count * D
# 残り移動回数が偶数なら0手前の座標が答え
remaining_move_count = K - before_zero_count
if remaining_move_count % 2 == 0:
    print(before_zero_point)
elif X < 0:
    print(D - before_zero_point)
else:
    print(abs(before_zero_point - D))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import math
from decimal import Decimal, ROUND_DOWN
from collections import deque
from itertools import combinations
import copy

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    (x, k, d) = [int(e) for e in lines[0].split(' ')]


    return (x, k, d)


def solve(x, k, d):

    y = abs(x)
    m =  y // d 

    if debug:
        log("x=%d, k=%d, d=%d, y=abs(x)=%d, m=%d" % (x, k, d, y, m))
    
    result = None
    if k <= m:
        result = y - k * d
    else:
        n = k - m
        if n % 2 == 0:
            result = y - m * d
        else:
            result = abs(y - m * d - d)
             

    return result
    


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
import math
x, k, d = map(int, input().split())
i = math.floor((d*k - x)/(2*d))
if 0<= i <=k:
  print(min([abs(x-d*k+2*d*j) for j in range(i-1, i+2)]))
else:
  print(abs(abs(x)-abs(d*k)))

Submission status: Accepted

Solution code:
def INT():
    return int(input())

def MI():
    return map(int, input().split())

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

X, K, D = MI()

if X < 0:
    X *= -1

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

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

Submission status: Accepted

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

Submission status: Accepted

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

dcnt=abs(x)//d
if abs(x)%d <= abs(abs(x)%d-d):
    dans=abs(x)%d
else:
    dans=abs(abs(x)%d-d)
    dcnt+=1


if dcnt<k:
    if (k-dcnt)%2==0:
        print(abs(dans))
    else:
        print(abs(dans-d))


else:
    print(abs(x)-d*k)

Submission status: Accepted

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

a = int(X/D)
b = K - a
result = X-D*a
if b%2==1:
    result = min(abs(result+D), abs(result-D))
print(result)

Submission status: Accepted

Solution code:
num_bars = 3
length_list = list(map(int,input().split()))

x = abs(length_list[0])
k = length_list[1]
d = length_list[2]

amari =  x % d
syou = x // d

#print(amari)
#print(syou)

val = x

if x - k * d > amari:
  val = x - k * d
elif (k - syou ) % 2 == 0:
  val = amari
else :
  val = d - amari
  
  
print(val)

Submission status: Accepted

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

n_min = abs(X // D)
n_remains = (K - n_min)

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

Submission status: Accepted

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

if q <= K:
    if (K-q)%2==0:
        ans=mod
    else:
        ans=D-mod
else:
    ans=X-K*D
print(ans)

Submission status: Accepted

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

if x >= d:
  i = x // d
  if i <= k:
    k -= i
    x -= i * d
  else:
    x -= k * d
    k = 0

if k % 2 != 0:
  x -= d

print(abs(x))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import combinations
from collections import Counter
import math


def main():
    x, k, d = map(int, input().split())
    t = abs(x) // d

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

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

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

X,K,D = LI()

sign = np.sign(X)
x = abs(X)

# if x//D==0:
#     if K%2==0:
#         ans = X
#     else:
#         ans = -1 * sign * (x - D)
if x//D>K:
    ans = sign * (x-K*D)
# ちょうど0で振動する場合
elif x%D==0:
    if (K - x//D) % 2 ==0:
        ans = 0
    else:
        ans = -1 * D
else:
    # 残り回数が偶数回
    if (K - x//D) % 2 ==0:
        # print('even')
        ans = sign * (x- (x//D)*D)
    else:
        # print('odd')
        ans = -1 * sign * (x - (x//D)*D -D)
print(abs(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

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