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 0 <= X:
    ans = X-D*K
    if 0 < ans:
        print(ans)
        exit(0)
if X < 0:
    ans = abs(X+D*K)
    if 0 < ans:
        print(ans)
        exit(0)

for i in range(K):
    if 0 <= X:
      absX = abs(X - D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X - D*(i+1))
              print(ans)
              exit(0)
    if X < 0:
      absX = abs(X + D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X + D*(i+1))
              print(ans)
              exit(0)

Submission status: Wrong Answer

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

print (x)

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:
    a = x // d
    z1 = x - a*d
    z2 = x - (a+1)*d
    if x - k*d == 0:
        print("0")
    elif z1 == 0 and (k-a) % 2 == 1:
        print(abs(z2))
    else:
        b = min(abs(z1), abs(z2))
        print(abs(b))

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:
  print(abs(x))

Submission status: Wrong Answer

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

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

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

if a < 0:
   a = 0

def f(x):
   return X - D * K + 2 * D * x

result = min([abs(f(a)), abs(f(a+1))])

print(result)

Submission status: Wrong Answer

Solution code:
def function(X, K, D):
    kai = int(abs(X)/D)
    if (abs(X) % D == 0) and (abs(X)/D <= K):
        if K % 2 == 1:
            ans = D
        else:
            ans = X
    elif kai >= K:
        if X > 0:
            ans = X-K*D
        else:
            ans = X+K*D
    else:
        K = K-kai
        if X > 0:
            X = X-kai*D
        else:
            X = X+kai*D
        if X > 0:
            if abs(X-D) < X:
                ans = X-D
            else:
                ans = X
        else:
            if abs(X+D) < abs(X):
                ans = X+D
            else:
                ans = X
    return ans


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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split()) 
k = b*c

if abs(a) > 0:
    l = abs(a)
    if l > k:
        print(l-k)
    if l == k:
        print(0)
    if l < k:
        d = l//c
        e = b - d
        if e%2== 0:
            print(l-c*d)
        else:
            print((c*d)+c -l)
if a == 0:
    if b%2==0:
        print(c)
    else:
        print(0)

Submission status: Wrong Answer

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

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

k = K - k1
x = X % D

while k > 0:
    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()
    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
jud = x//d 
if(k<=jud+1):
  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 == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
        
elif X > 0 :
    if X // D >= K:
        print(X - K*D)
    else:
        K -= X // D
        X %= D
        if K % 2 == 0:
            print(X)
        else:
            print(abs(X-D))
else:
    if (-X // D) >= K:
        print(X + K*D)
    else:
        X, K = X + (-X // D)*D , K - (-X // D)
        if K % 2 == 0:
            print(-X)
        else:
            print(abs(X+D))

Submission status: Wrong Answer

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


if K%2 == 0:
    ans = abs(X-(D*2))
    print(ans)
    exit()
else:
    ans = abs(X-D)
    while X >= abs((X-ans)-D) and cnt <= K:
        ans = abs(ans-D)
        cnt += 1
    print(ans)
    exit()

Submission status: Wrong Answer

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

# print(x,k,d)

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

c = min(x//d, k)
x = abs(x-d*c)
k = k-c
# print(k,c,x)
if x == 0:
    if k%2==0: # even
        print(0)
    else: # odd
        print(d)

else: # +
    print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

  if  f==0:
    if k%2==1:
      print(abs(f+d))
    else:
      print(0)

Submission status: Wrong Answer

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

c=0
while c<k:
  if abs(x-d)<abs(x+d):
    while abs(x-d)<abs(x+d):
      x-=d
      c+=1
  if abs(x+d)<abs(x-d):
    while abs(x+d)<abs(x-d):
      x+=d
      c+=1
  else:
    x+=d
    c+=1
print(abs(x))

Submission status: Wrong Answer

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

sm = abs(x)//d

if abs(x)-sm*d>0 :
    ans = x-sm*d 
elif abs(x)-d*k>0:
    ans = abs(x)-d*k
elif (k-sm)%2==0:
    ans = min(abs(x)-sm*d,abs(abs(x)-sm*d-2*d))
else:
    ans = min(abs(abs(x)-sm*d-d),abs(x)-sm*d+d)

print(abs(ans))

Submission status: Wrong Answer

Solution code:
[X,K,D]=list(map(int, input().split()))
abx=round(abs(X))
n=round(abx//D)
if n>K:
    if X>0:
        ans=X-K*D
    else:
        ans=X+K*D
else:
    x2=abx-n*D
    x1=(n+1)*D-abx
    if x2>=x1:
        if (K-n-1)%2==0:
            ans=x1
        else:
            ans=x2
    else:
        if (K-n)%2==0:
            ans=x2
        else:
            ans=x1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil, floor
X, K, D = map(int, input().split())
ans = abs(X)
fapp = (K*D - X)/(2*D)

if fapp < 0:
    list = [0]

else:
    list = [ceil(fapp), floor(fapp)]
for f in list:
    abso = abs(X + f*D - (K-f)*D)
    if abso < ans:
        ans = abso

print(ans)

Submission status: Wrong Answer

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

if X % D == 0:
    cnt = X // D
else:
    cnt = (X // D) + 1

if cnt >= K:
    ans = abs(X) - D * K
else:
    re = cnt - K
    if re % 2 == 0:
        ans = abs(X) - D * cnt
    else:
        ans = abs(X) - D * cnt + D

print(abs(ans))

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 == 0:
            ans = mod
        else:
            ans = D
    else:
        if q % 2 == 0:
            ans = mod
        else:
            ans = D - mod
else:
    div = q - K
    ans = div * D + mod

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 = abs(x) - abs(a * d)
    else:
        x = abs(x) - abs(a * d)

print(abs(x))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
if X<=D and D%2==0:
  print(X)
  exit()
elif X<=D and D%2==1:
  print(abs(X-D))
  exit()

s = X
if (-1)*X<=X-K*D:
  for i in range(K):
    s = min(X,abs(K-D))
print(s)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = input().split()
X = int(X)
K = int(K)
D = int(D)
a = X - K
b = K - D
if a < 0:
    a = -a
if b < 0:
    b = -b
if a < b:
    print(a)
else:
    print(b)

Submission status: Wrong Answer

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

X = abs(X)

n = round(X/D)

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

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cnt = x//d
mod = x%d
mini = 0

if k < cnt:
    print(x-(d*k))
    exit()

if mod > abs(mod-d):
    mini = abs(mod-d)
    cnt += 1
else:
    mini = mod

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

Submission status: Wrong Answer

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

ans = 0
count = X // D

if X >= 0:
    if K <= count:
        ans = X - D * K
    else:
        ans = X - D * count
        if ((K - count) % 2 == 1):
            ans = ans - D
else:
    if K <= (-1) * count:
        ans = X + D * K
    else:
        ans = X - D * count
        if ((K + count) % 2 == 1):
            ans = ans + D

if ans < 0:
    ans = ans * (-1)

print(ans)

Submission status: Wrong Answer

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

if K == 1:
    print(min(abs(X-D),abs(X+D)))
elif K == 2:
    print(min(abs(X+D+D),abs(X-D-D),abs(X)))
else:
    for i in range(3):
        if abs(X - D) >= abs(X+D):
            List.append(abs(X+D))
            X = X+D
        else:
            List.append(abs(X-D))
            X = X-D

    for i in range(K-3):
        if List[i] == List[i+2]:
            break
        else:
            if abs(X - D) >= abs(X+D):
                List.append(abs(X+D))
                X = X+D
            else:
                List.append(abs(X-D))
                X = X-D

    print(min(List))

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

if 2 * x < d:
    ans = x
elif x < d:
    if k % 2 == 0:
        ans = x
    else:
        ans = x - d
else:
    t = x // d
    if k >= t + 3:
        if (k - t) % 2 == 0:
            ans = min(abs(x - t * d), abs(x - (t + 2) * d))
        else:
            ans = min(abs(x - (t + 1) * d), abs(x - (t + 3) * d))
    else:
        ans = min(abs(x - k * d), abs(x - (k - 2) * d))

print(ans)

Submission status: Wrong Answer

Solution code:

def solve():

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

if __name__ == "__main__":
    X,K,D = list(map(int, input().split()))
    solve()

Submission status: Wrong Answer

Solution code:

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

plus=0
minus=k
ans=10**15+1
for i in range(k//2+1):
    a=x+plus*d+minus*(-d)
    a=abs(a)
    ans=min(ans,a)
    plus+=i
    minus=k-i

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

x,k,d=MI()
y=abs(x)//d
ans=abs(x)
for i in range(-4,4):
    z=y+i
    if z>k or z<0:
        continue
    if z%2!=k%2:
        continue
    ans=min(ans,abs(x-z*d))
    ans=min(ans,abs(x+z*d))
for i in range(-4,4):
    z2=k+i
    if z2>k or z2<0:
        continue
    if z2%2!=k%2:
        continue
    ans=min(ans,abs(x-z2*d))
    ans=min(ans,abs(x+z2*d))
print(ans)

Submission status: Wrong Answer

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

if abs(X) > K*D:
  if X >= 0:
    ans = X - K*D
  else:
    ans = X + K*D
else:
  if K%2 != 0:
    ans = X%D
  else:
    ans = D - X%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 = abs(x) - abs(a * d)
    else:
        x = abs(x) - abs(a * d)
        x = abs(x) - abs(d)

print(abs(x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
now2,kaisu,power = map(int,input().split())
now=abs(now2)
if abs(now)-kaisu*power>=0:
    print(abs(now)-kaisu*power)
else:
    if kaisu%2==0:
        n=round(now/(2*power))
        ans=[abs(now-2*n*power),abs(now-2*(n+1)*power),abs(now-2*(n-1)*power)]
        print(min(ans))
    if kaisu%2!=0:
        n=round(now/power)
        ans=[abs(now-2*n*power),abs(now-2*(n+1)*power),abs(now-2*(n-1)*power)]
        print(min(ans))

Submission status: Wrong Answer

Solution code:
X, K, D = input().split()
X, K, D = int(X), int(K), int(D)
count = 0
if X < 0:
  X = -X
  L = X
else:
  L = X
while L > 0 and K > 0:
  L = L - D
  K -= 1
  #print(str(L) + " " + str(K))
if L == 0:
  if K % 2 == 0:
    print(0)
    exit()
  else:
    print(D)
    exit()
if K == 0:
  print(abs(L))
  exit()
if abs(L) >= L + D:
  goal = L + D
  if K % 2 != 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()
elif abs(L) < L + D:
  goal = abs(L)
  if K % 2 == 0:
    print(goal)
    exit()
  else:
    print(abs(L))
    exit()

Submission status: Wrong Answer

Solution code:
inp = input()
XKD = inp.split()
X = int(XKD[0])
K = int(XKD[1])
D = int(XKD[2])
kai = (K*D-X)/(2*D)
if kai > 0:
  kai_r = round(kai)
  l = []
  l.append(abs(X+2*kai_r*D-K*D))
  l.append(abs(X+2*(kai_r-1)*D-K*D))
  l.append(abs(X+2*(kai_r+1)*D-K*D))
  print(min(l))
else:
  print(X-K*D)

Submission status: Wrong Answer

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

tmp = X // D
d1 = X - (D * tmp)
d2 = X - (D * (tmp + 1))

if tmp >= K:
  ans = X - (D * K)
else:
  if (tmp % 2) ^ (K % 2) == 0:
    ans = d1
  else:
    ans = d2

print(abs(ans))

Submission status: Wrong Answer

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

k1 = X // D
if k1 > K:
    print(abs(X - D * K))
    exit()
    
k = K - k1
x = X % D

while k > 0:
    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()
    if abs(x) <= D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

Submission status: Wrong Answer

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

X = abs(X)

n = X // D
m = K - n

A1 = abs(X - n*D)
A2 = abs(X - (n+1)*D)

Ans = A2

if n+1 < K:
    if (A1 < A2 and m % 2 == 0) or (A1 > A2 and m % 2 != 0):
            Ans = A1
else:
    Ans = X - K*D

print(Ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K ,D = input().split()
X = int(X)
K = int(K)
D = int(D)
if (K == 1):
  if (X >= 0):
    print(abs(X -D))
  if (int(X) < 0):
      print(abs(X + D))
  exit()
if (X == 0):
  print(abs( - (K % 2) *D))
elif (X > 0):
  while ((X > 0) or (K != 0)):
         X = X - D
         K -= 1
  print(abs(abs(X) - (K % 2) *D))
elif (X < 0):
  while ((X < 0) or (K != 0)):
         X = X + D
         K -= 1
  print(abs(abs(X) - (K % 2) *D))

Submission status: Wrong Answer

Solution code:
X = 0
K = 0
D = 0
X,K,D = input().split()
X = int(X)
K = int(K)
D = int(D)
m = 0
m = X
min = 0;
a = (m - m% D) / D
if a < 0:
  a *= -1
if a > K:
  if m > 0:
    min = m - D * K
  else:
    min = m + D*K
elif(m > 0):
  K -= (m - m % D) / D
  m %= D
elif(m > 0):
  k += (m - m% D) / D
  m %= D
if K % 2 == 0:
  min = m
else:
  min = m - D
if min < 0:
  min *= -1
print(min)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import math


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    X, K, D = LI()
    X = abs(X)

    a = math.ceil(X/D)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C_fix
# CreatedDate:  2020-08-15 21:52:53 +0900
# LastModified: 2020-08-15 22:07:30 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    x, k, d = map(int, input().split())
    tmp = 1e31
    s_index = 0
    flag = 1
    if x > 0:
        for s in range(k+1):
            if tmp >= abs(x-s*d):
                tmp = abs(x-s*d)
                s_index = s
            else:
                flag = 0
                break
    elif x < 0:
        for s in range(k+1):
            if tmp <= abs(x+s*d):
                tmp = abs(x+s*d)
                s_index = s
            else:
                flag = 0
                break
    else:
        s_index = 0
    if flag == 1:
        print(tmp)
    else:
        if (k-s_index)%2 == 0:
            print(tmp)
        elif x > 0:
            print(abs(tmp-d))
        else:
            print(tmp+d)





if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

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

a = abs(x // d)
if k % 2 == 0:
    if a % 2 != 0:
        i = (a - 1)*d
        j = (a + 1)*d
    else:
        i = a * d
        j = (a+2) * d
else:
    if a % 2 == 0:
        i = a * d
        j = (a + 2) * d
    else:
        i = (a - 1)*d
        j = (a + 1)*d
# a *= d
# print(a)
if x < 0:
    print(min(abs(x+i), abs(x+j)))
else:
    print(min(abs(x-i), abs(x-j)))

Submission status: Wrong Answer

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

ii=int(X/D)
if K>=ii and X>0:
    X-=ii*D
elif K>=ii and X<0:
    X+=ii*D
elif (K-ii)%2==0 and X>0:
    X-=K*D
elif (K-ii)%2==0 and X<0:
    X+=K*D
elif X>0:
    X-=(K+1)*D
else:
    X+=(K+1)*D

print(abs(X))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if  X == 0 and K >=1 and K%2!=0:
    print(D)
    exit()




for i in range(K):
    if X<=D:
        break
    if X-(K*D)>=D:
        break
    X-=D

_K = K-i
if X<=D and  _K>=2 and _K%2==0:
    print(X)
elif X<=D and _K>=3 and _K%2!=0:
    print(X)
elif X<=D and _K ==1:
    print(abs(X-D))
else:
    print(X)

Submission status: Wrong Answer

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

X = abs(X)

a = X % D

X -= min(a, K) * D
K -= min(a, K)

K %= 2
if K == 1:
    X -= D

result = abs(X)

print(result)

Submission status: Wrong Answer

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

direction = -1 if x > 0 else 1

move_counter = 1 if (k % 2) == 1 else 2

min_abs = abs(x)

while True:
    moved_x = x + (direction * d * move_counter)

    if abs(moved_x) < min_abs:
        min_abs = abs(moved_x)
    else:
        break

    move_counter += 2

print(min_abs)

Submission status: Wrong Answer

Solution code:
x,k,d=list(map(int, input("").split()))
x=abs(x)
c=x//d
m=x%d
if c<k:
    m=abs(m)
    if (k-c)%2==0:
        x=m
    else:
        if abs(m+d) <= abs(m-d):
            x=m+d
        else:
            x=m-d
else:
    x=m

print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	X,K,D = inputMap()
	flg = True
	if X < 0:
		flg = False

	ima = abs(X-0)
	if ima == 0:
		if K % 2 == 0:
			print(0)
			sys.exit()
		else:
			print(abs(D))
			sys.exit()

	koko = abs(X-D)
	if koko == 0:
		if K % 2 == 0:
			print(abs(D))
			sys.exit()
		else:
			print(0)
			sys.exit()

	imaima = abs(X-(K*D))
	if imaima == 0:
		print(0)
		sys.exit()

	if X - K*D > 0:
		ans = X - K*D
		print(ans)
		sys.exit()
	else:
		tmp = X // D
		nokori = K - tmp
		if nokori % 2 == 0:
			if flg == True:
				ans = X % D
				print(ans)
				sys.exit()
			else:
				o000 = X % D
				assd = abs(D - abs(D-o000))
				print(assd)
				sys.exit()
		else:
			if flg == True:
				o000 = X % D
				assd = abs(D - abs(D-o000))
				print(assd)
				sys.exit()
			else:
				ans = X % D
				print(ans)
				sys.exit()


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
ma = X + D*K
mi = X - D*K

if X > 0:
  ans = min(abs(mi),abs(X))
  dk = D*K
  ok = X
  ng = mi
  while ok-ng > 1:
    dk //= 2
    mid = X - dk
    if ans < abs(mid):
      ng = mid
      break
    else:
      ok = mid
      ans = abs(mid)
  print(ans)
elif X < 0:
  ans = min(abs(ma),abs(X))
  dk = D*K
  ok = ma
  ng = X
  while ok-ng > 1:
    dk //= 2
    mid = X + dk
    if ans < abs(mid):
      ng = mid
      break
    else:
      ok = mid
      ans = abs(mid)
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

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


if K <= X//D:
    print(abs(X-D*K))
    
else:
    if X%D == 0:
        print(abs(D))
    else:
        if K - X//D >= 2:
            Y = X%D
            a = abs(D)+Y
            b = abs(Y-D)
            print(min(Y,a,b))

        else:
            Y = X%D
            a = abs(D)+Y
            b = abs(Y-D)
            print(min(a,b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

minus = X < 0
if minus:
  X = -X

R = X // D

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

Submission status: Wrong Answer

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    if X == 0:
        if K%2 ==0:
            res = 0
        else:
            res = D
        print(res)
        sys.exit()

    bestp = X % D
    bestm = bestp - D

    ablep = False

    if abs(X-bestp)//D <= K:
        ablep = True

    ablem = False

    if abs(X-bestm)//D <= K:
        ablem = True

    if (not ablep) & (not ablem):
        if X >= 0:
            res = abs(X-K*D)
        else:
            res = abs(X+K*D)
        print(res)
        sys.exit()

    res = []

    if ablep:
        if X >= bestp:
            resid = (X - bestp) // D
        else:
            resid = (bestp - X) // D
        if resid%2 == 0:
            res.append(abs(bestp))
        else:
            res.append(abs(bestp-D))
            res.append(abs(bestp+D))

    if ablem:
        if X >= bestm:
            resid = (X - bestm) // D
        else:
            resid = (bestm - X) // D
        if resid%2 == 0:
            res.append(abs(bestm))
        else:
            res.append(abs(bestm-D))
            res.append(abs(bestm+D))

    res = min(res)
    print(res)



main()