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:
inp = list(map(int,input().split(" ")))

location = inp[0]
times = inp[1]
distance = inp[2]

x = float(abs(location / distance))

if x >= times:
    print(abs(location - (times*distance)))
elif (times - int(x))%2 == 0:
    print(abs(location - abs(int(x)*distance)))
else:
    print(abs(location - abs(int(x)*distance)-distance))

Submission status: Wrong Answer

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

location = inp[0]
times = inp[1]
distance = inp[2]

x = float(abs(location / distance))

if x >= times:
    print(abs(location - (times*distance)))
elif (times - int(x))%2 == 0:
    print(abs(location - abs(int(x)*distance)-distance))
else:
    print(abs(location - abs(int(x)*distance)))

Submission status: Wrong Answer

Solution code:

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

if K % 2 == 0:
    move1 =  (abs(X) // (2 * D))
    if K * D < abs(X):
        position = abs(X) - K*D
    else:
        position = min(abs((abs(X) - int(move1) * 2 * D)), abs(abs(X) - (int(move1) + 1) * 2 * D))
    
else:
    move1 = (abs(X) // (2 * D))
    if K * D < abs(X):
        position = abs(X) - K*D
    else:
        position = abs(X) - move1 * 2 * D - D
print(position)

Submission status: Wrong Answer

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

location = inp[0]
times = inp[1]
distance = inp[2]

x = float(abs(location / distance))

if x >= times:
    print(abs(location - (times*distance)))
elif (times - int(x))%2 == 0:
    print(abs(location - (int(x)*distance)))
else:
    print(abs(location - (int(x)*distance)-distance))

Submission status: Wrong Answer

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

if X <= K * D:
  if K % 2:
    print(X % D)
  else:
    x = (X % D) + D
    x = min((X % D) - D, x)
    print(abs(x))
else:
  print(X - (K * D))

Submission status: Wrong Answer

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

location = inp[0]
times = inp[1]
distance = inp[2]

for i in range(times):
    location = abs(location - distance)
    if location <= distance :
        if (times - (i + 1))%2 == 0:
            break
        else:
            location = abs(location - distance)
            break

print (location)

Submission status: Wrong Answer

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

location = inp[0]
times = inp[1]
distance = inp[2]

for i in range(times):
    location = abs(location - distance)
    if location < distance :
        if (times - (i + 1))%2 == 0:
            break
        else:
            location = abs(location - distance)
            break

print (location)

Submission status: Wrong Answer

Solution code:
import math

x, k, d = map(int, input().split())
left = 0
right =k
while(left<right):
    mid = (left+right)//2
    if(x-(d*mid)<0):
        right = mid
    else:
        left = mid+1
low= left
high = left-1
v1 = abs(x-(low*d))
v2 = abs(x-(high*d))
if(k%2==0):
    if(v1<=v2):
        if(low%2==0):
            print(v1)
        else:
            print(v2)
    else:
        if(high%2==0):
            print(v2)
        else:
            print(v1)
else:
    if(v1<=v2):
        if(low%2==1):
            print(v1)
        else:
            print(v2)
    else:
        if(high%2==1):
            print(v2)
        else:
            print(v1)

Submission status: Wrong Answer

Solution code:

inp = list(map(int,input().split(" ")))

location = inp[0]
times = inp[1]
distance = inp[2]

for i in range(times):
    if location - distance <= 0 :
        if (times - i)%2 == 0:
            break
        else:
            location = abs(location - distance)
            break
    location = abs(location - distance)

print (location)

Submission status: Wrong Answer

Solution code:
X, K, D = [int(a) for a in list(input().split())]
X = abs(X) 
q = X/D
r = X%D
if q >= K:
    print(X - D*K)
    exit()
rem = K - q
if rem % 2 == 0:
    print(r)
else:
    print(abs(D-r))

Submission status: Wrong Answer

Solution code:
X, K, D = [int(a) for a in list(input().split())]
X = abs(X) 
q = X/D
r = X%D
if q >= K:
    print(X - D*K)
    exit()
rem = K - q
if rem % 2 == 0:
    print(X)
else:
    print(abs(D-r))

Submission status: Wrong Answer

Solution code:
X, K, D = [int(a) for a in list(input().split())]
for i in range(X//D):
    X -= D
    K -= 1
    if K == 0:
        print(abs(X))
        exit()
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())

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
x,k,d = map(int,input().split())
c = int(x/d)
le = int(k-abs(c))
if x==0:
    if le%2==0:
        print(0)
    else:
        print(d)
    sys.exit()
else:
    if le>=0:
        ne = -d*c+x
    else:
        if x>0:
            ne = -d*k+x
        else:
            ne = d*k-x
        print(ne)
        sys.exit()
        
if le%2==0:
    print(abs(ne))
else:
    ans1=abs(ne-d)
    ans2=abs(ne+d)
    print(min(ans1,ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = abs(x)
e = round(x / d)
output = 0

a = k - e
a = a%2


if e >= k:
    output = x - (k * d)
    # print('abc',1)
elif a == 1:
    output = x - ((e + 1) * k)
    # print('abc',2)
elif a == 0:
    output = x - (e * d)
    # print('abc',3)

print(abs(output))

Submission status: Wrong Answer

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

x = abs(x)
e = round(x / d)
output = 0

a = k - e
a = a%2


if e >= k:
    output = x - (k * d)
    print('abc',1)
elif a == 1:
    output = x - ((e + 1) * k)
    print('abc',2)
elif a == 0:
    output = x - (e * d)
    print('abc',3)

print(abs(output))

Submission status: Wrong Answer

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

x = abs(x)
e = round(x / d)
output = 0

a = k - e
a = a%2

if a == 1:
    output = x - ((e + 1) * k)
if a == 0:
    output = x - (e * d)
if e >= k:
    output = x - (k * d)


print(abs(output))

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:
  print(min(X%D, abs(X%D - D)))

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:
  print(min(X%D, D-X%D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

#0をまたげないとき
if abs(X)-K*D >= 0:
    print(abs(X) - K*D)
else:#0をまたげるとき
    if X > 0:
        t = K - (X // D)
        X = X % D
    else:
        t = K - (-X // D) -1
        X = X % D + D
    
    if t % 2 == 0:
        print(X)
    else:
        print(-X + D)

Submission status: Wrong Answer

Solution code:
pos,cnt , dis = map(int,input().split())
if pos !=dis:
    if cnt %2 !=0:
        if abs(pos - dis)<abs(pos+dis):
            pos =pos-dis
            cnt -=1
        else:
            pos = pos +dis
            cnt -=1
    else:
        for i in range(0,2):
            if abs(pos - dis) < abs(pos + dis):
                pos = pos - dis              
            else:
                pos = pos + dis

print(abs(pos))

Submission status: Wrong Answer

Solution code:
pos,cnt , dis = map(int,input().split())
if cnt %2 !=0:
    if abs(pos - dis)<abs(pos+dis):
        pos =pos-dis
        cnt -=1
    else:
        pos = pos +dis
        cnt -=1
print(abs(pos))

Submission status: Wrong Answer

Solution code:
pos,cnt , dis = map(int,input().split())
if cnt %2 !=0:
    if pos>0:
        pos =pos-dis
        cnt -=1
    else:
        pos = pos +dis
        cnt -=1
print(abs(pos))

Submission status: Wrong Answer

Solution code:
import math
def ireturn(x,k,d):
  num=x-k*d
  den=2*d
  f=num/den
  f=round(f,3)
  if(f>=0):
    return 0
  else:
    f=abs(f)
    diff1=abs(f-math.ceil(f))
    diff2=abs(f-math.floor(f))
    if(math.floor(f)==math.ceil(f)):
      return math.floor(f)
    elif(diff1<=diff2):
      return math.ceil(f)
    elif(diff1>=diff2):
      return math.floor(f)
    
x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
if((k==0)or(d==0)):
  m=0
else:
  m=ireturn(x,k,d)
print(abs(2*m*d-k*d+x))

Submission status: Wrong Answer

Solution code:
import math
def ireturn(x,k,d):
  num=x-k*d
  den=2*d
  f=num/den
  if(f>=0):
    return 0
  else:
    f=abs(f)
    diff1=abs(f-math.ceil(f))
    diff2=abs(f-math.floor(f))
    if(math.floor(f)==math.ceil(f)):
      return math.floor(f)
    elif(diff1<=diff2):
      return math.ceil(f)
    elif(diff1>=diff2):
      return math.floor(f)
    
x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
if((k==0)or(d==0)):
  m=0
else:
  m=ireturn(x,k,d)
print(abs(2*m*d-k*d+x))

Submission status: Wrong Answer

Solution code:
import math
def ireturn(x,k,d):
  num=x-k*d
  den=2*d
  f=num/den
  if(f>=0):
    return 0
  else:
    f=abs(f)
    diff1=abs(f-math.ceil(f))
    diff2=abs(f-math.floor(f))
    if(math.floor(f)==math.ceil(f)):
      return math.floor(f)
    elif(diff1<=diff2):
      return math.ceil(f)
    elif(diff1>=diff2):
      return math.floor(f)
    
x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
m=ireturn(x,k,d)
print(abs(2*m*d-k*d+x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

maxDist=K*D
if(X>maxDist):
  print(X-maxDist)
else:
  sabun=maxDist-X
  ans1 = sabun % (D*2)
  ans2 = abs(ans1 - (D*2))
  if(ans1 > ans2):
    print(ans2)
  else:
    print(ans1)

Submission status: Wrong Answer

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

if ok == 0:
    print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(X) == D:
    print(0)
else:
    if abs(X) <= K * D:
        X2 = abs(X) % D
        K2 =  K - abs(X)//D
        if K2 % 2 == 0:
            print(abs(X2))
        else:
            print(min(abs(X2+D), abs(X2-D)))
    else:
        print(abs(abs(X)-K*D))

Submission status: Wrong Answer

Solution code:
def is_x(x,k,d):
    if x==0:
        if k%2==0:
            print(0)
            exit()
        else:
            print(d)

def x_is_mosbat(x,k,d):
    if x<0:
        x=-x
    p=x//d
    if p>=k:
        print(x-d*k)
        exit()
    else:
        x=x-d*p
        k-=p
        if abs(x-d)<x:
            x=x-d
            k-=1
        if k%2==0:
            print(abs(x))
            exit()
        else:
            print(abs(x-d)) if x>0 else print(x+d)
            exit()
    
x,k,d=map(int,input().split())
is_x(x,k,d)
x_is_mosbat(x,k,d)

Submission status: Wrong Answer

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

if X == D:
    print(0)
else:
    count = 0
    while count < K:
        if X >= 0:
            X -= D
            count += 1
        else:
            X += D
            count += 1
    print(abs(X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a = input().split()
x, k, d = abs(int(a[0])), int(a[1]), int(a[2])
if x > 0:
    while x > 0 and k > 0:
        x -= d
        k -= 1
    if x <= 0:
        if k % 2:
            print(x + d)
        else:
            print(abs(x))
    else:
        print(x)

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K <= Xs):
    print(abs(X-D*K))
elif (K-Xs) % 2 == 0:
    print(abs(Xm))
else:
    print(abs(D-Xm))

Submission status: Wrong Answer

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

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    X, K, D = list(map(int, input().split()))

    return X, K, D


def main(X: int, K: int, D: int) -> None:
    """
    メイン処理.

    Args:\n
        X (int): 座標(-10^{15} <= X <= 10^{15})
        K (int): 移動回数(1 <= K <= 10^{15})
        D (int): 移動距離(1 <= D <= 10^{15})
    """
    # 求解処理
    ans = int()
    straight = min(K, int(abs(X) / D))
    K -= straight
    X -= straight * D

    if K % 2 == 0:
        ans = X
    else:
        ans = D - X

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    X, K, D = get_input()

    # メイン処理
    main(X, K, D)

Submission status: Wrong Answer

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K <= Xs):
    print(abs(Xa-D*K))
elif (K-Xs) % 2 == 0:
    print(abs(Xm))
else:
    print(abs(D-Xm))

Submission status: Wrong Answer

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

x=abs(x)#正方向スタートで考える
if x-d*k >= 0:#一方向にのみ動かして済むとき
    ans=abs(x-d*k)
else:#往復運動するとき
    k_dash=x//d
    R=x-k_dash*d
    L=R-d

    if k_dash%2 == 0:
        ans=abs(R)
    else:
        ans=abs(L)

print(ans)

Submission status: Wrong Answer

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

x=abs(x)#正方向スタートで考える
if x-d*k >= 0:#一方向にのみ動かして済むとき
    ans=x-d*k
else:#往復運動するとき
    k_dash=x//d
    R=x-k_dash*d
    L=R-d

    if k_dash%2 == 0:
        ans=abs(R)
    else:
        ans=abs(L)

print(ans)

Submission status: Wrong Answer

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

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

print f(x,k,d)

Submission status: Wrong Answer

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

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

print f(x,k,d)

Submission status: Wrong Answer

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

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

print f(x,k,d)

Submission status: Wrong Answer

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

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

print f(x,k,d)

Submission status: Wrong Answer

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


def z(i):
    return i if i > 0 else -i


def m(a, b):
    a = z(a)
    b = z(b)
    return a if a < b else b


def a(x, k, d):
    if x // d < k:
        r = x % d
        if (x // d) % 2 == 0:
            return r
        else:
            return m(r + d, r - d)
    else:
        return m(x - k * d, x + k * d)


print(a(x, k, d))

Submission status: Wrong Answer

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


def z(i):
    return i if i > 0 else -i


def m(a, b):
    a = z(a)
    b = z(b)
    return a if a < b else b


def a(x, k, d):
    if x // d < k:
        r = x % d
        if (x // d) % 2 == 0:
            return m(r, r - d)
        else:
            return m(r + d, r - d)
    else:
        return m(x - k * d, x + k * d)


print(a(x, k, d))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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


def z(i):
    return i if i > 0 else -i


def m(a, b):
    a = z(a)
    b = z(b)
    return a if a < b else b

def a(x, k, d):
    if x // d < k:
        r = x % d
        return m(r + d, r - d)
    else:
        return m(x - k * d, x + k * d)


print(a(x, k, d))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

x = abs(x)
p = 0

if(x - k*d >=0):
  p = x - k*d

else:
    p = x%d
    if (k - x/d)%2 == 1:
        p = p-d
print(abs(p))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

x = abs(x)
p = 0

# 原点まで届かない時
if x/d >= k:
    p = x-(k*d)

# 原点まで寄せる時
else:
    p = x%d
    if (k - x/d)%2 == 1:
        p = p-d
print(abs(p))

Submission status: Wrong Answer

Solution code:
def ii():
    return int(input())
def mi():
    return map(int, input().split())
def li():
    return list(mi())

l = li()
x, k, d = l[0], l[1], l[2]
calc = x // d
mi = min(k, calc)
k -= mi
x -= mi * d

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if X < 0:
    X=-X  # これにより、X>=0の場合だけ考えればよい

#+D or -D
# X + D*(K-n) + (-D) *n ;0<=n <=K
# -> X + D*(K-2*n)
# 1, X>=K*D-> K回全部（-D）移動に使うことが最適->n=K
# 2, X < K*D ->
# X >= p*D を満たす最大のpを探す -> p回（-D）移動に使うことが最適
# 残り(K-p)%2=0なら行ったり来たりで消化できる.
# 残り回数が奇数の場合、R=X-D*pとすると、min(R+D,abs(R-D))

n = 0  # 負方向移動の回数
result = X + D*(K-2*n)

if X == 0:
    if K % 2 == 0:
        n = K/2
        print(result)
    else:
        print(D)

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

elif X < D*K:
    p = int(X/D)  # X/D回負の方向に移動すれば、原点に最も近い正の値になる。
    result=X-D*p
    if (K-p) % 2 == 0:
        print(result)
    elif (K-p)%2 ==1:
        print(abs(result-D))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

if k <= x//d:
    ans = abs(x-k*d)
else:
    if (k-x//d)%2!=0:
        ans = abs(x-(x//d+1)*d)
    else:
        ans = abs(x-(x//d)*d)

print(ans)

Submission status: Wrong Answer

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

if k <= x//d:
    ans = abs(abs(x)-k*d)
else:
    if (k-x//d)%2!=0:
        ans = abs(abs(x)-(abs(x)//d+1)*d)
    else:
        ans = abs(abs(x)-(abs(x)//d)*d)

print(ans)

Submission status: Wrong Answer

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

x = abs(x)
p = 0
if x != 0:
    # 原点まで届かない時
    if x/d > k:
        p = x-(k*d)

    # 原点まで寄せる
    else:
        p = x%d
        if (k - x/d)%2 == 1:
            p = p-d
print(abs(p))

Submission status: Wrong Answer

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

x = abs(x)
p = 0

if x/d > k:
    p = x-(k*d)

else:
    p = x%d
    if (k - x/d)%2 == 1:
        p = p-d
print(abs(p))

Submission status: Wrong Answer

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

x = abs(x)
p = 0

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

if x/d > k:
    p = abs(x-(k*d))

else:
    p = x%d
    if (k - x/d)%2 == 1:
        p = abs(p-d)
print(p)

Submission status: Wrong Answer

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

x = abs(x)
p = 0

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

if x/d > k:
    p = x-(k*d)

else:
    p = x%d
    if (k - x/d)%2 == 1:
        p = abs(p-d)
print(p)

Submission status: Wrong Answer

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

x = abs(x)
p = 0

if x/d > k:
    p = x-(k*d)

else:
    p = x%d
    if (k - x/d)%2 == 1:
        p = abs(p-d)
print(p)

Submission status: Wrong Answer

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

if k <= x//d:
    ans = abs(abs(x)-(x//d)*d)
else:
    if (k-x//d)%2!=0:
        ans = abs(abs(x)-(x//d+1)*d)
    else:
        ans = abs(abs(x)-(x//d)*d)

print(ans)

Submission status: Wrong Answer

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

if k <= x//d:
    ans = abs(abs(x)-k*d)
else:
    if (k-x//d)%2!=0:
        ans = abs(abs(x)-(x//d+1)*d)
    else:
        ans = abs(abs(x)-(x//d)*d)

print(ans)

Submission status: Wrong Answer

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

if k <= x//d:
    ans = abs(x)-k*d
else:
    if (k-x//d)%2!=0:
        ans = abs(abs(x)-(x//d+1)*d)
    else:
        ans = abs(abs(x)-(x//d)*d)

print(ans)

Submission status: Wrong Answer

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

print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(x)

Submission status: Wrong Answer

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

x = abs(x)

for i in range(1, k+1):
    if x > abs(x - d):
        x = abs(x - d)
    if x < abs(x - d):
        point = i
        if (k-point)%2 == 1:
            x += d
            break
        else:
            break

print(x)

Submission status: Wrong Answer

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

x = abs(x)

for i in range(k+1):
    if x > abs(x - d):
        x = abs(x - d)
    if x < abs(x - d):
        point = i
        if (k-point+1)%2 == 1:
            x += d
            break
        else:
            break

print(x)