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())
current_position = X

times = abs(X) // D

if times < K:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
    K = K - times
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
else:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
print(current_position)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
X = X-D
Y = X+D
if X == K:
    print(K)
elif X != K:
    print(X)
else:
    print(1)

Submission status: Wrong Answer

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

res = 0
if x <= (k*d):
    m1 = x % k
    c1 = x // k
    c2 = k - c1
    if c2%2 == 0:
        res = m1
    else:
        res = abs(m1-k)
else:
    res = x - (k*d)
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

temp = X // D
if K%2 != temp%2:
    if temp > 0:
        temp = temp + 1
    else:
        temp = temp - 1

if temp > K:
    temp = K

temp = D * temp

print(abs(X - temp))

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split()) 
k = b*c
l = abs(a)

if l > 0:
    if l > k:
        print(l-k)
    if l == k:
        print(0)
    if l < k:
        d = int(l//c)
        e = b - d
        if e%2== 0:
            print(l-c*d)
        else:
            f = d + 1
            print(f*c -l)
if l == 0:
    if b%2==0:
        print(c)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
# cook your dish here
x,k,d = list(map(int, input().split()))
mod = x%d
div = x//d
ans = 0
if mod==0:
    if k%2==0:
        ans = x
    else:
        ans = 0 #x-d=0
else:
    if div<=k:
        ans = x-k*d
    if div>mod:
        ans = mod
print(abs(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
s = input().split()
X=int(s[0])
K=int(s[1])
D=int(s[2])
q, mod = divmod(X, D)
if 2*mod < D:
   if K < q:
      if X > 0:
         m = X - D*K
      else:
         m = X + D*K
   else:
       restmovement = (K-q)%2
       if restmovement==0:
          m = mod
       else:
          m = D - mod
else:
   if X > 0:
      if K < q+1:
         m = X - D*K
      else:
         restmovement = (K - q - 1)%2
         if restmovement==0:
                    m = D - mod
         else:
                    m = mod
   else:
       if K < q-1:
          m=X - D*K
       else:
          restmovement = (K+1-q)%2
          if restmovement==0:
                m = D-mod
          else:
                m = mod
if m < 0:
   print(-m)
else:
   print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
[_x, _k, _d] = list(map(int, input().split(" ")))
_x = abs(_x)

if _x >= _d:
  _y = _x//_d
  if _k > _y:
    _y = _k
  _x = abs(_x - _d * _y)
  _k -= _y
if _k % 2 == 1:
  _x = abs(_x - _d)

print(_x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans=X

a=abs(X//D)

"""
for i in range(a):
    ans=min(abs(ans-D),abs(ans+D))
    
"""

if (K>a>0 or -K<a<0) and (K-a)%2==1:
    ans=min(abs(X-(a)*D),abs(X+(a)*D))
    
elif (K>a>0 or -K<a<0) and (K-a)%2==1:
    ans=min(abs(X-(a+1)*D),abs(X+(a+1)*D))
elif a>=K:
    ans=abs(X-K*D)
else:
    ans=abs(X+K*D)
    


print(ans)

Submission status: Wrong Answer

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

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

m = min(X % D, D - X % D)
l = X // D

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if x>=0:
    dummy=x
    for i in range(int(k/2)):
        if dummy>=0 and dummy-2*d<0:
            print(min(dummy,2*d-dummy))
            break
        dummy=dummy-2*d
    else:
        print(dummy-k*d)
else:
    dummy=x
    for i in range(int(k/2)):
        if dummy<=0 and dummy+2*d>0:
            print(min(-1*dummy,dummy+2*d))
            break
        dummy=dummy+2*d
    else:
        print(dummy+k*d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = input().split()
X, K, D = int(X), int(K), int(D)
count = 0
if X < 0:
  X = -X
  L = X
else:
  L = X
if K < L // D:
  L = L - K * D
  K = 0
elif K == L // D:
#  print("koko")
  L = L % D
  K = 0
else:
#  print("C")
  temp = L
  L = L % D - D
  K = K - temp // D - 1
if L == 0:
  if K % 2 == 0:
    print(0)
    exit()
  else:
    print(D)
    exit()
if K == 0:
  print(abs(L))
  exit()
if abs(L) >= L + D:
  goal = L + D
  if K % 2 != 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()
elif abs(L) < L + D:
  goal = abs(L)
  if K % 2 == 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()

Submission status: Wrong Answer

Solution code:
x,k,d=list(map(int,input().split()))
syou, amari = divmod(x,d)
if x>0:
    if x-k*d==0:
        print(0)
    elif x-k*d<0:
        if amari==0:
            newk=k-syou
            if newk%2==0:
                print(0)
            else:
                print(d)
        elif amari!=0:
            newk=k-syou+1
            if newk%2==0:
                print(abs(x-d*(syou+1)))
            else:
                print(abs(x-d*syou))
    elif x-k*d>0:
        print(abs(x-k*d))
if x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

Submission status: Wrong Answer

Solution code:
# cook your dish here
x,k,d = list(map(int, input().split()))
mod = x%d
div = x//d
ans = 0
if mod==0:
    if k%2==0:
        ans = x
    else:
        ans = x-d #x-d=0
else:
    if div<=k:
        ans = x-k*d
    if div>mod:
        ans = mod
print(abs(ans))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
print((X - K*D)%D)

Submission status: Wrong Answer

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

res = 0
if x < (k*d):
    m1 = x % k
    c1 = x // k
    c2 = k - c1
    if c2%2 == 0:
        res = m1
    else:
        res = abs(m1-k)
else:
    res = x - (k*d)
print(res)

Submission status: Wrong Answer

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

q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    ans1 = D-mod
    ans2 = mod

    if ans1<ans2:
        if (K-q-1)%2==1:
            ans = ans2
        else:
            ans = ans1
    else:
        if (K-q)%2==1:
            ans = ans1
        else:
            ans = ans2



print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = [int(a) for a in input().split()]
X0=X
u=max(abs(X),D)
l=-u
now=0
for i in range(K):
    if i==0:
        if X>0 and X-K*D>0:
            print(X-K*D)
            break
        if X<0 and X+K*D<0:
            print(X+K*D)
            break
        if X>0:
            i=X//D
            X=X-i*D
        if X<0:
            i=abs(X)//D
            X=X+i*D
    if X>0:X-=D
    elif X<=0:X+=D
    if X>0 and X<X0:
        u=X
        now=1
    if X<0 and X>-X0:
        l=X
        now=-1
    if X==abs(X0):
        u=X
        if i!=K-1:l=0
        now=1
    if X==-abs(X0):
        l=X
        if i!=K-1:u=0
        now=-1
    #print(X,u,l,i,now)
    if X==0 and i==K-1:
        print("0")
        break
    if (u>0 and u<X0 and l<0 and l>-X0) or (u==0 or l==0):
        if (K-1-i)%2==0:
            if now==1:
                print(u)
            else:
                print(abs(l))
        else:
            if now==1:
                print(abs(l))
            else:
                print(u)
        break
    if i==K-1:
        print(abs(X))
        break

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
a = X / D
A = math.floor(a)
if A < K:
  if A % 2 == 0 and K % 2 == 0:
    if A + 2 <= K:
  	  print(min(abs(X - D * A), abs(X - D * (A + 2))))
    else:
      print(abs(X - D * A))
  if A % 2 == 1 and K % 2 == 0:
    if A + 1 <= K:
      if A + 3 <= K:
        print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1)), abs(X - D * (A + 3))))
      else:
        print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 0 and K % 2 == 1:
    if A + 1 <= K:
      if A + 3 <= K:
        print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1)), abs(X - D * (A + 3))))
      else:
        print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 1 and K % 2 == 1:
    if A + 2 <= K:
  	  print(min(abs(X - D * A), abs(X - D * (A + 2))))
    else:
      print(abs(X - D * A))
else:
  print(abs(X - D * K))

Submission status: Wrong Answer

Solution code:
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()
if x/d >= k:
    print(x-k*d)
else:
    if (k - math.floor(x/d))%2 == 0:
        print(abs(x%d))
    else:
        print(abs(x%d-d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
lst = input().split()

X = int(lst[0])
K = int(lst[1])
D = int(lst[2])

a = int((D * K - X) / (2 * D))

if a < 0:
   a = 0

def f(x):
   return X - D * K + 2 * D * x

lst2 = [abs(f(a))]

if a + 1 <= K:
   lst2.append(abs(f(a+1)))

if 0 < a:
   lst2.append(abs(f(a-1)))

result = min(lst2)

print(result)

Submission status: Wrong Answer

Solution code:
[_x, _k, _d] = list(map(int, input().split(" ")))
_x = abs(_x)

if _x >= _d:
  _y = _x//_d
  _x = abs(_x - _d * _y)
  _k -= _y
if _k % 2 == 1:
  _x = abs(_x - _d)

print(_x)

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
ans = X if X != 0 else 10 * 10
minc_floor = math.floor(abs(X)/D)
minc_ceil = math.ceil(abs(X)/D)
min_kaisuu = K
# print(minc_ceil, minc_floor)
if minc_ceil <= K:
    for k in range(minc_floor, minc_ceil+1):
        for flug in [-1, 1]:
            if abs(X + flug*k*D) < abs(ans):
                ans = abs(X + flug*k*D)

                # print("ans" + str(ans))
                min_kaisuu = k
                # print(min_kaisuu)
    if (K-min_kaisuu) % 2 != 0:
        ans = min(abs(ans+D), abs(ans-D))
else:
    # ans = min(ans+K*D), abs(ans-K*D))
    if abs(ans+K*D) > abs(ans-K*D):
        ans = ans-K*D
    else:
        ans = ans+K*D
# print(min_kaisuu)
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(hikukazu):
    
    abs_x = abs_x - D

    # print(X)

print(abs(X))

Submission status: Wrong Answer

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

if X < 0:
  X = X * -1
  
if X >= K * D:
  print(X - K * D)
else:
  print(X % D)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(X: int, K: int, D: int):
    X=abs(X)

    mostl = X - K * D
    if mostl > 0:
        print(mostl)
        exit()

    ans=min(X, abs(X - K * D))
    if K%2 == 1:
        X -= D
        ans = min(ans, X)
    Kh = K // 2

    N = X // (D*2)
    ans = min(ans, abs(X - D * 2 * (N - 1)))
    ans = min(ans, abs(X - D * 2 * N))
    if N < Kh:
        ans = min(ans, abs(X - D * 2 * (N+1)))

    print(ans)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(X, K, D)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 0
tmp = 0
if X >= K*D:
  print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

Solution code:
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()
if x/d >= k:
    print(x-k*d)
else:
    if (k - int(x/d))%2 == 0:
        print(abs(x%d))
    else:
        if x >= 0:
            print(abs(x%d-d))
        else:
            print(abs(x%d+d))

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = d
elif k == 1:
    ans = abs(x - d)
elif 2 * x <= d:
    ans = x
elif x <= d:
    if k % 2 == 0:
        ans = x
    else:
        ans = abs(x - d)
else:
    t = x // d
    if k >= t + 1:
        if (k - t) % 2 == 0:
            ans = abs(x - t * d)
        else:
            ans = min(abs(x - (t - 1) * d), abs(x - (t + 1) * d))
    else:
        ans = min(abs(x - k * d), abs(x - (k - 2) * d))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools

if __name__ == '__main__':
    # 初期位置、移動回数、一回の移動量
    X, K, D = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split()))
    origX = X
    # oldX = X
    # lasti = 0
    # 7-3=4
    # 4-3=1
    # 1-3=-2

    # for i in range(K):
    #     X -= D
    #     if abs(X) > abs(oldX):
    #         X = oldX
    #         lasti = i
    #         break
    #     oldX = X
    # else:
    #     print(abs(X))
    #     exit()

    # 6 3 2
    lasti = X // D
    X = X - D*lasti

    if lasti >= K:
        print(abs(origX-D*K))
        exit()

    # if abs(X)-D < abs(X):
    #     X -= D
    #     lasti += 1


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
ma = X + D*K
mi = X - D*K
 
if X > 0:
  ans = min(abs(mi),abs(X))
  dk = D*K
  ok = X
  ng = mi
  while dk > 1:
    dk //= 2
    mid = X - dk
    if ans < abs(mid):
      break
    else:
      ans = abs(mid)
  print(ans)
elif X < 0:
  ans = min(abs(ma),abs(X))
  dk = D*K
  ok = ma
  ng = X
  while dk > 1:
    dk //= 2
    mid = X + dk
    if ans < abs(mid):
      break
    else:
      ans = abs(mid)
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

inp = list(map(int,raw_input().split()))
x = inp[0]
k = inp[1]
d = inp[2]
x = abs(x)
if(d==0 or k==0):
    print(x)
elif(x >= k*d):
    print(x-k*d)
else:
    res=x
    tmp = int(x/d)
    for y in range(-100,100):
        if((y+tmp)<=k and y+tmp >=0):
            rem = k-y-tmp
            if(rem%2 == 0):
                res=min(res,abs(x-(y+tmp)*d))
            else:
                res=min(res,abs(x-(y+tmp-1)*d))
                if(rem>=1):
                    res=min(res,abs(x-(y+tmp+1)*d))
    print(res)

Submission status: Wrong Answer

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

#十分に原点から離れている場合
if abs(X) >= K*D:
    ans = abs(X) - K*D
else:
    t = (abs(X) - D*(K//2)) // (2*D)
    t = 2*t + K//2
    A = [t-2,t,t+2,-t-2,-t,-t+2]
    ans = 3*D
    for a in A:
        if abs(X+D*a)<ans:
            ans = abs(X+D*a)


print(ans)

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:
    if X<K*D-X:
        ans=X
        for i in range(0,K):
            ans=ans-D
            if ans<0 and (K-i)%2==0:
                ans=ans+D
                break
            elif ans<0 and (K-i)%2==1:
                ans=-ans
                break
    else:
        ans=K*D-X
        for i in range(0,K):
            ans=ans+D
            if ans>0 and (K-i)%2==0:
                ans=-ans+D
                break
            elif ans>0 and (K-i)%2==1:
                ans=ans
                break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 0
tmp = 0
if abs(X) >= K*D:
  if X >= 0:
    print(X - K*D)  
  else:
    print(X + K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

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

# case0: 絶対値を超える範囲まで届かない場合
if x >= k*d:
    print (x - k*d)
# case1: 絶対値を超える範囲に届く場合: 0の両隣のどちらかを選択
else:
    #wall = (x // d) + 1 # 0を超えるのに必要な移動回数
    #print("wall=",wall)
    #if wall % 2 == 0: # Xが偶数時: どちらも選択可能
    #    print (abs(x - wall*d))
    #else: # Xが奇数時: どちらかのみとなるので注意
    #    print (abs(x - (wall-1)*d))

    div = x//d

    a1 = abs(x - div*d)
    a2 = abs(x - (div+1)*d)
    if div % 2 == 1 and k % 2 == 0:
        print (a2)
    else:
        print (a1)

Submission status: Wrong Answer

Solution code:
# cook your dish here
x,k,d = list(map(int, input().split()))
mod = x%d
div = x//d
ans = 0
if mod==0:
    if k%2==0:
        ans = x
    else:
        ans = x-d #x-d=0
else:
    if div<=mod:
        ans = x-k*d
    if div>mod:
        ans = mod
print(abs(ans))

Submission status: Wrong Answer

Solution code:
from math import ceil, floor
X, K, D = map(int, input().split())
ans = abs(X)
fapp = (K*D - X)/(2*D)

if fapp <= 0:
    list = [0]
elif fapp >= K:
    list = [K]

else:
    list = [ceil(fapp), floor(fapp)]
for f in list:
    abso = abs(X + f*D - (K-f)*D)
    if abso < ans:
        ans = abso

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
x,k,d=map(int,input().split())
f=k%2
x+=d
for i in range(k+1):
    x-=d
    if f==i%2:
        if abs(x)<d:
            print(x)
            sys.exit()
        elif abs(x)==d:
            print(x)
            sys.exit()
print(abs(x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if (k-(x-min(l))/d)%2 == 0:
  print(min(l))
else:
        print(min(l)+d)

Submission status: Wrong Answer

Solution code:

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    span = int(X / D)
    if X > D and (K - span) % 2 == 0:
        print(int(X - span * D))
    else:
        for i in range(K):
            if X >= 0:
                X = X - D
            else:
                X = X + D
        print(abs(X))

Submission status: Wrong Answer

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

x = abs(x)

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

Submission status: Wrong Answer

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

if x <= 0:#開始座標が負
    if x + k*d <=0:#一方向にだけ動かせるとき
        ans= abs(x + k*d)
    else:
        kk=abs(x)//d
        tmp=x+kk*d#負の座標間で0に最大まで近づける
        tmp2=x+d#正の座標間で0に最大まで近づける
        if tmp==0:
            tmp=d
            tmp2=d

        if tmp==tmp2 :#4回移動でループ
            if (k-kk)%2==1:
                ans=abs(tmp)
            else:
                ans=0
        else:#値が違うとき2回移動でループ
            if (k-kk)%2==0:
                ans=abs(tmp)
            else:
                ans=abs(tmp2)

else:#開始座標が正
    if x - k*d >=0:#一方向にだけ動かせるとき
        ans=abs(x - k*d)
    else:
        kk=abs(x)//d
        tmp=x-kk*d#正の座標間で0に最大まで近づける
        tmp2=x-d#負の座標間で0に最大まで近づける
        if tmp==0:
            tmp=d
            tmp2=d

        if tmp==tmp2 :#4回移動でループ
            if (k-kk)%2==1:
                ans=abs(tmp)
            else:
                ans=0
        else:#値が違うとき2回移動でループ
            if (k-kk)%2==0:
                ans=abs(tmp)
            else:
                ans=abs(tmp2)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
flag=10
x,k,d=map(int,input().split())
for i in range(k):
    if x>0:
        x-=d
        if x<0:
            flag=-1
            break
    elif x<0:
        x+=d
        if x>0:
            flag=1
            break
    else:
        flag=0
        break
if (k-i-1)%2==0:
    parity=0
else:
    parity=1
if flag==0:
    if parity:
        print(d)
    else:
        print(2*d)
elif flag==-1:
    if parity:
        print(x+d)
    else:
        print(abs(x))
elif flag==1:
    if parity:
        print(abs(x-d))
    else:
        print(x)
else:
    print(abs(x))

Submission status: Wrong Answer

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

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