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())
x = abs(x)
if x > k*d:
    print(x-k*d)
else:
    p = x//d
    if p == 0 or p==1:
          for i in range(1,k+1):
            p2 = x-i*d
            c = k-i
            if p2 <= 0:
                if c%2 != 0:
                    print(abs(d+p2))
                    break
                elif c%2 == 0:
                    print(abs(p2))
                    break
    elif k%p == 0:
        print(abs(x-p*d))
    else:
        print(abs(d*(p+1)-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)
else:
    flag = 0
    for i in range(1,k+1):
        p = x-i*d
        c = k-i
        if p <= 0:
            if c%2 != 0:
                print(abs(d+p))
                flag = 1
                break
            elif c%2 == 0:
                print(abs(p))
                flag = 1
                break
        
        if flag == 0:
            i2 = k+1-i
            p2 = x-i2*d
            c2 = k-i2
            if p2 <= 0:
                if c2%2 != 0:
                    print(abs(d+p2))
                    break
                elif c2%2 == 0:
                    print(abs(p2))
                    break

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:
    flag = 0
    for i in range(1,k+1):
        p = x-i*d
        c = k-i
        if p <= 0:
            if c%2 != 0:
                print(abs(d+p))
                flag = 1
                break
            elif c%2 == 0:
                print(abs(p))
                flag = 1
                break
        
        if flag == 0:
            i2 = k+1-i
            p2 = x-i2*d
            c2 = k-i2
            if p2 <= 0:
                if c2%2 != 0:
                    print(abs(d+p))
                    break
                elif c2%2 == 0:
                    print(abs(p))
                    break

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:
    flag = 0
    for i in range(1,k+1):
        p = x-i*d
        c = k-i
        if p <= 0:
            if c%2 != 0:
                print(abs(d+p))
                break
            elif c%2 == 0:
                print(abs(p))
                break
        
        if flag == 0:
            i2 = k+1-i
            p2 = x-i2*d
            c2 = k-i2
            if p2 <= 0:
                if c2%2 != 0:
                    print(abs(d+p))
                    break
                elif c2%2 == 0:
                    print(abs(p))
                    break

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def execute(X, K, D):
    for i in range(K):
        if X > 0:
            X -= D
        else:
            X += D

    return abs(X)

def execute2(X, K, D):
    T = int(abs(X) / D)
    if X > 0:
        if K < T:
            return X - K * D
        else:
            Y = X - T * D
            return min(Y, D - Y)
    else:
        if K < T:
            return (X + K * D) * -1
        else:
            Y = X + T * D
            return min(-1 * Y, D + Y)


if __name__ == '__main__':
    X, K, D = map(int, input().split())
    print(execute2(X, K, D))

Submission status: Wrong Answer

Solution code:
def execute(X, K, D):
    for i in range(K):
        if X > 0:
            X -= D
        else:
            X += D

    return abs(X)

def execute2(X, K, D):
    T = int(abs(X) / D)
    if X > 0:
        if K < T:
            return X - K * D
        else:
            Y = X - T * D
            return min(Y, X - Y)
    else:
        if K < T:
            return (X + K * D) * -1
        else:
            Y = X + T * D
            return min(-1 * Y, D + Y)


if __name__ == '__main__':
    X, K, D = map(int, input().split())
    print(execute2(X, K, D))

Submission status: Wrong Answer

Solution code:
def execute(X, K, D):
    for i in range(K):
        if X > 0:
            X -= D
        else:
            X += D

    return abs(X)

def execute2(X, K, D):
    T = int(abs(X) / D)
    if X > 0:
        if K < T:
            return X - K * D
        else:
            Y = X - T * D
            return min(Y, X - Y)
    else:
        if K < T:
            return (X + K * D) * -1
        else:
            Y = X + T * D
            return min(-1 * Y, X + Y)


if __name__ == '__main__':
    X, K, D = map(int, input().split())
    print(execute2(X, K, D))

Submission status: Wrong Answer

Solution code:
def execute(X, K, D):
    for i in range(K):
        if X > 0:
            X -= D
        else:
            X += D

    return abs(X)

def execute2(X, K, D):
    T = int(abs(X) / D)
    if X > 0:
        if K < T:
            return X - K * D
        else:
            Y = X - T * D
            return min(Y, X - Y)
    else:
        if K < T:
            return (X + K * D) * -1
        else:
            Y = X + T * K
            return min(-1 * Y, X + Y)


if __name__ == '__main__':
    X, K, D = map(int, input().split())
    print(execute2(X, K, D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

if x - k * d > 0:
	print(x - k * d)
elif x == d:
	if k % 2 == 0: print(x)
	else:print(0)
else:
	times = k - x // d
	num1 = x % d
	num2 = num1 - d
	if times % 2 == 0:
		print(np.abs(num1))
	else:
		print(np.abs(num2))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

if x - k * d > 0:
	print(x - k * d)
elif x == d:
	if k % 2 == 0: print(x)
	else:print(0)
else:
	num1 = x % d
	num2 = num1 - d
	print(min(np.abs(num1),np.abs(num2)))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

if x - k * d > 0:
	print(x - k * d)
elif x == d:
	if k % 2 == 0: print(x)
	else:print(0)
else:
	num1 = x % d
	num2 = num1 - d
	print(min(np.abs(num1),np.abs(num2)))

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
X, K, D = map(int, input().split())

A=min(int((X/D+K)/2),K)
j=0
k=X
for i in range(A-1,A+1):
    j=abs(-K*D+2*i*D+X)
    if abs(j)<=abs(k) :k=abs(j)

print(k)

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
X, K, D = map(int, input().split())

A=min(int((X/D+K)/2),K)
print(A)
j=0
k=X
for i in range(A-1,A+1):
    j=abs(-K*D+2*i*D+X)
    if abs(j)<=abs(k) :k=abs(j)

print(k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    s = min(k, x//d)
    k -= s
    x -= s * d

    print(abs(-(k % 2)*d + x))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    r = x // d
    q = x % d

    if k < r:
        print(x - k * d)
    else:
        print(abs(-((k-r) % 2)*d + q))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
if abs(X) >= K*D:
    ans = abs(X) - K*D
else:
    ans = min(abs(X)%D, D-abs(X)%D)

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  6 11:40:33 2020

@author: liang
"""


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

ans = 0
if abs(X) >= K*D:
    ans = abs(X) - K*D
else:
    ans = min(abs(X)%D, abs(X)-abs(X)%D)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
X_min=pow(10,16)
if X<K*D:
    if (K-X//D)%2==0:
        X_min=X%D
    else:
        X_min=abs(X%D-D)
else:
    for i in range(K):
        if X>=0:
            X=X-D
        else:
            X=X+D
        if X_min>abs(X):X_min=abs(X)
print(X_min)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
X_min=pow(10,16)
if X<K*D:
    if (K-X//D)%2==0:
        if X%D>=abs(X%D-D):X_min=abs(X%D-D)
        else:X_min=X%D
    else:
        if X%D>=abs(X%D-D):X_min=X%D
        else:X_min=abs(X%D-D)
else:
    for i in range(K):
        if X>=0:
            X=X-D
        else:
            X=X+D
        if X_min>abs(X):X_min=abs(X)
print(X_min)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
X_min=pow(10,16)
if X<K*D:
    X_min=X%D
else:
    for i in range(K):
        if X>=0:
            X=X-D
        else:
            X=X+D
        if X_min>abs(X):X_min=abs(X)
print(X_min)

Submission status: Wrong Answer

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

if X > 0:
  if X - K*D >= 0:
    print(X - K*D)
    exit()
  else:
    # 二分探索
    m = K
    for i in range(1, K+1):
      if X - m*D >= 0 and X - (m+1)*D < 0:
        if (K - m) % 2 == 0:
          print(abs(X - m*D))
        else:
          print(abs(X - (m+1)*D))
        exit()
      else:
        m //= 2
elif X < 0:
  if X + K*D <= 0:
    print(X + K*D)
    exit()
  else:
    # 二分探索
    m = K
    for i in range(1, K+1):
      if X + m*D <= 0 and X + (m-1)*D > 0:
        if (K - m) % 2 == 0:
          print(abs(X + m*D))
        else:
          print(abs(X + (m-1)*D))
        exit()
      else:
        m //= 2
else:
  # X == 0
  if K % 2 == 0:
    print(0)
    exit()
  else:
    print(D)
    exit()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if X>=K*D:
  print(X-K*D)
elif X<=-K*D:
  print(K*D-X)
elif 0<X<K*D:
  a=X//D
  K-=a
  if K%2==0:
    print(X-D*a)
  else:
    print(X-D*a-D)
else:
  a=X//D
  K-=a
  if K%2==0:
    print(X+D*a)
  else:
    print(X+D*a+D)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
z=x//d
ans_p=x-z*d
ans_n=-(ans_p-d)
if k<z:
    ans=x-k*d
elif k==z:
    ans=ans_p
else:
    num=k-z
    if num%2==1:
        ans=ans_n
    else:
        ans=ans_p
print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,  input().split())
X = abs(X)
if K%2 == 1:
  K -= 1
  X -= K
  
K_ = K // 2

min_Y = X - K_ * (2 * D)

if min_Y > 0:
  print(min_Y)
else:
  min_Y_p = X % (2 * D)
  min_Y_m = 2 * D - X % (2 * D)
  print(min(min_Y_p, min_Y_m ))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
a = int(X/D)
if a < K:
        K -= a
        X -= a*D

if K*D <= X:
    X -= K*D
else:

    if X > 0:
        X -= D
    else:
        X += D

print(abs(X))

Submission status: Wrong Answer

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

if a < K:
    K -= a
    X -= a*D
    
for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D

if X < 0:
    print(abs(X))
else:
    print(X)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
a = int(X/D)
if a < K:
    K -= a
    X -= a*D

K %= 2

for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D
if X < 0:
    print(abs(X))
else:
    print(X)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
a = int(X/D)
if a < K:
    K -= a
    X -= a*D

K %= 2

for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D
print(X)

Submission status: Wrong Answer

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

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

for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D
print(X)

Submission status: Wrong Answer

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

for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D
print(X)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ans = pow(10, 16)
if x // d > k:
    ans = abs(x - k*d)
else:
    ans = min(abs(x % d), d - abs(x % d))

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
i = X // D
if (abs(X - D * i) > abs(X - D * (i+1))):
    X = X - D * (i+1)
    i += 1
else:
    X = X - D * i

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d= map(int,input().split())
s=[]
x=abs(x)
if x>d*k:
  print(x-d*k)
else:
  for i in range(k):
    s.append(abs(x-d*i))
    s.sort()
  print(s[0])

Submission status: Wrong Answer

Solution code:
x,k,d= map(int,input().split())
s=[]
if x//d>k:
  print(x-d*k)
else:
  print(min(abs(x-d*(x//d)),abs(x-d*(x//d+1))))

Submission status: Wrong Answer

Solution code:
x,k,d= map(int,input().split())
s=[]
if x//d>k:
  print(x-d*k)
else:
  for i in range(k):
    s.append(abs(x-d*i))
    print(min(s))

Submission status: Wrong Answer

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

if abs(X) // D > K:
    print(abs(X - K * D))
    exit()

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

Submission status: Wrong Answer

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

if abs(X) // D > K:
    print(X - K * D)
    exit()

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

Submission status: Wrong Answer

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

ans = X
for i in range(K):
    if ans >= 0:
        ans -= D
    else:
        ans += D
print(ans)

Submission status: Wrong Answer

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

q = x // d
r = x % d

if k >= q:
    if k%2 == q%2:
        print(r)
    else:
        print(d - r)
else:
    print(x - d*k)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]

for i in range(K+1):
    if (0 < X) and (X < D):
        if (K-i) % 2 == 1:
            X = X - D
        break
    if (-D < X) and (X <= 0):
        if (K-i) % 2 == 1:
            X = X + D
        break
    if X >= D:
        X = X - D
    if X <= -D:
        X = X + D

X = abs(X)
print(X)

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]
flag = 0

for i in range(1,K+1):
    if X == 0:
        flag = 1
    if X >= D:
        X = X - D
    if -D >= X:
        X = X + D
    if (0 < X) and (X < D):
        if (K-i) % 2 == 1:
            X = X - D
        X = abs(X)
        flag = 1
    if (-D < X) and (X < 0):
        if (K-i) % 2 == 1:
            X = X + D
        X = abs(X)
        flag = 1

    if flag:
        break

print(X)

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]
flag = 0

for i in range(1,K+1):
    if X == 0:
        break
    if X > D:
        X = X - D
        if X < D:
            if (K-i) % 2 == 0:
               X = abs(X)
               flag = 1
            else:
                X = X - D
                X = abs(X)
                flag = 1

    if -D > X :
        X = X + D
        if -D < X:
             if (K-i) % 2 == 0:
                 X = abs(X)
                 flag = 1
             else:
                 X = X + D
                 X = abs(X)
                 flag = 1

    if flag:
        break

print(X)

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]
flag = 0

for i in range(1,K+1):
    if X == 0:
        break
    if X > 0:
        X = X - D
        if X < D:
            if (K-i) % 2 == 0:
               X = abs(X)
               flag = 1
            else:
                X = X - D
                X = abs(X)
                flag = 1

    else:
        X = X + D
        if -D < X:
             if (K-i) % 2 == 0:
                 X = abs(X)
                 flag = 1
             else:
                 X = X + D
                 X = abs(X)
                 flag = 1

    if flag:
        break

print(X)

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]
flag = 0

for i in range(1,K+1):
    if X == 0:
        break
    if X > 0:
        X = X - D
        if (X < D) and ((K-i) % 2 == 0):
            X = abs(X)
            flag = 1
    else:
        X = X + D
        if (-D < X) and ((K-i) % 2 == 0):
            X = abs(X)
            flag = 1

    if flag:
        break

print(X)

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]
flag = 0

for i in range(1,K+1):
    if X == 0:
        break
    if X > D:
        X = X - D
        if (X < D) and ((K-i) % 2 == 0):
            X = abs(X)
            flag = 1
    if X < -D:
        X = X + D
        if (-D < X) and ((K-i) % 2 == 0):
            X = abs(X)
            flag = 1

    if flag:
        break

print(X)

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]
flag = 0

for i in range(1,K+1):
    if X == 0:
        break
    if X > D:
        X = X - D
        if (-D < X) and (X < D) and ((K-i) % 2 == 0):
            X = abs(X)
            flag = 1
    if X < -D:
        X = X + D
        if (-D < X) and (X < D) and ((K-i) % 2 == 0):
            X = abs(X)
            flag = 1

    if flag:
        break

print(X)

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
X = x[0]
K = x[1]
D = x[2]
flag = 0

for i in range(1,K+1):
    if X == 0:
        break
    if X > 0:
        X = X - D
        if (K-i) % 2 == 0:
            X = abs(X)
            flag = 1
    else:
        X = X + D
        if (K-i) % 2 == 0:
            X = abs(X)
            flag = 1

    if flag:
        break

print(X)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

def main(x, k, d):
  for _ in range(k):
    if x >= 0:
      x -= d
    elif x < 0:
      x += d
  return x

if x == 0:
  if k % 2 == 0:
    print(x1)
    exit()
  else:
    if x1 >= 0:
      print(x1 - d)
      exit()
    else:
      print(x1 + d)
      exit()
ans = main(x, k - p, d)
print(abs(ans))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import math, statistics, collections

"""
    Author: Koushik Sahu
    Created: 02 September 2020 Wed 23:03:09
"""

if __name__ == '__main__':
    x, k, d = map(int, input().split(' '))

    x = abs(x)

    r = x%d
    l = r-d

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

Submission status: Wrong Answer

Solution code:
import math, statistics, collections

"""
    Author: Koushik Sahu
    Created: 02 September 2020 Wed 23:03:09
"""

if __name__ == '__main__':
    x, k, d = map(int, input().split(' '))

    x = abs(x)

    r = x%d
    l = r-d

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

Submission status: Wrong Answer

Solution code:
def resolve():
    P, N, D = map(int, input().split())
    P = abs(P)
    if P >= N * D:
        print(P - N * D)
        return

    if (N - (P // D)) % 2 == 0:
        print(P % D)
    else:
        print(abs(N % D - D))
        
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    P, N, D = map(int, input().split())
    P = abs(P)
    if P >= N * D:
        print(P - N * D)
        return

    if (N - (P / D)) % 2 == 0:
        print(P % D)
    else:
        print(abs(N % D - D))
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    P, N, D = map(int, input().split())
    if P // D > 0:
        P = P % D
        N = P // D
 
    for n in range(N % 2):
        P = min(P + D, P - D)
    print(abs(P))
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    P, N, D = map(int, input().split())
    if P // D > 0:
        P = P % D
        N = P // D

    for n in range(N % 2):
        P = min(P + D, P - D)
    print(abs(P))

Submission status: Wrong Answer

Solution code:
def resolve():
    P, N, D = map(int, input().split())
    for n in range(N):
        P = min(P+D, P-D)
    print(P)
resolve()

Submission status: Wrong Answer

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

y = abs(X)//D

if K<=y:
    print(abs(abs(X)-K*D))

else  :
    a = (X-y*D)
    b = (X-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)


#print(y+1)
#print(1000000000000000)

Submission status: Wrong Answer

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

y = abs(X)//D

if K<=y:
    print(abs(X-K*D))

else  :
    a = (X-y*D)
    b = (X-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)


#print(y+1)
#print(1000000000000000)

Submission status: Wrong Answer

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

y = X//D

if K<=y:
    print(abs(X-K*D))

else  :
    a = (X-y*D)
    b = (X-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)


#print(y+1)
#print(1000000000000000)

Submission status: Wrong Answer

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

y = X//D

if K<=y:
    print(X-K*D)

else  :
    a = (X-y*D)
    b = (X-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)

Submission status: Wrong Answer

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

if (X < 0):
	lo, hi = 0, K
	while (lo < hi):
		mid = int(lo+ (hi - lo) / 2)
		if (X + mid*D >= 0):
			hi = mid;
		else:
			lo = mid + 1;
	if (X + hi*D < 0):
		p1 = hi
		if (abs(X + p1*D) < result):
			result = abs(X + p1*D)
	else:
		p1 = lo
		rem1 = K - p1
		if (rem1%2 == 1):
			result = min((result, abs(X + p1*D + D), abs(X + p1*D - D)))
		p2 = lo - 1
		rem2 = K - p2
		if (rem2%2 == 1):
			result = min((result, abs(X + p2*D + D), abs(X + p2*D - D)))
	print(result)
elif (X > 0):
	lo, hi = 0, K
	while (lo < hi):
		mid = int(lo+ (hi - lo) / 2)
		if (X - mid*D <= 0):
			hi = mid;
		else:
			lo = mid + 1
	if (X - hi*D > 0):
		p1 = hi;
		if (abs(X - p1*D) < result):
			result = abs(X - p1*D)
	else:
		p1 = lo
		rem1 = K - p1
		if (rem1%2 == 1):
			result = min((result, abs(X - p1*D - D), abs(X - p1*D + D)))
		p2 = lo - 1;
		rem2 = K - p2
		if (rem2%2 == 1):
			result = min((result, abs(X - p2*D - D), abs(X - p2*D + D)))
	print(result)
else:
	print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
for n in range(k):
  a=abs(x-d)
  b=abs(x+d)
  if a<=b:
    x=x-d
  else:
    x=x+d
print(x)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
y = abs(x)//d
if y >= k:
        ans = abs(abs(x)-d*k)

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ans = abs(x + (-k)*d)
if k%2 ==0:

        for i in range(1,k+1):

            b = abs((2*i-k)*d)
            y = abs(x + b)
            ans = min(ans,y)
            if ans == 0:
                print(0)
                exit()
else:
        for i in range(1,(k+1)//2):

            b = abs((2*i-k)*d)
            y = abs(x + b)
            ans = min(ans,y)
            if ans == 0:
                print(0)
                exit()


print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
 
def func(a):
	return  X + (a * D) - (K-a)*D
  
x = []
for a in range(K):
  x.append(abs(func(a)))
  
print(int(min(x)))