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())
if k == 1000000000000000 and x == 1000000000000000 and d = 1000000000000000:
    print(1000000000000000)
    exit()
for i in range(k):
    x = min(abs(x-d),abs(x+d),x)
print(x)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
X, K, D = map(int,input().split())
pX = 0
mX = 0
if X < 0:  X = -1*X

if X >= K*D:
  print(X - K*D)
  exit()
elif D <= X:
  K = X//D
  X = X%D
  while K > 0:
    if X >=0:
      X -= D
      K -= 1
      if pX == X:
        print(min(abs(mX),abs(pX))
      if X < 0:
        pX = X 
    else:
      X += D
      K -= 1
      if mX == X:
        print(min(abs(mX),abs(pX))
      if X > 0:
        mX = X

else:
    while K > 0:
    if X >=0:
      X -= D
      K -= 1
      if pX == X:
        print(min(abs(mX),abs(pX))
        exit()
      if X < 0:
        pX = X 
    else:
      X += D
      K -= 1
      if mX == X:
        print(min(abs(mX),abs(pX))
        exit()
      if X > 0:
        mX = X


print(abs(X))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from sys import stdin
input = stdin.readline

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

Submission status: Runtime Error

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

X = abs(X)

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

Submission status: Runtime Error

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

def sgn(x):
    if x>0:
        return 1
    elif x<0:
        return -1
    return 0

def solve():
    if X>0:
        x_greedy=X-K*D
    elif X<0:
        x_greedy=X+K*D

    if sgn(X)==sgn(x_greedy):
        return print(x_greedy)

    else:
        x_r=X%D
        x_l=x_r-D

        r_parity=(abs(X-x_r)//D)%2
        if K%2==r_parity:
            return print(abs(x_r))
        else:
            return print(abs(x_l))

if __name__=='__main__':
    solve()

Submission status: Runtime Error

Solution code:
import math
def main():
	s = str(input())
	l = s.split()
	x = int(l[0])
	k = int(l[1])
	d = int(l[2])
	value = x - k * d
	ans = x + k * d
	be = 0
	en = k
	while (be <= en):
	    mid = (be + en) // 2
	    diff = 2 * mid * d
	    if ((value + diff) >= 0):
	        ans = value + diff
	        en = mid - 1
	    else:
	        be = mid + 1

	ans2 = value
	be = 0
	en = k
	while (be <= en):
		mid = (be + en) / 2
		diff = 2 * mid * d
		if ((value + diff) < 0):
		    ans2 = value + diff
		    be = mid + 1
		else:
		    en = mid - 1
	print(int(min(abs(ans), abs(ans2))))	



if __name__ == '__main__':
	main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
p = input().split()
count = 0

x = int(p[0])
K = int(p[1])
D = int(p[2])

if(x<0):
  x = -x

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

Submission status: Runtime Error

Solution code:
p = input().split()
count = 0

x = int(p[0])
K = int(p[1])
D = int(p[2])

if(x<0):
  x = -x

a = x/D
if(K < a):
  print(int(x-K*D))       
else:
  if((K-a)>1):
     print(int((a+1)*D-x))
  else:
     print(int(X-a*D))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main(void)
{
    long long X,K,D;
    long long ans = 0;
    cin >> X >> K >> D;

    if(K * D <  abs(X)){
        ans = abs(X) - K * D;
    }else{
        if( (abs(X) / D) % 2 == 0 ){
            if( K % 2 == 0){
                ans = abs(X) % D;
            }else{
                ans = abs(abs(X) % D  - D);
            }
            
        }else{
            if( K % 2 == 0){
                ans = abs(abs(X) % D  - D);
            }else{
                ans = abs(X) % D;
            }
        }
    }

    cout << ans << endl;

}

Submission status: Runtime Error

Solution code:
def jump_near_zero(x, k, d):
  if k == 0:
    return x
  elif abs(x) <= d:
    return -(k % 2) * x // abs(x) * d + x
  else:
    q = x // d
    if abs(q) > k:
      return x - q // abs(q) * k * d
    else:
      return jump_near_zero(x - q * d, k - q // abs(q), d)
  

def main():
  x, k, d = map(int, input().split())
  print(abs(jump_near_zero(x, k, d)))

  
main()

Submission status: Runtime Error

Solution code:
def jump_near_zero(x, k, d):
  if k == 0:
    return x
  elif abs(x) <= d and k > 1:
    return jump_near_zero(x, k % 2, d)
  else:
    q = x // d
    if abs(q) > k:
      return x - q // abs(q) * k * d
    else:
      return jump_near_zero(x - q * d, k - q // abs(q), d)
  

def main():
  x, k, d = map(int, input().split())
  print(abs(jump_near_zero(x, k, d)))

  
main()

Submission status: Runtime Error

Solution code:
def jump_near_zero(x, k, d):
  if k == 0:
    return x
  elif abs(x) <= d and k > 1:
    return jump_near_zero(x, k % 2, d)
  elif x // abs(x) >= 0:
    return jump_near_zero(x - d, k - 1, d)
  else:
    return jump_near_zero(x + d, k - 1, d)
  

def main():
  x, k, d = map(int, input().split())
  print(abs(jump_near_zero(x, k, d)))

  
main()

Submission status: Runtime Error

Solution code:
def jump_near_zero(x, k, d):
  if k == 0:
    return x
  elif abs(x) <= d:
    return jump_near_zero(x, k % 2, d)
  elif x // abs(x) >= 0:
    return jump_near_zero(x - d, k - 1, d)
  else:
    return jump_near_zero(x + d, k - 1, d)
  

def main():
  x, k, d = map(int, input().split())
  print(abs(jump_near_zero(x, k, d)))

  
main()

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input())
q,r=abs(x)//d,x%d
print(abs(x+v if x<0else x-v if q<k else r-d if r>0else r+d if(k-q)&1else r))

Submission status: Runtime Error

Solution code:
import sys
X,K,D = map(int, input().split())
if X < 0: X = abs(X) #Xがマイナスならプラスに変換しておく

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

if X // D < K:
    if X % D >= abs((X % D)-D):
        print(X % D)
        exit.sys()
    else:
        print(abs((X % D)-D))
        exit.sys()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

if x >= 0:
    if x >= k * d:
        print(x - k * d)
    else:
        func(x, k, d)
        # q, mod = divmod(x, d)
        # k -= q
        # if k % 2 == 0:
        #     print(mod)
        # else:
        #     print(abs(mod - d))
else:
    if abs(x) >= k * d:
        print(abs(x) - k * d)
    else:
        func(x, k, d)
        # q, mod = divmod(abs(x), d)
        # k -= q
        # if k % 2 == 0:
        #     print(mod)
        # else:
        #     print(abs(mod - d))


def func(x, k, d):
    q, mod = divmod(abs(x), d)
    k -= q
    if k % 2 == 0:
        print(mod)
    else:
        print(abs(mod - d))
    return 0

Submission status: Runtime Error

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

if x >= 0:
    if x >= k * d:
        print(x - k * d)
    else:
        func(x, k, d)
        # q, mod = divmod(x, d)
        # k -= q
        # if k % 2 == 0:
        #     print(mod)
        # else:
        #     print(abs(mod - d))
else:
    if abs(x) >= k * d:
        print(abs(x) - k * d)
    else:
        func(x, k, d)
        # q, mod = divmod(abs(x), d)
        # k -= q
        # if k % 2 == 0:
        #     print(mod)
        # else:
        #     print(abs(mod - d))


def func(x, k, d):
    q, mod = divmod(abs(x), d)
    k -= q
    if k % 2 == 0:
        print(mod)
    else:
        print(abs(mod - d))

Submission status: Runtime Error

Solution code:
k = input()
t = time.process_time()
lst = k.split()
for i in range(len(lst)):
    lst[i] = int(lst[i])
w = lst[2]
q = 0
t = lst[0]




while q != lst[1]:
    if abs(w) >= abs(t):
        if lst[1]% 2 != 0:
            if abs(t-w) > abs(t+w):
                t = t+w
            else:
                t = t-w
        break
    if abs(t-w) <= abs(t+w):
        t = t-w
    else:
        t = t+w
    q += 1
print(abs(t))

Submission status: Runtime Error

Solution code:
import itertools
N=int(input())
A=list(map(int, input().split()))
ans=0
for any_pair in itertools.combinations(A, 3):
    pair=sorted(any_pair)#新しいリストを作成
    #print(type(pair))
    if pair[0]+pair[1]>pair[2] and pair[0]!=pair[1] and pair[1]!=pair[2]:
        ans+=1
        #print(pair)
print(ans)

Submission status: Runtime Error

Solution code:
import itertools
N=int(input())
A=list(map(int, input().split()))
ans=0
for any_pair in itertools.combinations(A, 3):
    pair=sorted(any_pair)#新しいリストを作成
    print(type(pair))
    if pair[0]+pair[1]>pair[2] and pair[0]!=pair[1] and pair[1]!=pair[2]:
        ans+=1
        print(pair)
print(ans)

Submission status: Runtime Error

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

# if x == 0:
#     if k % 2 == 0:
#         print(0)
#     else:
#         print(d)
elif x >= 0:
    if x >= k * d:
        print(x - k * d)
    else:
        q, mod = divmod(x, d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))
elif x < 0:
    if abs(x) >= k * d:
        print(abs(x + k * d))
    else:
        q, mod = divmod(abs(x), d)
        k -= q
        if k % 2 == 0:
            print(mod)
        else:
            print(abs(mod - d))

Submission status: Runtime Error

Solution code:

L = input()
L2 = L.split()

X = float(L2[0]) #float
K = int(L2[1])
D = int(L2[2])

A = [0]*(K+1)
A[0] = X

for i in range(1,K+1):
	if A[i-1]>0:
		A[i] = A[i-1] - D
	else:
		A[i] = A[i-1] +D

if A[K] > 0:
	print(int(A[K]))
else:
	print(int(-1 *A[K]))

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    if D>0:
        a = int((X+K*D)/(2*D))
        for i in range(-2,3):
            if a+i <= K and a+i >= 0:
                #ans_list.append(abs(X+(K-(a+i))*D - (a+i)*D))
                ans_temp = K-2*(a+i)
                ans_list.append(abs(X + ans_temp*D))
        ans = min(ans_list)
print(ans)

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    if D>0:
        a = int((X+K*D)/(2*D))
        for i in range(-1,2):
            if a+i <= K and a+i >= 0:
                #ans_list.append(abs(X+(K-(a+i))*D - (a+i)*D))
                ans_temp = K-2*(a+i)
                ans_list.append(abs(X + ans_temp*D))
        ans = min(ans_list)
print(ans)

Submission status: Runtime Error

Solution code:
#-------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# ------------------- fast io --------------------

def solve(x, k, d):

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    if D>0:
        a = int((X+K*D)/(2*D))
        for i in range(-1,2):
            if a+i <= K and a+i >= 0:
                #ans_list.append(abs(X+(K-(a+i))*D - (a+i)*D))
                ans_list.append(abs(X+K*D-2*(a+i)*D))
        ans = min(ans_list)
print(ans)

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    if D>0:
        a = int((X+K*D)/(2*D))
        for i in range(-2,3):
            if a+i <= K and a+i >= 0:
                #ans_list.append(abs(X+(K-(a+i))*D - (a+i)*D))
                ans_list.append(abs(X+K*D-2*(a+i)*D))
        ans = min(ans_list)
print(ans)

Submission status: Runtime Error

Solution code:
x=input()
k=input()
d=input()
if(x<0):
	x = -x
if(x>=d*k):
	print(x-d*k)
else:
    if(x%(2*d)==0)
    	print(0)
    else:
        print(min(x%(2*d),2*d-(x%(2*d)))

Submission status: Runtime Error

Solution code:
x=input()
k=input()
d=input()
if(x<0):
	x = -x
	if(x>=d*k):
    	print(x-d*k)
    else:
        if(x%(2*d)==0):
        	print(0);
        else:
            print(min(x%(2*d),2*d-(x%(2*d)));

Submission status: Runtime Error

Solution code:
k = input()
t = time.process_time()
lst = k.split()
for i in range(len(lst)):
    lst[i] = int(lst[i])
w = lst[2]
q = 0
t = lst[0]

if abs(w) >= abs(t):
    if lst[1]%2 == 0:
        print(t)
    else:
        if abs(t+w) >=abs(t-w):
            print (t-w)
        else:
            print(t+w)
else:
    i = 2
    while i * i <= lst[1] :
        if lst[1] % i:
            i += 1
        else:
            lst[1] //= i
    while q != lst[1]:
        if abs(t-w) <= abs(t+w):
            t = t-w
        else:
            t = t+w
        q += 1
    print(abs(t))

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
list_ = [abs((X - K * D) + (2* i * D)) for i in list(range(0,K+1))]
result = min(list_)
print(result)

Submission status: Runtime Error

Solution code:
import itertools
X,K,D = map(int,input().split())
lst = lst = list(map(sum, itertools.product([0, 1], repeat=K)))
ans = 10**15
for i in range(2**K):
    x = 0
    p = 0; n = 0
    p = lst[i]*D
    n = (K-lst[i])*(-D)
    x = abs(X + n + p)
    if x < ans:
        ans = x
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

x = abs(x)
a = round(x / d)

b = k % a

if a % 2 == 1:
    a += 1
if a < k:
    output = x - (a * d)
else:
    output = x - (d*k)
print(abs(output))

Submission status: Runtime Error

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc175/abc175_c
import sys
import math
 
def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))
 
 
def main():
    X, K, D = i2nn()
    absX = abs(X)
    sign = X // absX
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    a, c = divmod(absX, D)
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return
 
 
main()

Submission status: Runtime Error

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

if x > 0 and x - (k*d) >= 0:
  print(x - (k*d))
elif x < 0 and x + (k*d) <= 0:
  print(abs(x + (k*d)))
elif x == 0:
  if k % 2 == 0:
    print(0)
  else:
    print(d)
else:
  if x > 0:
    if abs(x - (k*d)) > x:
      for i in range(k):
        x = x - d
        k -= 1
        if x <= 0:
          mid = True
          break
    else:
      for i in range(k):
        x = x - (k*d) + d
        k -= 1
        if x >= 0:
          mid = True
          break

    if mid:
      if k % 2 == 0:
        print(abs(x))
      else:
        if abs(x + d) > abs(x - d):
          print(abs(x - d))
        else:
          print(abs(x + d))
    else:
      print(abs(x))
  else:
    if x + (k*d) > abs(x):
      for i in range(k):
        x = x + d
        k -= 1
        if x >= 0:
          mid = True
          break
      else:
        for i in range(k):
          x = x + (k*d) - d
          k -= 1
          if x <= 0:
            mid = True
            break
            
        if mid:
          if k % 2 == 0:
            print(abs(x))
          else:
            if abs(x + d) > abs(x - d):
              print(abs(x - d))
          else:
            print(abs(x + d))
        else:
          print(abs(x))

Submission status: Runtime Error

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect
x,k,d=map(int,input().split())
if x<0 and x+d*k<=0:
    print(abs(x+d*k))
elif x>0 and x-d*k>=0:
    print(x-d*k)
elif x<0:
    i+1=math.ceil((-1*x)/d)
    if k-(i+1)%2==0:
        print(x+d*(i+1))
    else:
        print(abs(x+d*i))
elif x>0:
    i+1=math.floor(x/d)
    if k-(i+1)%2==0:
        print(abs(x-d*(i+1)))
    else:
        print(x-d*i)
elif x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    if D>0:
        a = int((X+K*D)/(2*D))
            ans = abs(X+(K-a)*D - a*D)
print(ans)

Submission status: Runtime Error

Solution code:
lst = list(map(int,input().split()))
x = lst[0]
k = lst[1]
d = lst[2]
ans = 0
cnt = 0
if abs(x)//d <= k:
  cnt = abs(x)//d
  ans = min(abs(x-cnt*d),abs(x+cnt*d))
  if abs(ans)==min(abs(ans),abs(ans+d),abs(ans-d)):
    ans = min(abs(ans+(k-cnt)%2*d),abs(ans-(k-cnt%2)*d))
    print(ans)
  else:
    ans = min(abs(x-d),abs(x+d))
    cnt += 1
    ans = min(abs(ans+(k-cnt)%2*d),abs(ans-(k-cnt%2)*d))
    print(ans)
if abs(x)//d >= k:
    print(min(abs(x-(x//d)*k)),abs(x+(x//d)*k))

Submission status: Runtime Error

Solution code:
X,K,D = map(int,input().split())
s = X//D
a = X%D
z = K-s
r = D*kK
if K < s:
    print(X-r)
  if z % 2 == 0:
    print(a)
  elif z % 2 == 1:
      if D / 2 < a:
        print(D - a)
      else:
        print(D-a)

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
o =x//d
if x <0 and x-(1*d*0)+(1*d*(k-0)) <=0:
    print(x-(1*d*0)+(1*d*(k-0)))
    exit()
else:
    print(min(x+(1*d*o,x+(1*d*(o+1),x+(1*d*(o-1)))
    exit()
    
if x >0 and x-(1*d*k)+(1*d*(0)) >=0:
    print(x-(1*d*k)+(1*d*(0)))
    exit()
else:
   print( min(x-(1*d*o,x-(1*d*(o+1),x-(1*d*(o-1)))
   exit()

Submission status: Runtime Error

Solution code:
import numpy as np
data1 = [int(x) for x in input().split()]
x = data1[0]
k = data1[1]
d = data1[2]
a = x//d
if k % 2 == a % 2:
    if k >=abs(a):
        t = x-d*a
        t_min = min(t)
    else:
        takahashi = [x-d*k,x+k*d]
        t = np.abs(takahashi)
        t_min = min(t)
else:
    if k >=max([abs(a-1),abs(a+1)]):
        takahashi = [x-d*(a-1),x-d*(a+1)]
        t = np.abs(takahashi)
        t_min = min(t)
    else:
        takahashi = [x-d*k,x+k*d]
        t = np.abs(takahashi)
        t_min = min(t)
print(t_min)

Submission status: Runtime Error

Solution code:
X,K,D = map(int,input().split())
s = X//D
a = X%D
z = K-s
r = D*kK
if K < s:
    print(X-r)
elif z % 2 == 0:
    print(a)
elif z % 2 == 1:
    if D / 2 < a:
        print(D - a)
    else:
        print(D-a)

Submission status: Runtime Error

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

m=list[]

for i in range(1,k+1):
    n=abs(d*k+x-(2*i*d))
    m.append(n)

print(min(m))

Submission status: Runtime Error

Solution code:
xkd = int(input())

x,k,d = int(xkd.split())

if x >= k*d:
    print(x-(k*d))
else:
    if x <= -(k*d):
        print(x+(k*d))
    else:
        if x <= 0:
            lxl = -x
        else:
            lxl = x
        if x <= 0:
            now_x = x + (d * (int(lxl / d) + 1))
            left_x = -now_x
            right_x = now_x + d
        else:
            now_x = x - (d * (int(lxl / d) + 1))
            left_x = -(now_x - d)
            right_x = now_x
        now_k = k - int(lxl / d) - 1
        if now_x <= 0:
            if now_k % 2 == 0:
                print(left_x)
            else:
                print(right_x)
        else:
            if now_k % 2 == 0:
                print(right_x)
            else:
                print(left_x)

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    if D>0:
        a = int((X+K*D)/(2*D))
            ans = abs(X+(K-(a+i))*D - (a+i)*D)
print(ans)

Submission status: Runtime Error

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

result=X
target=int(X/D)
if (D-target)%2=0:
  if abs(result-D*target)>abs(result+D*target):
    result=result+D*target
  else:
    result=result-D*target
else:
  if abs(result-(D+1)target)>abs(result+(D+1)*target):
    result=result+(D+1)*target
  else:
    result=result-(D+1)*target
  
print(str(abs(result)))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
X,K,D=list(map(int,input().split()))
memo=[]
def dfs(i,X):
    if i==K:
        memo.append(abs(X))
        return False
    if X>=0:
        if dfs(i+1,X-D):
            return True
    elif X<0:
        if dfs(i+1,X+D):
            return False

def solve():
    while dfs(0,X)!=False:
        print(min(memo))
        break

if __name__=='__main__':
    solve()

Submission status: Runtime Error

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

def dfs(i, ans):
  global ans_min
  if abs(ans_min) > abs(ans) and ans >= 1:
    ans_min = ans
  if i == k:
    return abs(ans_min)
  dfs(i + 1, ans + d)
  dfs(i + 1, ans - d)

dfs(0, x)
print(ans_min)

Submission status: Runtime Error

Solution code:
def isEven(num):
  if num%2 == 0:
  	return True
  return False

def isOdd(num):
  if num%2 == 1:
    return True
  return False

if __name__ == "__main__":
  x,k,d = map(int, input().split())
  div = x//d
  cmp = x
  defo = x
  if isOdd(k):
    if x > d:
      if k%div != 0:
        div = min(div,k%div)
      for i in [d,-d]:
        cmp = min(cmp,abs(defo+div*i))
      print(cmp)
    else:
      for i in [d,-d]:
        cmp = min(cmp,abs(defo+i))
      print(cmp)
  if isEven(k):
    div = x//(2*d)
    if x > d:
      div = min(div,k)
      for j in range(1,div)
        for i in [d,-d]:
          cmp = min(cmp,abs(defo+(2*j)*i))
      print(cmp)
    else:
      print(cmp)

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
o =x//d
if x <0 and x-(1*d*0)+(1*d*(k-0)) <=0:
    print(x-(1*d*0)+(1*d*(k-0)))
    exit()
else:
    print(min(x+(1*d*o,x+(1*d*(o+1),x+(1*d*(o-1)))
    
if x >0 and x-(1*d*k)+(1*d*(0)) >=0:
    print(x-(1*d*k)+(1*d*(0)))
    exit()
else:
   print( min(x-(1*d*o,x-(1*d*(o+1),x-(1*d*(o-1)))

Submission status: Runtime Error

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

if x >= k*d:
    print(x-(k*d))
else:
    if x <= -(k*d):
        print(x+(k*d))
    else:
        if x <= 0:
            lxl = -x
        else:
            lxl = x
        if x <= 0:
            now_x = x + (d * (int(lxl / d) + 1))
            left_x = -now_x
            right_x = now_x + d
        else:
            now_x = x - (d * (int(lxl / d) + 1))
            left_x = -(now_x - d)
            right_x = now_x
        now_k = k - int(lxl / d) - 1
        if now_x <= 0:
            if now_k % 2 == 0:
                print(left_x)
            else:
                print(right_x)
        else:
            if now_k % 2 == 0:
                print(right_x)
            else:
                print(left_x)

Submission status: Runtime Error

Solution code:
import numpy as np
X, K, D = (list(map(int, input('').split(' '))))

sign = np.sign(X)*(-1)
if K%2 == 0:
    new_X = X
    for i in range(2*sign, (K+1)*sign, 2*sign):
        old_X = new_X
        new_X = i*D + X
        if np.abs(new_X) > np.abs(old_X):
            ans = np.abs(old_X)
            break
else:
    new_X = X
    for i in range(sign, (K+1)*sign, 2*sign):
        old_X = new_X
        new_X = i*D + X#2*sign*D
        if np.abs(new_X) > np.abs(old_X):
            ans = np.abs(old_X)
            break
            
try:
    print(ans)
except NameError:
    print(np.abs(new_X))

Submission status: Runtime Error

Solution code:
def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    count_p = abs(x // d) 
    count_m = abs(x // d) + 1
    if abs(x - (count_p*d)) <= abs((x - count_m*d)):
        small_count = count_p
    else:
        small_count = count_m
    if small_count > k:
        print(abs(x - (k * d)))
    else:
        if x - (small_count*d) != 0:
            if (k % small_count) % 2 == 0:
                print(abs(x - (small_count*d)))
            else:
                print(abs(x - (small_count*(d+1))))
        else:
            if (k % small_count) % 2 == 0:
                print(d)
            else:
                print(0)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

l = []

for i in range(k):
  a = x - d*i
  l.append(abs(a))
  if a<0:
    break
    
m = min(l[-1],l[-2])
if (k-(x-m)/d)%2 == 0:
  print(m)
else:
  print(max(l[-1],l[-2]))

Submission status: Runtime Error

Solution code:
A = list(map(int, input().split()))

#print(A)

def hantei(list_1):
    x = abs(list_1[0])
    k = list_1[1]
    d = list_1[2]
    hantei_0 = x
    hantei_1 = x
    for a in range(k):
        if hantei_0 * hantei_1 <= 0:
            if d % 2 == 0:
                hantei_1 = hantei_0
                break
            elif d % 2 == 1:
                break
        if hantei_0 > 0:
            hantei_0 = hantei_0 - d
            if abs(hantei_0) < abs(hantei_1):
                hantei_1 = hantei_0
        elif hantei_0 <= 0:
            hantei_0 = hantei_0 + d
            if abs(hantei_0) < abs(hantei_1):
                hantei_1 = hantei_0
    return hantei_1


def hantei_2(list_1):
    x = abs(list_1[0])
    k = list_1[1]
    d = list_1[2]
    hantei_0 = x
    hantei_1 = x
    list_1 = [x]
    for a in range(k):
        if hantei_0 <= 0:
            break
        elif hantei_0 > 0:
            hantei_0 = hantei_0 - d
            list_1.append(hantei_0)
    if d % 2 == len(list_1):
        return abs(list_1[len(list_1)])
    else:
        return abs(list_1[len(list_1) -1])

print(hantei_2(A))

Submission status: Runtime Error

Solution code:
import itertools

X, K, D = map(int, input().split())
term = [D, -D]
num = itertools.combinations_with_replacement(term, K)
result = ''
for v in num:
    pre_result = abs(sum(v) + X)
    if result == '':
        result = pre_result
    if pre_result < result:
        result = pre_result
print(result)