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

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

Submission status: Accepted

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

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

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

    if (K-N)%2!=0:
        ans=(N+1)*D-abs(X)
        if ans > abs(abs(X)-(N-1)*D):ans = abs(abs(X)-(N-2)*D)
        if ans > abs(abs(X)-(N+3)*D):ans = abs(abs(X)-(N+2)*D)

print(ans)

Submission status: Accepted

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

need, mod = divmod(x, d)
if k <= need:
    ans = x - k*d
else:
    amari = k - need
    if amari % 2 == 0:
        ans = mod
    else:
        ans = mod - d


print(abs(ans))

Submission status: Accepted

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

#できるだけ0に近づける(K1:正の値ギリギリ K2:負の値ギリギリ)
X = abs(X)

K1 = X // D
K2 = K1 + 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    x, k, d = map(int, input().split())
    if x == 0:
        if k % 2 == 0:
            print(abs(x))
        else:
            r = abs(abs(x) - d)
            print(r)
    elif abs(x) < d:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(abs(abs(x) - d))
    else:
        t1 = abs(x) // d
        if t1 > k:
            r = abs(x) - k * d
            print(r)
        else:
            k -= t1
            x = abs(x) - t1 * d
            if k % 2 == 0:
                print(x)
            else:
                print(abs(x - d))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def main():
    X, K, D = list(map(int, input().split()))
    right = X % D
    left = right - D
    if X in [left, right]:
        if K % 2 == 0:
            print(abs(X))
        else:
            if X == left:
                print(right)
            else:
                print(abs(left))
        return
    if X > right:
        to_right = (X - right) // D
        if to_right >= K:
            print(X - K * D)
        else: # to_right < K
            if (K - to_right) % 2 == 0:
                print(right)
            else:
                print(abs(left))
    else:  # X < left
        to_left = (left - X) // D
        if to_left >= K:
            print(abs(X + K * D))
        else: # to_left < K
            if (K - to_left) % 2 == 0:
                print(abs(left))
            else:
                print(right)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

X = abs(X)
a=X%D
b=D-a

at=(X-a)//D
bt=at+1

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

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

Submission status: Accepted

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

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

a = abs(x) % d

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

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
if k % 2 == 0:
    evenk = True
else:
    evenk = False
if abs(x)//d%2 == 0:
    even = True
else:
    even = False
if k*d < abs(x):
    ans = abs(x)-k*d
elif (evenk == True and even == True) or (evenk == False and even == False):
    ans = abs(x)%d
else:
    ans = abs(abs(x)%d-d)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve(x, k, d):
    if x > 0 and x - k * d > 0:
        return x - k * d
    elif x < 0 and x + k * d < 0:
        return abs(x + k * d)
    elif k % 2 == 0:
        a = x % (2 * d)
        return min(a, abs(a - 2 * d))
    else:
        a = (x - d) % (2 * d)
        return min(a, abs(a - 2 * d))

# def solve_naive(x, k, d):
#     for i in range(k):
#         if x>0:
#             x-=d
#         else:
#             x+=d
#     return abs(x)

# import random

# def check(count=100):
#     for _ in range(count):
#         x = random.randint(-1e15, 1e15)
#         k = random.randint(1, 100)
#         d = random.randint(1, 1e15)
#         if solve(x, k, d) != solve_naive(x, k, d):
#             print(x, k, d, solve(x, k, d), solve_naive(x, k, d))

# check(100)

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

Submission status: Accepted

Solution code:
"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""


import sys
from functools import lru_cache, cmp_to_key
from collections import defaultdict as dd, deque, Counter as C
from bisect import bisect_left as bl, bisect_right as br, bisect
from heapq import heapify, heappop, heappush
from math import ceil, log, floor, sqrt
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+end)
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]


x, k, d = sp()
if x < 0:
    neg = True
else:
    neg = False
x = abs(x)
mv = x // d
t1, t2 = x - mv * d, x - (mv + 1) * d
if k <= mv:
    out(abs(x - k * d))
    exit()
if mv & 1:
    if k & 1:
        out(abs(t1))
    else:
        out(abs(t2))
    exit()
if k & 1:
    out(abs(t2))
else:
    out(abs(t1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

def main(x,k,d):
    x=abs(x)
    q=x//d
    r=x%d
    if k<q:
        ans=x-k*d
    else:
        if (k-q)%2==0:
            ans=r
        else:
            ans=d-r
    return ans

if __name__=='__main__':
    x,k,d=readinput()
    ans=main(x,k,d)
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#coding:utf-8

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

X = abs(X)

ans = 0
kan = X // D

if kan > K:
    ans = abs(X - K * D)
else:
    rest = K - kan
    if rest % 2 == 0:
        ans = abs(X - kan * D)
    else:
        ans = abs(X - (kan + 1) * D)

print("{}".format(ans))

Submission status: Accepted

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

ans = 0
if X - D * K >= 0:
    ans = X - D * K
elif X + D * K <= 0:
    ans = -(X + D * K)
else:
    n = int((K - X / D) / 2)
    ans = abs(X + D * (2 * n - K))
    n += 1
    ans = min(ans, abs(X + D * (2 * n - K)))

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

Submission status: Accepted

Solution code:




import sys
from io import StringIO
import unittest

def resolve():

    # スペース区切りの整数の入力
    X, K, D = map(int, input().split())
    # 文字列の入力
    if abs(X)-K*D>0:
        print("{}".format(abs(X)-K*D))
    else:
        mod = abs(X)%D
        modk = int(abs(X)/D)
        if (K-modk)%2==0:
            print("{}".format(mod))
        else:
            print("{}".format(D - mod))

    # 出力


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """6 2 4"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """7 4 3"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 1 2"""
        output = """8"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(input, output)


if __name__ == "__main__":
	resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    X, K, D = MI()


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



    
    print(ans)

if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

k_i = abs(x)//d

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

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())

x = abs(x)

remain_k = k - x // d
r = x % d
l = d - r

if remain_k <= 0:
    print(x - k * d)
elif remain_k % 2 == 0:
    print(r)
else:
    print(l)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
div, mod = divmod(x, d)
c = div - k
if c == 0:
    ans = mod
elif c > 0:
    ans = x - d * k
else:
    if c%2 == 1: ans = d - mod
    else: ans = mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    X,K,D = map(int,input().split())
    X = abs(X)
    
    if X-D*K >= 0:
        print(X-D*K)
    else:
        k0 = X//D
        x0 = X%D
        x1 = abs(x0-D)
        if (K - k0)%2==0:
            print(x0)
        else:
            print(x1)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

if x < 0:
    x = (-1)*x
if k*d <= x:
    ans = x-k*d
else:
    sho = x//d
    now = x-d*sho
    k -= sho
    if d-now < now:
        k -= 1
        now = d-now
    if k%2 == 0:
        ans = now
    else:
        ans = d-now
print(ans)

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
p = (abs(X)+D-1)//D
if p >= K:
    print(abs(abs(X)-K*D))
    exit()
K -= p
if X > 0:
    X = X-p*D
else:
    X = X+p*D
plus, minus = 0, 0
if X > 0:
    plus = K//2
    minus = (K+1)//2
else:
    plus = (K+1)//2
    minus = K//2
# print(X, plus, minus)
print(abs(X+plus*D-minus*D))

Submission status: Accepted

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

Submission status: Accepted

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


def f(C):
    NX = float("inf")
    if C > K:
        return NX

    if K % 2 == 0:
        if C % 2 == 0:
            NX = abs(abs(X) - C * D)
        else:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
    else:
        if C % 2 == 0:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
        else:
            NX = abs(abs(X) - C * D)

    return NX


C = min(abs(X) // D, K)

print(min(f(C), f(C + 1)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
tmp = abs(X) // D
if tmp >= K:
    print(abs(X) - K * D)
    exit()
X = abs(X) - tmp * D
K -= tmp
Kp = K - 1
Xp = abs(X - D)
K %= 2
Kp %= 2
if K:
    X = abs(X - D)
if Kp:
    Xp = abs(Xp - D)
print(min(X, Xp))

Submission status: Accepted

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

if (abs(X)>=K*D):
    ans = abs(abs(X)-K*D)
else:
    
    m = abs(X)//D
    remk = K - m

    now = abs(X)-m*D

    if remk%2==0:
        ans = now
    else:
        ans = abs(now-D)



    
print(ans)

Submission status: Accepted

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

X = abs(X)
k1 = min(K, X//D)
X -= k1 * D
K -= k1
if K&1: X -= D

print(abs(X))

Submission status: Accepted

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

a, b = divmod(X, D)
# print(f'{a=}, {b=}')

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

_K = K - a
# print(f'{_K=}')
if _K % 2 == 0:
    ans = b
else:
    ans = abs(b - D)
print(ans)

Submission status: Accepted

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

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

print(n)

Submission status: Accepted

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



if abs(x)>k*d:
    print( abs(abs(x)-(k*d)) ) 
        
else:
    
    if k%2==0:
        
        kaisuu_odd_0 = abs(x)//(2*d) *2
        kyori_odd_0 = abs( abs(x)-kaisuu_odd_0*d )
        
        kaisuu_odd_1 = kaisuu_odd_0 + 2
        kyori_odd_1 = abs( abs(x)-kaisuu_odd_1*d )
        
        print(min(kyori_odd_0,kyori_odd_1))
        
    else:
        kaisuu_odd_0 = abs(x)//(2*d) *2 +1
        kyori_odd_0 = abs( abs(x)-kaisuu_odd_0*d )
        
        kaisuu_odd_1 = kaisuu_odd_0 - 2
        kyori_odd_1 = abs( abs(x)-kaisuu_odd_1*d )
        
        print(min(kyori_odd_0,kyori_odd_1))

Submission status: Accepted

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

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

Submission status: Accepted

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

minx=x-k*d
maxx=x+k*d
targetx=minx%(2*d)
if targetx>d:
    targetx-=2*d
if targetx==d:
    if x<0:
        targetx-=2*d

if minx>targetx:
    print(abs(minx))
    exit()
if maxx<targetx:
    print(abs(maxx))
    exit()
print(abs(targetx))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

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

else:
    times_low = math.floor(abs(X) / D)
    times_hig = times_low + 1
    
    
    if ((K_kai - times_low) % 2 == 0):
        ans = abs(abs(X) - times_low * abs(D))
            
    else:
        ans = abs(abs(X) - times_hig * abs(D))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
# s = input().rstrip()

def resolve():
    import sys
    input = sys.stdin.readline

    x, k, d = [int(x) for x in input().rstrip().split(" ")]

    if abs(k * d) < abs(x):
        if x > 0:
            print(abs(x - k*d))
            return
        else:
            print(abs(x + k*d))
            return

    # 0直前まで
    step = x//(d)
    x -= d*step
    k -= step

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)

n = x // d
q = x % d
p = q - d

if n < k:
    l = k - n

    if l % 2 == 0:
        print(q)

    else:
        print(abs(p))

else:
    e = x - k * d
    print(e)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
if x >= 0:
    if x - k * d >= 0:
        print(x - k * d)
    else:
        # 最初に負になってしまうところとその直前を巡回する
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ok + ng) // 2
            if x - mid * d <= 0:
                # いきすぎ
                ok = mid
            else:
                ng = mid
        # ok回目で負にたどり着く？
        rest = k - ok
        if rest % 2 == 0:
            # いまいるところに戻ってこれる
            print(abs(x - ok * d))
        else:
            # 一回正方向にいどうしたもの
            print(x - (ok - 1) * d)
else:
    if x + k * d <= 0:
        print(abs(x + k * d))
    else:
        # 最初に正になるところ
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ng + ok) // 2
            if x + mid * d >= 0:
                ok = mid
            else:
                ng = mid
        rest = k - ok
        if rest % 2 == 0:
            print(x + ok * d)
        else:
            print(abs(x + ok * d - d))

Submission status: Accepted

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

if x < 0:
    x = abs(x)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if x < 0:
    x *= -1

num = x//d

if num >= k:
    print(x-d*k)
    exit()
else:
    tmp = k-num
    if tmp % 2 == 0:
        print(x-d*num)
    else:
        print(d*(num+1)-x)

Submission status: Accepted

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

def solve():
    X, K, D = list(map(int, input().split()))
    X = abs(X)

    syo = X//D
    if syo >= K:
        ans = X - D*K
        print(ans)
        return

    # 負の方にも行くならば
    plus = X - D*syo  # 正側の答え
    minus = plus - D  # 負側の答え
    restK = K - syo - 1

    if restK%2 == 0:
        print(abs(minus))
        return
    print(plus)
    return


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    x, k, d = ns()
    x = abs(x)
    ans = INF
    base = 0

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


if __name__ == '__main__':
    main()