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 sys, math, bisect


if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )
    """
    X, K, D = map(int, input().strip().split() )


    if(X<0):
        X *= -1
    moved = X//D

    if(moved >= K):
        x_end = X-D*K
        print(x_end)
    else:
        x_0 = X-D*moved
        if(x_0 > D/2):
            moved += 1
            x_0 -= D
        move_remain = K-moved
        if(move_remain>0):
            if(move_remain%2!=0):
                if(x_0>=0):
                    x_0 -= D
                else:
                    x_0 += D

        print( abs(x_0) )

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

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

if x > 0:
    k -= cnt
    x -= d*cnt
    if abs(x - d) < abs(x) and k > 0:
        k -= 1
        x -= d
else:
    k -= cnt
    x += d*cnt
    if abs(x + d) < abs(x) and k > 0:
        k -= 1
        x += d

if k == 0:
    print(abs(x))
    sys.exit()

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

#maincode-------------------------------------------------
x,k,d = inm()
x = abs(x)
ans = x - k * d
if ans < 0:
    c = x//d
    k -= c
    k = k%2
    ans = x - d * c
    ans -=  d * k
print(abs(ans))

Submission status: Accepted

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

Xmin = X - D*K
Xmax = X + D*K

if Xmin * Xmax <= 0:
    ans = min(Xmax%(2*D), 2*D - (Xmax%(2*D)))
elif Xmin > 0:
    ans = Xmin
else:
    ans = -Xmax

print(ans)

Submission status: Accepted

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


x= abs(x)

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

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

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

x, k, d = rm()
t = abs(x) // d
if t > k:
    if x > 0:
        print(abs(x - k*d))
    else:
        print(abs(x + k*d))
    exit()
if x > 0:
    if (k - t) & 1:
        print(abs(x - t*d - d))
    else:
        print(abs(x - t*d))
else:
    if (k - t) & 1:
        print(abs(x + t*d + d))
    else:
        print(abs(x + t*d))

Submission status: Accepted

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

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

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

count = k - x // d

if count % 2 == 0:
  print(x - (d * (x//d)))
else:
  print(-(x - (d * (x//d))-d))

Submission status: Accepted

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

ans = 0
step = abs(X) // D
if step >= K:
    ans = abs(X) - D*K
else:
    cur = abs(X)%D
    rest = K - step
    # print(cur)
    # print(rest)
    if rest%2 != 0:
        ans = min(abs(cur+D), abs(cur-D))
    else:
        ans = cur

print(ans)

Submission status: Accepted

Solution code:

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

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

else:
    K2 = abs(X) // D
    ato = K-K2
    now = abs(X) - D*K2
    if ato % 2 ==0:
        out = now
    else:
        out = abs(now - D)

print(out)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

x_ = abs(x)

if x_ >= d * k:
    print(x_ - d*k)
    exit(0)

a = x_ % d
b = - (a - d)
k_ = x_ // d
l = k - k_

if l % 2 == 0:
    print(a)
else:
    print(b)

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

Submission status: Accepted

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

if x < 0:
    x = -x

p = (x + d - 1) // d

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

Submission status: Accepted

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

if x<=0:
  x*=(-1)
shou=x//d
amb=x%d
if k<=shou:
  print(x-k*d)
  exit()
else:
  if (k-shou)%2==0:
    print(amb)
    exit()
  else:
    print(d-amb)
    exit()

Submission status: Accepted

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

m=int(abs(X)/D)

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

print(abs(ans))

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
n = (-x+k*d)//(2*d)
cur = float('inf')
if n < 0:
    print(abs(x-k*d))
else:
    if n == 0:
        L = [0,1]
    else:
        if n >= k:
            L = [k-1,k]
        else:
            L = [n-1,n,n+1]
    for i in range(len(L)):
        cur = min(cur, abs(x+L[i]*d-(k-L[i])*d))
    print(cur)

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)
if k<x//d:
  print(x-d*k)
  exit(0)
k -= x//d
x %= d

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

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    x, k, d = MII()
    if k*d <= abs(x):
        if x >= 0:
            print(x-k*d)
        else:
            print(abs(x+k*d))
    else:
        m = x//d
        n = x%d
        if (k-m)%2==0:
            print(n)
        else:
            print(abs(n-d))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x, k, d = [int(x) for x in input().split()]
if x < 0: x = -x
jmps = x//d
if (jmps >= k):
    x = x - d*k
    print(x)
else:
    k -= (x//d)
    x = x - (x//d)*d

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

Submission status: Accepted

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

x, k, d = map(int, input().split(" "))
is_plus = x >= 0
x = abs(x)

r = x // d
if r > k:
    y = x - k*d
else:
    y1 = x - r*d
    y2 = x - (r+1)*d
    k2 = k-r
    if k2 % 2 == 0:
        y = y1
    else:
        y = y2

y = abs(y)
print(y)

Submission status: Accepted

Solution code:
def main():
    orig, count, dist = [int(x) for x in input().split()]
    orig = abs(orig)
    q, mod = divmod(orig, dist)
    if q > count:
        return orig - dist * count
    else:
        if (count - q) % 2 == 0:
            return mod
        else:
            return abs(mod - dist)


if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(X)
n = x // D
e = x % D

if n >= K:
    print(x - D * K)
elif (K - n) % 2 == 0:
    print(e)
else:
    print(abs(e - D))

Submission status: Accepted

Solution code:
X,K,D = (int(x) for x in input().split())
if X > 0:
    num = int(X/D)
    ans = 0
    if num >= K:
        ans = X - D*K
    else:
        a = X - num*D
        b = X - (num+1)*D
        if (K-num)%2 == 0:
            ans = a
        else:
            ans = b
else:
    num = int(-X/D)
    ans = 0
    if num >= K:
        ans = X + D*K
    else:
        a = X + num*D
        b = X + (num+1)*D
        if (K-num)%2 == 0:
            ans = a
        else:
            ans = b
print(abs(ans))

Submission status: Accepted

Solution code:

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

# def
def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return x

def str_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(input()))
    return x

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

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

def print_space(l):
    return print(" ".join([str(x) for x in l]))

# import
import numpy as np



# main code
X,K,D = int_map()

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

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:
    y = round((k-x/d)/2)
    print(abs(x-(k-2*y)*d))

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


def resolve():
    # s = input()
    # n = int(input())
    x, k, d = map(int, input().split())
    # l = [list(map(int, input().split())) for _ in range(n)]

    m = abs(x) // d

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

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


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__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

if k*d < abs(x):
    ans = abs(x)-k*d
    print(ans)
    exit()
x = abs(x)
n = x//d
ans = x-n*d
nn = k-n
if nn % 2 == 0:
    print(ans)
else:
    print(abs(ans-d))

Submission status: Accepted

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

t = x//d

ans = 0
if k <= t:
    ans = x-k*d
else:
    ans = x-d*t
    k -= t
    if k % 2 == 1:
        ans -= d

print(abs(ans))

Submission status: Accepted

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

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

a = abs(x) // d
b = abs(x) // d + 1

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

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

Submission status: Accepted

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

ans = None

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(abs(ans))

Submission status: Accepted

Solution code:
#import numpy as np
#import math
#from decimal import *
#from numba import njit

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

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

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

main()

Submission status: Accepted

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

minimum_try = min(abs(X) // D, K)
tmp_goal = abs(abs(X) - minimum_try*D)

if (K-minimum_try)%2==0:
  res1 = tmp_goal
else:
  res1 = abs(tmp_goal-D)

minimum_try = min((abs(X) // D) + 1, K)
tmp_goal = abs(abs(X) - minimum_try*D)

if (K-minimum_try)%2==0:
  res2 = tmp_goal
else:
  res2 = abs(tmp_goal-D)

print(min(res1, res2))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

minusFLG = 0
if X < 0:
    minusFLG = 1
    X = abs(X)

minX = X % D
minXmove = X // D

# 移動回数足りない
if minXmove > K:
    print(X - K * D)
    exit()

# 移動回数超える
zanMove = K - minXmove
if zanMove % 2 == 0:
    print(minX)
else:
    print(abs(minX - D))

Submission status: Accepted

Solution code:
ns = [int(ls) for ls in input().split()]

X = ns[0]
K = ns[1]
D = ns[2]

if X + K * D <= 0:
  print(abs(X + K * D))
elif X - K * D >= 0:
  print(X - K * D)
elif X + (K - 2) * D <= 0:
  print(min(abs(X + K * D),abs(X + (K - 2) * D)))
elif X - (K - 2) * D >= 0:
  print(min(abs(X - K * D),abs(X - (K - 2) * D)))
else:
  if K % 2 == 1:
    K -= 1
    X += D
  n = X // (D * 2)
  print(min([abs(ns) for ns in
             [X - (2 * a * D) for a in list(range(n-2,n+3))]]))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

def main():
    x,k,d = map(int,input().split())
    x = abs(x)
    req = int(math.ceil(x/d))-1
    if req >= k:
        print(x-k*d)
    else:
        x -= req*d
        k -= req
        x1 = x
        x2 = abs(x1-d)
        if x1 <= x2:
            if k%2 == 0:
                print(x1)
            else:
                print(x2)
        else:
            if k%2 == 1:
                print(x2)
            else:
                print(x1)


main()

Submission status: Accepted

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

bigger = 0
smaller = 0

if x >= 0:
    bigger = x - min(x // d, k) * d
    smaller = bigger - d
else:
    smaller = x + min((-x) // d, k) * d
    bigger = smaller + d

if abs(x) // d >= k:
    bigger = abs(bigger)
    smaller = abs(smaller)
    
    if x >= 0:
        print(bigger)
    else:
        print(smaller)
else:
    k -= abs(x) // d
    bigger = abs(bigger)
    smaller = abs(smaller)

    if x >= 0:
        if k % 2 == 0:
            print(bigger)
        else:
            print(smaller)
    else:
        if k % 2 == 0:
            print(smaller)
        else:
            print(bigger)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math

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

a = x-k*d
b = x+k*d

if(a >= 0):
	print(a)
elif(b <= 0):
	print(abs(b))
else:
	b = b % (d * 2)
	print(min(abs(b), abs(b - d * 2)))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

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

num = x // d

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

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

Submission status: Accepted

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

x = abs(x)

t = x // d

if t > k:
  ans = x - k * d
else:
  pos = x - t * d
  r = k - t
  if r % 2 == 0:
    ans = pos
  else:
    ans = abs(pos - d)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
if X>=0:
    temp=X//D
    if temp>=K:
        last=X-K*D
        print(last)
        exit()
    else:
        ktemp=K-round(temp)
        if ktemp%2==0:
            last=X-D*(round(temp))
            print(abs(last))
            exit()
        else:
            last=X-D*(round(temp))
            last=min(abs(last+D),abs(last-D))
            print(last)
            exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D=map(int,input().strip().split())
X=abs(X)
a=X//D
b=X%D

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

Submission status: Accepted

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

passSinaiCount = abs(x)//d

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

passSinaiDistance = abs(x) - passSinaiCount * d
passSuruDistance = d - passSinaiDistance

nokoriCount = k - passSinaiCount
if nokoriCount % 2 == 0:
    print(passSinaiDistance)
else:
    print(passSuruDistance)

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)
div,mod = divmod(x,d)

cnt = min(k,div)
k -= cnt
ans = x - cnt * d

if k > 0:
    ans -= (k%2) * d

print(abs(ans))

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

quotient = X // D
if quotient >= K:
    print(X - K * D)
else:
    X -= quotient * D
    K -= quotient
    
    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)

y=X//d

if k<=y:
  ans=X-k*d
  print(ans)
  
else:
  ans1=X-y*d
  ans2=abs(X-y*d-d)
  if (k-y)%2==0:
    print(ans1)
  else:
    print(ans2)

Submission status: Accepted

Solution code:

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

a=abs(X)//D

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

elif (K-a)%2==0:
    print(abs(X)%D)

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

Submission status: Accepted

Solution code:
# import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

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

if abs(D * K) > abs(X):
    is_over = True
else:
    is_over = False

if not (is_over):
    print(abs(X) - abs(D * K))
else:
    m = abs(X) // abs(D)
    r = K - m
    val1 = abs(X) % abs(D)
    val2 = val1 - abs(D)
    # if abs(val1) <= abs(val2):
    if r % 2 == 0:
        print(abs(val1))
    else:
        print(abs(val2))
    # else:
    #     if r % 2 == 0:
    #         print(val1)
    #     else:
    #         print(val2)

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

Solution code:
import copy
import math
import time
import statistics
import math
import itertools
import bisect
from decimal import *

# a = get_int()
def get_int():
    return int(input())
# a = get_string()
def get_string():
    return input()
# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]
# a_list = get_string_list():
def get_string_list():
    return input().split()
# a, b = get_int_multi()
def get_int_multi():
    return map(int, input().split())
# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))
# print("{} {}".format(a, b))
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''

def main():

    start = time.time()

    x, k, d = get_int_multi()

    wk = k * d

    if x - wk >= 0:
        ans = x - wk
    elif x + wk <= 0:
        ans = abs(x + wk)
    else:
        wk2 = x // d
        if (k - wk2) % 2 == 0:
            ans = abs(x % d)
        else:
            ans = abs(x % d - d)

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

x=abs(x)
idoukaisuu=x//d
nokorikyori=x%d

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

x,k,d=MI()
if x>0:
    c=(x+d-1)//d
    if k<c:
        ans=abs(x-d*k)
    else:
        if k&1==c&1:
            ans=abs(x-d*c)
        else:
            ans=abs(x-d*(c-1))
else:
    c=(-x+d-1)//d
    if k<c:
        ans=abs(x+d*k)
    else:
        if k&1==c&1:
            ans=abs(x+d*c)
        else:
            ans=abs(x+d*(c-1))
print(ans)

Submission status: Accepted

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

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):
    m = (-X + K*D) / (2*D)
    mc = min(max(0, math.ceil(m)), K)
    mf = min(max(0, math.floor(m)), K)
    ans = min(abs(X - K*D + 2 * mc * D), abs(X - K*D + 2 * mf * D))
    return ans


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

Submission status: Accepted

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

if __name__ == "__main__":
    main()