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:
import sys
input = sys.stdin.readline


def print_ans(X, K, D):
    """Test Case
    >>> print_ans(6, 2, 4)
    2
    >>> print_ans(7, 4, 3)
    1
    >>> print_ans(10, 1, 2)
    8
    >>> print_ans(1000000000000000,1000000000000000,1000000000000000)
    1000000000000000
    """
    # 移動しない
    if K % 2 == 0 and D >= abs(X):
        print(abs(X))
    elif K % 2 != 0 and D >= abs(X):
        print(min(abs(X-D), abs(X+D)))
    elif X - D * K >= 0:
        print(abs(X - D * K))
    elif -X + D * K <= 0:
        print(abs(-X + D * K))
    else:
        # near 0
        tmp = abs(X) // D

        # X > 0
        ans = min(abs(abs(X) - tmp * D), abs(abs(X) - (tmp + 1) * D))
        print(ans)


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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
while k!=0:
    if x-d==0:
        if k %2 ==0:
            break
        else:
            x=x-d
            break
    if x-d < 0 :
        break
    else :
        x=x-d
        k=k-1
print (x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Y = X // D
if K > Y:
  A = Y
  if A % 2 != 0:
    if X > 0:
      X += D
    else:
      X -= D
else:
  A = K

print( abs( X ) - abs( A * D ) )

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# cook your dish here
# your code goes here
a, b, c = input().split()
x=int(a)
k=int(b)
d=int(c)
val=(k*d-x)/(2*d)
fs=int(val)
# print(fs)
if fs<0:
	ans=abs(x-k*d)
elif fs==0:
	ans1=abs(x-k*d)
	ans2=abs(x-k*d+2*d)
	ans=min(ans1,ans2)
else:
	ans1=abs(x-k*d+2*(fs-1)*d)
	ans2=abs(x-k*d+2*fs*d)
	ans3=abs(x-k*d+2*(fs+1)*d)
	ans=min(ans1,ans2,ans3)
print(ans)

Submission status: Wrong Answer

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


if k >= x//d:
    k -= x//d
    if x >= 0:
        diff = abs(x-d*(x//d))
    else:
        diff = abs(x+d*(x//d))

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

Submission status: Wrong Answer

Solution code:
X, K, D = [int(x) for x in input().strip().split(" ")]

X = abs(X)
k, x = divmod(X, D)
if K >= k:
    X = x
    K -= k
    if k % 2 == 0:
        print(abs(X))
    elif X < 0:
        print(abs(X + D))
    else:
        print(abs(X - D))
elif X < 0:
    print(abs(X + D * K))
else:
    print(abs(X - D * K))

Submission status: Wrong Answer

Solution code:
import sys
import itertools

if __name__ == '__main__':
    # 初期位置、移動回数、一回の移動量
    X, K, D = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split()))

    oldX = X
    lasti = 0
    # 7-3=4
    # 4-3=1
    # 1-3=-2
    for i in range(K):
        X -= D
        if abs(X) > abs(oldX):
            X = oldX
            lasti = i
            break
        oldX = X
    else:
        print(abs(X))
        exit()

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Y = X // D

if Y<=K:
  K -= Y
  X = X % D

  
for i in range(K):
  if 0<X:
    X -= D
  elif X<0:
    X += D
  else:
    if (i+Y)%2==0:
      break
    else:
      X += D
      break

print(abs(X))

Submission status: Wrong Answer

Solution code:
x,k,d = [int(p) for p in input().split()]
x=abs(x)
ans = min(x%d,d-x%d);
if(ans==0):
    p = x//d
    t = k;
    k-=p;
    if(k<0):
        print((x-t*d))
    elif(k%2==0):
        print(0)
    else:
        print(d)
elif(k*d>x):
    p = (x+d-1)//d
    t = k
    k-= p
    rem = abs(x-p*d)
    if(rem==ans):
        if(k%2==0):
            print(ans)
        else:
            print(d-rem)
    else:
        k-=1
        if(k<0):
            print(rem)
        elif(k%2==0):
            print(ans)
        else:
            print(d-rem)
else:
    print((x-k*d))

Submission status: Wrong Answer

Solution code:

def main():
    x,k,d = map(int,input().split())
    x = abs(x)
    first_move = abs(x // d)
    if k <= first_move:
        print(x - d * k)
        return

    if abs(x - d * first_move) > abs(x - d * first_move -d):
        first_move += 1
        x = abs(x - d*first_move - d)
    else:
        x = abs(x - d*first_move)

    left_move = k - first_move

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

if x < 0:
    x = -1 * x

if x // d > k - 1:
    print(x - k * d)
else:
    p = x // d
    neibor_1 = x - d * p

    if neibor_1 > 0:
        neibor_2 = x - d * (p + 1)
    else:
        neibor_2 = neibor_1 + d

    if abs(neibor_2) > abs(neibor_1) and (k - p) % 2 == 0:
        print(abs(neibor_1))
    else:
        print(abs(neibor_2))

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
import math
a1, a2 = min(K, math.ceil(X / D)), min(K, math.floor(X / D))
if a1 == K or a2 == K:
	X = abs(X - (K * D))
	K = 0
elif abs(X - (a1 * D)) <= abs(X - (a2 * D)):
	K -= a1
	X -= (a1 * D)
else:
	K -= a2
	X -= (a2 * D)
if K % 2 == 0:
	print(abs(X))
else:
	print(abs(abs(X) - D))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
a = 0
b = k + 1
mid = 0
if x < 0:
    x *= -1
while b - a > 1:
    mid = (a + b) >> 1
    if x - d * mid >= 1:
        a = mid
    else:
        b = mid

ans = x - a * d

if a < k:
    y = abs(ans - d)
    if y >= 1 and y < ans:
        ans = y
if x == 0:
    ans = d
print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = x - (a * d)
    else:
        x = x - (a * d)
        if x >= 0:
            x = x - abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split())
if(a<0):
    aa=-a
else:
    aa=a
if(aa>b*c):
    ans=aa-b*c
    print(ans)
else:
    amari=aa%c
    amarioruta=abs(amari-c)
    if(amari==0):
        hantei=aa/c
        if(hantei%2==0):       
            bb=b%2
            if(bb==0):
                print(0)
            else:
                print(c)
        else:
            bb=b%2
            if(bb==0):
                print(c)
            else:
                print(0)
    else:
        if(aa<=b*c<=aa+b):
            muri1=b*c-aa
            print(muri1)
        else:
            if(amari>amarioruta):
                print(amarioruta)
            else:
                print(amari)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

    mostl = X - K * D
    mostr = X + K * D
    if mostl*mostr > 0:
        print(min(mostl, mostr))
        exit()

    ans=X
    if K%2 == 1:
        X -= D
        K -= 1
        ans = min(ans, X)

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

    print(ans)

    return


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
if K * D >= X:
    cgoal = X % D
    fgoal = abs(abs(cgoal) - D)
    times2cgoal = (X - cgoal) // D
    if times2cgoal % 2 == K % 2:
        print(min(cgoal, fgoal))
    else:
        print(fgoal)
else:
    print(X - K * D)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
m = 0
s = 0
if abs(X) > K * D:
    # for i in range(K):
    if X > 0:
        m = X - (K * abs(D))
    else:
        m = X + (K * abs(D))
else:
    if X == 0:
        if X % 2 == 1:
            m = D
        else:
            m = 0
        print(m)
        exit()
    else:
        if X > 0:
            s = K * D - X       
        else:
            s = K * D + X

        if s > D:
            m = abs(X) % abs(D)
        else:
            m = s

print(m)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


def print_ans(X, K, D):
    """Test Case
    >>> print_ans(6, 2, 4)
    2
    >>> print_ans(7, 4, 3)
    1
    >>> print_ans(10, 1, 2)
    8
    >>> print_ans(1000000000000000,1000000000000000,1000000000000000)
    1000000000000000
    """
    # 移動しない
    if K % 2 == 0 and D >= X:
        print(abs(X))
    elif K % 2 != 0 and D >= X:
        print(min(abs(X-D), abs(X+D)))
    elif X - D * K >= 0:
        print(abs(X - D * K))
    elif -X + D * K <= 0:
        print(abs(-X + D * K))
    else:
        # near 0
        tmp = abs(X) // D

        # X > 0
        ans = min(abs(abs(X) - tmp * D), abs(abs(X) - (tmp + 1) * D))
        print(ans)


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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
xkd = input('').split()
x = int(xkd[0])
k = int(xkd[1])
d = int(xkd[2])

if x > k*d:
    print(x-(k*d))
else:
    # まずできるだけ近づく
    step1 = int(x%d)
    d1 = x - step1*d
    # 0をまたいだ先のほうが絶対値小さければ、そこが目的地
    if abs(d1) > abs(d1-d) and step1+1<k:
        d1 = abs(d1-d)
        step1 +=1
    # 消化ステップの残りを計算
    step2 = (k - step1)%2
    if step2 == 0:
        print(abs(d1))
    else:
        if abs(d1-d) > abs(d1+d):
            print(abs(d1+d))
        else:
            print(abs(d1-d))

Submission status: Wrong Answer

Solution code:
import math

start, kai, step = map(int, input().split(" "))

if step < start - (kai * step):
    print(abs(start - (kai * step)))
    exit()

ans1 = start % step
ans2 = abs(ans1 - step)
if (kai - abs(start // step)) % 2 == 0:
    print(abs(ans1))
else:
    print(abs(ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

goal=X-K*D
y1=X%D
y2=y1-D

if y1<=goal:
    print(int(goal))
else:
    if y1<=abs(y2):
        L=(X-y1)/D
    else:
        L=(X-y2)/D
    if L%2==0:
        print(int(min(y1,abs(y2))))
    else:
        print(int(max(y1,abs(y2))))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def f(v, x, d, k):
  return x + d * (2 * v - k)


def swap(x, y):
  r = x
  x = y
  y = r


lt = input().split(' ')
x = int(lt[0])
k = int(lt[1])
d = int(lt[2])

lf = 1
r = k

# for i in range(k):
#   print(f(i, x, d, k))

ans = abs(f(0, x, d, k))

while (lf <= r):
  mid = (lf + r) // 2
  cur = f(mid, x, d, k)
  nt = f(mid - 1, x, d, k)
  # print('mid = ', mid)
  cur = abs(cur)
  nt = abs(nt)
  # print('cur = ', cur)
  # print('nt = ', nt)
  # print('')
  swap(nt, cur)
  if (nt > cur):
    ans = min(ans, abs(cur))
    r = mid - 1
  else:
    lf = mid + 1
    ans = min(ans, abs(nt))

print(abs(ans))

Submission status: Wrong Answer

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

t = abs(X) // D
now = X - t*D

if t <= K:
    if (K - t) % 2 == 0:
        ans = now
    else:
        ans = abs(now - D)
else:
    ans = X - K*D

print(ans)

Submission status: Wrong Answer

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

if (x//d)<=k:
  k-=x//d
  x-=(x//d)*d
else:
  x-=d*k
  print(abs(x))
  exit()

for i in range(k):
  tmp=x
  x-=d
  if abs(x) >= abs(tmp):
    if (k-i)%2==0:
      print(abs(tmp))
      exit()
    else:
      print(abs(x))
      exit()
  else:
    pass
else:
  print(abs(x))

Submission status: Wrong Answer

Solution code:

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if x > 0:
    if abs(x-d) < x:
        div = x//d
        if div > k:
            print(x-d*k)
            sys.exit()
        else:
            if k%2 == 0:
                print(x)
                sys.exit()
            else:
                print(x-d)
                sys.exit()
    else:
        if k%2 == 0:
            print(x)
            sys.exit()
        else:
            print(x-d)
            sys.exit()
else:
    if abs(x+d) < x:
         div = x//d
         if div > k:
             print(x+d*k)
             sys.exit()
         else:
             print(x+d)
             sys.exit()
    else:
        if k%2 == 0:
            print(x)
            sys.exit()
        else:
            print(x+d)
            sys.exit()

Submission status: Wrong Answer

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

    r, m = divmod(X, D)

    if r <= K:
        mi = m
        K -= r
    else:
        mi = X - D*K
        K = 0
    
    K = K % 2

    if K == 0:
        print(mi)
    else:
        print(min(abs(mi-D), abs(mi+D)))

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

Solution code:
def Qc():
    x, k, d = map(int, input().split())
    x = abs(x)
    print(x,k,d)
    for v in range(k):
        x -= d
        ans = x
        if x <= 0:
            ans = min(abs(x-d), abs(x+d))
            break
    print(ans)


if __name__ == '__main__':
    Qc()

Submission status: Wrong Answer

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

q, mod = divmod(abs(X), abs(D))

ans = 0
if q < K:
    if mod == 0:
        if q % 2 == 1:
            ans = D - mod
        else:
            ans = mod
    else:
        ans = mod
else:
    div = q - K
    ans = div * D + mod

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
x,k,d=map(int,input().split())
if abs(x)>k*d:
  print(abs(x-k*d))
else:
  if k%2==1:
    if x>0:
      x-=d
    else:
      x+=d
  print(min(x%(2*d),2*d-x%(2*d)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if __name__ == "__main__":
  li = list(map(int, input().split()))
  s = li[0]
  for i in range(li[1]):
    if s >= li[2]:
      s = s - li[2]
    else:
      s = s + li[2]
  print(abs(s))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x % d > abs(d - x % d):
    mint = x // d + 1
else:
    mint = x // d

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    if (K-q)%2 == 1:
        a1 = mod - D
        a2 = mod + D
        ans = min(abs(a1),abs(a2))
    else:
        ans = mod

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

R = X // D
Q = X - D * K

if K <= R:
  result = Q
else:
  O = R - K
  O %= 2
  P = X - D * R
  if O == 0:
    result = P
  else:
    result = abs(P - K)

print(result)

Submission status: Wrong Answer

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

if X > 0:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X - D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X- D*tmp
        X2 = X1 - D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
elif X < 0:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X + D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X + D*tmp
        X2 = X1 + D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
else:
    if K %2 == 0:
        ans = 0
    else :
        ans = D
print(abs(ans))

Submission status: Wrong Answer

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

while i > 0:
  if x >= 0:
    x -= d
  else:
    x += d
  i -= 1
  if x <= d or x >= -d:
    i = i % 2

print(abs(x))

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
elif abs(x)<=d:
    if k%2==0:
        ans=abs(x)
    else:
        if x<=0:
            ans=x+d
        else:
            ans=d-x
else:
    for i in range(1,k+1):
        if abs(x)<=i*d:
            if (i-1)%2==0:
                ans=abs(x)-(i-1)*d
                break
            else:
                ans=abs(x-(i-1)*d-d)
                break
print(ans)

Submission status: Wrong Answer

Solution code:
import math

start, kai, step = map(int, input().split(" "))

if step < start - (kai * step):
    print(start - (kai * step))
    exit()

ans1 = start % step
ans2 = abs(ans1 - step)
if (kai - abs(start // step)) % 2 == 0:
    print(abs(ans1))
else:
    print(abs(ans2))

Submission status: Wrong Answer

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

d = X // D

if X < 0:
    X *= -1

if d <= K+1:
    a1 = X - D*d
    a2 = D*(d+1) - X
    print(min(a1, a2))
elif d == K:
    print(X - D*d)
else:
    print(X - D*K)

Submission status: Wrong Answer

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

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

lowest_value = X

lst_of_values = ["empty"]

while K != 0:
    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1
        if lowest_value == lst_of_values[-1]:
            break
        else:
            lst_of_values.append(lowest_value)
    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
        if lowest_value == lst_of_values[-1]:
            break
        else:
            lst_of_values.append(lowest_value)
    else:
        break
print(lowest_value)

Submission status: Wrong Answer

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

k_min=abs(x//d)
if k_min>=k:
  k_min=k
if x>0:
  d_min=x-((k_min)*d)
else:
  d_min=x+((k_min)*d)

dk=k-k_min
if dk%2==0:
  print(d_min)
else:
  print(abs(min(d_min+d,d_min-d)))

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split())
if(a<0):
    aa=-a
else:
    aa=a
if(aa>b*c):
    ans=aa-b*c
    print(ans)
else:
    amari=aa%c
    amarioruta=abs(amari-c)
    if(amari==0):
        hantei=a/c
        if(hantei%2==0):       
            bb=b%2
            if(bb==0):
                print(0)
            else:
                print(c)
        else:
            bb=b%2
            if(bb==0):
                print(c)
            else:
                print(0)
    else:
        if(aa<=b*c<=aa+b):
            muri1=b*c-a
            print(muri1)
        else:
            if(amari>amarioruta):
                print(amarioruta)
            else:
                print(amari)

Submission status: Wrong Answer

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

if x2 - k * d > 0:
    print(x2 - k * d)
    exit()

for i in range(k):
    if x < 0:
        ans = min(abs(x),abs(x + d))
        i2 = i
        if i2 % 2 == k % 2:
            #print("a")
            print(abs(x))
            exit()
        else:
            #print("b")
            print(x + d)
            exit()
    x = x - d

print(min(abs(x),x+d))

Submission status: Wrong Answer

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

if x == 0 and k % 2 == 0:
  ans = 0
elif x == 0 and k % 2 == 1:
  ans = d
elif x > 0 and x - k * d >= 0:
  ans = x - k * d
elif x < 0 and x + k * d <= 0:
  ans = x + k * d

if x > 0 and x - k * d < 0:
  limit = int(x // d)
  a = x - limit * d
  b = a - d
  if limit % 2 == k % 2:
    ans = a
  else:
    ans = abs(b)
  
if x < 0 and x + k * d > 0:
  limit = int(abs(x) // d)
  a = x + limit * d
  b = a + d
  if limit % 2 == k % 2:
    ans = abs(a)
  else:
    ans = b

print(ans)

Submission status: Wrong Answer

Solution code:
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
with decimal.localcontext() as ctx:
    ctx.prec = 150
    t=floor(decimal.Decimal(abs(x))/decimal.Decimal(d))
    

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 17:44:21 2020

@author: Paras
"""

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

moves = abs(x)//d
if moves>k:
    print(abs(x)-(k*d))
    
elif moves==k:
    print("0")
    
else:
    closest1 = abs(x)-moves*d
    closest2 = closest1-d
    if closest1==abs(closest2):
        if closest1==0:
            leftmoves = k-moves
            if leftmoves%2==0:
                print("0")
            else:
                print(closest1+d)
        else:
            print(closest1)
    else:
        if (k-moves)%2==0:
            print(abs(closest1))
        else:
            print(abs(closest2))