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 sys
input = sys.stdin.readline
X, K, D = map(int, input().split())
if abs(X) >= K * D:
  print(abs(X) - K * D)
  exit(0)
a = abs(X) // D
b = -(-abs(X) // D)
res = float("inf")
if (K - a) % 2:
  res = min(res, abs(X) % D + D, abs(abs(X) % D - D))
else: res = min(res, abs(X) % D)
print(res)

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

min_x = x - k * d
max_x = x + k * d
if min_x >= 0:
    print(min_x)
elif max_x <= 0:
    print(abs(max_x))
else:
    a1 = max_x % (d * 2)
    a2 = abs(min_x) % (d * 2)
    print(min(a1, a2))

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

minimum = x - d * k
maximum = x + d * k

if minimum >= 0:
    print(minimum)
elif maximum <= 0:
    print(-maximum)
else:
    c1 = maximum % (2*d)
    c2 = c1 - 2*d
    print(min(abs(c1), abs(c2)))

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

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    x,k,d = map(int,ipt().split())
    if abs(x) > k*d:
        print(abs(abs(x)-k*d))
    else:
        t = abs(x)//d
        dd = abs(x)%d
        if (t-k)%2:
            print(d-dd)
        else:
            print(dd)


    return None

if __name__ == '__main__':
    main()

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

tmp = min(k, abs(x) / d)
k -= tmp

if k % 2 == 1:
    tmp += 1

if x > 0:
    x -= tmp * d
else:
    x += tmp * d

print abs(x)

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

X=abs(X)

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

Solution (Python):
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

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

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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    X,K,D=MI()
    t=abs(X)//D
    if t>=K:
        ans=abs(X)-D*K
    else:
        a=abs(abs(X)-D*t)
        rem=K-t
        if rem%2==0:
            ans=a
        else:
            ans=a=abs(abs(X)-D*(t+1))
        
        
    print(ans)
    
    

main()

Solution (Python):
X,K,D=map(int,input().split())
X=abs(X)
if X>K*D:
  print(X-K*D)
  exit()
M=X//D
X-=(M*D)
K-=M
if K&1:
  print(D-X)
else:
  print(X)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


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

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    x,k,d = LI()
    if x < 0:
        x = -x
    m = x//d
    if k < m:
        print(x-k*d)
        return
    x -= d*m
    k -= m
    k &= 1
    x -= d*k
    print(abs(x))
    return


if __name__ == "__main__":
    solve()

Solution (Python):
mod = 1000000007
eps = 10**-9


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

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


if __name__ == '__main__':
    main()

Solution (Python):

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

nex = abs(X) - K * D

if nex > 0:
    print (nex)

else:
    num = abs(X) // D

    a = abs(X) % D
    b = a - D

    if K % 2 == num % 2:
        print (a)
    else:
        print (abs(b))