Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

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

Sample Input 1

6 2 4

Sample Output 1

2

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

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

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

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

Sample Input 2

7 4 3

Sample Output 2

1

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

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

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

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline().strip()

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

def main():
    INF = 999999999999999999999999
    MOD = 10**9+7

    x,k,d = get_all_int()
    x = abs(x)

    if k*d < x:
        print(x - k*d)
        return
    
    y = (x+d-1) // d

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

main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import setrecursionlimit

setrecursionlimit(10 ** 8)


def main(x, k, d):
    for i in range(k):
        if abs(x + d) < abs(x - d):
            x = x + d
        else:
            x = x - d

        if abs(x - d) > abs(x) and abs(x + d) > abs(x):
            if (k - i - 1) % 2 == 1:
                if abs(x - d) < abs(x + d):
                    x = x - d
                else:
                    x = x + d
            break

    print(abs(x))


def sub(x, k, d):
    print(abs(x) - d * k)


a, b, c = map(int, input().split())

if abs(a) - b * c >= 0:
    sub(a, b, c)
else:
    main(a, b, c)

Submission status: Accepted

Solution code:
pos, times, ranges = map(int, input().split())
first_try = abs(pos) // ranges

if first_try >= times: 
    print(abs(pos) - times * ranges)
else: #正の範囲で０に近づくだけ移動する
    remain_times = times - first_try
    second_pos = abs(pos) - first_try * ranges
    if remain_times % 2 == 0: #のこりが偶数回しかないなら終了
        print(abs(second_pos))
    else:
        if abs(second_pos + ranges) <= abs(second_pos - ranges):
            print(abs(second_pos + ranges))
        else:
            print(abs(second_pos - ranges))

Submission status: Accepted

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

Submission status: Accepted

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

t = x/d

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

Submission status: Accepted

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

t = float(abs(x/d))

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

Submission status: Accepted

Solution code:
import math

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

dis_X = abs(X)
stps = int(dis_X/D)

if K < stps:
  print(dis_X - K*D)
else:
  diff_stps = K - stps
  if diff_stps % 2 : #odd
    print( (stps+1)*D - dis_X )
  else: # even
    print( dis_X - stps*D )

Submission status: Accepted

Solution code:
inp = list(map(int,input().split(" ")))
location = abs(inp[0])
times = inp[1]
distance = inp[2]

x = float(abs(location / distance))


if x >= times:
    print(abs(location - (times*distance)))
elif (times-int(x))%2==0:
    print(abs(location-abs(int(x)*distance)))
else:
    print(abs(location-abs(int(x)*distance)-distance))

Submission status: Accepted

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

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

x = abs(x)
if (x//d)%2 == k%2:
  print(x % d)
  exit()

else:
  print(-(x % d - d))
  exit()

Submission status: Accepted

Solution code:
import math

x, k, d = map(int, input().split())
left = 0
right =k
x = abs(x)
while(left<right):
    mid = (left+right)//2
    if(x-(d*mid)<0):
        right = mid
    else:
        left = mid+1
low= int(left)
high = int(left-1)
v1 = abs(x-(low*d))
v2 = abs(x-(high*d))
if(k%2==0):
    if(v1<=v2):
        if(low%2==0):
            print(v1)
        else:
            print(v2)
    else:
        if(high%2==0):
            print(v2)
        else:
            print(v1)
else:
    if(v1<=v2):
        if(low%2==1):
            print(v1)
        else:
            print(v2)
    else:
        if(high%2==1):
            print(v2)
        else:
            print(v1)

Submission status: Accepted

Solution code:
import sys
x,k,d = map(int,input().split())
x=abs(x)
c = int(x/d)
le = int(k-abs(c))
if x==0:
    if k%2==0:
        print(0)
    else:
        print(d)
    sys.exit()
else:
    if le>=0:
        ne = -d*c+x
    else:
        if x>0:
            ne = -d*k+x
        else:
            ne = d*k-x
        print(abs(ne))
        sys.exit()
        
if le%2==0:
    print(abs(ne))
else:
    ans1=abs(ne-d)
    ans2=abs(ne+d)
    print(min(ans1,ans2))

Submission status: Accepted

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

Submission status: Accepted

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

y = x // d


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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

    X = abs(X)
    div, mod = X // D, X % D
    if K <= div:
        print(X - K * D)
    else:
        print(abs(mod - D * ((K - div) % 2 == 1)))


if __name__ == "__main__":
    main()

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

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
    div = X // (D*2)
    ans = min(X - min(div, K//2) * (D*2),
              abs(X - min(div+1, K//2) * (D*2)))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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


def solve(X: int, K: int, D: int):
    X = abs(X)
    n = X // D
    if n >= K:
        return X - D * K
    m = X % D
    if (K - n) % 2 == 0:
        return m
    else:
        return D-m


def main():
    tokens = chain(*(line.split() for line in sys.stdin))
    X = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    ans = solve(X, K, D)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = [int(a) for a in list(input().split())]
X = abs(X) 
q = X//D
r = X%D
if q >= K:
    print(X - D*K)
    exit()
rem = K - q
if rem % 2 == 0:
    print(r)
else:
    print(abs(D-r))

Submission status: Accepted

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

x=abs(x)

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#C_Walking Takahashi 
X,K,D = map(int,input().split())
X = abs(X)
step = min(K,int(X/D))
K -= step
X -= step * D
if (K % 2 == 0):
    out = X
else:
    out = D - X
   
# 出力
print(out)

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
if x < 0:
    x *= -1
t1 = x % d
t2 = t1 - d
n = (x - t1) // d
if k < n:
    print(x - k * d)
else:
    n = k - n
    if n % 2 == 0:
        print(t1)
    else:
        print(-t2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#n = int(input())
x, k, d = map(int, input().split())
#l = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]

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

Submission status: Accepted

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

n = abs(x)//d 

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

Submission status: Accepted

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

print(x)

Submission status: Accepted

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

if X < 0:
    X = -X
a = X // D
b = X % D

if K <= a:
    ans = X - D * K
else:
    if (K - a) % 2:
        ans = D - b
    else:
        ans = b
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if x>=k*d:
    print(x-k*d)
    exit()
else:
    kk=x//d
    kkk=kk+1
    ans=float("inf")
    ans2=float("inf")
    xx=k-kk
    if xx%2==1:
        ans=abs(x-kk*d-d)
    else:
        ans=abs(x-kk*d)
    y=k-kkk
    if y%2==1:
        ans2=abs(x-kkk*d+d)
    else:
        ans2=abs(x-kkk*d)
    print(min(ans,ans2))

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# # input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

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

Submission status: Accepted

Solution code:
x,k,d = map(int, input().split())
if x < 0:
    x = -x
x_min = x
if x//d >= k:
    x_min -= k * d
else:
    x_min -= x//d * d
    k -= x//d
    if k % 2 == 1:
        x_min = abs(x_min - d)
print(x_min)
#xが更新されてる！！

Submission status: Accepted

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

maxDist=K*D
if(zX>maxDist):
  print(zX-maxDist)
else:
  sabun=maxDist-zX
  ans1 = sabun % (D*2)
  ans2 = abs(ans1 - (D*2))
  if(ans1 > ans2):
    print(ans2)
  else:
    print(ans1)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def is_x(x,k,d):
    if x==0:
        if k%2==0:
            print(0)
            exit()
        else:
            print(d)
            exit()

def x_is_mosbat(x,k,d):
    if x<0:
        x=-x
    p=x//d
    if p>=k:
        print(x-d*k)
        exit()
    else:
        x=x-d*p
        k-=p
        if abs(x-d)<x:
            x=x-d
            k-=1
        if k%2==0:
            print(abs(x))
            exit()
        else:
            print(abs(x-d)) if x>0 else print(x+d)
            exit()
    
x,k,d=map(int,input().split())
is_x(x,k,d)
x_is_mosbat(x,k,d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = map(int, raw_input().split())
def f(x,k,d):
	if k == 0:
		return abs(x)

	if abs(x) <= d:
		if k % 2 == 0:
			if k <= 2:
				return abs(x)
			else:
				return min(abs(x),abs(x-2*d), abs(x+2*d) )#f(x+d,k-1,d), f(x-d,k-1,d))	
		else:
			return min(abs(x+d), abs(x-d))
	else:
		if x > d:
			if x/d <= k:
				return f(x - (x/d) * d, k - x/d, d)
			elif x / d > k:
				#get as close to zero as u can
				return f(x - k*d, 0, d)
		elif x < -d:
			return f(-x, k, d)

print f(x,k,d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k, d = map(int,input().split(' '))
n = abs(n)
ans = []
cur = n // d
if cur >= k: print (n - k * d)
else:
    n -= (cur * d)
    ans.append(n)
    ans.append(abs(n - d))
    k -= cur
    print (ans[k & 1])

Submission status: Accepted

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

Submission status: Accepted

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


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

    else:
        print(D)

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import math


def read():
    X, K, D = map(int, input().strip().split())
    return X, K, D


def solve(X, K, D):
    X = abs(X)
    k = X // D 
    if K % 2 == 0:
        if k % 2 == 0:
            i = min(X // D, K)
        else:
            i = min(X // D + 1, K)
    else:
        if k % 2 == 0:
            i = min(X // D + 1, K)
        else:
            i = min(X // D, K)
    ans = X - i * D
    return abs(ans)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

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

Xa = abs(X)
Xs = Xa // D
Xm = Xa % D

if (K <= Xs):
    print(abs(Xa-D*K))
elif (K-Xs) % 2 == 0:
    print(abs(Xm))
else:
    print(abs(D-Xm))

Submission status: Accepted

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

a = x // d

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

Submission status: Accepted

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

X = abs(Xa)
Xs = X // D
Xm = X % D

if K < Xs:
#     print("A")
    print(abs(X-K*D))
# elif Xa == 0:
#     if K % 2 == 0:
#         print(0)
#     else:
#         print(D)
# elif Xm == 0:
#     if K % 2 == 0:
#         print(D)
#     else:
#         print(0)
elif ((K-Xs) % 2 == 0):
    print(abs(Xm))
else:
    print(abs(D-Xm))
# else:
# #     print(abs(D-K))
#     if ((K-Xs) % 2 == 0):
#         print(abs(D-K))
#     else:
#         print(abs(D-Xm))

Submission status: Accepted

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

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    X, K, D = list(map(int, input().split()))

    return X, K, D


def main(X: int, K: int, D: int) -> None:
    """
    メイン処理.

    Args:\n
        X (int): 座標(-10^{15} <= X <= 10^{15})
        K (int): 移動回数(1 <= K <= 10^{15})
        D (int): 移動距離(1 <= D <= 10^{15})
    """
    X = abs(X)

    # 求解処理
    ans = int()
    straight = min(K, int(abs(X) / D))
    K -= straight
    X -= straight * D

    if K % 2 == 0:
        ans = X
    else:
        ans = D - X

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    X, K, D = get_input()

    # メイン処理
    main(X, K, D)

Submission status: Accepted

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

X = abs(Xa)
Xs = X // D
Xm = X % D

if K < Xs:
#     print("A")
    print(abs(X-K*D))
elif Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif ((K-Xs) % 2 == 0):
    print(abs(Xm))
else:
    print(abs(D-Xm))
# else:

Submission status: Accepted

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

Submission status: Accepted

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

x=abs(x)#正方向スタートで考える
if x-d*k >= 0:#一方向にのみ動かして済むとき
    ans=abs(x-d*k)
else:#往復運動するとき
    k_dash=x//d
    R=x-k_dash*d
    L=R-d

    if (k-k_dash)%2 == 0:#移動できる残り回数を調べる 0:左側 1:右側
        ans=abs(R)
    else:
        ans=abs(L)

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#memo
"""
繰り返しやった時に絶対値が最小になればよい。
毎回0に近づく動きをさせよう。
"""

list_in = list(map(int,input().split()))
xin = list_in[0]
kin = list_in[1]
din = list_in[2]

now_place = abs(xin)
rem = kin

cnt = min(now_place//din, kin)
now_place -= din * cnt
rem -= cnt

if rem %2 == 1:
    now_place = now_place - din

ans = abs(now_place)

print(ans)

Submission status: Accepted

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

x=abs(x)
ans=0
if x>=d*k:
  ans=x-k*d
else:
  e= int(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 = list(map(int, input().split()))

x = abs(x)
p = 0

if x/d >= k:
    p = x-(k*d)

else:
    p = x%d
    if (k - int(x/d))%2 == 1:
        p = p-d
print(abs(p))

Submission status: Accepted

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

x = abs(x)

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

else:
  print(abs((x - int(x/d)*d)-d))

Submission status: Accepted

Solution code:
def ii():
    return int(input())
def mi():
    return map(int, input().split())
def li():
    return list(mi())

l = li()
x, k, d = l[0], l[1], l[2]
x = abs(x)
calc = x // d
mi = min(k, calc)
k -= mi
x -= mi * d

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if X < 0:
    X=-X  # これにより、X>=0の場合だけ考えればよい

#+D or -D
# X + D*(K-n) + (-D) *n ;0<=n <=K
# -> X + D*(K-2*n)
# 1, X>=K*D-> K回全部（-D）移動に使うことが最適->n=K
# 2, X < K*D ->
# X >= p*D を満たす最大のpを探す -> p回（-D）移動に使うことが最適
# 残り(K-p)%2=0なら行ったり来たりで消化できる.
# 残り回数が奇数の場合、R=X-D*pとすると、min(R+D,abs(R-D))

n = 0  # 負方向移動の回数
result = X + D*(K-2*n)

# if X == 0:
#     if K % 2 == 0:
#         n = K/2
#         print(result)
#     else:
#         print(D)

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

elif X < D*K:
    p = int(X/D)  # X/D回負の方向に移動すれば、原点に最も近い正の値になる。
    result=X-D*p
    if (K-p) % 2 == 0:
        print(result)
    elif (K-p)%2 ==1:
        print(abs(result-D))

Submission status: Accepted

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

if abs(X) > K*D:
    X = abs(X) - K*D
else:
    # とりあえず同方向に移動できるところまでいく
    # 移動回数
    T = abs(X)//D
    # 移動先の座標
    Y = abs(X)%D
    
    # 残りは往復の2択
    X = abs(Y - ((K-T)%2)*D)   
    
print(X)

Submission status: Accepted

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

# x >= 0
if x < 0:
    x *= -1

if k * d < x:
    ans = x - k * d
else:
    q, r = divmod(x, d)
    k -= q
    if k & 1:
        ans = d - r
    else:
        ans = r

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# (X-D) mod D = (X+D) mod D
#  |Y-X|/D mod 2 = K mod 2

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

X = abs(X)

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

n = X//D
x = X%D
k = K-n

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

Submission status: Accepted

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

X = abs(X) 

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

x,k,d = readints()
x = abs(x)

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

print(x)

Submission status: Accepted

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

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

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

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

else:
    print(abs(D - X % D))

Submission status: Accepted

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

X = abs(X)

nokori = abs(X - (K * D)) #残りマス
q = abs(X // D) #商
p = abs(X % D) #余り
nokorik = K-q #残り回数
if K <= q:
    print(nokori)
else:
    if nokorik % 2 == 0:
        print(p)
    else:
        print(abs(p-D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
"""#
Y = X (mod D)
|Y - X|/ D = K (mod 2)
x = p * D + r (p, r == Z, 0 <= r < D)
#"""
#XからYへ移動を繰り返した後
# 余った回数（偶数回）だけ-Dした座標との間を行き来すればよい

m = abs (int(X / D))
if K < m:   #途中でK回使い切った場合
    print(X - D * K)
else:
    K = K - m
    A = X - D * m
    if K % 2 == 0:
        print(A)
    else:
        print(abs(A - D))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
import sys

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

if x<0:
  x*=-1

num=math.floor(x/d)

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

n=x-num*d
remaining=k-num

if remaining%2==0:
  if n>0:
    print(n)
    
  else:
    print(n*-1)
    
else:
  if n>0:
    n-=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)
      
  else:
    n+=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)