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.

Solution (Python):
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")

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()


s,k,d = Ri()
flag = False
if s < 0:
    flag = True

totk=  k
s = abs(s)
times = s//d

minn = s-k*d
if minn >= 0:
    print(s-k*d)
else:
    pos1 = s-times*d
    pos2 = INF
    k-=times
    if pos1 == 0:
        if k %2 == 1:
            print(d)
        else:
            print(0)
        exit()
    pos2 = abs(pos1 - d)
    if k == 1:
        print(pos2)
    elif k % 2 == 0:
        print(pos1)
    else:
        print(pos2)

Solution (Python):
def hoge(X,K,D):
  abs_X=abs(X)
  if K*D<abs_X:
    return abs_X-K*D
  K-=X//D
  X=X%D
  if K%2==0:
    return X
  else:
    return abs(X-D)

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

Solution (Python):

import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


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


def instr():
    s = input()
    return(list(s[:len(s)]))


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








def main():
    x, k, d = invr()
    x = abs(x)
    d_move = min(k, x // d)
    curr = x - d_move*d
    baki = k - d_move
    if baki % 2 != 0:
        print(d - curr)
    else:
        print(curr)



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)
def input(): return sys.stdin.readline().rstrip("\r\n")




if __name__ == "__main__":
    main()

Solution (Python):
import math

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


if abs(x) >= d * k:
    print(abs(abs(x)-d*k))
else:
    temp = math.ceil(abs(x)/d)
    if temp % 2 == 1:
        if k % 2 == 1:
            print(abs(abs(x)-temp*d))
        else:
            print(abs(abs(x)-temp*d+d))
    elif temp % 2 == 0:
        if k % 2 == 0:
            print(abs(abs(x)-temp*d))
        else:
            print(abs(abs(x)-temp*d+d))

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

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

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

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

def counta(x,k,d):
    
    x=abs(x)
    if d<2*x:
        ans=x//d
        
        if ans>k:
            print(abs(x-k*d))
            return
        k=k-ans
        x=x-ans*d
        if k%2==0:
            print(x)
            return
        else:
            print(abs(x-d))
            return
    else:
        if k%2==0:
            print(x)
            return
        else:
            print(abs(x-d))
            return
counta(x,k,d)

Solution (Python):
def solve(X, D, lb, ub):
    
    t = round(-X / D)
    ans = abs(X)
    for i in [-2, -1, 0, +1, +2]:
        tt = t + i
        tt = min(tt, ub)
        tt = max(tt, lb)
        y = X + tt * D
        ans = min(ans, abs(y))
    return ans

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

y1 = X - K * D
y2 = X + K * D
if (y1 > 0 and y2 > 0) or (y1 < 0 and y2 < 0): 
    print(min(abs(y1), abs(y2)))
else:
    if K % 2 == 0:
        ans = solve(X, D * 2, -(K // 2), +(K // 2))
    else:
        ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
        ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
        ans = min(ans1, ans2)
    print(ans)

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

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

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

print(ans)

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

pm = 1 if x >= 0 else -1
a = abs(x)//d
b = a if abs(x-pm*a*d) < abs(x-pm*(a+1)*d) else a+1
c = min(b, k)
e = x-pm*c*d
if k < b or (k-b) % 2 == 0:
    print(abs(e))
else:
    print(min(abs(e+d), abs(e-d)))

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

Solution (Python):
import sys












stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    
    x,k,d = map(int, stdin.readline().rstrip().split())
    
    
    
    
    
    if abs(x) >= k*d:
        print(abs(x) - k*d)
        exit()
    else:
        a = abs(x) // d
        if abs(abs(x) - d*a) >= abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 0:
            print(abs(abs(x) - d*(a+1)))
            exit()
        elif abs(abs(x) - d*a) >= abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 1:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) <= abs(abs(x) - d*(a+1)) and (k-a)%2 == 0:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) <= abs(abs(x) - d*(a+1)) and (k-a)%2 == 1:
            print(abs(abs(x) - d*(a+1)))
            exit()

if __name__ == '__main__':
    solve()

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

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

print(ans)

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

X = abs(X)

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

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

if x < 0:
  x *= -1

r = x % d
q = x // d

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

Solution (Python):

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

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

Solution (Python):
X,K,D=map(int,input().split())
X=abs(X)
a=X%D
b=a-D
c=X//D
if a>abs(b):
    if c>K:
        print(X-D*K)
    elif c%2==K%2:
        print(a)
    else:
        print(abs(b))
else:
    if c+1>K:
        print(abs(X-D*K))
    elif c%2!=K%2:
        print(abs(b))
    else:
        print(a)

Solution (Python):
start, num_move, distance = map(int, input().split())


desired_move = abs(start) // distance

another_move = num_move - desired_move
if another_move <= 0:
    ans_abs = abs(start) - num_move * distance
    print(ans_abs)

else:
    if another_move % 2 == 0:
        ans_abs = abs(start) - desired_move * distance
        print(ans_abs)
    else:
        ans_abs = abs(abs(start) - (desired_move+1) * distance)
        print(ans_abs)

Solution (Python):
import math

def a():
    s = input()
    if s == 'RRR':
        print(3)
    elif 'RR' in s:
        print(2)
    elif 'R' in s:
        print(1)
    else:
        print(0)


def b():
    n = int(input())
    l = list(map(int, input().split()))
    l.sort()
    count = 0
    for i, li in enumerate(l):
        for j, lj in enumerate(l[i + 1:],i + 1):
            for lk in l[j + 1:]:
                if lk < li + lj and li != lj and lj != lk:
                    
                    count += 1
    print(count)




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



def d():
    pass




c()

Solution (Python):
X, K, D = map(int, input().split())
s = 0
g = K

def d(h):
    return X + (h - (K-h))*D

if d(s) >= 0:
    ans = d(s)
elif d(g) <= 0:
    ans = abs(d(g))
else:
    half = (s + g)//2
    while s < half:
        if d(half) < 0:
            s = half
        else:
            g = half
        half = (s + g)//2

    ans = min(abs(d(s)) , abs(d(g)) )
print(ans)

Solution (Python):
import math
from collections import defaultdict,deque
from itertools import permutations
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
ips=lambda:input().split()

"""========main code==============="""

t=1

for _ in range(t):
    a,b,c=ml()
    a=abs(a)
    rem=a//c
    rem=abs(rem)
    
    if(b>=rem and b%2==rem%2):
        ans=abs(abs(a)-c*rem)
        print(ans)
    elif(b>=rem+1 and b%2!=rem%2):    
        ans=abs(abs(a)-c*(rem+1))
        print(ans)
    else:
        print(abs(abs(a)-c*b))

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

Solution (Python):
def resolve():
    X, K, D = list(map(int, input().split()))
    k = min(abs(X) // D, K)
    x = min(abs(X - D*k),abs(X + D*k))
    l = K-k
    if l % 2 == 1:
        x = min(abs(x-D), abs(x+D))
    print(x)
    
resolve()

Solution (Python):
from sys import stdin
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]


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

x,k,d = ip()
print(solve(abs(x),k,d))

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

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

ax = abs(x)
t = k * d

if t == ax:
    print(0)
elif t < ax:
    print(ax - t)
else:
    a = ax % d
    b = d - a
    f = ax // d

    if k % 2 == f % 2:
        print(a)
    else:
        print(b)

Solution (Python):
x, k, d = map(int, input().split())
x = abs(x)
s = int(x/d)
ans = x%d

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

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



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

Solution (Python):
from sys import stdin,stdout
for _ in range(1):
    
    x,k,d=list(map(int,stdin.readline().split()))
    '''if x==k==d:
        print(0 if k&1 else x)
        continue'''
    x=abs(x)
    des=x%d
    op=x//d
    
    a=[des,des-d]
    if k<op:
        print(x-k*d)
    else:
        print(abs(a[(k-op)%2]))

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

n = abs(x)//d
sign_x = 1 if x>0 else -1

if n>=k:  
  x -= sign_x*k*d
  print(abs(x))
else:
  cand = [x-sign_x*n*d, x-sign_x*(n+1)*d]
  rest_n = k-n
  if rest_n%2:
    print(abs(cand[1]))
  else:
    print(abs(cand[0]))

Solution (Python):
""" C
    https://atcoder.jp/contests/abc175/tasks/abc175_c
"""
import sys
import math
from functools import reduce
from bisect import bisect_left

def readString():
    return sys.stdin.readline()

def readInteger():
    return int(readString())

def readStringSet(n):
    return sys.stdin.readline().split(" ")[:n]

def readIntegerSet(n):
    return list(map(int, readStringSet(n)))

def readIntegerMatrix(n, m):
    return reduce(lambda acc, _: acc + [readIntegerSet(m)], range(0, n), [])

def _pattern(N):
    if N > 1:
        pa = _pattern(N-1)
        return [[0] + p for p in pa] + [[1] + p for p in pa]
    else:
        return [[0], [1]]

def main(X, K, D):
    if abs(X) > abs(D) * K:
        return abs(X) - abs(D) * K
    else:
        k = abs(X) // abs(D)
        if (K - k) % 2 == 0:
            return abs(X) - k * abs(D)
        else:
            return min(abs(abs(X) - (k+1) * abs(D)), abs(abs(X) - (k-1) * abs(D)))

if __name__ == "__main__":
    _X, _K, _D = readIntegerSet(3)

    print(main(_X, _K, _D))

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

Solution (Python):
import math
X,K,D = map(int,input().split())
if(X>0):
    posneg = 0
else:
    posneg = 1

if(posneg == 0):
    Ki = math.ceil(X/D)
    D = D*(-1)
else:
    Ki = abs(math.floor(X/D))

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

Solution (Python):



import sys
import glob

class InputGenerator(object):
    def __init__(self, input_path=None):
        if input_path != None:
            self.g = self.input_generator(input_path)
        else:
            self.g = None

    def input_generator(self, input_path):
        with open(input_path) as f:
            for line in f:
                yield line

    def get_input(self):
        if self.g == None:
            return input()
        else:
            return next(self.g)

def solve(gen):
    len_list = gen.get_input().rstrip().split()
    [x, k, d] = list(map(int, len_list))
    

    r = abs(x) % d
    l = d - r
    q = abs(x) // d

    if abs(x) > k * d:
        print(abs(x) - k * d)
    elif (k - q) % 2 == 0:
        print(r)
    else:
        print(l)

if __name__ == '__main__':
    test_flag = len(sys.argv) > 1

    if test_flag == True:
        file_path_list = sorted(glob.glob('test/*'))
    else:
        file_path_list = [None]

    for file_path in file_path_list:
        gen = InputGenerator(file_path)
        solve(gen)

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

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

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

Solution (Python):
x,k,d = map(int,input().split())
x = abs(x)
n = x//d
if n <= k :
    x -= d*n
    count = k-n+2
    if count % 2 == 0 :
        now = x
    else :
        now = d-x
else :
    now = x - k*d

print(abs(now))

Solution (Python):
x,k,d = map(int,input().split())
if x < 0:
    x = -x
if x - k*d > 0:
    print(x-k*d)
    quit()

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

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

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

Solution (Python):


import math


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


if __name__ == "__main__":
    main()

Solution (Python):
x,k,d=map(int,input().split())
x=abs(x)
if x>k*d:
	print(x-k*d)

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

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

if k%2 == 0: 
    flg = 1
else:
    flg = 0
x = abs(x) 
if 2*x < d:
    if flg: 
        print(x)
    else:
        print(d-x)
else:
    if x < d: 
        if flg:
            print(x)
        else:
            print(d-x)
    else: 
        tmp = x // d 
        if tmp >= k:
            print(x-d*k)
        else:
            s = x - d*tmp
            t = abs(s-d)
            if tmp%2 == 0: 
                if flg:
                    print(s)
                else:
                    print(t)
            else:
                if flg:
                    print(t)
                else:
                    print(s)

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

print(ans)

Solution (Python):
import sys


def resolve(in_):
    X, K, D = map(int, next(in_).split())

    c, a = X // D, X % D
    b = a - D

    if X >= 0:
        ans = X - K * D
        if ans <= b:
            if c % 2 == K % 2:
                ans = a
            else:
                ans = abs(b)
        elif ans <= a:
            ans = a
        else:
            pass
    else:
        ans = X + K * D
        if ans >= a:
            if c % 2 == K % 2:
                ans = a
            else:
                ans = abs(b)
        elif ans >= b:
            ans = abs(b)
        else:
            ans = abs(ans)
    return ans

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

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


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

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

Solution (Python):
X,K,D=map(int,input().split())
res = X
n = abs(int(X/D))
rest = 0
if n >= K:
  if X < 0:
    res = X+K*D
  else:
    res = X-K*D
else:
  if X < 0:
    res = X+n*D
  else:
    res = X-n*D
  rest = K-n
  if rest % 2 == 1:
    if res < 0:
      res = res+D
    else:
      res = res-D
  else:
    pass
res = abs(res)
print(res)

Solution (Python):
import sys

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

if X >= 0: 
    temp1 = X // D
    if temp1+1 > K:
        print(X - D * K)
        sys.exit()
    else:
        ite1 = (K - temp1) % 2
        if ite1 == 1:
            print(abs(X % D - D))
            sys.exit()
        else:
            print(X % D)
            sys.exit()
else:
    temp1 = abs(X // D) - 1
    if temp1+1 > K:
        print(abs(X + D * K))
        sys.exit()
    else:
        ite1 = (K - temp1) % 2
        if ite1 == 1:
            print(X % D)
        else:
            print(abs(X % D - D))

Solution (Python):
[x, k, d] = map(int , input().split())
t = ((k*d - x) / (2*d))
t = round(t)
if k < t:
	t = k
elif t < 0:
	t = 0
min = x - k*d + 2*t*d
print(abs(min))

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

if X // D <= K:
    now = X % D
    K -= X // D
else:
    now -= K * D
    K = 0

if K > 0:
    ans1 = now
    ans2 = abs(now - D)

    if ans2 < ans1 and K % 2 == 1 or ans2 > ans1 and K % 2 == 1:
        now = ans2
print(abs(now))

Solution (Python):
X, N, D = map(int, input().split())
X = abs(X)
maxD = N * D

if X > maxD :
  print(X - maxD)
else :
  N -= X // D
  num = N % 2
  if num == 0:
    print(X % D)
  else :
    print(D - X % D)

Solution (Python):
import sys
si = sys.stdin.readline


def main():
    [x, k, d] = [int(e) for e in si().split()]
    if x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(abs(d))
    elif x < 0:
        mul = (0-x)//d
        if k <= mul:
            x += d*k
            print(abs(x))
            return
        else:
            x += d*mul
            k -= mul
        while x < 0 and k:
            x += d
            k -= 1
        if k > 0:
            if k % 2:
                print(abs(x-d))
            else:
                print(x)
        else:
            print(abs(x))
    else:
        mul = (x-0)//d
        if k <= mul:
            x -= d*k
            print(x)
            return
        else:
            x -= d*mul
            k -= mul
        while x > 0 and k:
            x -= d
            k -= 1
        if k > 0:
            if k % 2:
                print(x+d)
            else:
                print(abs(x))
        else:
            print(abs(x))


if __name__ == '__main__':
    main()

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

Solution (Python):
x, k, d = map(int, input().split())
i = 1
x = abs(x)

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

Solution (Python):
dis,num,move=map(int,input().rstrip().split())
if dis<0:
    dis=-dis
a=dis%move
b=move-dis%move
c=int((dis-a)//move)
if dis-num*move>0:
    result=dis-num*move
elif (num-c)%2==0:
    result=a
else:
    result=b
print(result)

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

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

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

Solution (Python):
X, K, D = map(int, input().split())
n = (K-X//D)//2

if X - K*D > 0:
    ans = X-K*D
elif X + D*K < 0:
    ans = abs(X+D*K)
else:
    ans = float('inf')
    for i in range(-1, 2):
        res = X + D*(K-2*(n+i))
        ans = min(ans, abs(res))
    for i in range(-1, 2):
        res = X + D*(2*(n+i)-K)
        ans = min(ans, abs(res))
        

print(ans)

Solution (Python):


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

    if X - K * D >= 0:
        print(X - K * D)
    elif X + K * D <= 0:
        print(-D * K - X)
    else:
        y = (D * K - X) // (2 * D)
        yy = (D * K - X + 2 * D) // (2 * D)
        print(min(abs(2 * D * y + X - D * K), abs(2 * D * yy + X - D * K)))
main()

Solution (Python):
X,K,D = list(map(int , input().split()))
ans = 0
if (X >=0):
    a = K*D
    if (X >= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X -= D
            time += 1
            if X <= 0:
                break
        nokori = (K - time) % 2
        ans = X + D*nokori
else:
    a = -(K*D)
    if (X <= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X += D
            time += 1
            if X >= 0:
                break
        nokori = (K - time) % 2
        ans = X - D*nokori
Answ = abs(ans)
print(Answ)

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

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

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

div = x // d
if k <= div:
    ans = x - k * d
else:
    if (k - div) % 2 == 1:
        ans = x - d * (div + 1)
    else:
        ans = x - d * div

print(abs(ans))

Solution (Python):
import sys

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

if X < 0:
    X = -X

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

temp_X = X
temp_K = K





k = X//D

temp_X = X-k*D
temp_K = K-k

if temp_K%2==0:
    print(temp_X)
else:
    print( abs(temp_X-D) )

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

r = X % D

maxMoveLength = K * D

plusMin = r
minusMin = r - D

countForPlusMin = abs(X - plusMin) // D
countForMinusMin = abs(X - minusMin) // D

goal = 0
if K >= countForPlusMin and K % 2 == countForPlusMin % 2:
    goal = plusMin
elif K >= countForMinusMin and K % 2 == countForMinusMin % 2:
    goal = minusMin
elif K < countForMinusMin or K < countForPlusMin:
    if X < 0:
        goal = X + D * K
    else:
        goal = X - D * K

print(abs(goal))

Solution (Python):
import numpy as np






import math



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



c,C=0,0

X=abs(X)

if X-D*K>=0:
    print(X-D*K)

else:
    x = math.ceil(X/D)

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

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