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:
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 K % 2 == 1:
            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
        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())
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 > D:
            s = K * D % abs(X)
            if s > D:
                if K % 2 != 1:
                    m = abs(X) % abs(D)
                else:
                    m = s
            else:
                m = s     
        else:
            s = D - abs(X)
            if K % 2 != 1:
                m = abs(X)
            else:
                m = abs(s)


       

print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
Y,Z = X+1,X+2

for i in range(K):
    Z = Y
    Y = X
    if X >= 0:
        X = X - D
    else:
        X = X + D
    if X == Z: #roop
        if i%2 == 0: #even
            ans = Y
        else: #odd
            ans = X
        break
    if i == K-1:#last
        ans = X
print(abs(ans))

Submission status: Wrong Answer

Solution code:
# [] = list(map(int, input().split()))

[x, k, d] = list(map(int, input().split()))
x = abs(x)
howMany = x // d
remainder = x % d
stepLeft = k - howMany
if k < howMany:
    print(abs((howMany - k) * d))
elif stepLeft % 2 == 0:
    print(remainder)
elif stepLeft % 2 == 1:
    print(abs(d - remainder))

Submission status: Wrong Answer

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


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

	num= abs(x)//d
	if k<num:
		if x<0:
			x=x+k*d
		else:
			x=x-k*d
		print(x)
	else:
		if x<0:
			x=x+num*d
		else:
			x=x-num*d

		k=k-num

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

Submission status: Wrong Answer

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

if D * K <= X:
    print(X - (D*K) + 2)
else:
    nokori = K - shou
    if nokori % 2 == 0:
        print(amari)
    else:
        print(abs(amari - D))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ct = 0
for i in range(k):
    if x >= 0:
        ct +=1
        x = x - d
        if x < 0:
            break
    else:
        ct += 1
        x = x + d
        if x <= 0:
            break

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

Submission status: Wrong Answer

Solution code:
from decimal import *
x, k, d = map(Decimal, input().split())

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

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 = abs(x) - abs(k*d)
    x = 0
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())

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

ans = 0
if q < K:
    ddd = q % 2
    l = q % 2
    if l == 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())
flag = True
if abs(X)-K*D>=D:
  ans = abs(abs(X)-K*D)
  flag = False
else:
  ans = X

res = abs(X)%D
r = X//D
if flag:
  if r%2 == K%2:
    ans = min(ans, res)
  else:
    ans = min(ans, abs(res-D))
  
print(ans)

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

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

Submission status: Wrong Answer

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

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:14:18 +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())
if x>=k*d:
    print(x-k*d)
elif -x>=k*d:
    print(x+k*d)
else:
    x=abs(x)
    a,b=divmod(x,d)
    if k%2==a%2:
        print(b)
    else:
        print(abs(b-d))

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)
kai_r = round(kai)
if kai_r >= 0:
  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:
  l = []
  l.append(abs(X-K*D))
  l.append(abs(X+2*D-K*D))
  print(min(l))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if k%2 == 0 and abs(x)==abs(d):
  print(abs(x))
  exit()
if x>0 and abs(x)>abs(x-d):
  x = x-d
if x<0 and abs(x)>abs(x+d):
  x = x+d
for i in range(k):
  if abs(x) > abs(x - (d*i) + d*(k-i)):
    #print(x)
    x = x - (d*i) + d*(k-i)
print(abs(x))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    span = int(X / D)
    if (K - span) % 2 == 0:
        print(int(X - span * D))
    else:
        for i in range(K):
            if X >= 0:
                X = X - D
            else:
                X = X + D
        print(abs(X))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


if X>0:
    S = int(abs(X) / D)
    aX = X -D * S
    if S>=K:
        print(X-D*K)
    elif (K-S)%2==0:
        print(abs(aX))
    else:
        print(abs(aX-D))
else:
    S = int(abs(X) / D)
    aX = X + D * S
    if S>=K:
        print(abs(X+D*K))
    elif (K-S)%2==0:
        print(abs(aX))
    else:
        print(abs(aX-D))

Submission status: Wrong Answer

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

if X > 0 :
  q, mod = divmod(X, D)
  if q >= K:
    print(X-D*q)
  else:
    cnt = K-q
    if cnt%2 == 0:
      print(mod)
    if cnt%2 == 1:
      print(D-mod)
      
elif X < 0 :
  Y = abs(X)
  q, mod = divmod(Y, D)
  if q >= K:
    print(X+D*q)
  else:
    cnt = K-q
    if cnt%2 == 0:
      print(abs(mod))
    if cnt%2 == 1:
      print(D-mod)

Submission status: Wrong Answer

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

ans=X

a=abs(X//D)

"""
for i in range(a):
    ans=min(abs(ans-D),abs(ans+D))
    
"""

if (K>a>0 or -K<a<0) and (K-a)%2==0:
    ans=min(abs(X-a*D),abs(X+a*D),abs(X+(a+2)*D),abs(X-(a+2)*D))
elif (K>a>0 or -K<a<0) and (K-a)%2==1:
    ans=min(abs(X-(a+1)*D),abs(X+(a+1)*D),abs(X-(a-1)*D),abs(X+(a-1)*D))

elif a>=K:
    ans=X-K*D
else:
    ans=X+K*D
    


print(ans)

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-c*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())

x = abs(x)
ans = 0

if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = d
elif 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:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	#a = int(input())
	x, k, d = tin()
	#s = input()
	na = x // d
	#pa(na)
	if abs(na) >= k:
		return min(abs(x - d*k), abs(x+d*k))
	else:
		xa = min(abs(x - d*na), (x+d*na))
		ka = k-abs(na)
		kb = ka%2
		#pa(xa)
		if kb==1:
			return min(abs(xa+d), abs(xa-d))
		else:
			return min(abs(xa-2*d),abs(xa+2*d),abs(xa))
		
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

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

sm = abs(x)//d

if abs(x)-sm*d>0 :
    ans = abs(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 = map(int,input().split())

# mを得るために必要な最小の移動回数
n = abs(x) // d

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

k -= n

x = x % d

if x > abs(x - d):
    x = x - d
    k -= 1

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

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-=K*D
elif K<=ii and X<0:
    X+=K*D
elif (K-ii)%2==0 and X>0:
    X-=ii*D
elif (K-ii)%2==0 and X<0:
    X+=ii*D
elif X>0:
    X-=(ii+1)*D
else:
    X+=(ii+1)*D

print(abs(X))

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"]

if X == K == D:
    if K // 2 == 0:
        K = 0
        lowest_value = X
    else:
        K = 0
        lowest_value = 0

while K != 0:

    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1
        lst_of_values.append(lowest_value)

    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
        lst_of_values.append(lowest_value)
    else:
        break

if K // 2 == 0:
    K = 1

if K == 1:
    value1 = abs(lowest_value + D)
    value2 = abs(lowest_value - D)
    if value1 < value2:
        lowest_value = value1
    else:
        lowest_value = value2
print(lowest_value)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:

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

if x==0 and k%2==0:print(0)
if x==0 and k%2==1:print(d)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
x=(abs(i[0]))

if x>=i[1]*i[2]:
    print(x-i[1]*i[2])
else:
    if ((x%i[2]>=(i[2]-x%i[2]))^((i[1]-x//i[2])%2==1)):
        print(i[2]-x%i[2])
    else:
        print(x%i[2])

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 = int(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())
n = 1

if(X >= 0):
    while(X - n * D >= 0):
        n += 1
    a = K - n
    if(a % 2 == 0):
        print(abs(X - n * D))
    else:
        print(abs(X - (n-1)*D))
else:
    while(n * D + X <= 0):
        n += 1
    a = K - n
    if(a % 2 == 0):
        print(abs(X + n * D))
    else:
        print(abs(X + (n-1)*D))

Submission status: Wrong Answer

Solution code:
param_x, param_k, param_d = map(int, input().split())

param_x_tmp = param_x

if param_x > 0:
  int_times_nega_posi = -1
else:
  int_times_nega_posi =  1

count = 0
while param_x_tmp*param_x > 0:
  
  param_x_tmp = param_x_tmp + param_d*int_times_nega_posi
  
  count = count + 1
  
  if param_k - count == 0:
    print(param_x_tmp*int_times_nega_posi*-1)

if count < param_k:
  int_move = param_k - count #move left
  int_move_devide_2_no_amari = int_move % 2
  print(abs(param_x_tmp + param_d*int_move_devide_2_no_amari*int_times_nega_posi*-1))

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(min((abs(abs(X) - abs(a * D)-abs(D))),(abs(abs(X) - abs(a * D)+abs(D)))))

Submission status: Wrong Answer

Solution code:
X,K,D = [int(a) for a in input().split()]
X0=X
u=max(abs(X),D)
l=-u
now=0
for i in range(K):
    if X>0:X-=D
    elif X<=0:X+=D
    if X>0 and X<X0:
        u=X
        now=1
    if X<0 and X>-X0:
        l=X
        now=-1
    if X==abs(X0):
        u=X
        if i!=K-1:l=0
        now=1
    if X==-abs(X0):
        l=X
        if i!=K-1:u=0
        now=-1
    #print(X,u,l,i,now)
    if X==0 and i==K-1:
        print("0")
        break
    if (u>0 and u<X0 and l<0 and l>-X0) or (u==0 or l==0):
        if (K-1-i)%2==0:
            if now==1:
                print(u)
            else:
                print(abs(l))
        else:
            if now==1:
                print(abs(l))
            else:
                print(u)
        break
    if i==K-1:
        print(abs(X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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(s,abs(X-D*(i+1)))
 
print(s)

Submission status: Wrong Answer

Solution code:
flag=10
x,k,d=map(int,input().split())
for i in range(k):
    if x>0:
        x-=d
        if x<0:
            flag=-1
            break
    elif x<0:
        x+=d
        if x>0:
            flag=1
            break
    else:
        flag=0
        break

if flag==0:
    print(d)
elif flag==-1:
    print(min(abs(x),x+d))
elif flag==1:
    print(min(abs(x-d),x))
else:
    print(abs(x))

Submission status: Wrong Answer

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	#a = int(input())
	x, k, d = tin()
	#s = input()
	na = x // d
	#pa(na)
	if abs(na) > k:
		return min(abs(x - d*k), abs(x+d*k))
	else:
		xa = min(abs(x - d*na), (x+d*na))
		ka = k-abs(na)
		kb = ka%2
		#pa(xa)
		if kb==1:
			return min(abs(xa+d), abs(xa-d))
		else:
			return min(abs(xa-2*d),abs(xa+2*d),abs(xa))
		
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

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

x = abs(x)
mov = x // d
ans = 0
if mov >= k:
    ans = x - k * d
else:
    cand1 = x % d
    cand2 = abs(cand1 - d)
    ans = min(cand1, cand2)

print(ans)

Submission status: Wrong Answer

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

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

print(abs(A))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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)
kai_r = round(kai)
if kai_r > 0:
  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:
  l = []
  l.append(abs(X-K*D))
  l.append(abs(X+2*D-K*D))
  print(min(l))

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(d==1):
    if((k-jud)%2==1):
      print(1)
    else:
      print(0)
  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:
nowPoint, count, move = map(int, input().split())
movecount = 0
conparisionNum = 0
result = 0
if nowPoint == 0:
  result = 0
if 0 < nowPoint:
  while move < nowPoint and count - movecount > 0:
    nowPoint -= move
    movecount += 1
  result = nowPoint
  if count - movecount > 0:
    if nowPoint - move < 0:
      conparisionNum = (nowPoint - move) * -1
      if nowPoint < conparisionNum:
        result = nowPoint
      else:
        result = conparisionNum
    else:
      result = nowPoint - move
else:
  while nowPoint < move and count - movecount > 0:
    nowPoint += move
    movecount += 1
  result = nowPoint
  if count - movecount > 0:
    if 0 < nowPoint + move:
      conparisionNum = (nowPoint - move) * -1
      if conparisionNum < nowPoint:
        result = nowPoint * -1
      else:
        result = conparisionNum * -1
    else:
      result = (nowPoint + move) * -1
print(result)

Submission status: Wrong Answer

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


for i in range(k):

    if x + d >= 0 and x < 0:
        break
    elif x - d <= 0 and x > 0:
        break

    elif abs(x + d) < abs(x - d):
        x = x + d
        a += 1
    else:
        x = x - d
        a += 1

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

elif (k - a) % 2 == 0:
    print(abs(x))

else:
    if x == 0:
        print(d)
    elif x < 0:
        print(x + d)
    else:
        print(x - d)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

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

    if x > 0:
        for _ in range(k):
            #-進める
            x = x - d
            if x <= 0:
                #判定
                x = min(abs(x+d),abs(x-d))
                print(x)
                exit()
    else:
        for _ in range(k):
            #+進める
            x = x + d
            if x >= 0:
                #判定
                x = min(abs(x+d),abs(x-d))
                print(x)
                exit()
    #判定
    print(x)

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:
    if x<0:
      print(d-a[1])
    else:
      print(abs(a[1]-d))
  else:
    print(a[1])
else:
  print(abs(x)-d*k)

Submission status: Wrong Answer

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


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

	num= abs(x)//d
	if k<num:
		if x<0:
			x=x+k*d
		else:
			x=x-k*d
		print(x)
	else:
		if x<0:
			x=x+num*d
		else:
			x=x-num*d

		k=k-num

		if k%2==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())
import sys
z = abs(x) // d
if z <= k:
    y = abs(x) - d * z


else:
    y = abs(x) - d * k
if x == d:
    if k % 2 == 0:
        y = x
    else:
        y = 0


print(y)

Submission status: Wrong Answer

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

min_pos = x % d
min_neg = min_pos - d

num_to_min_pos = x // d
if k <= num_to_min_pos:
    print(x - d * k)
    
else:
    if (k - num_to_min_pos) % 2 == 0:
        print(min_pos)
    else:
        print(abs(min_neg))

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 999999999
tmp = 0
if X == 0:
  if K % 2 == 0:
    print(0)
  else:
    print(D)
  sys.exit()
if X >= K*D:
  print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  if X >= 0:
    b = a - D*2
  else:
    b = a + D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  if X >= 0:
    b = a - D*2
  else:
    b = a + D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

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

if abs(x)/k < d:
    if x > 0 and x != d:
        x = x - math.floor(x/d) *d
    elif x != d:
        x = x - math.floor(x/d) *d
    
    for i in range(10**5):
        ans = min(abs(x-k), abs(x+k))
        if abs(x-k) < abs(x+k):
            x = x - k
        else:
            x = x + k
else:
    if x > 0:
        ans = x - k*d
    else:
        ans = x + k*d
print(ans)

Submission status: Wrong Answer

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

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"]

if X == K == D:
    if K // 2 == 0:
        K = 0
        lowest_value = X
    else:
        K = 0
        lowest_value = 0 

while K != 0:

    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1
        lst_of_values.append(lowest_value)

    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
        lst_of_values.append(lowest_value)
    else:
        break

if K // 2 != 0:
    K = 1 

if K == 1:
    value1 = abs(lowest_value + D)
    value2 = abs(lowest_value - D)
    if value1 < value2:
        lowest_value = value1
    else:
        lowest_value = value2
print(lowest_value)

Submission status: Wrong Answer

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

m_k = x//d
m_d = x%d

m_m = k - m_k

if m_m >= 0:
    if m_m % 2 == 0:
        print(m_d)
    else:
        print(abs(m_d - d))
else:
    print(-1 * m_m * d )