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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d=list(map(int,input().split()))
b=100000000000
for i in range(1,k+1):
  a=abs(x-k*d+8*i)
  b=min(b,a)
print(b)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int, input().split())
if abs(x)//d>=k:
    #print("ひ")
    print(abs(x-d*k))
elif (k-abs(x)//d)%2==0:
    #print("fu")
    print(abs(x)%d)
else:
    #print("mi")
    print(d-abs(x)%d)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int, input().split())
if abs(x)//d>=k:
    #print("ひ")
    print(abs(x-d*k))
elif (k-abs(x)//d)%2==0:
    #print("fu")
    print(abs(x%d))
else:
    #print("mi")
    print(abs(d-x%d))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 0
kk = k
 
while k > 0:
  temp = min(abs(x-d), abs(x+d))
  k -= 1 
  i += 1
  if temp == abs(x-d):
    lists.append(0)
  if temp == abs(x+d):
    lists.append(1)
  if len(lists) >= 2 and lists[0] == lists[1]:
    if kk % 2 == 0: 
      temp = abs(x - i*d)
      k = 0
    elif kk % 2 == 1:
      temp = min(abs(x - i*d - d), abs(x - i*d +d))
      k = 0
  if len(lists) >=2 and lists[0]  != lists[1]: 
    lists.pop(0)
    lists.pop(0)
 
print(temp)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
x = abs(x)
 
 
if x//d >= k:
  answer = x-k*d
elif x//d < k:
  mod = x % d
  if mod != 0:
    answer = abs(mod)
  else: 
    if k % 2 == 0:
      answer = k
    else:
      answer = 0
 
print(answer)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
 
step = int(x/d) 

if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = abs(x - step * d - d)

print(answer)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
 
step = int(x/d) 

if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = abs(d)

print(answer)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
 
step = int(x/d) 

if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = abs(x - step * x - d)

print(answer)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 1
 
step = int(x/d) 

if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = min(abs(x % d - d), abs(x % d + d))

print(answer)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 1
 
step = int(x/d) 

if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = abs(min(x % d - d, x % d + d))

print(answer)

Submission status: Wrong Answer

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

div = X // D
move = X - D * div  #絶対値近くまで移動

if(X >= K * D):
    print(abs(X) - K * D)
elif(div % 2 == 0 ):
    print(abs(move))
elif(move >= 0):
    print(abs(move - D))
elif(move < 0):
    print(abs(move + D))

Submission status: Wrong Answer

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


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

print(answer)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 0
kk = k

while k > 0:
  temp = min(abs(x-d), abs(x+d))
  k -= 1 
  i += 1
  if temp == abs(x-d):
    lists.append(0)
  if temp == abs(x+d):
    lists.append(1)
  if len(lists) >= 2 and lists[0] == lists[1]:
    if kk % 2 == 0: 
      temp = abs(x - i*d)
      k = 0
    elif kk % 2 == 1:
      temp = min(abs(x - i*d - d), abs(x - i*d +d))
      k = 0
  if len(lists) >=2 and lists[0]  != lists[1]: 
    lists.pop(0)
    lists.pop(0)
  if temp == 0:
    k = 0
print(temp)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 0
kk = k

while k > 0:
  temp = min(abs(x-d), abs(x+d))
  k -= 1 
  i += 1
  if temp == abs(x-d):
    lists.append(0)
  if temp == abs(x+d):
    lists.append(1)
  if len(lists) >= 2 and lists[0] == lists[1]:
    if kk % 2 == 0: 
      temp = abs(x - i*d)
      k = 0
    elif kk % 2 == 1:
      temp = min(abs(x - i*d - d), abs(x - i*d +d))
      k = 0
  if len(lists) >=2 and lists[0]  != lists[1]: 
    lists.pop(0)
    lists.pop(0)

print(temp)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 0
kk = k

while k > 0:
  temp = min(abs(x-d), abs(x+d))
  k -= 1 
  i += 1
  if temp == abs(x-d):
    lists.append(0)
  if temp == abs(x+d):
    lists.append(1)
  if len(lists) >= 2 and lists[0] == lists[1]:
    if kk % 2 == 0: 
      temp = abs(x - i*d)
      k = 0
    elif kk % 2 == 1:
      temp = abs(x - i*d) + d
      k = 0
  if len(lists) >=2 and lists[0]  != lists[1]: 
    lists.pop(0)
    lists.pop(0)

print(temp)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 0
kk = k

while k > 0:
  temp = min(abs(x-d), abs(x+d))
  k -= 1 
  i += 1
  if temp == abs(x-d):
    lists.append(0)
  if temp == abs(x+d):
    lists.append(1)
  if len(lists) >= 2 and lists[0] == lists[1]:
    if kk % 2 == 0: 
      temp = abs(x - i*d)
      k = 0
    elif kk % 2 == 1:
      temp = abs(x - i*d) - d
      k = 0
  if len(lists) >=2 and lists[0]  != lists[1]: 
    lists.pop(0)
    lists.pop(0)

print(temp)

Submission status: Wrong Answer

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

print(int(absX - step * D + D * ((K - step) % 2)))

Submission status: Wrong Answer

Solution code:
1

Submission status: Wrong Answer

Solution code:
x, k, d=map(int,input().split())
x=abs(x)
#k回すべて戻っても0に達せない場合
if x//d>=k:
  print(x-k*d)
#k回以内に0の前後まで戻れた場合
elif k%2!=0:
  print(x%d)
else:
  print(x)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
way=k*d
place=[]
for i in range(-way, way, d):
  point=x
  point+=i
  point=abs(point)
  place.append(point)
print(min(place))

Submission status: Wrong Answer

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

    if abs(x) % d == 0:
        exceed_num = (abs(x) // d)
    else:
        exceed_num = (abs(x) // d) + 1


    if k <= exceed_num :
        print(abs( abs(x) - (d*k) ))
    else:
        if (k - exceed_num) % 2 == 0:
            print(abs( abs(x) - (d*k) ))
        else:
            print(abs( abs(x) - (d*(exceed_num-1) )))



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

N = X // D
if K < abs(N):
    ans = abs(X - K * D)
else:
    if (K - abs(N)) % 2 == 0:
        ans = abs(X - N * D)
    else:
        ans = abs(X - (N+1) * D)

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:


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


xx = abs(x)
count = k

num = xx // d

if k == num:
    xx -= num * d
elif k > num:
    a = abs(num * d)
    b = abs(num * (d+1))
    if a > b:
        xx -= b
    else:
        xx -= a


if xx == 0:
    xx += num * d
    

print(abs(xx))

Submission status: Wrong Answer

Solution code:


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


xx = abs(x)
count = k

num = xx // d

if k == num:
    xx -= num * d
elif k > num:
    xx -= (num+1) * (d)


print(abs(xx))

Submission status: Wrong Answer

Solution code:
# 175 C
X, K, D = list(map(int, input().split()))
div = abs(X)//D
mod = abs(X)%D
print(mod) if (K-div)%2 == 0 else print(D-mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

# x = abs(int(input()))
# k = int(input())
# d = int(input())

array = list(map(int, input().strip().split()))
x = array[0]
k = array[1]
d = array[2]
if (d == 0):
    amari = abs(x)

else:
    move_cnt = int(x / d)
    if (move_cnt <= k):
        k -= move_cnt
        amari = x - move_cnt * d

    else:
        amari = x - k * d
        k = 0

    if (k > 0):
        if (k % 2 == 1):
            if (amari > 0):
                amari -= d
            else:
                amari += d

print(abs(amari))

Submission status: Wrong Answer

Solution code:
import sys

array = list(map(int, input().strip().split()))
x = array[0]
k = array[1]
d = array[2]
move_cnt = int(x / d)
if (move_cnt <= k):
    k -= move_cnt
    amari = x - move_cnt * d

else:
    amari = x - k * d
    k = 0

if (k > 0):
    if (k % 2 == 1):
        if (amari > 0):
            amari -= d
        else:
            amari += d

print(abs(amari))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#今Xにいる、K回 D移動する
[X, K, D] = [int(i) for i in input().split()]
juage = []
#flag = 0

if K % 2 == 0 and X > 0:
    #flag = 1
    for i in range(K):
        X -= 2*D
        if X < 0:
            juage.append(abs(X+2*D))
            juage.append(abs(X))
            break

elif K % 2 == 0 and X < 0:
    #flag = 2
    for i in range(K):
        X += 2*D
        if X > 0:
            juage.append(abs(X-2*D))
            juage.append(X)
            break
        
elif K % 2 == 1 and X > 0:
    #flag = 3
    for i in range(K):
        X -= D
        if X < 0:
            juage.append(abs(X+D))
            juage.append(abs(X))
            break

elif K % 2 == 1 and X < 0:
    #flag = 4
    for i in range(K):
        X -= D
        if X > 0:
            juage.append(abs(X-D))
            juage.append(X)
            break
        
#print(juage, flag)
if juage == []:
    print(X)
else:
    print(min(juage))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

mincount = x // d

if mincount < k:
  k = (k-mincount)%2
  result = x - d*mincount -d*k
else:
  result = x - k*d
print(abs(result))

Submission status: Wrong Answer

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

for i in range(k):
    if x > d:
        x -= d*i
    elif x < -d:
        x += d*i
    else:
        if (k-i)%2==0:
            break
        else:
            if x <= 0:
                x += d
            elif x > 0:
                x -= d
            break
print(abs(x))

Submission status: Wrong Answer

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

for i in range(k):
    if x > d:
        x -= d*i
    elif x < -d*i:
        x += d
    else:
        if (k-i)%2==0:
            break
        else:
            if x <= 0:
                x += d
            elif x > 0:
                x -= d
            break
print(abs(x))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
if k % 2 != 0 and x!= d:
    k = k//2
    for i in range(k):
        if x > 0:
            x-=d
        else:
                x+=d
print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# author:  Taichicchi
# created: 15.09.2020 21:22:26

import sys

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

X = abs(X)

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x,k,d = mi()
pre = x
cnt = 0

for i in range(k):
    if pre*x > 0:
        pre = x
        cnt += 1
        if x > 0:
            x -= d
        else:
            x += d
    else:
        if (k-cnt) % 2 == 0:
            print(abs(x))
            exit()
        else:
            print(abs(pre))
            exit()
print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
ans = X
for i in range(K):
    if abs(ans) >= D:
        ans -= D
    elif abs(ans) < D and ans >= 0:
        ans -= D
    else:
        ans += D
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(x) > k*d:
    print(abs(x)-k*d)

else:
    print(min((abs(x)-k*d)%d, d-(abs(x)-k*d)%d))

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
        x += a * d
        if (k - a) % 2 == 0:
            print(-x)
        else:
            print(x + d)
            
else:
    if x - k * d > 0:
        print(x - k * d)
    else:
        a = x // d
        x -= a * d
        if (k - a) % 2 == 0:
            print(x)
        else:
            print(d - x)

Submission status: Wrong Answer

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

a = 0

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

Submission status: Wrong Answer

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

a = 0

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

Submission status: Wrong Answer

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

print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a=input().split(" ")
b=[x,k,d]= [int(a[0]),int(a[1]),int(a[2])]  
ans = 0
if x - k*d > 0:
  ans = x - k*d
elif x + k*d < 0:
  ans = k*d -x
else:
  if k%2==0:
    ans= x%(2*d)
    if d<x%(2*d):
      ans= 2*d-x%(2*d)
  elif k%2==1:
    ans= (x+d)%(2*d)
    if d<(x+d)%(2*d):
      ans= 2*d-(x+d)%(2*d)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

def sign(x):
    return 1 if x >= 0 else -1


ans = abs(X) - K * D

if abs(X) - K * D < 0:
    divs = abs(X) // D
    mods = abs(X) % D
    nums = K - divs

    print(divs, mods, nums)

    ans = abs(mods - (nums%2) * D)

    # if nums % 2 == 0:
    #     ans = mods
    # else:
    #     ans = min(mods, abs(mods - D))


print(ans)


# ans = min([abs(X+D*i) for i in range(-K, K+1, 2)])
# 
# print(ans)

Submission status: Wrong Answer

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

def sign(x):
    return 1 if x >= 0 else -1


ans = abs(X) - K * D

if abs(X) - K * D < 0:
    divs = abs(X) // D
    mods = abs(X) % D
    nums = K - divs

    if nums % 2 == 0:
        ans = mods
    else:
        ans = min(mods, abs(mods - D))


print(ans)


# ans = min([abs(X+D*i) for i in range(-K, K+1, 2)])
# 
# print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = [int(i) for i in input().split()]
times = x//d
t = times +1
pls = x - d*times
neg = pls - d
if (k-times)%2==0:
    print(max(pls, x-d*k))
else:
    print(max(abs(neg), x-d*k))

Submission status: Wrong Answer

Solution code:
x,k,d = [int(i) for i in input().split()]
times = x//d
t = times +1
pls = x - d*times
neg = pls - d
if (k-times)%2==0:
    print(pls)
else:
    print(abs(neg))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

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

Submission status: Wrong Answer

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




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

print(abs(x))

Submission status: Wrong Answer

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

if x+2*d >=k*d:
    print(abs(x-k*d))
    exit()

if k%2==1:
    x=x-d

kaisu=int(x/(2*d))
ans=abs(x-kaisu*d*2)
print(ans)

Submission status: Wrong Answer

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

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

if k%2==1:
    x=x-d

kaisu=int(x/(2*d))
ans=abs(x-kaisu*d*2)
print(ans)

Submission status: Wrong Answer

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

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

if k%2==1:
    x=x-d

kaisu=int(x/(2*d))
ans=abs(x-kaisu*d*2)
print(ans)

Submission status: Wrong Answer

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

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

if k%2==1:
    x=x-d

kaisu=int(x/(2*d))
ans=abs(x-kaisu*d*2)
print(ans)

Submission status: Wrong Answer

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

while k>0:
    if x>0:
        x=x-d
        k-=1
    else:
        x=x+d
        k-=1

print(x)

Submission status: Wrong Answer

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

if x < 0:
    x = -x

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

Submission status: Wrong Answer

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

if x < 0:
    x = -x

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

Submission status: Wrong Answer

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

if x < 0:
    x = -x

if x-d*k >= 0:
    an = x-d*k
else:
    for i in range(k):
        x -= d
        if an > abs(x):
            an = x
        else:
            if (k-i+1)%2 == 0 and i!=0:
                an = x
            break
        
print(abs(an))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if x < 0:
    x = -x
    
for i in range(k):
    x -= d
    if an > abs(x):
        an = x
    else:
        if (k-i)%2 != 0 and i!=0:
            an = x
        break
        
print(abs(an))

Submission status: Wrong Answer

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

if x < 0:
    x = -x
    
for i in range(k):
    x -= d
    if an > abs(x):
        an = x
    else:
        if (k-i)%2 != 0 and i!=0:
            print((k-i)%2)
            an = x
        break
        
print(abs(an))

Submission status: Wrong Answer

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

if x < 0:
    x = -x
    
for i in range(k):
    x -= d
    if an > abs(x):
        an = x
    else:
        if (k-i)%2 != 0:
            an = x
        break
        
print(abs(an))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
an = x
for i in range(k):
    a = x+d
    b = x-d
    if abs(a) < abs(b):
        x = a
    else:
        x = b
    
    if an > abs(x):
        an = x
    else :
        if (k-i)%2 != 0:
            an = x
        break
        
print(abs(an))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
an = x
for i in range(k):
    a = x+d
    b = x-d
    if abs(a) < abs(b):
        x = a
    else:
        x = b
    
    if an > abs(x):
        an = abs(x)
    elif an == abs(x):
        if (k-i)%2 == 0:
            if abs(x+d) < abs(x-d):
                an += d
            else:
                an -= d
        break
        
print(abs(an))

Submission status: Wrong Answer

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