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: Runtime Error

Solution code:
X,K,D=map(int, input().split())
X=abs(X)
if X-K*D>0:
  print(X-K*D)
else:
  if K%2==0:
    s=X-2*D*int(X/(2*D))
    t=-(s-2*D)
    if s<t:
      print(s)
    else:
      print(t)
  else:
    s=X-2*D*int((X-D)/(2*D))+D
    t=-(s-2*D)
    r=s-2D
    print(min(s,t,r))

Submission status: Runtime Error

Solution code:
X = int(input())
K = int(input())
D = int(input())
loc = 0
lis = []

def calucu_min(X,K,D):
    for num in range(K) :
        lis.append( abs(abs(2 *D *num -K *D) - abs(X)))
        lis_unique = set(lis)
        return (max(lis_unique))

calucu_min(X,K,D)

Submission status: Runtime Error

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

walk = min(k,x//d)
k -= walk

x = abs(x)

if x > 0:
    x -= walk*d

    if abs(x - d) < x and k > 0:
        k -= 1
        x -= d

    if k % 2 == 0:
        ans = abs(x)
    else:
        ans = min(abs(x-d),abs(x+d))

print(ans)

Submission status: Runtime Error

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

n=0

x_abs=abs(x)

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

else:
    while n>=0:
        n=x_abs-(d*i)
    if (i-1)%2==k%2:
        print(abs(n))
        break
    else:
        print(x_abs-(d*(i-2)))
        break

Submission status: Runtime Error

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

n=0

x_abs=abs(x)

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

else:
    while n>=0:
        n=x_abs-(d*i)
    else:
        if (i-1)%2==k%2:
            print(abs(n))
            break
        else:
            print(x_abs-(d*(i-2)))
            break

Submission status: Runtime Error

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

if x%d==0:
    minNear  = x%d + d
    minFar = x%d
    minTransitionToNear = (x-minNear)/d
else:
    minNear  = x%d
    minFar = x%d - d
    minTransitionToNear = (x-minPlus)/d
    
if k<minTransitionToNear:
    print(x-k*d)
else:
    if (k-minTransitionToNear)%2==0:
        print(minNear)
    else:
        print(minFar)

Submission status: Runtime Error

Solution code:
X,K,D=(int(x) for x in input().split())
x=abs(X)
a=x//D

b=x-a*D
B=abs(x-(a+1)*D)

if b>B:
  a=a+1

if a>=K:
  print(abs(x-K*D))

else:
  c=K-a
  if c%2 == 0:
    print(abs(x-a*D))
  elif:
    d=abs(x-(a-1)*D)
    e=abs(x-(a+1)*D)
    print(min(d,e))

Submission status: Runtime Error

Solution code:
X, K, D = map(int,input().split())
import math
f = math.fabs 
def solve(x,k,d):
    if k%2 ==0:
        if k == 0:
            return int(f(x))
        elif f(x-2*d) > f(x) and f(x+2*d) > f(x):
            return int(f(x))
        elif k%4 == 0:
            return int(min(solve(x-k//2*d,k//2,d),solve(x+k//2*d,k//2,d)))
        elif k%4 != 0:
            return int(min(solve(x-(k+2)//2*d,(k-2)//2,d),solve(x+(k+2)//2*d,(k-2)//2,d)))
    elif k%2 != 0:
        if k == 1:
            return int(min(f(x-d),f(x+d)))
        else:
            return int(min(solve(x-(k+1)//2*d,(k-1)//2,d),solve((x+(k+1)//2*d,(k-1)//2,d))))
print(solve(X,K,D))

Submission status: Runtime Error

Solution code:
X, K, D = map(int,input().split())
import math
f = math.fabs 
def solve(x,k,d):
    if k%2 ==0:
        if k == 0:
            return f(x)
        elif f(x-2*d) > f(x) and f(x+2*d) > f(x):
            return f(x)
        elif k%4 == 0:
            return min(solve(x-k//2*d,k//2,d),solve(x+k//2*d,k//2,d))
        elif k%4 != 0:
            return min(solve(x-(k+2)//2*d,(k-2)//2,d),solve(x+(k+2)//2*d,(k-2)//2,d))
    elif k%2 != 0:
        if k == 1:
            return min(f(x-d),f(x+d))
        else:
            return min(solve(x-(k+1)//2*d,(k-1)//2,d),solve((x+(k+1)//2*d,(k-1)//2,d)))
print(solve(X,K,D))

Submission status: Runtime Error

Solution code:
Coodinate, K, Distance = map(int, input().split())

dp = [0]*(K+1)
dp[0] = Coodinate
#dp[1] = min( abs(dp[0]+Coodinate), abs(dp[0]-coodinate) )
count = 0
for i in range(1,K+1):
    dp[i] = min( abs(dp[i-1]+Distance), abs(dp[i-1]-Distance) )
    #if dp.count(dp[i]) == 2:
    #    break
    #else:
    #    count += 1
#print(count)
#print(dp)
print(dp[K])

Submission status: Runtime Error

Solution code:
from functools import lru_cache
def walkingT(x, k, d):
    @lru_cache(None)
    def move(x, k, d):
        if k==0:
            return abs(x);
        move1 = move(x-d, k-1, d)
        move2 = move(x+d, k-1, d)
        return min(abs(move1), abs(move2))
    
    return move(x, k, d)


inp = input();
data = inp.split(' ')
x = int(data[0])
k = int(data[1])
d = int(data[2])
print(walkingT(x, k, d))

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
for i in range(K):
  if X > 0:
    X -= D
  else:
    X += D
    
print(ans(X))

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
for i in range(K):
  if X > 0:
    X -= D
  else:
    X += D
    
print(point)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys

def input(): return sys.stdin.readline().rstrip("\r\n")
def List(): return list(map(int, input().split()))
def Num(): return int(input())


x, k, d = List()
x = abs(x)
if k * d > x:
    point1 = x - (x // d) * d
    point2 = point1 - d
    travel_to_point1 = (x - point1) // d
    travel_to_point2 = (x - point2) // d
    pair = [[abs(point1), abs(travel_to_point1)], [abs(point2), abs(travel_to_point2)]]
    pair.sort(key = lambda i: i[0])
    # print(pair)
    if pair[0][1] != 0 or pair[1][1] != 0:
        if k % pair[0][1] == 0:
            if pair[0][1] != 1:
                print(pair[0][0])
            else:
                if k & 1:
                    print(pair[0][0])
                else:
                    print(pair[1][0])
        else:
            if pair[1][1] != 1:
                print(pair[1][0])
            else:
                if k & 1:
                    print(pair[1][0])
                else:
                    print(pair[0][0])
    else:
        if pair[0][1] == 0:
            if k % 2 == 0:
                print(pair[0][0])
            else:
                print(pair[1][0])
        else:
            if k % 2 == 0:
                print(pair[1][0])
            else:
                print(pair[0][0])

else:
    print(x - k * d)

Submission status: Runtime Error

Solution code:
import sys

def input(): return sys.stdin.readline().rstrip("\r\n")
def List(): return list(map(int, input().split()))
def Num(): return int(input())


x, k, d = List()
x = abs(x)
if k * d > x:
    point1 = x - (x // d) * d
    point2 = point1 - d
    travel_to_point1 = (x - point1) // d
    travel_to_point2 = (x - point2) // d
    pair = [[abs(point1), abs(travel_to_point1)], [abs(point2), abs(travel_to_point2)]]
    pair.sort(key = lambda i: i[0])
    # print(pair)
    if k % pair[0][1] == 0:
        if pair[0][1] != 1:
            print(pair[0][0])
        else:
            if k & 1:
                print(pair[0][0])
            else:
                print(pair[1][0])
    else:
        if pair[1][1] != 1:
            print(pair[1][0])
        else:
            if k & 1:
                print(pair[1][0])
            else:
                print(pair[0][0])

else:
    print(x - k * d)

Submission status: Runtime Error

Solution code:
X = input()
K = input()
D = input()
x = int(X)
k = int(K)
d = int(D)
p = k*d-x
q = p//(2*d)
r = p/(2*d)
if r <= 0:
    print(-p)
elif 0 < r < k:
    s = min(p-2*d*q,-p+2*d*(q+1))
    print(s)
else:
    print(x+k*d)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
x,k,d = [int(i) for i in input().split()]
for i in range(k):
  x+=d if x<0 else -d
  if abs(x)<d:
    x=(abs(x)-d)*x/abs(x) if (k-1-i)&1==1 else x
    break
print(x)

Submission status: Runtime Error

Solution code:
X = input("X:")
K = input("K:")
D = input("D:")
x = int(X)
k = int(K)
d = int(D)
p = k*d-x
q = p//(2*d)
r = p/(2*d)
if r <= 0:
    print(-p)
elif 0 < r < k:
    s = min(p-2*d*q,-p+2*d*(q+1))
    print(s)
else:
    print(x+k*d)

Submission status: Runtime Error

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

a = [abs(x // d), x % d]

if a[0] > k:
   ans = min(abs(x+k*d) ,abs(x-k*d))
else:
    b = (k- a[0]) % 2
    ans = min(abs(a[1]+b*d) ,abs(a[1]-b*d))

print(ans))

Submission status: Runtime Error

Solution code:
x = int(input())
k = int(input())
d = int(input())

i = 0

while i!=k:
    if(x>0):
        x = x-d    
    else:
        x = x+d
    i = i+1
            
           
print(x)

Submission status: Runtime Error

Solution code:
x = int(input())
k = int(input())
d = int(input())

i = 0


    while i==d:
        if(x>0):
            x -= d
            i += 1
        else:
            x += d
            i += 1
            
            
    print(x)

Submission status: Runtime Error

Solution code:
X,K,D = map(int,input()split())
put = False
lst = []
for i in range(K):
    if abs(X+D) < abs(X-D):
        X += D
    else:
        X -= D
    if X in lst:
        check = lst[-2:]
        check = sorted(check)
        print(abs(check[0]))
        put = True
        break
    else:
        lst.append(X)
    K -= 1
if put == False:
    print(abs(X))

Submission status: Runtime Error

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

#0にどれだけ近づけるか？を考える

xx = abs(x) #初期座標を正としても一般性は失われない
n = xx // d 
v = xx - d * n
w = xx - d * (n + 1)

if n >= k:
    final_position = abs(xx - d * k)
elif n < k:
    if abs(v) < abs(w):
        remain = k - n 
        if remain % 2 == 0:
            final_position = abs(v)
        else:
            final_position = min(abs(v + d), abs(v - d))
    if abs(v) > abs(w):
        remain = k - (n + 1)
        if remain % 2 == 0:
            final_position = abs(w)
        else:
            final_position = min(abs(w + d), abs(w - d))

print(abs(final_position))

Submission status: Runtime Error

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

xx = abs(x)
n = xx // d 
v = xx - d * n
w = xx - d * (n + 1)

if n >= k:
    final_position = xx - d * k
elif n < k:
    if abs(v) < abs(w):
        remain = k - n
        if remain % 2 == 0:
            final_position = abs(v)
        else:
            final_position = min(abs(v + d), abs(v - d))
    if abs(v) > abs(w):
        remain = k - (n + 1)
        if remain % 2 == 0:
            final_position = abs(w)
        else:
            final_position = min(abs(w + d), abs(w - d))

print(abs(final_position))

Submission status: Runtime Error

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

xx = abs(x)
n = xx // d 
v = xx - d * n
w = xx - d * (n + 1)

if n >= k:
    final_position = xx - d * k
elif n < k:
    if abs(v) < abs(w):
        remain = k - n
        if remain % 2 == 0:
            final_position = abs(v)
        else:
            final_position = min(abs(v + d), abs(v - d))
    if abs(v) > abs(w):
        remain = k - (n + 1)
        if remain % 2 == 0:
            final_position = abs(w)
        else:
            final_position = min(abs(w + d), abs(w - d))

print(abs(final_position)

Submission status: Runtime Error

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

#0にどれだけ近づけるか？を考える

xx = abs(x) #初期座標を正としても一般性は失われない
n = xx // d 
v = xx - d * n
w = xx - d * (n + 1)

if n >= k:
    final_position = xx - d * k
elif n < k:
    if abs(v) < abs(w):
        remain = k - n 
        if remain % 2 == 0:
            final_position = abs(v)
        else:
            final_position = min(abs(v + d), abs(v - d))
    if abs(v) > abs(w):
        remain = k - (n + 1)
        if remain % 2 == 0:
            final_position = abs(w)
        else:
            final_position = min(abs(w + d), abs(w - d))

print(abs(final_position)

Submission status: Runtime Error

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

#0にどれだけ近づけるか？を考える

xx = abs(x) #初期座標を正としても一般性は失われない
n = xx // d 
v = xx - d * n
w = xx - d * (n + 1)

if n >= k:
    final_position = xx - d * k
elif n < k:
    if abs(v) < abs(w):
        remain = k - n 
        if remain % 2 == 0:
            final_position = abs(v)
        else:
            final_position = min(abs(v + d), abs(v - d))
    if abs(v) > abs(w):
        remain = k - (n + 1)
        if remain % 2 == 0:
            final_position = abs(w)
        else:
            final_position = min(abs(w + d), abs(w - d))

print(abs(final_position)

Submission status: Runtime Error

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

#0にどれだけ近づけるか？を考える

xx = abs(x) #初期座標を正としても一般性は失われない
num = xx // d 
v = xx - d * num
w = xx - d * (num + 1)

if num >= k:　#(x,k,d) = (100, 2, 5)のような場合
    final_pos = xx - d * k
elif num < k:
    if abs(v) < abs(w):　
        #例えば(7, 3, 4)の場合、num = 1　<　k　であり abs(7-4*1) = 3 > abs(7-4*(1+1))　= 1となる
        #他方(7, 3, 3)では、num = 2 < k　であるがabs(7-3*2) = 1 <  abs(7-3*(2+1)) =2となる
        remain = k - num #残り移動回数
        if remain % 2 == 0:
            final_position = abs(v)　#往復を繰り返して回数を消化
        else:
            final_position = min(abs(v + d), abs(v - d)) #あと一回分の移動をどうするか
    if abs(v) > abs(w):
        remain = k - (num + 1)
        if remain % 2 == 0:
            final_position = abs(w)
        else:
            final_position = min(abs(w + d), abs(w - d))

print(final_position)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

if (x >= 0) x -= d * hi;
else x += d * hi;

k -= hi;

if ( (k % 2) == 0):
  if (k == 0) :
    print(abs(x))
  else :
    print(min(abs(x), abs(x - d - d), abs(x + d + d)));
else :
  if (k == 1) :
    print(min(abs(x - d), abs(x + d)))
  else :
    print(min(abs(x - d), abs(x + d), abs(x + d + d + d), abs(x - d - d - d)));

Submission status: Runtime Error

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

x -= d * hi;
x = abs(x);
k -= hi;


if ( (k % 2) == 0):
  if (k == 0) :
    print(abs(x))
  else :
    print(min(abs(x), abs(x - d - d), abs(x + d + d));
else :
  print(min(abs(x - d), abs(x + d)))

Submission status: Runtime Error

Solution code:
n = int(input())
x = [int(w) for w in input().split()]

d = {}

for i in x:
    d[i] = d.get(i, 0) + 1
    
ans = 0
l = list(d.keys())
l = sorted(l)
#print(l)
for i in range(len(l)-2):
    for j in range(i+1, len(l)-1):
        for k in range(j+1, len(l)):
            if l[k] < l[i] + l[j]:
                #print(l[i], l[j], l[k])
                ans += d[l[i]]*d[l[j]]*d[l[k]]
            
            else:
                break
            
print(ans)

Submission status: Runtime Error

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >=0:
            x-=d

        k-=1
        elif abs(x-d)>=abs(x) and k%2==0:
            print(x)
            break
        elif abs(x-d)>=abs(x) and k%2!=0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and k%2==0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and k%2!=0:
            print(abs(x))
            break
        
    else:
        print(x)

Submission status: Runtime Error

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while x>=0 and k:
        k-=1
        prev = x
        x-=d
        
    print(min(abs(prev),abs(x)))

Submission status: Runtime Error

Solution code:
X, K, D = input().split()
X = abs(X)
Q, mod = divmod(X, D)
if K < Q:
    ans = X - K*D
else:
    Rest = (K - Q)%2
    if Rest == 0:
         ans = mod
    else:
         ans = D - mod
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
from numba import njit

X,K,D = map(int,input().split())
i = 0
@njit
def solve(i,x,d,k)
  while i < k:
    i = i + 1
    if x >= 0:
      x = x - abs(d)
    else:
      x = x + abs(d)
  return x

print(abs(solve(0,X,D,K)))

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
if (abs(X) - K * D) >= 0:
  print(abs(X) - K * D)
else:
  print(min(X%D, abs(X%D - D))

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input.split())
if (abs(X) - K * D) > = 0:
  print(abs(X) - K * D)
else:
  print(min(X%D, D-X%D))

Submission status: Runtime Error

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

print(x)

Submission status: Runtime Error

Solution code:
x,k,d = map(int, raw_input().split())
def f(x,k,d):
	if k == 0:
		return abs(x)

	if abs(x) <= d:
		if k % 2 == 0:
			if k == 0:
				return x
			else:
				return min(f(x+d,k-1,d), f(x-d,k-1,d))	
		else:
			return min(abs(x+d), abs(x-d))
	else:
		if x > d:
			if x/d <= k:
				return f(x - (x/d) * d, k - x/d, d)
			elif x / d > k:
				#get as close to zero as u can
				return f(x - k*d, 0, d)
		elif x < -d:
			return f(-x, k, d)

print f(x,k,d)

Submission status: Runtime Error

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
E=X%D
A=(X-E)/D
A=K-A
if X>=K*D:
  print(X-K*D)
eise:
  E=X%D
  A=(X-E)/D
  A=K-A
  A=A%2
  if A==0:
    print(E)
  eise:
    print(D-E)

Submission status: Runtime Error

Solution code:
import math
 
X,K,D = map(int, input().split())
tm = round(X/D)
if K <= tm:
  print(abs(X+D*K)if absabs(X+D*K) < abs(X-D*K)else abs(X-D*K))
else:
  if (K-tm)%2==1:
    print(abs(X-D*(tm+1)) if abs(X-D*(tm+1)) < abs(X-D*(tm-1))else abs(X-D*(tm-1)))
  else:
    print(abs(X+D*K)if absabs(X+D*K) < abs(X-D*K)else abs(X-D*K))

Submission status: Runtime Error

Solution code:
b=list(map(int,input().split()))
X=int(b[0])
K=int(b[1])
D=int(b[2])
X=abs(X)
for i in range(round(X/D),K+1):
    if abs(X-D*i+D*(K-i))<a:
        a=X-D*i+D*(K-i)

print(a)

Submission status: Runtime Error

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

if x==0:
	if k%2==1:
		print(d)
		exit()
	if k%2==0:
		print(0)
		exit()
if x==d:
	if k%2==1:
		print(0)
		exit()
	if k%2==0:
		print(x)
		exit()
if x<d:
	print(min(x,abs(x-d))
	exit()
if x>d:
  if x-(k*d)>0:
      print(x-(k*d))
      exit()
  if x-(k*d)<0:
      print(min(x%d,abs((x%d)-d)))
      exit()

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
L = range(int(K/2)+1)
M = range(int((K-1)/2)+1)
N = []
O = []

if X<0:
    X = -X

if K % 2 == 0:
    for num in L:
        N.append(abs(X - 2*D*num))      
    print(min(N))

else:
    for num in M:
        O.append(abs(X - 2*D*num - D)
    print(min(O))

Submission status: Runtime Error

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

if x==d:
	if k%2==1:
		print(0)
		exit()
	if k%2==0
		print(x)
		exit()
if x-(k*d)>0:
	print(x-(k*d))
	exit()
if x-(k*d)<0:
	print(min(x%d,abs((x%d)-d)))
	exit()

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
L = []
if K % 2 == 0:
    if X < 0:
        for num in range(int(K/2)):
            L.append(abs(X +2* D*num)) 
        print(min(L))
    else:
        for num in range(int( K/2)):
            L.append(abs(X -2* D*num))
        print(min(L))
else:
    if X < 0:
        for num in range(int((K-1)/2)):
            L.append(abs(X + 2*D*num + D))   
        print(min(L))
    else:
        for num in range(int((K-1)/2)):
            L.append(abs(X - 2*D*num - D))
    print(min(L))

Submission status: Runtime Error

Solution code:
x,k,d=map(int, input().split())
if abs(x-d)<abs(x+d):
    a=x-d
if abs(x-d)>abs(x+d):
    a=x+d

for i in range(k-1):
    if abs(a-d)<abs(a+d):
        a=a-d
    if abs(a-d)>abs(a+d):
        a=a+d
print(a)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
    x,k,d=map(int,input().split())
     
    if (x//d)<=k:
      k-=x//d
      x-=(x//d)*d
    else:
      x-=d*k
      print(abs(x))
      exit()
     
    for i in range(k):
      tmp=x
      x-=d
      if abs(x) >= abs(tmp):
        if (k-i)%2==0:
          print(abs(tmp))
          exit()
        else:
          print(abs(x))
          exit()
    else:
      print(abs(x))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
t = int(input())

for i in range(t):
    n, k, d = map(int, input().split())
    n = abs(n)
    ok = n // d
    left = k - (ok)
    if left % 2:
        print(d - (n%d))
    else:
        print(n%d)

Submission status: Runtime Error

Solution code:
x,k,d = map(int,input().split())
cs = [x]
for i in range(k):
    if(abs(x-d)<abs(x+d)):
        x = x - d
    else:
        x = x + d
    cs.append(x)
    if (abs(cs[-1]) > abs(cs[-2])):
        if ((k - i - 1) % 2 != 0):
            res = abs(cs[-2])
        else:
            res = max(x+d,x-d)

print(res)

Submission status: Runtime Error

Solution code:
x,k,d = map(int,input().split())
cs = [x]
for i in range(k):
    if(abs(x-d)<abs(x+d)):
        x = x - d
    else:
        x = x + d
    cs.append(x)
    if (abs(cs[-1]) > abs(cs[-2])):
        if ((k - i - 1) % 2 != 0):
            res = abs[cs[-2]]
        else:
            res = max(x+d,x-d)
        
print(res)

Submission status: Runtime Error

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

pos_list = []

def pos(x, k, d):
    if k > 0:
        pos(x + d, k - 1, d)
        pos(x - d, k - 1, d)
    else:
        pos_list.append(abs(x))

def main():
    x, k, d = map(int, input().split())
    pos(x + d, k - 1, x)
    pos(x - d, k - 1, x)
    pos_list.sort()
    print(pos_list[0])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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:
      mod = abs(X) // D
      if (K-mod) % 2 == 0:
        print(abs(abs(X) - K * mod))
      else:
        print(abs(abs(X) - K * (mod + 1)))

Submission status: Runtime Error

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

if x>k*d:
    print(x-k*d)
else:
    k-=x//d
    x-=(d*(x//d))
    and=[x,abs(d-x)]
    print(ans[k%2])

Submission status: Runtime Error

Solution code:
q=list(map(int,input().split()))
x=q[0]
d=q[2]
k=q[1]
f=0
g=0
if x==0:
  print((k%2)*d)
if x>0:
  t=x-(d*k)
  if t<0:
    f=1
elif x<0:
  t=x+(d*k)
  if t>0:
    f=1
if f==0:
  print(t)
else:
  x=abs(x)
  l=x//d
  k=k-l
  a=x-(l*d)
  b=a-d
  if k%2==0:
    print(a)
  else:
    print(b)

Submission status: Runtime Error

Solution code:
line = input()

strL = line.split()
X = int(strL[0])
K = int(strL[1])
D = int(strL[2])

mod = X % D
div = int(X / D)

if div > K:
    ans = abs(X - K * D)
elif (K - div) % 2 != 0:
    if mod > 0:
        ans = mod - D
    else:
        ans = mod + D

print(int(abs(ans)))

Submission status: Runtime Error

Solution code:
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
    if b==0:

        return a
    else:
        return gcd(b,a%b)
def power(x, p,m):
    res = 1
    while p:
        if p & 1:
            res = (res * x) % m
        x = (x * x) % m
        p >>= 1
    return res
def inar():
    return [int(k) for k in input().split()]

def lcm(num1,num2):
    return (num1*num2)//gcd(num1,num2)

def main():
    #t=int(input())
    t=1
    for _ in range(t):

        #n=int(input())
        x,k,d=inar()
        #arr=inar()
        if (x//d)>k:
            print(x-k*d)
        else:
            value=x % d
            k-=(x//d)
            if k % 2==0:
                print(value)
            else:
                print(min(abs(value-d),abs(value+d))



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

if __name__ == "__main__":
    main()
    #threadin.Thread(target=main).start()

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second
#define vll vector<ll>
#define pii pair<ll,ll>
#define sz(v) (v).size()
#define srt(v) sort(v.begin(),v.end())
#define ull unsigned long long
#define tell(a) cout<<a<<" "
#define el cout<<endl
#define rev(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ld long double
#define mem(a,b) memset(a,b,sizeof(a) )
#define M 4000010
#define md 1000000007

ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){   if (y & 1)res = (res*x) % p;y = y>>1;
x = (x*x) % p;}return res;}
/*inline ll add(ll a,ll b){return (a+b)%md;}
inline ll mul(ll a,ll b){return (a*b)%md;}
ll f[M],iv[M];
ll C(ll n,ll r){return mul(f[n],mul(iv[r],iv[n-r]));}
void prep_fac(){
    f[0]=1;
    rep(i,1,M)f[i]=mul(i,f[i-1]);
    iv[M-1]=power(f[M-1],md-2,md);
    for(ll i=M-2;i>=0;--i)iv[i]=mul(i+1,iv[i+1]);
}*/



int main()
{
	IOS;
	ll x,k,d;
	cin>>x>>k>>d;
  x=abs(x);
	ll ans=x/(2*d)+k/2;
// 	cout<<ans<<" ";
	ll p=1e16;
	for(ll i=max(0LL,ans-1000);i<=ans+1000;i++){
		if(i>=0&&i<=k){
			ll temp=x+d*(k-2*i);
			temp=abs(temp);
			p=min(p,temp);
		}
	}
	cout<<p;
	
}

Submission status: Runtime Error

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

zx=x
if abs(x)<d*k:
    wk,x=divmod(abs(x),d)
    k-=wk    
else:
    abs(x)-=d*k
    k=0

for i in range(k):
    xm=abs(x+d)
    xu=abs(x-d)
    x=min(xm,xu)
    k-=1
    if zx+x==d:
        if k%2!=0:
            x=zx
            break
        else:
            break
    zx=x
    
print(x)

Submission status: Runtime Error

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

x,k,d = inpl()
x = abs(x)
if x - k*d >= 0:
    print(x - k*d)
    quit()
res1 = x - x//d*d0 
res2 = abs(x - (x//d+1) *d)
if (k-x//d)%2:
    print(res2)
else:
    print(res1)

Submission status: Runtime Error

Solution code:
import sys


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

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