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):
"""
    Satwik_Tiwari ;) .
    14th AUGUST , 2020  - FRIDAY
"""



from __future__ import division, print_function

from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase

from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter  
from itertools import combinations as comb 
from itertools import permutations as permutate
from bisect import bisect_left as bl


from bisect import bisect_right as br
from bisect import bisect





BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

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

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

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

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


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


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


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





from types import GeneratorType
def iterative(f, stack=[]):
  def wrapped_func(*args, **kwargs):
    if stack: return f(*args, **kwargs)
    to = f(*args, **kwargs)
    while True:
      if type(to) is GeneratorType:
        stack.append(to)
        to = next(to)
        continue
      stack.pop()
      if not stack: break
      to = stack[-1].send(to)
    return to
  return wrapped_func





mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") 
def out(var): sys.stdout.write(str(var))  
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())

def zerolist(n): return [0]*n
def nextline(): out("\n")  
def testcase(t):
    for p in range(t):
        solve()
def printlist(a) :
    for p in range(0,len(a)):
        out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
    res = 1     
    x = x % p  
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : 
            res = (res * x) % p

        y = y >> 1      
        x = (x * x) % p
    return res
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) :
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6
    return True



def chck(a,b,c):
    if(a+b>c and b+c>a and c+a>b):
        return True
    return False

def solve():
    x,k,d = sep()
    if(x==0):
        if(k%2==0):
            print(0)
        else:
            print(x+d)
        return

    x = abs(x)

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









testcase(1)

Solution (Python):
import sys


def solve(x, k, d):
    x = abs(x)
    if d * k <= x:
        return x - d * k
    p, q = divmod(x, d)
    if (k - p) % 2 == 0:
        return q
    else:
        return d - q


def main():
    input = sys.stdin.buffer.readline
    x, k, d = map(int, input().split())
    print(solve(x, k, d))


if __name__ == "__main__":
    main()

Solution (Python):

import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

x,k,d = ip()
if k*d <= abs(x):
    print(abs(x)-(k*d))
    exit()
if x < 0:
    n = abs(x)//d
    k -= n
    x += n*d
    if k%2:
        print(abs(x+d))
    else:
        print(abs(x))
elif x > 0:
    n = x//d
    x -= n*d
    k -= n
    if k%2:
        print(abs(x-d))
    else:
        print(abs(x))
else:
    if k%2:
        print(d)
    else:
        print(0)

Solution (Python):
x,k,d = map(int,input().split())
x_max = x+k*d
x_min = x-k*d
if x_max<=0:print(-x_max)
elif x_min>=0:print(x_min)
else:
    t = x_max%(2*d)
    t2 = -t+2*d
    print(min(t,t2))

Solution (Python):








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

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


print(ans)

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

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

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

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

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

X = abs(X)
if (tmp := X - K * D) >= -D:
    print(abs(tmp))
else:
    tmp = (X - (K % 2) * D) % (2 * D)
    print(min(abs(tmp), abs(tmp - 2 * D)))

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

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

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

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

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

a, b = X//D, X%D
if a > K:
  print(X-D*K)
elif (K-a)%2:
  print(D-b)
else:
  print(b)

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

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

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

x, K, D = map(int, readline().split())
if x - K * D >= 0:
    print(abs(x - K * D))
elif x + K * D <= 0:
    print(abs(x + K * D))
else:
    low, high = 0, K
    while high - low > 1:
        mid = (high + low) // 2
        if x + (-K + 2 * mid) * D <= 0:
            low = mid
        else:
            high = mid
    print(min(abs(x + (-K + 2 * low) * D), abs(x + (-K + 2 * high) * D)))

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

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

Solution (Python):
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007

inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')



def solve():
    
    

        


        
        x,k,d= mi()
        x = abs(x)
        c = x//d
        if c>=k:
            print(x-k*d)
            exit(0)
        k -= c
        c1 = x%d
        c2 = c1-d
        if k&1:
            print(abs(c2))
        else:
            print(abs(c1))
    



















        
if __name__ =="__main__":

    if path.exists('input.txt'):
        sys.stdin=open('input.txt', 'r')
        sys.stdout=open('output.txt','w')
    else:
        input=sys.stdin.readline
    solve()

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

    a = x // d
    if a>=k:
        print(x-k*d)
    else:
        k -= a
        if k%2 == 0:
            print(x-a*d)
        else:
            v = x-(a+1)*d
            if v<0: v=-v
            print(v)
main()

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

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

Solution (Python):
mod = 10**9 + 7
def solve():
    x, k, d = map(int, input().split())
    t = min(abs(x) // d, k)
    if x < 0:
        x += t * d
    else:
        x -= t * d
    k -= t
    k %= 2
    ans = x
    if k > 0:
        ans = min(abs(x + d), abs(x - d))
    else:
        ans = abs(x)
    print(abs(ans))
t = 1
while t > 0:
    solve()
    t -= 1

Solution (Python):
import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


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


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

DIV = (10**9) + 7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s += a
        yield s


sys.setrecursionlimit(200005)


def dijkstra(G,start=0):
    heap = []
    cost = [INF]*len(G)
    heappush(heap,(0,start))
    while len(heap)!=0:
        c,n = heappop(heap)
        if cost[n] !=INF:
            continue
        cost[n]=c
        for e in G[n]:
            ec,v = e
            if cost[v]!=INF:
                continue
            heappush(heap,(ec+c,v))
    return cost


def main():
    X,K,D = isRaw()
    X = abs(X)
    W =  X//D
   
    if W > K:
        print(X-D*K)
        return
    if (K-W)%2==0:
        print(X%D)
        return
    else:
        print(D-X%D)
         
main()

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


if X < 0:
    t = min(X//(-D), K)
    X = X + D*t
    K -= t
else:
    t = min(X//(D), K)
    X = X - D*t
    K -= t

if K % 2 == 1:
    if X >= 0:
        X -= D
    else:
        X += D

print(abs(X))

Solution (Python):


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


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

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


x,k,d = list(map(int, input().split()))
x = abs(x)
if x>=k*d:
    ans = x-k*d
elif x<=-k*d:
    ans = x+k*d
else:
    v = x//d
    val = x%d
    val2 = abs(val-d)
    if (k-v)%2==0:
        ans = val
    else:
        ans = val2
print(ans)

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

cnt = abs(x) // d
x = abs(x)
if k % 2 == cnt % 2:
    ans = min(abs(x - d * min(k,cnt)), abs(x + d * min(k,cnt)))
elif k % 2 != cnt % 2:
    ans = min(abs(x - d * min(k,(cnt + 1))), abs(x - d * min(k,(cnt + 1))))
print(ans)

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

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

x = max(x, -x)

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

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

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

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

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

most_nearest_do = X//D


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

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

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

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

    if X >= 0:
        use = min(X // D, K)
        K -= use
        X -= use * D
        if K % 2:
            X -= D

    else:
        use = min((-X) // D, K)
        K -= use
        X += use * D
        if K % 2:
            X += D

    print(abs(X))


if __name__ == '__main__':
    main()

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

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)








INF = 1 << 50


def run():
    X, K, D = map(int, sysread().split())
    ans = INF
    if abs(X) > K * D:
        ans = abs(X) - K * D
        print(ans)
        return

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




if __name__ == "__main__":
    run()

Solution (Python):
X,K,D = map(int,input().split())
X = abs(X)
ans = int(1e9) + 7
val = X//D

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

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

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

Solution (Python):
X, K, D = map(int, input().split())
deka = pow(10,20)
X = abs(X)

if K * D < X:
    print(abs(X - K * D))
else:
    cnt1 = X // D
    ans1 = deka
    if cnt1 <= K:
        rem = K - cnt1
        if rem % 2 == 0:
            ans1 = X - cnt1 * D
        else:
            ans1 = abs(X - cnt1 * D - D)
    
    cnt2 = (X+D-1) // D
    ans2 = deka
    if cnt2 <= K:
        rem = K - cnt2
        if rem%2 == 0:
            ans2 = cnt2 * D - X
        else:
            ans2 = abs(cnt2 * D - X - D)
    
    print(min(ans1, ans2))

Solution (Python):



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

    X = abs(X)

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

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

    return


main()

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

if X > 0:
    p = math.ceil(X/D)
    if K < p:
        ans = X - K*D
    else:
        X -= p*D
        K -= p
        ans = X + (K % 2) * D
else:
    p = math.ceil(abs(X/D))
    if K < p:
        ans = X + K*D
    else:
        X += p*D
        K += p
        ans = X - (K % 2) * D

print(abs(ans))

Solution (Python):

import sys


def solve(X: int, K: int, D: int):
    X = abs(X)
    t = X // D
    if t <= K:
        if t % 2 == K % 2:
            print(abs(X - D * t))
            return
        else:
            print(abs(X - D * (t + 1)))
            return
    else:
        print(abs(X - D * K))
        return
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    K = int(next(tokens))  
    D = int(next(tokens))  
    solve(X, K, D)

if __name__ == '__main__':
    main()

Solution (Python):

"""
Created on Sat Aug 15 21:12:05 2020

@author: NEC-PCuser
"""

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

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

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

Solution (Python):
X, K, D = [abs(int(i)) for i in input().split()]
sho = X//D
if sho > K:
    print(X-K*D)
else:
    if (sho - K) % 2 == 0:
        print(X % D)
    else:
        print(D-X % D)

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

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



ans1=x%d
ans2=abs((x%d)-d)

moved=x//d
nokori=k-moved
if nokori==0:
    print(ans1)
    exit()

if nokori%2==0:
    print(ans1)
else:
    print(ans2)

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

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

n = abs(X)//D

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

Solution (Python):
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy

x, k, d = map(int, sys.stdin.readline().split())



a = (abs(x)//d) + 1

if a >= k:
    if x >= 0:
        ans = (x - k * d)
    else:
        ans = (x + k * d)
else:
    if x >= 0:
        if (k - a) % 2 == 0:
            ans = (x - a * d)
        else:
            ans = ((x - a * d) + d)
    else:
        if (k - a) % 2 == 0:
            ans = (x + a * d)
        else:
            ans = ((x + a * d) - d)

print(abs(ans))

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

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
x,k,d = readInts()
ikeru = abs(x)//d


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

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math
from collections import deque
import itertools
import heapq

from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint




def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default




def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    
    x,k,d = map(int, input().split())
    tempkai=abs(x)//d
    ans=INFI
    if tempkai>=k:
        ans=abs(x)-d*k
    else:
        if (k-tempkai)%2==0:
            ans=abs(x)-d*tempkai
        else:
            temp=abs(x)-d*tempkai
            temp2=abs(temp-d)
            temp3=abs(temp+d)
            ans=min(temp3,temp2)
    print(ans)

if __name__ == "__main__":

    main()

Solution (Python):
X, K, D = map(int, input().split())
X = abs(X)
if X // D > K:
    result = X - D*K
else:
    n = X//D
    rem_jump = K - n
    p1 = X % D
    p2 = D - p1

    if rem_jump % 2 == 0:
        result = p1
    else:
        result = p2

print(result)

Solution (Python):


import sys

input = iter(sys.stdin.read().splitlines()).__next__

def solve(X, K, D):
   if X < 0:
      X = -X
   if X - K*D >= 0:
      return X - K*D
   k = X // D
   X -= k*D
   assert X >= 0
   K -= k
   assert K > 0

   K %= 2
   if K == 1:
      X -= D
   if X < 0:
      X = -X
   return X

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

res = solve(X, K, D)
print(res)

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

ab = abs(x)//d
ab_1 = ab +1

koho1 = abs(x) - abs(d*ab)
koho2 = abs(abs(x) -abs(d*ab_1))

tmp = k-ab

koho_s = []

if(tmp > 0):
    if(tmp%2 == 0):
        ans = koho1
    else:
        ans = koho2
elif(tmp == 0):
    ans = koho1
else:
    ans = abs(x) - abs(d*k)

print(ans)

Solution (Python):

import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    x, k, d = MI()
    x = abs(x)

    min_idou = x // d
    amari = x % d

    if min_idou >= k:
        print(min(abs(x - k*d), x))
        return

    k -= min_idou
    if k % 2 == 0:
        print(amari)
    else:
        print(-1*(amari - d))


main()

Solution (Python):
import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict






def solve(x,k,d):  
    console("----- solving ------")
    console(x,k,d)

    x = abs(x)

    if k*d <= x:
        return x - k*d

    remainder = x%d
    moves = (x - remainder) // d

    leftover_moves = (k - moves) % 2

    console(remainder, moves, leftover_moves)

    return min(abs(remainder + leftover_moves*d), abs(remainder - leftover_moves*d))


def console(*args):  
    print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return




for case_num in [1]:
    
    

    
    
    
    
    

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

    
    
    
    
    
    

    res = solve(x,k,d)  
    
    
    

    
    print(res)

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

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

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
input = sys.stdin.readline
INF = float("inf")



def main():
    X, K, D = map(int, input().split())
    
    if abs(X) - K*D >= 0:
        print(abs(X) - K*D)
    else:
        L = K*D - abs(X)
        m = L // D
        if m % 2 == 0:
            print(L - m*D)
        else:
            print((m+1)*D - L)



if __name__ == '__main__':
    main()

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

def ceil(a, b):
	return -(-a//b)

mint = abs(x)//d
targ1 = min(abs(x-d*mint), abs(x+d*mint))
targ2 = min(abs(targ1-d), abs(targ1+d))

if mint >= k:
	print(min(abs(x-k*d), abs(x+k*d)))
	exit()
else:
	targ3 = k-mint
	if targ3 % 2 == 0:
		print(targ1)
	else:
		print(targ2)

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

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

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

a = (x+ d*k)//(2*d)
b = a+1

if abs(x+d*k-2*d*a) < abs(x+d*k-2*d*b):
    best = a
else:
    best = b

if 0 <= best <= k:
    print(abs(x+d*k-2*d*best))
elif best < 0:
    print(abs(x+d*k))
else:
    print(abs(x-d*k))

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

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

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

l, r, d1 = -k, k, k + 1
while (l <= r):
    mid = (l + r) // 2
    if (x + mid * d >= 0):
        d1 = mid 
        r = mid - 1
    else:
        l = mid + 1

l, r, d2 = -k, k, k + 1
while (l <= r):
    mid = (l + r) // 2
    if (x - mid * d < 0):
        d2 = mid 
        l = mid + 1
    else:
        r = mid - 1

ans = int(1e30)
if (d1 != k + 1):
    r = (k - abs(d1)) % 2
    if (r == 0):
        ans = min(ans, abs(x + d * (d1 + r)))
    else:
        ans = min(ans, abs(x + d * (d1 - 1)))
        ans = min(ans, abs(x + d * (d1 + 1)))
if (d2 != k + 1):
    r = (k - abs(d2)) % 2
    if (r == 0):
        ans = min(ans, abs(x + d * (d2 + r)))
    else:
        ans = min(ans, abs(x + d * (d2 - 1)))
        ans = min(ans, abs(x + d * (d2 + 1)))


print(ans)

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