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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
"""
    Satwik_Tiwari ;) .
    14th AUGUST , 2020  - FRIDAY
"""

#===============================================================================================
#importing some useful libraries.
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  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================
#fast I/O region
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)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
### START ITERATE RECURSION ###
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
#### END ITERATE RECURSION ####

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
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 graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
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     # Initialize result
    x = x % p  # Update x if it is more , than or equal to p
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : # If y is odd, multiply, x with result
            res = (res * x) % p

        y = y >> 1      # y = y/2
        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

#===============================================================================================
# code here ;))
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):
        print(0)
        return

    x = abs(x)

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









testcase(1)
# testcase(int(inp()))

Submission status: Wrong Answer

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

jump=x//d

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
n = (-x+k*d)//(2*d)
cur = float('inf')
if n < 0:
    print(abs(x-k*d))
else:
    if n == 0:
        L = [0,1]
    else:
        L = [n-1,n,n+1]
    for i in range(len(L)):
        cur = min(cur, abs(x+L[i]*d-(k-L[i])*d))
    print(cur)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

    # 普通に絶対値に近づくだけ
    sho = X // D
    if sho >= K:
        ans = abs(X - D * K)
    elif (K - sho) % 2 == 0:
        ans = abs(X % D)
    else:
        ans = abs(abs(X % D) - D)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

if x == 0:
    print(0)
    exit()

if x < 0:
    x = -x

p = (x + d - 1) // d

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

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(-1, 2):
    res = X + D*(K-2*(n+i))
    ans = min(ans, abs(res))
    #print(res)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

X, K, D = MAP()

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

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


K -= abs(X)//D
if X < 0:
	X += D*(abs(X)//D)
else:
	X -= D*(abs(X)//D)


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[12]:


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


# In[13]:


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


# In[ ]:

Submission status: Wrong Answer

Solution code:
import copy
import math
import time
import statistics
import math
import itertools
import bisect
from decimal import *

# a = get_int()
def get_int():
    return int(input())
# a = get_string()
def get_string():
    return input()
# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]
# a_list = get_string_list():
def get_string_list():
    return input().split()
# a, b = get_int_multi()
def get_int_multi():
    return map(int, input().split())
# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))
# print("{} {}".format(a, b))
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''

def main():

    start = time.time()

    x, k, d = get_int_multi()

    wk = k * d

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

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
def main():
    X, K, D = map(int, input().split())
    if abs(X) >= K * D:
        if X < 0:
            return X + K * D
        else:
            return X - K * D
    overzero_K = math.ceil(X/D)
    overzero_X1 = X + overzero_K * D if X < 0 else X - overzero_K * D
    overzero_X2 = overzero_X1 + D if overzero_X1 < 0 else overzero_X1 - D
    left_K = K - overzero_K
    if left_K % 2 == 1: return overzero_X2
    else: return overzero_X1

print(abs(main()))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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))
    if X <= 0:
        pos = D - (abs(X) % D)
        neg = - (abs(X) % D)
        if K % 2 == (abs(X) // D) % 2:
            print(abs(neg))
        else:
            print(pos)

Submission status: Wrong Answer

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    X, K, D = il()
    max_dist = K * D
    c = X // D
    if c < K:
        d = K - c
        if d % 2 == 0:
            print(abs(X - (d * D)))
        else:
            print(abs(X - ((d + 1) * D)))
    else:
        print(abs(X - max_dist))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(x) > k * d:
    if x < 0:
        print(x + k * d)
        exit()
    else:
        print(x - k * d)
        exit()

a = abs(x) % d
s = 1
if x < 0:
    s = -1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

passSinaiCount = abs(x)//d

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

passSinaiDistance = x - passSinaiCount * d
passSuruDistance = d - passSinaiDistance

nokoriCount = k - passSinaiCount
if nokoriCount % 2 == 0:
    print(passSinaiDistance)
else:
    print(passSuruDistance)

Submission status: Wrong Answer

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

Xmin = X - D*K
Xmax = X + D*K

if Xmin * Xmax <= 0:
    ans = min(X%(2*D), 2*D - X%(2*D))
elif Xmin > 0:
    ans = Xmin
else:
    ans = -Xmax

print(ans)

Submission status: Wrong Answer

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

nk = abs(x) // d
if k <= nk:
    if x >= 0:
        x -= d * k
    else:
        x += d * k
    print(x)
    exit(0)

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

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

Submission status: Wrong Answer

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

r = x%d
l = r-d

lc = (x-l)//d
rc = (x-r)//d

ans = abs(x-d*k)
if(k>=lc):
    if((k-lc)%2): ans = min(ans, min(abs(l+d), abs(l-d)))
    else: ans = min(ans, abs(l))
if(k>=rc):
    if((k-rc)%2): ans = min(ans, min(abs(r+d), abs(r-d)))
    else: ans = min(ans, abs(r))
print(ans)

Submission status: Wrong Answer

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

if abs(X) < K * D:
    k = abs(X) // D
    if k % 2 != K % 2:
        k += 1

    t1 = abs(abs(X) - D * k)
    t2 = abs(abs(X) - D * (k - 1))
    t3 = abs(abs(X) - D * (k + 1))

    print(min(t1, t2, t3))

else:
    t1 = abs(abs(X) - K * D)
    if K == 1:
        t2 = 10 ** 9
    else:
        t2 = abs(abs(X) - (K - 2) * D)
    print(min(t1, t2))

Submission status: Wrong Answer

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


if x >= 0:
    if k % 2 != 0:
        ans = x - d
    else:
        ans = x
else:
    if k % 2 != 0:
        ans = x - d
    else:
        ans = x

print(abs(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
x,k,d=map(int, input().split())

for i in range(k):
    if x > 0:
        if np.sign(x) != np.sign(x-d):
            print(np.abs(x))
            exit()
        x = x - d
    elif x < 0:
        if np.sign(x) != np.sign(x+d):
            print(np.abs(x))
            exit()
        x = x + d
    elif x == 0:
        if k % 2 != 0:
            print(0)
            exit()
        else:
            print(d)
            exit()
print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(k):
  for j in [d,-d]:
    cmp = min(cmp,abs(x+j))
    
print(cmp)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
n = (-x+k*d)//(2*d)
cur = float('inf')
if n < 0:
    print(x-k*d)
else:
    if n == 0:
        L = [0,1]
    else:
        L = [n-1,n,n+1]
    for i in range(len(L)):
        cur = min(cur, abs(x+L[i]*d-(k-L[i])*d))
    print(cur)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

aX = abs(X)
if aX >= K * D:
    ans = aX - K * D
else:
    x = aX % D
    k = aX // D
    ans = x if k % 2 == 0 else D - x
print(ans)

Submission status: Wrong Answer

Solution code:

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
n=max((k*d-x)//(2*d),0)
print(min(abs(x+2*n*d-k*d),abs(x+2*(n+1)*d-k*d)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


if x == 0:

    if k % 2 == 0:
        ans = 0
    else:
        ans = d
elif x > 0:
    amari = x % d
    kouho1 = amari
    kouho2 = amari - d
    kyori1 = abs(x - kouho1)
    kyori2 = abs(x - kouho2)
    if kyori1 // d > k:
        ans = x - k * d

    elif (kyori1 // d) % 2 == 1:
        ans = abs(kouho2)
    else:
        ans = abs(kouho1)
else:
    x *= -1
    amari = x % d
    kyori1 = abs(x - amari)
    kyori2 = abs(x - (amari - d))
    if kyori1 // d > k:
        ans = abs(x - k * d)
    elif (kyori1 // d) % 2 == 1:
        ans = abs(amari - d)
    else:
        ans = abs(amari)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

X, K, D = LI()

"""
X 
K回数 D移動
"""
if X >= 0:
    tmp = X - D * K
    if tmp <= 0:
        if (X - X % D) // D % 2 == K % 2:
            ans = X % D
        else:
            ans = abs(X % D - D)
    else:
        ans = tmp
else:
    tmp = X + D * K
    if tmp >= 0:
        if (X - X % D)//D % 2 == K % 2:
            ans = X % D
        else:
            ans = abs(X % D - D)
    else:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = [int(i) for i in input().split()]

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

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

	koko = abs(X-D)
	if koko == 0:
		if K % 2 == 0:
			print(abs(D))
			sys.exit()
		else:
			print(0)
			sys.exit()


	ima = abs(X-0)

	if ima == 0:
		if K % 2 == 0:
			print(0)
			sys.exit()
		else:
			print(abs(D))
			sys.exit()

	hituyou = ima // D
	saidai = ima % D

	if hituyou <= K:
		print(saidai)
		sys.exit()
	else:
		ido = K * D
		ans = ima - ido
		print(ans)
		sys.exit()


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
for i in range(k):
  if x>0:
    x-=d
  else:
    x+=d
print(x)

Submission status: Wrong Answer

Solution code:
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))
    if X <= 0:
        pos = D - (abs(X) % D)
        neg = - (abs(X) % D)
        if K % 2 == (X // D) % 2:
            print(abs(neg))
        else:
            print(pos)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def solve():
    if X > K*D:
        return X - K*D
    elif -1*X > K*D:
        return K*D - X
    elif X >= 0:
        first = X // D
        rem = K - first
        if rem % 2 == 0:
            return X % D
        else:
            return D - (X % D)
    else: # X < 0
        first = (-1*X) // D
        rem = K - first
        if rem % 2 == 0:
            return (-1 * X) % D
        else:
            return D - (-1 * X) % D

print(solve())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = (int(x) for x in input().split())
num = int(X/D)
ans = 0
if num >= K:
    ans = X - D*K
else:
    a = X - num*D
    b = X - (num+1)*D
    if (K-num)%2 == 0:
        ans = a
    else:
        ans = b
print(abs(ans))

Submission status: Wrong Answer

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

bigger = 0
smaller = 0

if x >= 0:
    bigger = x - min(x // d, k) * d
    smaller = bigger - d
else:
    smaller = x + min((-x) // d, k) * d
    bigger = smaller + d

if abs(x) // d >= k:
    if x >= 0:
        print(bigger)
    else:
        print(smaller)
else:
    k -= abs(x) // d
    bigger = abs(bigger)
    smaller = abs(smaller)

    if x >= 0:
        if k % 2 == 0:
            print(bigger)
        else:
            print(smaller)
    else:
        if k % 2 == 0:
            print(smaller)
        else:
            print(bigger)

Submission status: Wrong Answer

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

if x - k * d >= 0:
    print(x - k * d)
elif x + k * d <= 0:
    print(x + k * d)
else:
    dis = x % d
    dis_p = d - dis
    c = k - x // d

    if c % 2 == 0:
        print(dis)
    else:
        print(dis_p)