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

Submission status: Wrong Answer

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

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

elif count%2 == 0:
  print(X-count*D)

elif count%2 != 0:
  print(abs(X-(X//D)*D - D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

elif count%2 == 0:
  print(X-count*D)

elif count%2 != 0:
  print(abs(X-(X//D)*D - D))

Submission status: Wrong Answer

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

straight = min(K, X // D)
K -= straight
X -= straight * D

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

Submission status: Wrong Answer

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

ans=1e+15

for i in range(K):
	init_X=X
	init_X+=i*D-(K-i)*D
	if abs(init_X)<ans:
		ans=abs(init_X)

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

left=X-K*D
right=X+K*D

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

elif right<=0:
	print(-right)
else:
	print(left)

Submission status: Wrong Answer

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

    def solve(x, k, d):
        to_l_max = x - (k * d)
        to_r_max = x + (k * d)

        if to_l_max > 0:
            return to_l_max
        if to_r_max < 0:
            return to_r_max

        current_x = x

        for i in range(k):
            current_x = min(abs(current_x + d), abs(current_x - d))
        return current_x

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_1()

Submission status: Wrong Answer

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

    def solve(x, k, d):
        to_l_max = x - (k * d)
        to_r_max = x + (k * d)

        if to_l_max > 0:
            return to_l_max
        if to_r_max < 0:
            return to_r_max

        current_x = x

        for i in range(k):
            current_x = min(abs(current_x + d), abs(current_x - d))
        print(current_x)

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_1()

Submission status: Wrong Answer

Solution code:
data = list(map(int, input().split()))
final_abs = 0
i = 0

while i < data[1]:
    if abs(data[0]) > data[2]:
        if data[0] > 0:
            data[0] -= data[2]
            i += 1
        else:
            data[0] += data[2]
            i += 1
    else:
        if i%2==0:
            break
        else:
            if data[0] >= 0:
                data[0] -= data[2]
                break
            else:
                data[0] += data[2]
                break

final_abs = abs(data[0])
print(final_abs)

Submission status: Wrong Answer

Solution code:

    
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    pass


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >0:
            x-=d

        
        elif abs(x-d)>=abs(x) and (k)%2==0:
            print(x)
            break
        elif abs(x-d)<=abs(x) and (k-1)%2==0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and (k-1)%2!=0:
            print(abs(x))
            break
        elif abs(x-d)>=abs(x) and (k)%2!=0:
            print(abs(x-d))
            break
        
        k-=1
        
    else:
        print(abs(x-d))

Submission status: Wrong Answer

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

x = A[0]
k = A[1]
d = A[2]

if (k - 1) % 2 == 0:
  plus = x + d
  minus = x - d
  if abs(plus) > abs(minus):
    x = minus
  else:
    x = plus
else:
  for i in range(2):
    plus = x + d
    minus = x - d
    if abs(plus) > abs(minus):
      x = minus
    else:
      x = plus

print(abs(x))

Submission status: Wrong Answer

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

ans=1e+15

for i in range(K):
	init_X=X
	init_X+=i*D-(K-i)*D
	if abs(init_X)<ans:
		ans=abs(init_X)

print(ans)

Submission status: Wrong Answer

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

if abs(X) < (K * D):
    a = abs(X) % D
    b = abs(D - a)
    c = abs(X) // D + 1
    if c % 2 == 0:
        goal = b
    else:
        goal = a
else:
    goal = abs(X) - ( D * K )

print(goal)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

x -= d * hi;
k -= hi;

if ( (k % 2) == 0):
  if (k == 0) :
    print(abs(x))
  else :
    print(min(abs(x), abs(x - d - d), abs(x + d + d)));
else :
  if (k == 1) :
    print(min(abs(x - d), abs(x + d)))
  else :
    print(min(abs(x - d), abs(x + d), abs(x + d + d + d), abs(x - d - d - d)));

Submission status: Wrong Answer

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

if abs(X) <= D * K:
    if 0 <= X:
        a = X % D
        b = a - D
        if abs(a) < abs(b):
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        if abs(a) > abs(b):
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
        if abs(a) == abs(b):
            goal = abs(a)
    else:
        a = -(abs(X) % D)
        b = a + D
        if abs(a) < abs(b):
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        if abs(a) > abs(b):
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
        if abs(a) == abs(b):
            goal = abs(a)

else:
    if 0 <= X:
        goal = X - D * K
    else:
        goal = X + D * K

print(goal)

Submission status: Wrong Answer

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

if abs(X) < D * K:
    if 0 <= X:
        a = X % D
        b = a - D
        if abs(a) <= abs(b):
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        else:
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
    else:
        a = -(abs(X) % D)
        b = a + D
        if abs(a) <= abs(b):
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        else:
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
else:
    if 0 <= X:
        goal = X - D * K
    else:
        goal = X + D * K

print(goal)

Submission status: Wrong Answer

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

x -= d * hi;
x = abs(x);
k -= hi;


if ( (k % 2) == 0):
  if (k == 0) :
    print(abs(x))
  else :
    print(min(abs(x), abs(x - d - d), abs(x + d + d)));
else :
  if (k == 1) :
    print(min(abs(x - d), abs(x + d)))
  else :
    print(min(abs(x - d), abs(x + d), abs(x + d + d + d), abs(x - d - d - d)));

Submission status: Wrong Answer

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

x -= d * hi;
x = abs(x);
k -= hi;


if ( (k % 2) == 0):
  if (k == 0) :
    print(abs(x))
  else :
    print(min(abs(x), abs(x - d - d), abs(x + d + d)));
else :
  if (k == 1) :
    print(min(abs(x - d), abs(x + d)))
  else :
    print(min(abs(x - d), abs(x - d), abs(x + d + d + d), abs(x - d - d - d)));

Submission status: Wrong Answer

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

x -= d * hi;
x = abs(x);
k -= hi;


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

Submission status: Wrong Answer

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

if abs(X) <= D * K:
    if 0 <= X:
        a = X % D
        b = a - D
        if abs(a) <= abs(b):
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        else:
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
    else:
        a = -(abs(X) % D)
        b = a + D
        if abs(a) <= abs(b):
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        else:
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
else:
    if 0 <= X:
        goal = X - D * K
    else:
        goal = X + D * K

print(abs(goal))

Submission status: Wrong Answer

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

lo = 0
hi = k

while (lo < hi) :
  mid = lo + (hi - lo) // 2 + 1
  if (x >= 0) :
    if (x - mid * d >= 0) : 
      lo = mid;
    else :
      hi = mid - 1
  else :
    if (x + mid * d <= 0) :
      lo = mid;
    else :
      hi = mid - 1;

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

Submission status: Wrong Answer

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

if abs(X) <= D * K:
    if 0 <= X:
        a = X % D
        b = a - D
        if abs(a) <= abs(b):
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        else:
            c = X // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
    else:
        a = -abs(X) % D
        b = a + D
        if abs(a) <= abs(b):
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(a)
            else:
                goal = abs(b)
        else:
            c = abs(X) // D
            d = K - c
            if d % 2 == 0:
                goal = abs(b)
            else:
                goal = abs(a)
else:
    if 0 <= X:
        goal = X - D * K
    else:
        goal = X + D * K

print(abs(goal))

Submission status: Wrong Answer

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

if abs(X) <= D * K:
    for i in range(K):
        a = abs(X) - (D * (i + 1))
        b = abs(X) - (D * i)
        if a < 0:
            break
    if abs(a) < abs(b):
        c = K - (i + 1)
        if c % 2 == 0:
            if 0 <= X:
                goal = a
            else:
                goal = -a
        else:
            if 0 <= X:
                goal = b
            else:
                goal = -b

    if abs(b) <= abs(a):
        c = K - i
        if c % 2 == 0:
            if 0 <= X:
                goal = b
            else:
                goal = -b
        else:
            if 0 <= X:
                goal = a
            else:
                goal = -a
else:
    if 0 <= X:
        goal = X - D * K
    else:
        goal = X + D * K

print(goal)

Submission status: Wrong Answer

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

if abs(X) <= D * K:
    for i in range(K):
        a = abs(X) - (D * (i + 1))#跨いだ後 a=-2 i+1=3
        b = abs(X) - (D * i)#跨ぐ直前b=1 i=2
        print(a,b)
        if a < 0:
            break
    if abs(a) < abs(b):
        c = K - (i + 1)
        if c % 2 == 0:
            if 0 <= X:
                goal = a
            else:
                goal = -a
        else:
            if 0 <= X:
                goal = b
            else:
                goal = -b

    if abs(b) <= abs(a):
        c = K - i
        if c % 2 == 0:
            if 0 <= X:
                goal = b
            else:
                goal = -b
        else:
            if 0 <= X:
                goal = a
            else:
                goal = -a
else:
    if 0 <= X:
        goal = X - D * K
    else:
        goal = X + D * K

print(goal)

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    X, K, D = [int(x) for x in input().strip().split()]
    ans = 0
    if abs(X) >= K * D:
        print(abs(X-K*D))
    else:
        X, K = X - D * (X // D), K - X // D
        if K % 2:
            print(abs(X-D))
        else:
            print(X)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 間違いコード
x,k,d = map(int,input().split())
ans = 0

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

Submission status: Wrong Answer

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

if K * D <= abs(X):
    res = X - K * D
else:
    sign = -1 if X < 0 else 1
    
    amari = abs(X) % D
    
    shou = abs(X) // D
    
    K -= shou
    
    
    
    if K % 2 == 0:
        res = amari
    else:
        if sign == 1:
            res = abs(amari - D)
        else:
            res = abs(amari + D)


print(res)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if x>0 and x-k*d>0:
  ans = x-k*d

elif x<0 and x+k*d>0:
  ans = x+k*d

else:
  if x < 0:
    x = -x

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

print(ans)

Submission status: Wrong Answer

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

#x zahyo   k k kaisu  d hohaba

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

Submission status: Wrong Answer

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

if A/D>K:
    ans = A - D*K
else:
    e = A//D-1
    K -= e
    A =e*D
    if K%2 == 1:
        K=abs(K-D)
    ans = K

print(ans)

Submission status: Wrong Answer

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

if A/D>K:
    ans = A - D*K
else:
    count = A//D-1
    K -= count
    A =count*D
    if K%2 == 1:
        K=abs(K-D)
    ans =K

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
pos, times, ranges = map(int, input().split())
first_try = abs(pos) // ranges

if first_try >= times: 
    print(abs(pos) - times * ranges)
else: 
    remain_times = times - first_try
    second_pos = abs(pos) - first_try * ranges
    if remain_times % 2 == 0: 
        print(second_pos)
    else:
        temp_next_pos = abs(second_pos - ranges)
        if temp_next_pos <= second_pos:
            print(temp_next_pos)
        else:
            print(second_pos + ranges)

Submission status: Wrong Answer

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

sum = A
if K%2 == 0:
    print(sum)
if K%2 != 0:
    for i in range(0,K%2):
        sum1 = abs(sum + D)
        sum2 = abs(sum - D)
        if sum1 < sum2:
            sum = sum1
        elif sum2 < sum1:
            sum = sum2
        elif sum2 == sum1:
            sum =sum1
    print(sum)

Submission status: Wrong Answer

Solution code:
pos, times, ranges = map(int, input().split())
first_try = pos // ranges

if first_try >= times: 
    print(pos - times * ranges)
else: 
    remain_times = times - first_try
    second_pos = pos - first_try * ranges
    if remain_times % 2 == 0: 
        print(second_pos)
    else:
        temp_next_pos = abs(second_pos - ranges)
        if temp_next_pos <= second_pos:
            print(temp_next_pos)
        else:
            print(second_pos + ranges)

Submission status: Wrong Answer

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

sum = A
for i in range(0,K%2):
    sum1 = abs(sum + D)
    sum2 = abs(sum - D)
    if sum1 < sum2:
        sum = sum1
    elif sum2 < sum1:
        sum = sum2
    elif sum2 == sum1:
        sum =sum1

print(sum)

Submission status: Wrong Answer

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

y = max(0, x)
t = abs(y)//d
ans = 0

if k <= t:
    ans = y - d*k
 
else:
    ans = y - d*t
    if k>t:
        if (k-t)%2 == 1:
            ans = abs(ans - d)

if x < 0:
    ans += x  
print(abs(ans))

Submission status: Wrong Answer

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >0:
            x-=d

        
        elif abs(x-d)>=abs(x) and (k)%2==0:
            print(x)
            break
        elif abs(x-d)<=abs(x) and (k-1)%2==0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and (k-1)%2!=0:
            print(abs(x))
            break
        elif abs(x-d)>=abs(x) and (k)%2!=0:
            print(abs(x-d))
            break
        
        k-=1
        
    else:
        print(abs(x-d))

Submission status: Wrong Answer

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

sum = A
for i in range(0,2):
    sum1 = abs(sum +L[1])
    sum2 = abs(sum -L[2])
    if sum1 < sum2:
        sum = sum1
    elif sum2 < sum1:
        sum = sum2
    elif sum2 == sum1:
        sum =sum1
print(sum)

Submission status: Wrong Answer

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >=0:
            x-=d

        
        elif abs(x-d)>=abs(x) and (k)%2==0:
            print(x)
            break
        else:
            print(abs(x-d))
            break
        
        k-=1
        
    else:
        print(x)

Submission status: Wrong Answer

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

t = float(abs(x/d))
ans = 0

#print(t)
if k <= t:
    ans = abs(x - d*k)

else:
    if (k-int(t))%2 == 0:
        ans = abs(x - d*int(t))
    else:
        ans = abs(x - d*int(t) - d)
        
print(abs(ans))

Submission status: Wrong Answer

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >=0:
            x-=d

        
        elif abs(x-d)>=abs(x) and (k)%2==0:
            print(x)
            break
        elif abs(x-d)>=abs(x) and (k)%2!=0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and (k-1)%2==0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and (k-1)%2!=0:
            print(abs(x))
            break
        k-=1
        
    else:
        print(x)

Submission status: Wrong Answer

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

t = float(abs(x/d))
ans = 0

#print(t)
if k <= t:
    ans = abs(x - d*k)

else:
    if (k-t)%2 == 0:
        ans = abs(x - d*int(t))
    else:
        ans = abs(x - d*int(t) - d)
        
print(abs(ans))

Submission status: Wrong Answer

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

t = abs(x//d)
ans = 0

#print(t)
if k <= t:
    ans = abs(x - d*k)

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

Submission status: Wrong Answer

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

t = abs(x)//d
ans = 0

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

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

Submission status: Wrong Answer

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >=0:
            x-=d
        elif abs(x-d)>=abs(x) and k%2==0:
            print(x)
            break
        elif abs(x-d)>=abs(x) and k%2!=0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and k%2==0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and k%2!=0:
            print(abs(x))
            break
        k-=1
    else:
        print(x)

Submission status: Wrong Answer

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

t = x//d
ans = 0

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

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

Submission status: Wrong Answer

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >=0:
            x-=d
        elif abs(x-d)>=abs(x) and k%2==0:
            print(x)
            break
        elif abs(x-d)>=abs(x) and k%2!=0:
            print(abs(x-d))
            break
        elif abs(x-d)<=abs(x) and k%2==0:
            print(abs(x-d))
            break
        elif abs(x-d)>=abs(x) and k%2!=0:
            print(abs(x))
            break
        k-=1
    else:
        print(x)

Submission status: Wrong Answer

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    prev = x
    while x>0 and k:
        k-=1
        prev = x
        x-=d
        
    print(min(abs(prev),abs(x)))

Submission status: Wrong Answer

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    prev = x
    while x>=0 and k:
        k-=1
        prev = x
        x-=d
        
    print(min(abs(prev),abs(x)))

Submission status: Wrong Answer

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

a = True

for i in range(k):
  if abs(x+d) > abs(x-d):
    nx = x+d
  else:
    nx = x-d
  if abs(x) <= abs(nx):
    if (k-i-1)%2 == 0:
      print(abs(nx))
      a = False
      break
    else:
      print(abs(x))
      a = False
      break
  else:
    x = nx

if a == True:
  print(abs(x))

Submission status: Wrong Answer

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

if x > x - k*d >= 0:
  print(x-k*d)
  exit()
if x < x + k*d <= 0:
  print(-x-k*d)
  exit()

x = abs(x)
if (x//d)%2 == 0:
  print(x % d)
  exit()

else:
  print(-(x % d - d))
  exit()

Submission status: Wrong Answer

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

if x > x - k*d > 0:
  print(x-k*d)
  exit()
if x < x + k*d < 0:
  print(-x-k*d)
  exit()

x = abs(x)
if (x//d)%2 == 0:
  print(x % d)
  exit()

else:
  print(-(x % d - d))
  exit()

Submission status: Wrong Answer

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

a = True

for i in range(k):
  nx = min(abs(x+d), abs(x-d))
  if x <= nx:
    if (k-i-1)%2 == 0:
      print(nx)
      a = False
      break
    else:
      print(x)
      a = False
      break
  else:
    x = nx

if a == True:
  print(x)

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 k%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(abs(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 k%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(abs(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(abs(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:
x, k, d = map(int, input().split())
x = abs(x)

if k == 1:
    print(abs(x - d))
    exit()

count = 0
while x > abs(x - d):
    x = abs(x - d)
    count += 1

y = abs(x - d)

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