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())
X = abs(X)

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

Submission status: Accepted

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

if abs(X)>K*D:
    ans=abs(X)-K*D
elif (abs(X)//D)%2==K%2:
    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=abs(x-0)
if (x-(k*d))>0:
    print((x-(k*d)))
else:
    k1=0
    k1=x//d
    k-=k1
    a=(x-(k1*d))
    if k%2==0:
        print(a)
    else:
        print(abs(a-d))

Submission status: Accepted

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

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

print(abs(X))

Submission status: Accepted

Solution code:
def main():
    X,K,D = map(int,input().split())
    cnt = 0
    #k回以内でできるだけ0に同じ符号で近づける:s
    #そして同じ方向にもう一度だけ動かす:t
    if X == 0:
        s = 0
        t = s + D
        cnt += 1
        if cnt == K:
            return t
    else:
        X = abs(X)
        cnt += (X//D)
        if cnt >= K:
            ans = X - (K*D)
            return ans
        s = X - (cnt*D)
        t = s - D
        t = abs(t)
        cnt += 1
        if cnt == K:
            return t

    nokori = K - cnt
    if nokori % 2 == 0:
        return t
    else:
        return s

print(main())

Submission status: Accepted

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

X = abs(X)

n = X // D
x = X % D

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
input_line = input()
data = input_line.split(" ")
x = abs(int(data[0]))
k = int(data[1])
d = int(data[2])
a = x // d
b = x % d
res = abs(a)
if res >= k :
  ans = x - k * d
else :
  num = k - res
  if num % 2 == 1 :
    ans = abs(b - d)
  else :
    ans = b
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


def next_line():
    return input()


def next_int():
    return int(input())


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


def next_int_array_multi_lines(size):
    return [int(input()) for _ in range(size)]


def next_str_array(size):
    return [input() for _ in range(size)]


def main():
    x, k, d = map(int, input().split())
    f = False
    if x < 0:
        f = True
        x = -x
    min_to_eq = x // d
    res = 0
    if min_to_eq >= k:
        # 同じ方向に動き続けるだけ
        res = x - d * k
    else:
        k -= min_to_eq
        x = x - d * min_to_eq
        if k % 2 != 0:
            res = x - d
        else:
            res = x
    if f:
        res = -res
    print(abs(res))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if r<=0:
  print(-p)
elif 0<r<k:
  s=min(p-2*d*q,-p+2*d*(q+1))
  print(s)
else:
  print(-x-k*d)

Submission status: Accepted

Solution code:
X, K, D = (int(x) for x in input().split())
X = abs(X) #X<0のときでも正数から0に近づけるアルゴリズムにするため
L = X//D #0に近づけるだけの移動回数(A//Bは整数の商)
if K <= L:
    Min = X - (K*D)
else:
    Min = X - (L*D)
    temp = K - L #残りの移動回数
    if temp % 2 == 1:
        Min = abs(Min) - D
print(abs(Min))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
x = abs(x)
a = x // d
b = x % d
ans = 0
# 0を超えない
if k - a < 0:
  ans = x - k * d
else:
  if (k - a) % 2 == 0:
    ans = b
  else:
    ans = abs(b - d)

print(ans)

Submission status: Accepted

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

def y(l): return abs(x + l * d - (k - l) * d)

lf = min(max(0, (k - x / d) / 2), k)
l = int(lf)
if lf - l > 0 and l + 1 <= k:
    print(min(y(l), y(l + 1)))
else:
    print(y(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = [abs(int(i)) for i in input().rstrip().split()]
print (max(x-k*d,(x%d,d-x%d)[k-x//d&1]))

Submission status: Accepted

Solution code:
x,k,d = map(lambda i:abs(int(i)), input().rstrip().split())
print (max(x-k*d,(x%d,d-x%d)[k-x//d&1]))

Submission status: Accepted

Solution code:
x,k,d = map(abs,map(int, input().rstrip().split()))
print (max(x-k*d,(x%d,d-x%d)[k-x//d&1]))

Submission status: Accepted

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

Submission status: Accepted

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

a = X // D
b = X % D

remain_count = K - a

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

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

Submission status: Accepted

Solution code:
var = input()
x = int(var.split()[0])
k = int(var.split()[1])
d = int(var.split()[2])
x = abs(x)
cnt = x // d
if (k <= cnt) :
  print(x - d * k)
else :
  kk = k - cnt
  if (kk % 2 == 0) :
    print(x - d * cnt)
  else :
    print(d - (x - d * cnt))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A = list(map(int, input().split())) 

x = A[0]
k = A[1]
d = A[2]

first_k = abs(x) // d
if k < first_k:
  final_min = abs(x) - k * d
else:
  first_min = abs(x) % d
  second_min = abs(x) - d * (first_k + 1)

  if abs(first_min) < abs(second_min):
    first_k += 1
    final_min = second_min
  else:
    final_min = first_min
  remain_k = k - first_k
  if remain_k % 2 != 0:
    plus = final_min + d
    minus = final_min - d
    if abs(plus) > abs(minus):
      final_min = abs(minus)
    else:
      final_min = abs(plus)

print(abs(final_min))

Submission status: Accepted

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

a=X//D
b=X%D

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

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase


def main():
    x,k,d = map(int,input().split())
    
    t = abs(x)//d
    
    if t<=k and x<0:
        x+=t*d
        k-=t
    elif t<=k and x>0:
        x-=t*d
        k-=t
    elif t>k and x<0:
        x+=k*d
        k-=k
    elif t>k and x>0:
        x-=k*d
        k-=k
    
    while k:
        if x<0:
            if x+d<0:
                x+=d
            elif abs(x+d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x+d))
                break
            elif abs(x+d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 != 0:
                print(abs(x+d))
                break
        else:
            if x-d>0:
                x-=d
            elif abs(x-d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x-d))
                break
            elif abs(x-d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 != 0:
                print(abs(x-d))
                break
        k-=1
    else:
        print(abs(x))
            
        
    


BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

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

# endregion



import collections
if __name__=='__main__':
    main()
##    
##

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase


def main():
    x,k,d = map(int,input().split())
    
    t = abs(x)//d
    
    if t<=k and x<0:
        x+=t*d
        k-=t
    elif t<=k and x>0:
        x-=t*d
        k-=t
    elif t>k and x<0:
        x+=k*d
        k-=k
    elif t>k and x>0:
        x-=k*d
        k-=k
    
    while k:
        if x<0:
            if x+d<0:
                x+=d
            elif abs(x+d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x+d))
                break
            elif abs(x+d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 != 0:
                print(abs(x+d))
                break
        else:
            if x-d>0:
                x-=d
            elif abs(x-d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x-d))
                break
            elif abs(x-d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 != 0:
                print(abs(x-d))
                break
        k-=1
    else:
        print(abs(x))
            
        
    

# region fastio
##
##BUFSIZE = 8192
##
##
##class FastIO(IOBase):
##    newlines = 0
##
##    def __init__(self, file):
##        self._fd = file.fileno()
##        self.buffer = BytesIO()
##        self.writable = "x" in file.mode or "r" not in file.mode
##        self.write = self.buffer.write if self.writable else None
##
##    def read(self):
##        while True:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            if not b:
##                break
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines = 0
##        return self.buffer.read()
##
##    def readline(self):
##        while self.newlines == 0:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            self.newlines = b.count(b"\n") + (not b)
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines -= 1
##        return self.buffer.readline()
##
##    def flush(self):
##        if self.writable:
##            os.write(self._fd, self.buffer.getvalue())
##            self.buffer.truncate(0), self.buffer.seek(0)
##
##
##class IOWrapper(IOBase):
##    def __init__(self, file):
##        self.buffer = FastIO(file)
##        self.flush = self.buffer.flush
##        self.writable = self.buffer.writable
##        self.write = lambda s: self.buffer.write(s.encode("ascii"))
##        self.read = lambda: self.buffer.read().decode("ascii")
##        self.readline = lambda: self.buffer.readline().decode("ascii")
##
##
##sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
##input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion



import collections
if __name__=='__main__':
    main()
##    
##

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
k = abs(X)//D
dk = K-k
dd = min(abs(X-k*D), abs(X+k*D))

if dk>=0:
    if dk%2:
        print(min(abs(dd-D), abs(dd+D)))
    else:
        print(abs(dd))
else:
    print(min(abs(X-K*D), abs(X+K*D)))

Submission status: Accepted

Solution code:
# import itertools
# import math
import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
X, K, D = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

X_abs = abs(X)

if X_abs > K * D:
    print(X_abs - (K * D))
    sys.exit()
else:
    j = X_abs // D
    i = X_abs - (j * D)
    if (K - j) % 2 == 0:
        print(i)
    else:
        print(abs(i - D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

x, k, d = rm()
c = abs(x)//d
if k<c:
    print(abs(x)-d*k)
else:
    e=abs(x)-d*c
    if c%2==k%2:
        print(e)
    else:
        print(d-e)

Submission status: Accepted

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

X = abs(X)

Y = min(K, X // D)
K -= Y
X -= Y * D

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

print(ans)

Submission status: Accepted

Solution code:
def c_walking_takahashi():
    X, K, D = [int(i) for i in input().split()]
    x = abs(X)
    need_move, r = divmod(x, D)  # 位置 r に到達するには need_move 回の移動が必要
    if need_move >= K:
        return x - K * D  # r には到達できない。動ける限り動いた結果がこれ
    can_move = K - need_move
    # 動ける回数が偶数回だと位置 r で、奇数回だと abs(r-D)=D-r で終了する
    if can_move % 2 == 0:
        return r
    return D - r

print(c_walking_takahashi())

Submission status: Accepted

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

a = [abs(x // d), x % d]

if a[0] > k:
   ans = min(abs(x+k*d) ,abs(x-k*d))
else:
    b = (k- a[0]) % 2
    ans = min(abs(a[1]+b*d) ,abs(a[1]-b*d))

print(ans)

Submission status: Accepted

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

print(abs(X))

Submission status: Accepted

Solution code:
import math
x,k,d=map(int,input().split())
count=int(math.sqrt(x*x)/d)
if k<=count:
    count=k
    k=2
else:
    k=k-count
ans=int(math.sqrt(x*x)-d*count)
if k%2==1:
    ans=math.sqrt((d-ans)**2)
print(int(ans))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
a,b,c = map(int, input().rstrip().split())
a = abs(a)

if a > b * c:
    print(abs(a - b*c))
else:
    n = a // c
    surplus = a % c
    m = b - n
    if m % 2 == 0:
        print(surplus)
    else:
        print(abs(min(surplus + c, surplus - c)))

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())
X = abs(X)
a = 0

a = X//D
if a <= K:
    K -= a
else:
    a = K
    K = 0

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

Submission status: Accepted

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

straight = min(K, X // D)
K -= straight
X -= straight * D

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

Submission status: Accepted

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

ans=1e+15

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

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

left=X-K*D
right=X+K*D

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

elif right<=0:
	print(-right)
else:
	print(left)

Submission status: Accepted

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

#0をまたげないとき
if abs(X)-K*D >= 0:
    print(abs(X) - K*D)
else:#0をまたげるとき
    if X > 0:
        t = K - (X // D)
        X = X % D
    else:
        t = K - (-X // D) -1
        X = -(-X % D) + D
    
    if t % 2 == 0:
        print(X)
    else:
        print(abs(X - D))

Submission status: Accepted

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


       

print(abs(m))

Submission status: Accepted

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


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

Submission status: Accepted

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

Submission status: Accepted

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

if abs(X) < (K * D):
    a = abs(X) % D
    b = abs(D - a)
    c = abs(X) // D + 1
    if (K - c) % 2 == 0:
        goal = b
    else:
        goal = a
else:
    goal = abs(X) - ( D * K )

print(goal)

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*(x//d))
    else:
 
        print(abs(d-(x-d*(x//d))))

Submission status: Accepted

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

        print(abs(d-(x-d*(x//d))))

Submission status: Accepted

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

x = abs(x)
m = min(k, x // d)
k -= m
x -= m * d

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

Submission status: Accepted

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

if (x >= 0) :
  x -= d * hi
else :
  x += d * hi

k -= hi;

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

Submission status: Accepted

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

if (x >= 0) :
  x -= d * hi
else :
  x += d * hi

k -= hi;

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

Submission status: Accepted

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

if X >= D*K:
    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:
import math

def solve(x,k,d):
	x = abs(x)
	if x >= k*d:
		return x-k*d
	else:
		n = x//d
		x -= n*d
		k -= n
		if k%2==0: return x
		else: return abs(x-d)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x=abs(x)

move=min(k, x//d)

k-=move
x-=move*d

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

Submission status: Accepted

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

if X < 0:
    X = -X

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

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

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

x=x if x>0 else -x

if k*d<x:
    print(x-k*d)
else:
    rma=(k*d-x+2*d-1)//(2*d)
    rmi=(k*d-x)//(2*d)

    print(min(abs(x-(k-2*rmi)*d),abs(x-(k-2*rma)*d)))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    X, K, D = [int(x) for x in input().strip().split()]
    ans = 0
    X = abs(X)
    if X >= K * D:
        print(abs(X-K*D))
    else:
        X, K = X - D * (X // D), K - X // D
        if K % 2:
            print(abs(X-D))
        else:
            print(X)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 間違いコード
x,k,d = map(int,input().split())
ans = 0

if x>0:
  if x-k*d > 0:
    ans = x-k*d
  else:
    if (k-(x//d))%2 == 0: #右
      ans = x-(x//d)*d    
    else:
      ans = abs(x-(x//d+1)*d)  
else:
  if x<0 and x+k*d < 0:
    ans = abs(x+k*d)
  else:
    if (k-(-x//d))%2 == 0: #左
      ans = abs(x + (-x//d)*d )
    else:
      ans = abs(x +(-x//d+1)*d )
print(ans)

Submission status: Accepted

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

x = abs(x)

if x-k*d > 0:
  ans = x-k*d
else:
  if (k-(x//d))%2 == 0: #右
    ans = x-(x//d)*d    
  else:
    ans = abs(x-(x//d+1)*d)  

print(ans)

Submission status: Accepted

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

#x zahyo   k k kaisu  d hohaba

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

Submission status: Accepted

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

x=abs(x)

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

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

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