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:
# abc175_c.py
# https://atcoder.jp/contests/abc175

global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    # X = int(lines[0])
    X, K, D = list(map(int, lines[0].split()))
    # values = list(map(int, lines[0].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(N):
    #     valueses.append(list(map(int, lines[i+1].split())))

    # 全部プラス
    all_plus = X + D * K
    # 全部マイナス
    all_minus = X - D * K

    # 全部プラスと全部マイナスが同じ符号側だったら最小値をReturn (0を含む)
    if all_plus * all_minus >= 0:
        result = min(abs(all_plus), abs(all_minus))
        return [result]

    # D*2で除算 (X = q * (D*2) + mod)
    q, mod = divmod(X, D*2)

    # ※偶数回か奇数回で異なる

    # 偶数
    if K % 2 == 0:
        values = list()
        values.append(abs(mod - D*2))
        values.append(abs(mod))
        values.append(abs(mod + D*2))
        result = min(values)

    # 奇数
    else:
        values = list()
        values.append(abs(mod - D*3))
        values.append(abs(mod - D*1))
        values.append(abs(mod + D*1))
        values.append(abs(mod + D*3))
        result = min(values)

    return [result]


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['6 2 4']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['7 4 3']
        lines_export = [1]
    if pattern == 3:
        lines_input = ['10 1 2']
        lines_export = [8]
    if pattern == 4:
        lines_input = ['1000000000000000 1000000000000000 1000000000000000']
        lines_export = [1000000000000000]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Accepted

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

if abs(x)//d < k:
  #最小までいく。
  if (k-abs(x)//d)%2==0:
    if x>=0:
      print(abs(x-(x//d)*d))
    else:
      print(abs(x+(abs(x)//d)*d))
  else:
    if x>=0:
      print(abs(x-(x//d)*d-d))
    else:
      print(abs(x+(abs(x)//d)*d+d))
else:
  if x>=0:
    print(abs(x-k*d))
  else:
    print(abs(x+k*d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

now = abs(X)
move_count = K
move_size = D

moved_count, surplus = divmod(now, D)

if move_count - moved_count < 0:
    now -= move_count * move_size
    move_count = 0
else:
    move_count -= moved_count
    now = surplus

move_count %= 2
if move_count == 0:
    print(abs(now))
    exit()

if now <= 0:
    now += move_size
elif now > 0:
    now -= move_size
print(abs(now))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import itertools

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

res = 0
saiteki_kaisu = min(abs((x // d)), k)
saigoni_hituyo = (k - saiteki_kaisu) % 2
if x > 0:
    res = x - (saiteki_kaisu * d)
else:
    res = x + (saiteki_kaisu * d)

if res > 0:
    res -= saigoni_hituyo * d
else:
    res += saigoni_hituyo * d

print(abs(res))

Submission status: Accepted

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

first_step = abs(x // d)
rem_k = k - first_step

now = x
if k < first_step:
    if x < 0:
        now = x + (d * k)
    else:
        now = x - (d * k)
else:
    if x < 0:
        now = x + (d * first_step)
        if rem_k % 2 == 1:
            if now < 0:
                now += d
            else:
                now -= d
    else:
        now = x - (d * first_step)
        if rem_k % 2 == 1:
            if now < 0:
                now += d
            else:
                now -= d

print(abs(now))

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if x==0:
    if k%2==0:
        ans=0
    else:
        ans=d
else:
    if x<0:
        x=-x

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


print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# by the authority of GOD     author: manhar singh sachdev #

import os,sys
from io import BytesIO, IOBase

def main():
    x,k,d = map(int,input().split())
    if x < 0:
        y = x+k*d
    else:
        y = x-k*d
    if x==0:
        if k%2:
            print(d)
        else:
            print(0)
    elif y*x >= 0:
        print(abs(y))
    else:
        z = abs(x)%d
        z1 = d-abs(x)%d
        if ((abs(x)-z)//d)%2 == k%2:
            print(z)
        else:
            print(z1)

#Fast IO Region
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")

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if abs(x)-k*d >= 0: # 0の反対側へ行けない
    ans = abs(x)-k*d
else:
    c = abs(x)//d#0を超えないときの移動回数
    ans1 = abs(x) - c*d
    ans2 = abs(x) - (c+1)*d
    if (k-c)%2 == 0:
        ans = abs(ans1)
    else:
        ans = abs(ans2)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

X, K, D = LI()

"""
X 
K回数 D移動
"""

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

else:
    ans = tmp

print(ans)

Submission status: Accepted

Solution code:
# = int(input())
# = str(input())
X, K, D = map(int, input().split())
# = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]

if X == 0:
    ans = [D, 0]

elif X>0:
    if X == D:
        ans = [0, D]
    elif X-D<0:
        ans = [D-X, X]
    else:
        if X-D*K>=0:
            print(X-D*K)
            exit()
        else:
            plus = X%D
            minus = D-X%D
            if (X//D)%2 == 1:
                ans = [plus, minus]
            else:
                ans = [minus, plus]

else:
    T = -1*X
    if T == D:
        ans = [0, T]
    elif T-D<0:
        ans = [D-T, T]
    else:
        if T-D*K>=0:
            print(T-D*K)
            exit()
        else:
            plus = T%D
            minus = D-T%D
            if (T//D)%2 == 1:
                ans = [plus, minus]
            else:
                ans = [minus, plus]

if K%2 == 1:
    print(ans[0])
else:
    print(ans[1])

Submission status: Accepted

Solution code:
def solve():
    x, k, d = map(int, input().split())
    x = abs(x)
    ref, a = divmod(x, d)
    if k < ref:
        print(x - k * d)
    elif ref % 2 == k % 2:
        print(a)
    else:
        print(d - a)

solve()

Submission status: Accepted

Solution code:
import math

x,k,d=map(int, input().split())
if abs(x) > k*d:
    Ans = abs(x)-k*d
    print(Ans)
else:
    move = math.floor(abs(x) / d)
    oufuku = k - move
    if oufuku % 2 == 0:
        Ans =abs(abs(x)-d*move)
    else:
        Ans=abs(abs(x)-d*(move+1))
    print(Ans)

Submission status: Accepted

Solution code:
# 現在地x　k回d移動
from decimal import Decimal
x, k, d = map(Decimal, input().split())

def solve(x, k, d):
    if x < 0:
        x = -x

    if x > k*d:
        return x - k*d

    x, k = x - d*(x//d), k - x//d
    if k%2 == 0:
        return x
    else:
        return d - x

print(solve(x, k, d))

Submission status: Accepted

Solution code:
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))

def main():
    x, k, d = ril()
    a = 2 * d
    b = - x + k * d
    f = lambda i : a * i - b
    if b <= 0:
        ans = -b
    elif a * k < b:
        ans = -f(k)

    elif b % a == 0:
        ans = 0
    else:
        x1 = b // a
        x2 = x1 + 1
        ans = min(-f(x1), f(x2))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    x,k,d = map(int, input().split())
    x = abs(x)
    n = x//d
    ans = x
    if n >= k:
        print(x-k*d)
        exit()

    x -= n*d
    k -= n
    ans = x
    ans_2 = abs(ans - d)
    if k%2 ==0:
        print(ans)
    else:
        print(ans_2)

if __name__=="__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

a = abs(x) // d
b = abs(x) % d

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

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

Submission status: Accepted

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

Submission status: Accepted

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

s = K * D
if X < -s:
  b = X + s
elif X > s:
  b = X - s
else:
  a = X // D
  b = X % D
  if (a - K) % 2 == 1:
    b = b - D
print(abs(b))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
X,K,D=map(int, sys.stdin.readline().split())
X=abs(X)
a,b=X/D,X%D
if a<K:
    K-=a
    if K%2==0:
        print b
    else:
        print abs(b-D)
else:
    print X-D*K

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

first_step = min(X // D, K)

X -= first_step * D
K -= first_step

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

Submission status: Accepted

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

Submission status: Accepted

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

X,K,D=(int(x) for x in input().rstrip('\n').split())
if X>0 and X-K*D>=0:
  print(X-K*D)
elif X<0 and X+K*D<=0:
  print(-X-K*D)
else:
  if X>0:
    x = X%D
    y=X//D
    if (K-y)%2==0:
      print(x)
    else:
      print(D-x)
  elif X<0:
    a = (-1)*X
    x = a%D
    y = a//D
    if (K-y)%2==0:
      print(x)
    else:
      print(D-x)
  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)

k = X // D
if abs(X - k * D) <= abs(X - (k + 1) * D):
    best_k = k
else:
    best_k = k + 1

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

Submission status: Accepted

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

if abs(X)//D > K:
    if X < 0:
        X += D*K
    if X > 0:
        X -= D*K

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

print(abs(X))

Submission status: Accepted

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


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

Submission status: Accepted

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

Submission status: Accepted

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

# In[23]:


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


# In[25]:


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


# In[ ]:

Submission status: Accepted

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

Submission status: Accepted

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

if x < 0:
    x = -x

to_zero = x//d

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

Submission status: Accepted

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

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

if k >= y:
    dx = dx - y * d
    if (k-y) % 2 == 0:
        print(dx)
    else:
        dx = abs(dx - d)
        print(dx)
elif k < y:
    dx = dx - k * d
    print(dx)

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
n1=max((K-X/D)//2,0)
n2=min(n1+1,K//2)
n3=max(n1-1,0)
d1=abs(X-D*(K-2*n1))
d2=abs(X-D*(K-2*n2))
d3=abs(X-D*(K-2*n3))
print(int(min(d1,d2,d3)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

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

if new_K % 2 == 0:
    print(new_X)
else:
    print(abs(new_X-D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#c
x, k, d=map(int, input().split())
rest = k - (abs(x) // abs(d))

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

else:
    if x >= 0:
        near = x- d*(abs(x) // abs(d))
    else:
        near = x+ d*(abs(x) // abs(d))

    if rest % 2 == 0:
        print(abs(near))
    else:
        if near >= 0:
            print(abs(near - d))
        else:
            print(abs(near + d))

Submission status: Accepted

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


if x<0:
  x=(-1)*x
if d<0:
  d=(-1)*d
  
s=math.floor(x/d)

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

Submission status: Accepted

Solution code:
X,K,D = (int(x) for x in input().split())
X=abs(X)
if X>K*D:
    print(X-K*D)
else:
    s=X//D
    X-=s*D
    K-=s
    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:
  nk=k-x//d
  x%=d
  if nk%2:
    print(abs(x-d))
  else:
    print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
X, K, D = map(int, input().split())
X = abs(X)
# ずっとマイナスにいく
if X-K*D >= 0:
    print(X-K*D)
    sys.exit()

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

Submission status: Accepted

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

Submission status: Accepted

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

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

        if(times%2 == K%2):
            print(abs(min_l))
        else:
            print(abs(min_r))
    else:
        print(abs(X+D*K))

Submission status: Accepted

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

Submission status: Accepted

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

if x > k*d or -x > k*d:
    print(abs(abs(x) -k*d))
else:
    num = abs(x) // d
    rest = k - num
    if rest%2 == 1:
        print(abs(abs(x)%d -d))
    else:
        print(abs(abs(x)%d))

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
first = 0
if X < 0:
    first = -1
elif X > 0:
    first = 1
ans = 0
X = abs(X)
judge = 0 # 奇数なら1偶数なら0
if first == 0:
    if D % 2 == 1:
        ans = D
    else:
        ans = 0
else:
    if K < X // D:
        ans = X - (K * D)
    else:
        judge = (X // D) % 2
        if judge == 0:
            if K % 2 == 0:
                ans = X % D
            else:
                ans = X % D - D
        else:
            if K % 2 == 0:
                ans = X % D - D
            else:
                ans = X % D
print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

q, mod = divmod(X, D)

if q < K:
    # print("if")
    X = X - (q * D)
    if (K - q) % 2 == 0:
        ans = X
    else:
        ans = X - D
else:
    # print("else")
    ans = X - (K * D)
print(abs(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)
        exit()
    
    else:
        print(d)
        exit()

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



xxx=x//d

yyy=x%d

zzz=k-xxx

if zzz%2==0:
  print(yyy)

else:
    print(abs(yyy-d))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

x = abs(x)

if x - (k * d) >= 0:
    print(x - (k * d))
    sys.exit(0)
else:
    remain = k - (x // d)
    p_min = x % d
    n_min = abs(p_min -d)
    if remain % 2 == 0:
        print(p_min)
    else:
        print(n_min)

Submission status: Accepted

Solution code:
"""MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWO:,'..'xXWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .:kOo.,kNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .lNWWo..lXMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MNOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMMMMWKxoloxKWNc  .:xxd;  ,dxxxkXMXkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMM
MKc''''''''''''''''.  ..'''''.   .''''.  .''MMMWx..,:. .xXo''.  .''.  ..'';kMk;'''''.  .'.''.  .''''''.  ..''.   .''MM
MWNXNNNNNNNNNNNNNNNO' ;KNNNNNKo. cKNNNd. lXNMMMK; ,0Wx. :NWXXl .dNNO' ;KNNNWMWNNNNNXc .kNNNNx. cXNNNNNO' ;KNN0; 'ONNMM
MMMMMMMMMMMMMMMMMWM0' :NMMMMWMX: .OMWWx. oWMMMMWx. ';. .dNMWWo .xMM0' :NWWMMMMMMMMWWl .OMMMWk. lWWMMWM0' ;XMMX: ,KMMMM
MMMMMMWMWkccccccccc;. :NMWNNNKd. :XMMWx. oWMMMWKo. .',. .,ldx; .dMM0' :NMMMMMMMNNWMWl .OMMMWk. lWMMMMM0' ;XMMX: ,0MMMM
MMWXKNWMWxcc,. .;cc;. :XMk,''..'lKWMMWx. oWMNx:. .lONNKxl.     .xMM0' :NMMMMMM0:,oKO' ,KMMMMk. lWWNkoc,. .;::;. ,KMMMM
MMXc.lXMMWWWNd..xWM0' :NM0;  ;kXWMMMWNl  oWMK:.;xXWWMWXx:. ,c' .dMM0' :NMMMMMMKc. ...;kWMMMMk. lWWc .:;. .clc:. ,KMMMM
MWWx. lXMWMMMK, lWM0' :NMMNx'.,ldxxoc'   oWMWNKNWWWN0o' .:kNWo .dMM0' :NMMMMMMMNO;  ,OWWWMMMk. lWM' lXx. lNMMX: ,KMMMM
MMMWk'.,xKNX0c..kWM0' :NMMMMNkl;,'',:l:  oWMMMMMWMWO' 'l0WMWWo .xMM0' :NMMMMMMMMMNd. .oXWMMMk. lWMd.....;0WMMX: ,KMMMM
MMMMWKo'...'..:kWMM0' :NMMMMMMMWWNNWWWx. oWMMMMMMMMWOxXWMWMWWo .xMM0' :NMMMMMMMMMMMKl. ,kNMMk. lWMWKkxx0NMWMMX: ,KMMMM
MMMMMMMN0kxk0XNWMMWKl;xNMMMMMMMMMMMMWM0c;kWMMMMMMMMMMMWWMMMMWk;:OMMXl,dNMMMMMMMMMMMMWO; .dNM0c;kWWMMMMMMMMMMMNd,oXMMMM
MMMMMMMMWWMWMMMMMMMMMWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWWMMWWMMWMMMMMMMMMMMMMMNkxKWMMWWMMMMMMMMMMMMMMWMMWMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"""


#				 Author: Udit "luctivud" Gupta @ https://www.linkedin.com/in/udit-gupta-1b7863135/					 #


import math;   		from collections import *
import sys;   		from functools import reduce
import time;   		from itertools import groupby

# sys.setrecursionlimit(10**6)

def input()         : return sys.stdin.readline()
def get_ints()      : return map(int, input().strip().split())
def get_list()      : return list(get_ints())
def get_string()    : return list(input().strip().split())
def printxsp(*args) : return print(*args, end="")
def printsp(*args)  : return print(*args, end=" ")


DIRECTIONS = [(+0, +1), (+0, -1), (+1, +0), (+1, -1)] 
NEIGHBOURS = [(-1, -1), (-1, +0), (-1, +1), (+0, -1),\
              (+1, +1), (+1, +0), (+1, -1), (+0, +1)]



# MAIN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# for _test_ in range(int(input())): 

x, k, d = get_ints()
x = abs(x)
el_facto = min(k, (x + d-1) // d)
moves = []
if k & 1:
	if el_facto & 1:
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
	else:
		el_facto -= 1
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
else:
	if not (el_facto & 1):
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
	else:
		el_facto -= 1
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
ans = float('inf')
x = abs(x)
# print(moves)
for i in moves:
	ans = min(ans, abs(x - i * d))
print(ans)