Problem:


Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


X, K, D = inl()


def solve():
    if X >= K * D:
        return X - K * D
    if X <= -K * D:
        return abs(X + K * D)
    y = X + K * D
    assert y > 0
    r = y // (2 * D)

    ans = y
    for k in range(r - 10, r + 10):
        if k < 0 or k > K:
            continue
        z = y - k * 2 * D
        ans = min(ans, abs(z))

    return ans


print(solve())

Submission status: Accepted

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

Submission status: Accepted

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

s = 0

if x - k * d > 0:
    s = x - k * d
elif x + k * d < 0:
    s = abs(x + k * d)
else:
    e = x // d
    r = x % d
    if (k-e) % 2 == 1:
        s = abs(r-d)
    else:
        s = r

print(s)

Submission status: Accepted

Solution code:
X,K,D = map(int, input().split())
if X == 0:
  if K % 2 == 0:
    print(0)
  else:
    print(D)
  exit(0)

if X < 0:
  xminus = True
  X = (-1) * X
else:
  xminus = False
  
if abs(X) > K*D:
  ans = X - K*D
else:
  wk = X // D
  K -= wk
  now = X % D
  #print(wk,now)
  if K % 2 == 0:
    ans = now
  else:
    ans = abs(now-D)
    
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

mv_cnt_near_zero = abs(x)//d
point_near_zero = abs(x)%d

ret = 0
if mv_cnt_near_zero >=k:
    ret = abs(x) - k * d
else:
    rest_can_mv = k - mv_cnt_near_zero
    if rest_can_mv % 2 ==0:
        ret = point_near_zero
    else:
        ret = abs(point_near_zero - d)

print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

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

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi


def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)


def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y


for _ in range(int(input()) if not True else 1):
    #n = int(input())
    x, k, d = map(int, input().split())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    if x > 0:
        req = x // d
        if k <= req:
            print(abs(x - k*d))
        else:
            k -= req
            cur = x % d
            print(abs(cur) if k % 2 == 0 else abs(cur - d))
    else:
        req = (-x) // d
        if k <= req:
            print(abs(x + k*d))
        else:
            k -= req
            cur = - (-x%d)
            print(abs(cur) if k % 2 == 0 else abs(cur + d))

Submission status: Accepted

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

if x < 0:
    flag = -1
else:
    flag = 1
x = abs(x)

tmp = x//d

if k < tmp:
    print(abs(x-d*k))
else:
    x=x-d*tmp
    k = k-tmp
    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)
n=X//D

if K<=n:
	print(X-K*D)
elif K%2==n%2:
	print(X-n*D)
else:
	print(abs(X-n*D-D))


# 2darray [[0] * 4 for i in range(3)]
# import itertools

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D = [int(hoge) for hoge in input().split()]
#とりあえず全力でゼロに近づく
X = abs(X)#正ということにする
Risou = X//D#できる限りの移動回数
Amari = X%D #その際にたどり着く座標
if K<=Risou:
    print(X - D*K)
else:#反復横跳び
    Idou = K-Risou
    Idou %= 2
    Amari -= D*Idou
    print(abs(Amari))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


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

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

Submission status: Accepted

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

Submission status: Accepted

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

print(main())

Submission status: Accepted

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

nn, r = divmod(x, d)
kk = k-nn
if kk <= 0:
    print(x-k*d)
elif kk%2 == 0:
    print(r)
else:
    print(abs(r-d))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

if abs(x) - abs(k*d) > 0: #届かない場合
    if x > 0:
        print(abs(x - k*d))
    else:
        print(abs(x + k*d))
else: #往復ステップを始める場合
    r = math.floor(abs(x)/d)
    if x > 0:
        p = x - r*d
        if (k - r) % 2 == 0:
            print(abs(p))
        else:
            print(abs(p-d))
    elif x < 0:
        p = x + r*d
        if (k - r) % 2 == 0:
            print(abs(p))
        else:
            print(abs(p+d))
    else:
        if k % 2 == 0:
            print(0)
        else:
            print(abs(d))

Submission status: Accepted

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

x = abs(x)

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

Submission status: Accepted

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

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

MOD = 10 ** 9 + 7
INF = 10 ** 18

#N = int(input())
X, K, D = map(int, input().split())
#A = list(map(int, input().split()))
#A = [int(input()) for _ in range(N)]
#ABC = [list(map(int, input().split())) for _ in range(N)]
#S = input()
#S = [input() for _ in range(N)]

X = abs(X)
n = X // D + 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if X < 0:
    X = -1*X

M = math.ceil(X/D)
right = abs(X-(M-1)*D)
left = abs(X-M*D)

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

if right <= left:
    if (M-1)%2 == K%2:
        print(right)
    else:
        print(left)
else:
    if M%2 == K%2:
        print(left)
    else:
        print(right)

Submission status: Accepted

Solution code:
xkd=input().split()
x=int(xkd[0])
k=int(xkd[1])
d=int(xkd[2])

ans = abs(x + k*d)
ans = min(ans, abs(x - k*d))

pos = x%d

if (abs(x-pos) // d) % 2 == k % 2 and k >= abs(x-pos)//d: ans=min(ans, pos)

negpos = pos - d

if(abs(x-negpos) // d) % 2 == k % 2 and k >= abs(x-negpos)//d: ans=min(ans,abs(negpos))

print(ans)

Submission status: Accepted

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

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

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

print(answer)

Submission status: Accepted

Solution code:
from math import gcd

from math import factorial as f

from math import ceil, floor, sqrt
import math

import bisect
import re
import heapq

from copy import deepcopy
import itertools
from itertools import permutations

from sys import exit

ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

yes = "Yes"
no = "No"


def main():
    x, k, d = mi()
    x = abs(x)
    if k % 2 == 1:
        x -= d
        x = abs(x)
        k -= 1
    d2 = 2 * d
    kaisuu = x // d2
    k //= 2
    if kaisuu <= k:
        k -= kaisuu
        x -= kaisuu * d2
    else:
        x -= d2 * k
        print(x)
        exit()
    if abs(x - d2) < x and k > 0:
        print(abs(x - d2))
    else:
        print(x)


main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    x, k, d = map(int, input().split())
    cnt = abs(x) // d
    if cnt >= k:
        res = abs(x) - d * k
        print(res)
    else:
        now = abs(x) - cnt * d
        k -= cnt
        if k % 2 == 0:
            print(now)
        else:
            print(abs(now - d))


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if X<0:
    X=-X

A=X//D

if K<A:
    print(X-K*D)
else:
    K-=A
    X-=A*D

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

Submission status: Accepted

Solution code:
# import sys; input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

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

#処理内容
def main():
	X, K, D = getlist()
	if X - K * D >= 0:
		print(X - K * D)
	elif X + K * D <= 0:
		print(abs(X + K * D))

	else:
		X = abs(X)
		K -= X // D
		X = X % D

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



if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

    if (K - m) % 2 == 0:  print(ans1)
    else:  print(ans2)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
from collections import Counter, deque, defaultdict
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def solve(x, k, d):
    if x - k * d >= 0:
        return x - k * d
    else:
        j = x // d
        xj = abs(x - d * j)
        xj1 = abs(x - d * (j + 1))
        rem = k - j
        if rem % 2 == 0:
            return xj
        else:
            return xj1
        # print(x, k, d, j, xj, xj1, rem)
    # return None

def main():
    from builtins import int, map
    X, K, D = map(int, input().split())
    if X < 0:
        X *= -1
    print(solve(X, K, D))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if abs(X) >= K * D:
  if X > 0:
    ans = X - K * D
  else:
    ans = X + K * D
  print(abs(ans))
else:
  count = int(abs(X) / D)
  if X > 0:
    one = X - D * count
    two = one - D
    if (K - count) % 2 == 0:
      print(abs(one))
    else:
      print(abs(two))
  else:
    one = X + D * count
    two = one + D
    if (K - count) % 2 == 0:
      print(abs(one))
    else:
      print(abs(two))

Submission status: Accepted

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

l = X - K * D
r = X + K * D


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

Submission status: Accepted

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




if __name__ == "__main__":
    main()

Submission status: Accepted

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

k = abs(X // D)
r = X % D

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
import copy
# from fractions import gcd

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

x, k, d = map(int, input().split())
x = abs(x)
if x - k*d >= 0:
    print(x - k*d)
else:
    k = k - x//d
    x = 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)

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

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

Submission status: Accepted

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

mod = x % d
mod_d = d - mod

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

Submission status: Accepted

Solution code:
import copy
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    x,k,d=INTM()
    if abs(x)-k*d>0:
        flg=1
        ans=abs(x)-k*d
    else:
        flg=2
        a=x%d
        b=a-d
        #c,d=max(a,abs(b)),min(a,abs(b))
        if (k-((x-a)//d))%2==0:
            flg=3
            ans=abs(a)
        else:
            flg=4
            ans=abs(b)
            
    print(ans)
    #print(a,b,flg)
    #print(((x-a)//d))
    
    
if __name__ == '__main__':
    do()

Submission status: Accepted

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

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

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

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

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

x,k,d=mp()
x=abs(x)
if k*d<=x:
    print(x-k*d)
    exit()
y=x//d
if (k-y)%2==1:
    print(abs(x-d*(y+1)))
else:
    print(abs(x-d*y))

Submission status: Accepted

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

Submission status: Accepted

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

if x < 0:
    x = -x

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

Submission status: Accepted

Solution code:
import sys, math
from functools import lru_cache
from collections import defaultdict
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def solve():
    X, K, D = map(int, readline().split())

    X = abs(X)

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

solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(abs(X))

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

Submission status: Accepted

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

kaisu = abs(x) // d

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

x, k, d = map(int, input().split())
x = abs(x)
init = x // d
x -= min(init, k) * d
k -= min(init, k)

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

Submission status: Accepted

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

X, K, D = LI()


def main():
    x = abs(X)
    a = x // D
    if K <= a:
        ans = x - K * D
        print(ans)
        return
    
    x -= a * D
    k = K - a
    if k % 2 == 0:
        ans = x
    else:
        ans = abs(x - D)
    print(ans)
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""


x,k,d = m()

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

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

inf = 10 ** 18
mod = 10 ** 9 + 7

x,k,d = lr()
dis = abs(x)
if k*d < dis:
    print(dis-k*d)
else:
    tmp = dis//d
    ret = k-tmp
    if ret%2 == 0:
        print(dis-tmp*d)
    else:
        print(abs(dis-(tmp+1)*d))

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