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)
kk = k - int(x/d)
if kk <= 0:
    print(x - k * d)
    exit()

a = x - int(x/d)*d
if kk % 2 == 0:
    ans = a
else:
    ans = abs(a - d)

print(ans)

Submission status: Accepted

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

x = abs(x)

n = x // d

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

kk = k - n

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

k-=x//d
x%=d

if k&1:
  print(-x+d)
else:
  print(x)

Submission status: Accepted

Solution code:
def C():
    X, K, D = map(int, input().split())
    X = abs(X)
    if X//D >= K:
        return X - K*D

    pos1 = abs(X - (X//D)*D)
    pos2 = abs(X - (X//D+1)*D)

    if pos1 > pos2:
        K -= X//D+1
    else:
        K -= X//D
    
    if K % 2 == 0:
        return min(pos1, pos2)
    else:
        return abs(min(pos1, pos2) - D)

print(C())

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
r=k-x//d #minについた後何回余るか
m=x%d #min

if x>=k*d:
  print(x-k*d)
elif r%2:
  print(d-m)
else:
  print(m)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
x = abs(x)
m = min(x//d,k)
x -= d*m
k -= m

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

Submission status: Accepted

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

Abs_X = abs(X)
step  = Abs_X // D

if step < K:
    if (K - step) % 2 == 0:
        answer = min(abs(X + step*D), abs(X - step*D))
    else:
        answer = min(abs(X + (step+1)*D), abs(X - (step+1)*D))
else:
    answer = min(abs(X + D*K), abs(X - D*K))

print(answer)

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

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
zettaitix = abs(X)
if zettaitix >= K*D:
    kotae = abs(zettaitix-(K*D))
elif zettaitix < K*D:
    kai, mod = divmod(zettaitix, D)
    ds = kai*D
    if (K-kai)%2==0:
        kotae = abs(zettaitix-ds)
    elif (K-kai)%2!=0:
        kotae = abs(zettaitix-ds-D)
print(kotae)

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
kai = 0
xy = abs(X)
if xy >= K*D:
    kotae = abs(xy-(K*D))
if xy < K*D:
    kai, mod = divmod(xy, D)
    ds = kai*D
    if (K-kai)%2==0:
        kotae = abs(xy-ds)
    if (K-kai)%2!=0:
        kotae = abs(xy-ds-D)
print(kotae)

Submission status: Accepted

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

x = abs(x)

# 0をまたがないなら0の方向にk回移動下座標が答え
if x - k * d > 0:
    print(x - k * d)
    exit()

# 0をまたぐ瞬間の移動回数
step_num = x // d + 1

remain_steps = k - step_num

if remain_steps % 2 == 0:
    ans = abs(x - step_num * d)
else:
    ans = x - (step_num - 1) * d

print(ans)

Submission status: Accepted

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

p = x // d
if p > k:
  print(x - k * d)
else:
  min = [x, x]
  min[p%2] = x - p * d
  min[(p+1)%2] = abs(min[p%2] - d)
  print(min[k%2])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
time = x // d
if abs(time) > k:
    print(min(abs(x - d * k), abs(x + d * k)))
    exit()
k -= time
x -= d * time
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())
x = abs(x)
s = x//d
if s>k:
    print(x-d*k)
elif s<=k:
    if (k-s)%2==0:
        print(x-d*s)
    elif (k-s)%2==1:
        print(abs(x-d*(s+1)))

Submission status: Accepted

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

max_x = X + K * D
min_x = X - K * D

res = 0

if max_x <= 0:
  res = abs(max_x)
  print(res)
  exit()
elif min_x >= 0:
  res = abs(min_x)
  print(res)
  exit()

a = (K*D -X) / (2*D)

max_x = 2 * math.ceil(a) * D + min_x
min_x = 2 * math.floor(a) * D + min_x
res = min(abs(max_x), abs(min_x))
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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


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

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

    # See:
    # https://www.youtube.com/watch?v=auQRcs5JMwE&feature=youtu.be
    # KeyInsight:
    # △: 対称性から、x>=0の場合を考えれば良い。実装も楽になる。

    if kd <= x:
        # ◯: 原点付近で往復する前に、シミュレーションが終わる。
        print(x - kd)
    else:
        # ◯: 原点付近で往復する
        count = x // d
        k_dash = k - count  # 残りの移動回数
        x -= count * d

        # △: 移動回数の残りが偶奇で変わる
        if k_dash % 2 == 0:
            print(x)
        else:
            print(d - x)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
inp=list(map(int,input().split()))
x=inp[0]
k=inp[1]
d=inp[2]
if x<0:
    flag=1
else:
    flag=0
if x==0:
    if k%2==0:
        print (0)
    else:
        print (d)
else:
    ini=x
    x=abs(x)
    a=x%d
    z=(x-a)//d
    if z>=k:
        if flag==1:
            final=ini+k*d
        else:
            final=ini-k*d
        print (abs(final))
    else:
        if flag==1:
            a=-1*a
        k-=z
        final=a
        if k%2==0:
            print (abs(final))
        else:
            if final<0:
                final+=d
                print (abs(final))
            else:
                final-=d
                print (abs(final))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
count = abs(X) // D
distance = abs(X) % D
if K <= count:
  print(abs(X) - (K * D))
else:
  remain = K - count
  if remain % 2 == 1:
    # if odd, move from current position
    print(abs(distance - D))
  else:
    # even, thus stays at current position
    print(distance)

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)
time = X//D
rest = K - time

if rest <= 0:
    print(X-K*D)
elif rest%2 == 0:
    print(min(X-time*D, abs(X-(time+2)*D)))
else:
    print(min(X-(time-1)*D, abs(X-(time+1)*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:
    l=x%d
    s=x//d
    rem=k-s
    if rem&1: print(d-l)
    else: print(l)

Submission status: Accepted

Solution code:
x, k, d = input().split(' ')
x = int(x)
k = int(k)
d = int(d)
x = x - k * d
ans = abs(x)
low = 0
high = k
d = d * 2
while low <= high:
	mid = (low + high) // 2
	pos = x + mid * d
	ans = min(ans, abs(pos))
	if pos == 0:
		break
	elif pos > 0:
		high = mid - 1
	else:
		low = mid + 1
print(ans)

Submission status: Accepted

Solution code:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-08-16 22:37
# @url:https://atcoder.jp/contests/abc175/tasks/abc175_c

import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio

BUFSIZE = 8192

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")
# ------------------------------
def main():
    x,k,d=map(int,input().split())
    x=abs(x)
    n=x//d
    if k<=n:
        print (abs(x-k*d))
    else:
        l,r=abs(x-(n+1)*d),abs(x-n*d)
        if k%2==n%2:
            print (r)
        else:
            print (l)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

m_n = p_n - d
ans = ""
if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = k
else:
    if k <= p_n_num:
        ans = x - (k*d)
    else:
        if (p_n_num - k) % 2 == 0:
            ans = p_n
        else:
            ans = m_n
print(abs(ans))

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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


def solve(X, K, D):
    X = abs(X)

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


print(solve(X, K, D))

Submission status: Accepted

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

    X = abs(X)

    r, m = divmod(X, D)

    if r <= K:
        mi = X - D*r
        K -= r
    else:
        mi = X - D*K
        K = 0
    
    K = K % 2

    if K == 0:
        print(mi)
    else:
        print(min(abs(mi-D), abs(mi+D)))

if __name__ == "__main__":
    main2()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cur = abs(x)#座標は正に
rem = k#残り移動回数

cnt = min(cur//d,rem)#0に向かって移動する回数
cur -= d*cnt #0にできる限り近づいた値
rem -= cnt

if rem > 0 :#残りがまだ残っている場合
    if rem % 2 ==1:#奇数なら1回負方向に移動するのが最小、偶数の場合は行ったり来たり往復なので同じ位置が最小
        cur -= d
ans = abs(cur)
print(ans)

Submission status: Accepted

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


def Repetition(X, K, D):
    if K%2 == 0:
        return X
    else:
        return abs(abs(X) - D)

X = abs(X)
if X < D:
    print(Repetition(X, K, D))
    exit()

n = X//D
if n >= K:
    print(X - D*K)
else:
    X = X - D*n
    K = K - n
    print(Repetition(X, K, D))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

x = abs(x)
kk = x // d

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

k -= kk

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    X = abs(X)
    q, r = divmod(X, D)
    if K < q:
        ans = X - D * K
    else:
        if (K - q) % 2 == 0:
            ans = r
        else:
            ans = D - r

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    X, K, D = readnums()
    if X <= 0:
        X = -X
    if X // D >= K:
        print(X - K * D)
    else:
        a1 = X % D
        a2 = D - X % D
        if (K - X // D) % 2:
            print(a2)
        else:
            print(a1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def calc(M):
	return ( -1 * D * M ) + ( D * ( K - M ) )


MAX_X = 1e15
L = 0
R = K
curMin = MAX_X + 1
sumTmp = 0
while True:
	M = ((L+R)//2)

	sumTmp = calc( M )
	# print(M, L,R, sumTmp, X+sumTmp)

	if abs(curMin - abs(X+sumTmp)) < 1:
		# print("curMin:", curMin, abs(X+sumTmp))
		break

	# if M == 0:
	# 	break

	if 0 < X + sumTmp:
		# increase left range
		# (decrease current)
		L = M + 1
		# print("left inc",L,R)
	elif X == sumTmp:
		L = R = M
		curMin = 0
		break
	else:
		# sumTmp < X
		# decrease right range
		# (increase current)
		# print("right dec",L,R)
		R = M - 1

	if L < 0 or R < 0:
		L = R = 0
		break
	elif L > K or R > K:
		L = R = K
		break
	elif R < L:
		# FIXME: これ微妙では。。。?
		# 短調増加でなくなってしまっているため、これがありえる？
		L,R = R,L

	curMin = min(abs(curMin), abs(X+sumTmp))


# print("X:",X,"K:",K, "L:",L, "D:", D)
# print(calc(L) )
print(abs( X + calc(L)))

Submission status: Accepted

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    X, K, D = readnums()
    if X > 0:
        if X // D >= K:
            print(X - K * D)
        else:
            a1 = X % D
            a2 = D - X % D
            if (K - X // D) % 2:
                print(a2)
            else:
                print(a1)
    else:
        X = -X
        if X // D >= K:
            print(X - K * D)
        else:
            a1 = X % D
            a2 = D - X % D
            if (K - X // D) % 2:
                print(a2)
            else:
                print(a1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

q = abs(x) // d
a = abs(x) % d
p = q + 1
l = abs(p * d - abs(x))
if k < q:
    ans = abs(x) - k * d
else:
    if k % 2 == 0:
        if q % 2 == 0:
            ans = a
        else:
            ans = l
    else:
        if q % 2 == 0:
            ans = l
        else:
            ans = a
print(ans)

Submission status: Accepted

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    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")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = abs(X) // D
        #print(syo)
        if syo > 3 :
            if X > 0 :
                val[0] = X - (syo-2) * D
            else :
                val[0] = X + (syo - 2) * D
            #print(val[0])
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 2
            else:
                limit = 3
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            #print(val[i+1])
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                #print("a")
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                #print("b")
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
if X > K*D:
    print(X-K*D)
else:
    K -= X//D
    X %= 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)
a=X//D
if K%2==a%2:
    ans=abs(a*D-X)
else:
    ans=abs((a+1)*D-X)
if a>K:
    ans=abs(K*D-X)
print(ans)

Submission status: Accepted

Solution code:
line = input()

strL = line.split()
X = abs(int(strL[0]))
K = int(strL[1])
D = int(strL[2])

mod = X % D
div = int(X / D)

if div > K:
    ans = X - K * D
elif (K - div) % 2 != 0:
    ans = mod - D
else:
    ans = mod

print(abs(int(ans)))

Submission status: Accepted

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

def sgn(x):
    if x > 0:
        return 1
    if x < 0:
        return -1
    return 0

if x > 0:
    x_greedy = x - k*d
else:
    x_greedy = x + k*d

if sgn(x) == sgn(x_greedy):
    print(abs(x_greedy))
    exit()

x_r = x % d
x_l = x_r -d

r_parity = (abs(x - x_r) // d) %2

if k%2 == r_parity:
    print(abs(x_r))
else:
    print(abs(x_l))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

def solve(x,k,d):
    x=abs(x)
    if (x>0 and x/d>=k):
        x=x-k*d
        return x
    y=x//d
    x-=y*d
    k-=y
    if k%2==0:
        return abs(x)
    return min(abs(x+d),abs(x-d))
print(solve(x,k,d))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

move_count = k - move_count

if move_count % 2 == 0:
    print(current)
else:
    print(abs(current - d))

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

Solution code:
import numpy

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

ans = 0
if numpy.abs(X) > longest:
    ans = numpy.abs(X) - longest
else:
    m = numpy.abs(X) // D
    if (K - m) % 2 == 0:
        ans = numpy.abs(X) - D * m
    else:
        ans = numpy.abs(X) - D * (m + 1)

ans = numpy.abs(ans)
print(ans)

Submission status: Accepted

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

x = abs(x)

num = x // d 

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10


    x,k,d = LMIIS()

    absX = abs(x)
    # 0にできるだけ近づいて終わり
    if k*d <= abs(x):
        print(absX-k*d)
        return
    # マイナスにならない範囲でできるだけ0に近づく
    num = absX // d
    k -= num
    # 残りの移動回数が奇数なら、あと1回だけ移動する
    if k & 1:
        num += 1
    print(abs(absX-num*d))

     
    



    
main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
X, K, D = map(int, input().split())
X = abs(X)
ret = 0
if K*D <= X:
    ret = abs(X - K*D)
else:
    t = X // D
    X -= t * D
    K -= t # 残りの回数
    if K % 2 == 0:
        ret = abs(X)
    else:
        ret = abs(X-D)


print(ret)

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10


    x,k,d = LMIIS()
    sign = -1 if x < 0 else 1
    absX = abs(x)
    if k*d <= abs(x):
        print(absX-k*d)
        return
    num = absX // d
    k -= num
    if k & 1:
        num += 1
    print(abs(absX-num*d))

     
    



    
main()

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

X=abs(X)

q=X//D
r=X%D

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

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

Submission status: Accepted

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

x=abs(x)

cnt=abs(x)//d


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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x,k,d = inpm()

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