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=X
if X>0:
    for i in range(K):
        x -= D
        if abs(x)<D:
            break
    
if X<0:
    for i in range(K):
        x += D
        if abs(x)<D:
            break

print(x)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

def func(i):
  if i >= 0: return i
  else: return -i

n = X // D
if K <= n: print(X - K * D)
else:
  X -= n * D
  K -= n
  if K % 2 == 0: print(X)
  else: print(func(-X + D))

Submission status: Wrong Answer

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

def func(i):
  if i >= 0: return i
  else: return -i

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

Submission status: Wrong Answer

Solution code:
def solve(string):
    x, k, d = map(int, string.split())
    x = abs(x)
    if k % 2:
        x = min(x, x - d, x + d, key=abs)
        k -= 1
    if x < k * d:
        return str(min(x % (2 * d), abs((x % (2 * d)) - 2 * d)))
    else:
        return str(x - k * d)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Wrong Answer

Solution code:
def solve(string):
    x, k, d = map(int, string.split())
    if k % 2:
        x = min(x, x - d, x + d, key=abs)
    if x > 0:
        return str(abs(min(range(x, max(-d, x - k * d) - 1, -2 * d), key=abs)))
    else:
        return str(abs(min(range(x, min(d, x + k * d) + 1, 2 * d), key=abs)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Wrong Answer

Solution code:
def solve(string):
    x, k, d = map(int, string.split())
    if x > 0:
        return str(min(range(x, -d, -d), key=abs))
    else:
        return str(min(range(x, d, d), key=abs))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
flag=True
X=abs(X)
I=X//D
X-=D*I
K-=I
if K<1:
    X+=D*K
    flag=False
ans=X
if flag:
    if K%2!=0:
        ans-=D
        ans=abs(ans)
        if ans>abs(X+D):
            ans+=D
            ans+=D
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# ABC172　C - Walking Takahashi
X,K,D=map(int,input().split())

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

Submission status: Wrong Answer

Solution code:
# ABC172　C - Walking Takahashi
X,K,D=map(int,input().split())

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

Submission status: Wrong Answer

Solution code:
# ABC172　C - Walking Takahashi
X,K,D=map(int,input().split())

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if K%2 == 1:
    ans = A - D
print(abs(ans))

Submission status: Wrong Answer

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

if K > 0:
    if K%2 == 1:
        ans = abs(A - D)
print(ans)

Submission status: Wrong Answer

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

if K > 0:
    if K%2 == 1:
        A = A - D
print(abs(A))

Submission status: Wrong Answer

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

if K > 0:
    if K%2 == 1:
        A = A - D
print(abs(A))

Submission status: Wrong Answer

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

if K > 0:
    if K%2 == 1:
        A = A - D
print(abs(A))

Submission status: Wrong Answer

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

print(abs(A))

Submission status: Wrong Answer

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

if K > 0:
    if K%2 == 1:
        A = A - D

print(abs(A))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X < 0: X = -X
ans = 0
if X - K*D >= 0:
  ans = X - K*D
else:
  K = K - math.floor(X / D)
  if K % 2 == 0: 
    ans = X - K*D
  else: 
    ans = D - (X - K*D)
    
print(ans)

Submission status: Wrong Answer

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

ans = min(abs(x-int(k/2)*d),min(abs(x - k*d + d),abs(x - ((k-int(k/2)) * d - int(k/2)*d))))

print(ans)

Submission status: Wrong Answer

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

ans = (x - k*d)%d

print(ans)

Submission status: Wrong Answer

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


nega = x-k*d
ans = abs(nega)
for i in range(0,int(k/2)+1):
	ans = min(ans,abs(nega + i*d))

print(ans)

Submission status: Wrong Answer

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

ans = 1000000000000000
negative = -n*d
for i in range(0,n+1):
	ans = min(ans,abs(x + negative + i*d))
	
print(ans)

Submission status: Wrong Answer

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

ans = 1000000000000000
negative = -n*d
for i in range(0,n):
	ans = min(ans,abs(x + negative + i*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:
  y=X//D
  z=K-y
  if z%2==0:
    print(K-y*D)
  else:
    print(abs(K-(y+1)*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:
  y=X//D
  z=K-y
  if z%2==0:
    print(K-y*D)
  else:
    print(K-(y+1)*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:
  y=X//D
  z=K-y
  if z%2==0:
    print(K-y*D)
  else:
    print(abs(K-(y+1)*D))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(g)

Submission status: Wrong Answer

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

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

print(g)

Submission status: Wrong Answer

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

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

print(g)

Submission status: Wrong Answer

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

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

print(g)

Submission status: Wrong Answer

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

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

print(g)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
ans=x
for i in range(k):
  x=abs(x-d)
  ans=min(x,ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if K - (X // D) < 0:
        X -= K * D
        print(X)
else:
        K = K - (X // D)
        X = X - (D * (X // D))

        if abs(X) >= D:
                X -= D
                X = abs(X)
                K -= 1
        else:
                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 K - (X // D) < 0:
        while 1:
                if K == 0:
                        print(abs(X))
                        break
                elif abs(X) >= D:
                        X -= D
                        X = abs(X)
                        K -= 1
                else:
                        if K % 2 == 0:
                                print(abs(X))
                        else:
                                print(abs(X-D))
                        break
else:
        K = K - (X // D)
        X = X - (D * (X // D))

        while 1:
                if K == 0:
                        print(abs(X))
                        break
                elif abs(X) >= D:
                        X -= D
                        X = abs(X)
                        K -= 1
                else:
                        if K % 2 == 0:
                                print(abs(X))
                        else:
                                print(abs(X-D))
                        break

Submission status: Wrong Answer

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

while 1:
        if K == 0:
                print(abs(X))
                break
        elif abs(X) >= D:
                X -= D
                X = abs(X)
                K -= 1
        else:
                if K % 2 == 0:
                        print(abs(X))
                else:
                        print(abs(X-D))
                break

Submission status: Wrong Answer

Solution code:
def walkingtakahashi(X, K, D):
    # x_curr = X
    # for i in range(K):
    #     if x_curr > 0:
    #         x_curr -= D
    #     else:
    #         x_curr += D
    # return abs(x_curr)
    if K < X//D:
        return X - D*K
    else:
        if (K - X // D) % 2 == 0:
            return X % D
        else:
            return abs(X % D - D)
if __name__ == "__main__":
    X, K, D = map(int, input().split())
    print(walkingtakahashi(X, K, D))

Submission status: Wrong Answer

Solution code:
def walkingtakahashi(X, K, D):
    # x_curr = X
    # for i in range(K):
    #     if x_curr > 0:
    #         x_curr -= D
    #     else:
    #         x_curr += D
    # return abs(x_curr)
    if K < X//D:
        return X - D*K
    elif (K - X // D) % 2 == 0:
        return X % D
    else:
        return abs(X % D - D)
if __name__ == "__main__":
    X, K, D = map(int, input().split())
    print(walkingtakahashi(X, K, D))

Submission status: Wrong Answer

Solution code:
def walkingtakahashi(X, K, D):
    # x_curr = X
    # for i in range(K):
    #     if x_curr > 0:
    #         x_curr -= D
    #     else:
    #         x_curr += D
    # return abs(x_curr)
    if (K - X // D) % 2 == 0:
        return X % D
    else:
        return abs(X % D - D)
if __name__ == "__main__":
    X, K, D = map(int, input().split())
    print(walkingtakahashi(X, K, D))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
cur=abs(x)
count=min(cur//d,k)
cur-=d*count
s=k-count
if s%2==1:
  cur-=-d
print(abs(cur))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = [int(i) for i in input().split()]
l = abs(k * d - abs(x))
print(min([abs(l % (2*d)), abs(l % (2*d) - 2*d)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = [int(i) for i in input().split()]
print(min([abs(x // d), abs(x // d - d)]))

Submission status: Wrong Answer

Solution code:
import math

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

X = abs(X)

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

Submission status: Wrong Answer

Solution code:
import math

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

X = abs(X)

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

Submission status: Wrong Answer

Solution code:
x,k,d=list(map(int,input().split()))
print(x,k,d)
result=abs(x)-k*d
swithn=0

if result<0:
    if k%2==1:
        x-=d
    result=min(x%(2*d),-x%(2*d))
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#移動の最小値(高速化問題)
x,k,d = map(int,input().split())
x = abs(x)
if (x//d) > k:
    ans = x - d*k
elif k- (x//d)%2 != 0:
    y = x-(x//d)*d
    ans = y
else:
    y = x-(x//d)*d
    ans = y-d
print(abs(ans))

Submission status: Wrong Answer

Solution code:
#移動の最小値(高速化問題)
x,k,d = map(int,input().split())
x = abs(x)
if (x//d) >=k:
    ans = x - d*k
else:
    y = x-(x//d)*d
    if k- (x//d)%2 != 0:
        ans = y
    else:
        ans = y-d
print(abs(ans))

Submission status: Wrong Answer

Solution code:
#移動の最小値(高速化問題)
x,k,d = map(int,input().split())
x = abs(x)
if (x//d) >=k:
    ans = x - d*k
else:
    y = x-(x//d)*d
    if k- (x//d)%2 != 0:
        ans = y
    else:
        ans = y+d
print(abs(ans))

Submission status: Wrong Answer

Solution code:
x,k,d=list(map(int,input().split()))
print(x,k,d)
result=abs(x)-k*d

if result<0:
    result%=2
print(result)

Submission status: Wrong Answer

Solution code:
#移動の最小値(高速化問題)
x,k,d = map(int,input().split())
x = abs(x)
if (x//d) >=k:
    ans = x - d*k
else:
    y = x-(x//d)*d
    if k- (x//d)%2 != 0:
        ans = y
    elif k-(x//d)%2 == 0:
        ans = y-d
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

#print(a,b)
#for i in range(k,0,-1):
#  if x > 0:
#    x -= d
#  else:
#    x += d * (i % 2)
#    break
print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x, k, d = map(int, input().split())
r = x%d
q = x//d
if k&1:
	if r < d-r:
		if q&1: print(r)
		else: print(d-r)
	else:
		if q&1: print(d-r)
		else: print(r)
else:
	if r < d-r:
		if q&1: print(d-r)
		else: print(r)
	else:
		if q&1: print(r)
		else: print(d-r)

Submission status: Wrong Answer

Solution code:
import sys; input = sys.stdin.readline
from math import ceil
x, k, d = map(int, input().split())
if k&1:
	r = x%d
	print(min(r, d-r))
else:
	r = x%d
	q = x//d
	if r < d-r:
		if q&1: print(d-r)
		else: print(r)
	else:
		if q&1: print(r)
		else: print(d-r)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int, input().split())
s=X
if X<0:
  s=X*(-1)
for i in range(K):
  a=X+i*D-(K-i)*D
  if a<0:
    a=a*(-1)
  if a<s:
    s=a
print(s)

Submission status: Wrong Answer

Solution code:
X,D,K=map(int, input().split())
s=X
if X<0:
  s=X*(-1)
for i in range(K):
  a=X+i*D-(K-i)*D
  if a<0:
    a=a*(-1)
  if a<s:
    s=a
print(s)

Submission status: Wrong Answer

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

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

while k > 0:
    x = min(abs(x + d), abs(x - d))
    k -= 1
print(x)

Submission status: Wrong Answer

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

k_ = x // d
k = k - k_
x = x-k*d

while k > 0:
    x = min(abs(x + d), abs(x - d))
    k -= 1
print(x)

Submission status: Wrong Answer

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

if a < 0:
    x = -a
else:
    x = a

y = x % d
l = x // d
m = k - l

if m < 0:
    ans = x - (k * d)
elif m % 2 ==0:
    ans = y
else :
    ans = y - d

Submission status: Wrong Answer

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

y = x % d
l = x // d
m = k - l

if m <= 0:
    ans = x - (k * d)
elif m % 2 ==0:
    ans = y
else :
    ans = y - d

print(abs(ans))

Submission status: Wrong Answer

Solution code:
# C - Walking Takahashi

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

d = X // D
m = X % D

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

Submission status: Wrong Answer

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

o_count = 0
rest = 0

# x:位置　k：回数　d:距離

#往復に辿りつく前に終わるパターン
if(abs(x)-d*k >= 0):
    print(abs(abs(x)-d*k))
#0またいで往復するパターン
else:
    #往復直前までにかかる回数
    r_count = x//d
 
    #残り捜査回数
    rest = k - r_count

    #残り回数の奇遇によって計算を判断
    
    #残り回数が偶数の時はr_count分動かした時と変わらない
    if(rest %2 == 0):
        print(abs(abs(x) - d*r_count))
    #残り回数が奇数:さらにD移動した分？
    else:
        print(abs(abs(abs(x) - d*r_count)-d))

Submission status: Wrong Answer

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

o_count = 0
rest = 0

# x:位置　k：回数　d:距離

#往復に辿りつく前に終わるパターン
if(abs(x)-d*k >= 0):
    print(abs(abs(x)-d*k))
#0またいで往復するパターン
else:
    #往復直前までにかかる回数
    r_count = x//d
 
    #残り捜査回数
    rest = k - r_count

    #残り回数の奇遇によって計算を判断
    
    #残り回数が偶数の時はr_count分動かした時と変わらない
    if(rest %2 == 0):
        print(abs(x) - d*r_count)
    #残り回数が奇数:さらにD移動した分？
    else:
        print(abs(abs(abs(x) - d*r_count)-d))

Submission status: Wrong Answer

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

o_count = 0
rest = 0

# x:位置　k：回数　d:距離

#往復に辿りつく前に終わるパターん
if(abs(x)-d*k > 0):
    print(abs(x)-d*k)
#0またいで往復するパターン
else:
    #往復直前までにかかる回数
    r_count = x//d
 
    #残り捜査回数
    rest = k - r_count

    #残り回数の奇遇によって計算を判断
    
    #残り回数が偶数の時はr_count分動かした時と変わらない
    if(rest %2 == 0):
        print(abs(x) - d*r_count)
    #残り回数が奇数:さらにD移動した分？
    else:
        print(abs(abs(abs(x) - d*r_count)-d))

Submission status: Wrong Answer

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

o_count = 0
rest = 0

# x:位置　k：回数　d:距離

#往復に辿りつく前に終わるパターん
if(abs(x)-d*k >= 0):
    print(abs(x)-d*k)
#0またいで往復するパターン
else:
    #往復直前までにかかる回数
    r_count = x//d
 
    #残り捜査回数
    rest = k - r_count


    #残り回数の奇遇によって計算を判断
    
    #残り回数が偶数の時はr_count分動かした時と変わらない
    if(rest %2 == 0):
        print(abs(x) - d*r_count)
    #残り回数が奇数:さらにD移動した分？
    else:
        print(abs(abs(abs(x) - d*r_count)-d))

Submission status: Wrong Answer

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

o_count = 0
rest = 0

# x:位置　k：回数　d:距離

#往復に辿りつく前に終わるパターん
if(abs(x)-d*k >= 0):
    print(abs(x)-d*k)
#0またいで往復するパターン
else:
    #往復直前までにかかる回数
    r_count = x//d
 
    #残り捜査回数
    rest = k - r_count

    #残り回数の奇遇によって計算を判断
    
    #残り回数が偶数の時はr_count分動かした時と変わらない
    if(rest %2 == 0):
        print(abs(x) - d*r_count)
    else:
        print(abs(abs(x) - d*r_count - d))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

if x > 0:
    sgn_x = 1
    x_greedy = x - k * d
else:
    sgn_x = -1
    x_greedy = x + k * d

if x_greedy > 0:
    sgn_x_greedy = 1
else:
    sgn_x_greedy = -1

if sgn_x == sgn_x_greedy:
    print(abs(x_greedy))
    exit()

x_r = x % d
x_l = -(x_r - d)
print(min(x_r, x_l))

Submission status: Wrong Answer

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


n = abs(x) // d
if n > k:
    ans = x - k*d
else:
    a = x - n*d
    if (k - n) % 2:
        ans = abs(a - d)
    else:
        ans = abs(a)

print(ans)

Submission status: Wrong Answer

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


n = int(x / d + 0.5)
if n > k:
    ans = x - k*d
else:
    a = x - n*d
    if (k - n) % 2:
        if a > 0:
            ans = a - d
        else:
            ans = a + d
    else:
        ans = a

print(abs(ans))

Submission status: Wrong Answer

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


n = int(x / d + 0.5)
if n > k:
    ans = x - k*d
else:
    a = x - n*d
    if (k - n) % 2:
        if a > 0:
            ans = a - d
        else:
            ans = a + d
    else:
        ans = a
        
print(ans)

Submission status: Wrong Answer

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

for i in range(math.ceil(math.log2(k)) + 1):
    if abs(ans-d) < abs(ans+d):
        ans = abs(ans-d)
    else:
        ans = abs(ans+d)
        
print(ans)

Submission status: Wrong Answer

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

for i in range(math.ceil(math.log2(k))):
    if abs(ans-d) < abs(ans+d):
        ans = abs(ans-d)
    else:
        ans = abs(ans+d)
        
print(ans)

Submission status: Wrong Answer

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

a = abs(x)//d

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
##C - Walking Takahash
##正負が反転しないなら単調増加、減少
##反転するなら、反転した瞬間で残りの回数が偶数なら、その値、奇数なら、一つ前にの値
X,K,D = map(int,input().split())
R = K - X // D
##X1の正負フラグがあれば簡潔に書けるかも
if X < 0:
    X = -X
if X > 0 and X-K*D >= 0:
    print(X-K*D)
else:
    if R % 2 == 0:
        print(X - D*(X//D))
    else:
        print(-X + D*((X//D)+1))

Submission status: Wrong Answer

Solution code:
##C - Walking Takahash
##正負が反転しないなら単調増加、減少
##反転するなら、反転した瞬間で残りの回数が偶数なら、その値、奇数なら、一つ前にの値
X,K,D = map(int,input().split())
R = K - X // D
##X1の正負フラグがあれば簡潔に書けるかも
if X < 0:
    X = -X
if X > 0 and X-K*D >= 0:
    print(X-K*D)
else:
    if R % 2 == 0:
        print(X - D*(X//D))
    else:
        print(X - D*((X//D)+1))