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())
r = x//d
x = abs(x)
ans = 0
if r>k:
    ans = abs(x-k*d)
else:
    l = (k-r)%2
    if l==0:
        ans = abs(x-d*r)
    else:
        ans = abs(x-d*(r+1))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

l = 0
r = K
min_distance = abs(X)
diff_set = set()

while (l <= r):
    n = (r + l) // 2
    diff = (2 * n - K) * D + X
    if abs(diff) < min_distance :
        min_distance = abs(diff)

    if diff in diff_set:
        break
    else:
        diff_set.add(diff)

    if diff > 0:
        r = n
    else:
        l = n

print(min_distance)

Submission status: Wrong Answer

Solution code:
X , K , D = map(int, input().split())
first_ct = int(X/D)
second_posi = X - D*first_ct
leave_ct = K - first_ct
x = second_posi
for trans in range(leave_ct):
    foward_X = x + D
    back_X = x - D
    if abs(foward_X) > abs(back_X):
        x = back_X
    else:
        x = foward_X
print(abs(x))

Submission status: Wrong Answer

Solution code:
X , K , D = map(int, input().split())
first_ct = int(X/4)
second_posi = X - D*first_ct
leave_ct = K - first_ct
x = second_posi
for trans in range(leave_ct):
    foward_X = x + D
    back_X = x - D
    if abs(foward_X) > abs(back_X):
        x = back_X
    else:
        x = foward_X
print(abs(x))

Submission status: Wrong Answer

Solution code:
import itertools

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

l = 0
r = K - 1
min_distance = X
diff_set = set()

while (l <= r):
    n = (r + l) // 2
    diff = (n - K) * D + X
    if abs(diff) < min_distance :
        min_distance = abs(diff)

    if diff in diff_set:
        break
    else:
        diff_set.add(diff)

    if diff > 0:
        r = n
    else:
        l = n

print(min_distance)

Submission status: Wrong Answer

Solution code:
x,k,d=input().split()
x=abs(int(x))
k=int(k)
d=int(d)
a=x//d
b=x-a*d
c=(x-b)/d
i=int(c)
if k<=i:
  print(abs(x)-d*k)
  print('a')
else:
  if (k-(i))%2==0:
    print(abs(x)-d*(i))
    print('b')
  else:
    print(abs(abs(x)-d*(i+1)))
    print('c')

Submission status: Wrong Answer

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

if X == 0:
    res = 0 if K%2 == 0 else D
    print(res)
    exit()
count = abs(X) // D
pseudo = np.sign(X) * (abs(X) - count * D)

if abs(X) >= K * D:
    res = abs(X - K * D)
elif (K-count) % 2 == 0:
    res = abs(pseudo)
else:
    res = min(abs(pseudo + D), abs(pseudo - D))

print(res)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
def sign(x):
    if x> 0:return 1
    elif x<0 :return -1
    return 0

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

if sign(x)==sign(x_distance):
    print(x_distance)
    exit()

x_r= x_distance % d
x_l=x_r -d
kairi=(abs(x-x_r)//d) %2

if k%2==kairi:
    print(abs(x_r))
else:print(abs(x_l))

Submission status: Wrong Answer

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

count = abs(X) // D
pseudo = np.sign(X) * (abs(X) - count * D)

if abs(X) >= K * D:
    res = abs(X - K * D)
elif (K-count) % 2 == 0:
    res = abs(pseudo)
else:
    res = min(abs(pseudo + D), abs(pseudo - D))

print(res)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
def sign(x):
    if x> 0:return 1
    elif x<0 :return -1
    return 0

if x>0:
    x_distance = x - k*d
else:
    x_distance = -x - k*d

if sign(x)==sign(x_distance):
    print(x_distance)
    exit()

x_r= x_distance % d
x_l=x_r -d
kairi=(abs(x-x_r)//d) %2

if k%2==kairi:
    print(abs(x_r))
else:print(abs(x_l))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a=list(map(int,input().split()))
if a[0]<0:
    a[0]=-a[0]
x=a[0]%a[2]
y=a[0]/a[2]
if y>=a[1]:
    print(a[0]-a[1]*a[2])
elif (a[1]-y)%2==1:
    print(a[2]-x)
else:
    print(x)

Submission status: Wrong Answer

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

N = X // D

base = X - min(N, K) * D

if K <= N:
    res = base
else:
    if (K - N) % 2 == 0:
        res = min(base, abs(base - 2 * D))
    else:
        res = min(abs(base - D), base + D)

print(res)

Submission status: Wrong Answer

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

def calcu(x,k,D, func):
    if x == 0:
        if k%2 == 0:
            return 0
        else:
            return D

    a = int(x/D)
    if k <= a:
        x = func(x, k*D)
        return x
    else:
        x1 = func(x, a*D)
        x2 = func(x1,D)
        k = k - a
        if k%2 == 0:
            return x1
        else:
            return abs(x2)

        
def plus(x,y):
    return x+y

def minus(x,y):
    return x-y


if X >= 0:
    print(calcu(X,K,D,minus))
else:
    print(calcu(X,K,D,plus))

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math
INT_MIN=-10**13
import bisect as B
###############################################################################\
x,k,d=INPUT()
if x<0:
    if k*d+x<=0:
        print(k*d+x)
    else:
        p=x//d
        neg=abs(p*d+x)
        pos=abs((p+1)*d)
        if neg<pos:
            if (k-p)%2==0:
                print(neg)
            else:
                print(pos)
        else:
            if (k-p-1)%2==0:
                print(pos)
            else:
                print(neg)
elif x==0:
    if k%2==0:
        print(0)
    else:
        print(d)
else:
    if x-k*d>=0:
        print(x-k*d)
    else:
        p=x//d
        pos=x-p*d
        neg=abs(x-(p+1)*d)
        if pos<neg:
            if (k-p)%2==0:
                print(pos)
            else:
                print(neg)
        else:
            if (k-p-1)%2==0:
                print(neg)
            else:
                print(pos)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ml = min(x//d, k)
mu = min(ml+1, k)
diff_ml = k - ml
diff_mu = k - mu
print(x-ml*d if diff_ml%2 == 0 else mu*d-x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(X) < D:
    if K % 2 == 0:
        ans = abs(X)
    else:
        ans= abs(abs(X) - 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

print(ans)

Submission status: Wrong Answer

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

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

if(X/D >= K):
  print(X-D*K)
else:
  if(((X-min_pos)/D)%2 == 0):
    print("A", min_pos)
  else:
    print(abs(min_neg))

Submission status: Wrong Answer

Solution code:
import math
import sys
#####sorting a dictionary by the values#####
def dict_sort(ans):
    ans=sorted(ans.items(),reverse=True,key=lambda kv:(kv[1]))
##### naive method for testing prime or not#####
def is_prime(n):
    if n==1:
        return 0
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
#####greatest common divisor of two numbers#####
def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
#####least common multiplyer of two numbers#####
def lcm(a,b):
    return (a*b)//gcd(a,b)
#####function that return all the letters#####
def alphbates():
    return "abcdefghijklmnopqrstuvwxyz"
#####binary search#####
def binary_search(ls,n,flag):
    low=0
    hi=n-1
    while(low<=hi):
        mid=(low+hi)//2
        if ls[mid]==flag:
            return mid
        elif ls[mid]>flag:
            hi=mid-1
        else:
            low=mid+1
    return -1
#####taking an array/list as input#####
def inp():
    ls=list(map(int,input().split()))
    return ls
#####taking multiple inputs#####
def mult_inp():
    return map(int,input().split())
#####Main function starts from here#####
x,k,d=mult_inp()
if x==k==d:
    if x%2==0:
        print(x)
    else:
        print(0)
elif x==0:
    if d%2==0:
        print(0)
    else:
        print(k)
elif x>0:
    if x//d>k:
        print(x-k*d)
    else:
        print(min(abs(x-math.floor(x/d)*d),abs(x-math.ceil(x/d)*d)))
else:
    x=x*-1
    if x//d>k:
        print(x-k*d)
    else:
        print(min(abs(x-math.floor(x/d)*d),abs(x-math.ceil(x/d)*d)))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  k=k-sa
  for i in range(k):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=ans if (k-i)%2==0 else bk1
        break
      else:
        bk3=max(mansbk,pansbk) 
    else:
      ans=0 if (k-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  k=k-sa
  for i in range(k):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=ans if (k-i)%2==1 else bk1
        break
      else:
        bk3=max(mansbk,pansbk) 
    else:
      ans=0 if (k-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

Submission status: Wrong Answer

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

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

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

Submission status: Wrong Answer

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

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

if(X/D > K):
  print(X-D*K)
else:
  if(int(X/D)%2 == 0):
    print(int(X/D))
    print("A", min_pos)
  else:
    print(int(X/D))
    print("B", abs(min_neg))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
if x-k*d>=0:
  print(x-k*d)
  exit()
x%=(2*d)
if k&1:
  x=min(x+d,abs(x-d))
print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x , k , d = list( map( int , input().split() ))
if x // d <= k:
  print( min( x%d , d -  x%d ))
else:
  print( x - k *d )

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
X=abs(X)
if D>=X and K%2==0:
    ans=X
elif D>=X and K%2==1:
    ans=D-X
elif D<X and K*D<X:
    ans=X-K*D
else:
    ans=min(X,K*D-X)%D
    for i in range(0,K):
        ans=ans-1
        if ans<0 and (K-i)%2==0:
            ans=ans+1
            ans=ans*D
            break
        elif ans<0 and (K-i)%2==1:
            ans=-ans*D
            break
print(ans)

Submission status: Wrong Answer

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)
    sys.exit() #ここはOK

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

Submission status: Wrong Answer

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

val = n % d

left = 0
if n >= 0:
	if n - (k * d) < 0:
		left = k - ((n - val)/d)
		if left % 2 == 0:
			print(val)
		else:
			print(abs(val - d))
	else:
		print(abs(n - (k * d)))
else:
	if n + (k * d) > 0:
		left = k - ((val + n)/d)
		if left % 2 == 0:
			print(val)
		else:
			print(abs(val - d))
		print(min(val, abs(val-d)))
	else:
		print(abs(n + (k*d)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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)
    sys.exit() #ここはOK

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

Submission status: Wrong Answer

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

cnt = X // D

X = abs(X)

if cnt > K:
    print(abs(X - D * K))
else:
    X -= cnt * D
    K -= cnt

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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)
    sys.exit()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X=abs(X)
a=min(K,X//D)
X-=a*D
K-=a
#print(X,K)
if K==1:
    X=abs(X-D)
ans=X
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy

e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
t=k
if k>10000:
 if k%2==0:
   t=10000
 else:
   t=10001
kou=numpy.gcd(x,d)
x=int(x/kou)
d=int(d/kou)
if d>100 and d%2==0:
  d=100
if d>100 and d%2==1:
  d=1001
for i in range(t):
  if abs(x+d)<abs(x-d):
    x+=d
  else:
    x-=d
print(abs(x)*kou)

Submission status: Wrong Answer

Solution code:
def li():
    return [int(x) for x in input().split()]

x, k, d = li()

x = abs(x)

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

xr = x % d
xl = xr - d

r_parity = (x - xr) // d % 2
k_parity = k % 2
if r_parity == k_parity:
    print(xr)
else:
    print(xl)

Submission status: Wrong Answer

Solution code:
def questionC():
  X,K,D = map(int, input().split())
  
  ans = 10**15
  for i in range(K+1):
      dummyX = abs(X + D*i - D*(K-i))
      if ans > dummyX:
          ans = dummyX
      else:
          return ans
  return ans

print(questionC())

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
x = abs(x)
# math.floor(x/d)+1回!absの方向に行く
# k>x/dのときx-math.floor(x/d)*dとx-(math.floor(x/d)+1)*dのうち小さいほうが答え
if k > x/d:
    if (k-math.floor(x/d)) % 2 == 0:
        print(abs(x-math.floor(x/d)*d))

    else:
        print(abs(x-math.floor(x/d)*d-k+math.floor(x/d)+1))
#     if x-math.floor(x/d)*d < abs(x-(math.floor(x/d)+1)*d):
#         print(x-math.floor(x/d)*d)
#     else:
#         print(abs(x-(math.floor(x/d)+1)*d))
else:
    print(x-k*d)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
mini=0
l1=x-d
l2=x+d
for i in range(k):
 if abs(l1)<abs(l2):
  mini=abs(l1)
  l1=mini-d
  l2=mini+d
 elif abs(l1)>abs(l2):
  mini=abs(l2)
  l1=mini-d
  l2=mini+d
 elif abs(mini)<d and i>=1:
  mini=min(abs(l1),abs(l2))
  break
print(mini)

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
x = abs(x)
# math.floor(x/d)+1回!absの方向に行く
# k>x/dのときx-math.floor(x/d)*dとx-(math.floor(x/d)+1)*dのうち小さいほうが答え
if k > x/d:
    if (k-math.floor(x/d)) % 2 == 0:
        print(abs(x-math.floor(x/d)*d))

    else:
        print(abs(x-math.floor(x/d)*d-k+math.floor(x/d))+1)
#     if x-math.floor(x/d)*d < abs(x-(math.floor(x/d)+1)*d):
#         print(x-math.floor(x/d)*d)
#     else:
#         print(abs(x-(math.floor(x/d)+1)*d))
else:
    print(x-k*d)

Submission status: Wrong Answer

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

x,k,d = [int(x) for x in input().split()]
if x == 0 and k % 2 ==0:
    ans = 0
elif x == 0 and k % 2 == 1:
    ans = d
elif x >= k*d:
    ans = x - k*d
else:
    moves = x // d
    if k % 2 == moves % 2:
        ans = x - moves*d
    else:
        ans = abs(x - (moves+1)*d)
print(ans)

Submission status: Wrong Answer

Solution code:
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
x = xkd[0] // xkd[2]
x = abs(x)
xkd[0] -= xkd[2] * x
xkd[1] -= x
if xkd[1] < 1:
  xkd[0] += abs(xkd[1]) * xkd[2]
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if ans > xkd[0] + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))

Submission status: Wrong Answer

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

x,k,d = [int(x) for x in input().split()]
if x >= k*d:
    ans = x - k*d
else:
    moves = x // d
    if k % 2 == moves % 2:
        ans = x - moves*d
    else:
        ans = abs(x - (moves+1)*d)
print(ans)

Submission status: Wrong Answer

Solution code:
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
x = xkd[0] // xkd[2]
xkd[0] -= xkd[2] * x
xkd[1] -= abs(x)
if xkd[1] < 1:
  xkd[0] += abs(xkd[1]) * xkd[2]
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if ans > xkd[0] + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))

Submission status: Wrong Answer

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

cnt = abs(X) // D
ans1 = X - cnt * D
ans2 = abs(ans1 - D)

if cnt < K:
  if (K - cnt)%2 == 0:
    print(ans1)
  else:
    print(ans2)
else:
    print(X - K * D)

Submission status: Wrong Answer

Solution code:
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
x = xkd[0] // xkd[2]
xkd[0] -= xkd[2] * x
xkd[1] -= 1 * x
if xkd[1] < 1:
  xkd[0] += abs(xkd[1]) * xkd[2]
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if ans > xkd[0] + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))

Submission status: Wrong Answer

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


if abs(x)==d:
    if k&1:
        print(0)
    else:
        print(k)
else:

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

        else:
            if x>0:
                print(abs(x-d*(tem+1)))
            else:
                print(x + d * (tem + 1))

Submission status: Wrong Answer

Solution code:
p=input()
p=p.split()
x=int(p[0])
k=int(p[1])
d=int(p[2])

if(x>0):
	i=-1
else:
	i=1


div = x/d
div = int(div)
if(div < k):
	x=x+(i)*div*d
	k-=div
else:
	x=x+(i)*k*d
	k-=k
ans = abs(x)
if (k>0):
	if(ans==0):
		ans = abs(d)
	else:
		ans = min(abs(x), abs(x+(i)*d))

print(ans)

Submission status: Wrong Answer

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

if np.abs(X)/D <= K:
    new_K = K - int(X/D)
    new_X = X - D*int(X/D)
    if new_K%2==0:
        print(new_X)
    else:
        print(np.abs(new_X-D))
else:
    new_K = K - int(X/D)
    new_X = X - D*int(X/D)
    if new_K%2==0:
        print(new_X)
    else:
        print(np.abs(new_X-D))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math
X, K, D = list(map(int, input().split(" ")))

x = abs(X)
k = int(math.floor(x/D))
if X==K==D:
    print(X)
    exit()
elif k>=K:
    print(abs(x - K*D))
    exit()
x -= k*D
if K%2==0:
    print(abs(x))
else:
    print(abs(x - D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy

e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
t=k
if k>1000:
 if k%2==0:
   t=1000
 else:
   t=1001
kou=numpy.gcd(x,d)
x=int(x/kou)
d=int(d/kou)
if d>100 and d%2==0:
  d=100
if d>100 and d%2==1:
  d=1001
for i in range(t):
  if abs(x+d)<abs(x-d):
    x+=d
  else:
    x-=d
print(abs(x)*kou)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math
X, K, D = list(map(int, input().split(" ")))

x = abs(X)
k = int(math.floor(x/D))
if X==K==D:
    print(X)
    exit()
elif k>=K:
    print(abs(x - K*D))
    exit()
x -= k*D
if K%2==0:
    print(x)
else:
    print(abs(x - D))

Submission status: Wrong Answer

Solution code:
import numpy

e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
t=0
if k%2==0:
  t=1000
else:
  t=1001
kou=numpy.gcd(x,d)
x=int(x/kou)
d=int(d/kou)
if d>100 and d%2==0:
  d=100
if d>100 and d%2==1:
  d=1001
for i in range(t):
  if abs(x+d)<abs(x-d):
    x+=d
  else:
    x-=d
print(abs(x)*kou)

Submission status: Wrong Answer

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

Solution code:
# coding: utf-8
import math
X, K, D = list(map(int, input().split(" ")))

x = abs(X)
k = int(math.floor(x/D))
if X==K==D:
    print(X)
    exit()
elif k>=K:
    print(abs(x - K*D))
    exit()
x = abs(x - k*D)
if K%2==0:
    print(x)
else:
    print(abs(x - D))

Submission status: Wrong Answer

Solution code:
import numpy

e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
t=0
if k%2==0:
  t=1000
else:
  t=1001
kou=numpy.gcd(x,d)
x=int(x/kou)
d=int(d/kou)
print(x)
if d>100 and d%2==0:
  d=100
if d>100 and d%2==1:
  d=1001
for i in range(t):
  if abs(x+d)<abs(x-d):
    x+=d
  else:
    x-=d
print(abs(x)*kou)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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)
        r = abs(mod - d)
        k -= q
        if k == 0:
            print(mod)
        if k % 2 == 0:
            print(mod)
        else:
            print(r)
elif x < 0:
    if abs(x) >= k * d:
        print(x + k * d)
    else:
        q, mod = divmod(abs(x), d)
        r = abs(mod - d)
        k -= q
        if k == 0:
            print(abs(x + k * d))
        elif k % 2 == 0:
            print(mod)
        else:
            print(r)

Submission status: Wrong Answer

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


if abs(x)==d:
    if k&1:
        print(0)
    else:
        print(k)
else:

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

        else:
            if x>0:
                print(abs(x-d*(tem+1)))
            else:
                print(x + d * (tem + 1))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
i = 0
if X >= 0:
    while X > 0 and i <= K:
        X -= D
        i += 1
    if K-i % 2 == 0:
        print(-X)
    else:
        print(X+D)
else:
    while X < 0 and i <= K:
        X += D
        i += 1
    if K-i % 2 == 0:
        print(X)
    else:
        print(X-D)