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 = list(map(int, input().split()))
if x == 0 and k % 2 == 0:
  ans = 0
elif x == 0 and k % 2 == 1:
  ans = d
elif x > 0 and x - k * d >= 0:
  ans = x - k * d
elif x > 0 and x - k * d < 0:
  limit = x // d
  if limit % 2 == k % 2:
    a = x - limit * d
    b = abs(a - 2 * d)
    ans = min(a, b)
  else:
    a = x - (limit - 1) * d
    b = abs(a - 2 * d)
    ans = min(a, b)
elif x < 0 and x + k * d <= 0:
  ans = x + k * d
elif x < 0 and x + k * d > 0:
  limit = abs(x) // d
  if limit % 2 == k % 2:
    a = x + limit * d
    b = a + 2 * d
    ans = min(abs(a), b)
  else:
    a = x + (limit - 1) * d
    b = a + 2 * d
    ans = min(abs(a), b)
print(ans)

Submission status: Wrong Answer

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

x = abs(X)
# 相殺するために必要な回数
n = round(X / D)

if( K < n ):
    print(x - (K * D))
else:
    x = abs(x - (n * D))
    if((K - n) % 2 == 0):
        print(x)
    else:
        print(abs(x - D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if a < 0:
    if abs(a) >= b*c:
        print(abs(a)-b*c)
    else:
        d = abs(a)//c
        e = b -d
        if e%2== 0:
            print(abs(a)-c*d)
        else:
            print(a+c*(d+1))
if a > 0:
    if abs(a) >= b*c:
        print(abs(a)-b*c)
    else:
        d = abs(a)//c
        e = b - d
        if e%2==0:
            print(abs(a)-d*c)
        else:
            print(abs(a-c*(d+1)))
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())
def solve():
    if x == 0:
        if k%2 == 0:
            print(0)
        else:
            print(d)
    elif x > 0:
        i = x//d
        pi = x - i*d
        pii = x - (i+1)*d
        if pi == 0:
            if (k-i) % 2 == 0:
                print(0)
            else:
                print(abs(x - (i+1)*d))
            exit()
        elif pii == 0:
            if (k-(i+1)) % 2 == 0:
                print(0)
            else:
                print(abs(x - i*d))
            exit()
        if abs(pi) <= abs(pii):
            if (k-i) % 2 == 0:
                print(pi)
            else:
                print(abs(x - (i+1)*d))
            exit()
        else:
            if (k-(i+1)) % 2 == 0:
                print(abs(pii))
            else:
                print(abs(x - i*d))
            exit()
    else:
        i = x//d
        pi = x + i*d
        pii = x + (i+1)*d
        if pi == 0:
            if (k-i) % 2 == 0:
                print(0)
            else:
                print(abs(x + (i+1)*d))
            exit()
        elif pii == 0:
            if (k-(i+1)) % 2 == 0:
                print(0)
            else:
                print(abs(x + i*d))
            exit()
        if abs(pi) <= abs(pii):
            if (k-i) % 2 == 0:
                print(abs(pi))
            else:
                print(abs(x + (i+1)*d))
            exit()
        else:
            if (k-(i+1)) % 2 == 0:
                print(abs(pii))
            else:
                print(abs(x + i*d))
            exit()

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
if K*D <= X:
    print(X - K*D)
else:
    q, mod = divmod(X, D)
    if q % 2 == 0 and K % 2 == 0:
        print(abs(X - D * q))
    elif q % 2 == 0 and K % 2 == 1:
        print(abs(X - D * (q+1)))
    elif q % 2 == 1 and K % 2 == 1:
        print(abs(X - D * q))
    elif q % 2 == 1 and K % 2 == 0:
        print(abs(X - D * (q+1)))

Submission status: Wrong Answer

Solution code:
def resolve():
    X, K, D = list(map(int, input().split()))
    X = -X if X < 0 else X
    m = X//D
    if m < K and X%D > D-X%D:
        nmove = m+1
        print(D-X%D if (K-nmove)%2==0 else X%D)
    else:
        nmove = m
        print(X%D if (K-nmove)%2==0 else D-X%D)


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
for _ in range(1):#int(stdin.readline())):
    # n=int(stdin.readline())
    x,k,d=list(map(int,stdin.readline().split()))
    if x==k==d:
        print(0 if k&1 else x)
        continue
    des=x%d
    op=x//d
    a=[des,des-d]
    if k<=op:#check for eq
        print(x-k*d)
    else:
        print(abs(a[(k-op)%2]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X > 0:
    k = X // D
    if k <= K:
        X -= X // D * D
        K -= k
    else:
        X -= D * K
        K = 0

    cur_abs = abs(X)
    while K > 0:
        X -= D
        if abs(X) > cur_abs:
            X += D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)
elif X < 0:
    k = abs(X) // D
    if k <= K:
        X += X // D * D
        K += k
    else:
        X += D * K
        K = 0
    
    cur_abs = abs(X)
    while K > 0:
        X += D
        if abs(X) > cur_abs:
            X -= D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)
else:   # X == 0
    if K % 2 == 0:
        print(0)
    else:
        print(min(abs(X + D), abs(X - D)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
if k * d <= x:
    print(x - k * d)
    exit()
move = math.ceil(x/d)
x = x - d * move
k -= move
if k % 2 == 0:
    print(abs(x))
else:
    x = x + d
    print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
s=0
e=k 
ans=x
if(k%2!=0):
    ans=abs(x-d)
while(s<=e):
    mid=(s+e)//2
    if abs(x-mid*d)<=ans:
        ans=min(ans,abs(x-mid*d))
        s=mid+1
    else:
        e=mid-1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(X // D) >= K:
    ans = X - (D * K) * (X // abs(X))
    ans = abs(ans)
else:
    K -= abs(X//D)
    ans = X - (D * abs(X//D)) * (X // abs(X))
    if K % 2 == 0 and K == 0:
        ans = abs(ans)
    elif K % 2 == 0 and K >= 2:
        ans = min([abs(ans), abs(ans-D), abs(ans+D)])
    else:
        ans = min(abs(ans+D), abs(ans-D))
print(ans)

Submission status: Wrong Answer

Solution code:

def main():
    x, k, d = map(int, input().split())
    num = x
    ans_1 = 0
    ans_2 = 0
    if k == d:
        print(abs(num))
        return
    for i in range(0, k):
        ans_1 = num - d
        ans_2 = num + d
        if abs(ans_2) >= abs(ans_1):
            num = ans_1
        else:
            num = ans_2
    print(abs(num))
    return

main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline

X, K, D = map(int, input().split())
X = abs(X)
Xs = X // D
Xm = X % D
if K < Xs:
#     print("A")
    print(abs(X-K*D))
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif Xm / 2 > K:
#     print("B")
    print(abs(D-Xm))
else:
    print(abs(D-K))

Submission status: Wrong Answer

Solution code:
a, b, c = map(int, input().split())
ans1 = a % c
ans2 = b-ans1
# print(ans1, ans2)
if b*c < a:
    print(a-b*c)
else:
    kaisuu = a//c

    saisyo = a % c
    b = b-kaisuu
    if b % 2 == 1:
        print(abs(c-saisyo))
    else:
        print(abs(saisyo))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = abs(X)
n = round(X / D)

if( K < n ):
    print(x - (K * D))
else:
    x = abs(x - (n * D))
    if((K - n) % 2 == 0):
        print(x)
    else:
        print(abs(x - D))

Submission status: Wrong Answer

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

@author: Paras
"""

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
x,k,d=map(int,input().split())
x=abs(x)
if x<d:
    print(min(abs(x-d),x))
    sys.exit()

val = x//d

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

x-=(val*d)
k-=val
if k&1==0:
    print(x)

else:
    ans = min(x+d,abs(x-d))
    print(ans)

Submission status: Wrong Answer

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

#x = abs(x)
#0に最も近い距離になる。
ans = x % d
#ansへの移動回数分、kから引く
if k- (x//d) < 0:
    print(x - k*d)
    exit()
else:
    k -= x//d

tmp = abs(ans - d)

if k % 2 == 0:
    print(min(ans, tmp))
else:
    print(max(ans, tmp))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:


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

def sol(x, k, d):

    temp = x // d

    if temp >= k:
        return x - d * k

    x -= temp * d
    k -= temp

    if k % 2 == 0:
        return x
    
    return abs(x - d)

print (sol(x, k, d))

Submission status: Wrong Answer

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

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()
	X = abs(X)

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


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

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

	hituyou = X // D
	saidai = X % D

	hituyou2 = hituyou+1
	saidai2 = X+D % D

	if saidai >= saidai2:
		if hituyou > K:
			#print("dd")
			ido = K * D
			ans = ima - ido
			print(ans)
			sys.exit()
		else:
			tmp = K - hituyou
			if tmp % 2 == 0:
				#print("aa")
				print(saidai)
				sys.exit()
			else:
				#print("bb")
				print(saidai + D)
				sys.exit()
	else:
		if hituyou2 > K:
			#print("dd")
			ido = K * D
			ans = ima - ido
			print(ans)
			sys.exit()
		else:
			tmp = K - hituyou2
			if tmp % 2 == 0:
				#print("aa")
				print(saidai2)
				sys.exit()
			else:
				#print("bb")
				print(saidai2 + D)
				sys.exit()





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

Submission status: Wrong Answer

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

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
n=max(round(k/2-x/(2*d)),0)
print(abs(x+(2*n-k)*d))

Submission status: Wrong Answer

Solution code:
def move(X,D):
  p = abs(X + D)
  n = abs(X - D)
  if p <= n:
    return X + D
  else:
    return X - D

X , K , D = map(int, input().split())
for k in range(K):
  X = move(X,D)
print(X)

Submission status: Wrong Answer

Solution code:
def resolve():
    X, K, D = list(map(int, input().split()))
    k = min(X // D, K)
    ans = min(abs(X - D*k),abs(X + D*k))
    l = K-k
    if l % 2 == 1:
        ans = min(abs(ans-D), abs(ans+D))
    print(ans)
    
resolve()

Submission status: Wrong Answer

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

flag = 0
a = abs(X)

while flag == 0:
    if X == 0:
        flag = 1
    elif X > 0:
        if abs(X-D) < a:
            X -= D
            K -= 1
            a = abs(X)
        else:
            flag = 1
    elif X < 0:
        if abs(X+D) < a:
            X += D
            K -= 1
            a = abs(X)
        else:
            flag = 1
    if K == 0:
        flag = 1
        
        
if K % 2 == 0:
    print(X)
else:
    print(min(abs(X+D),abs(X-D)))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

K2= K - (abs(X) // D)


X= abs(X) - abs(((K-K2)*D))

if K2 % 2 == 1:
    X= abs(X) -D


print(abs(X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

    y = x

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


    print(abs(x))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if X < 0:
    X = -1 * X

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

Submission status: Wrong Answer

Solution code:
import math
def main():
  X, K, D = map(int, input().split())
  if abs(X) > K * D:
    print(X-K*D)
  else:
    Y = math.ceil(X / D)
    if (K-Y) % 2 == 0:
      print(D*Y-abs(X))
    else:
      Z = abs(X)-D*(Y-1)
      print(Z)
main()

Submission status: Wrong Answer

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

d, r = divmod(X, D)
if K >= d:
    if (K - abs(d)) % 2 == 0:
        print(min(r, abs(X - D*K)))
    else:
        print(min(abs(r - D), abs(X - D*K)))
else:
     print(abs(X - D*K))

Submission status: Wrong Answer

Solution code:
def main():
    X, K, D = map(int, input().split())
    if X == 0:
        if K % 2 == 0:
            ans = 0
        else:
            ans = D
    elif X > 0:
        if X - K*D < 0:
            a = X % D
            b = a - D
            k = X // D
            if (K - k) % 2 == 0:
                ans = abs(a)
            else:
                ans = abs(b)
        else:
            ans = X - K*D
    else:
        if X + K*D > 0:
            _X = -X
            a = _X % D
            b = -1 * (_X % D) + D
            k = _X // D
            if (K - k) % 2 == 0:
                ans = abs(a)
            else:
                ans = abs(b)
        else:
            ans = X + K*D

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
k_needed = x // d
if x > k*d:
  print(x-k*d)

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

Submission status: Wrong Answer

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

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

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)
elif x == k*d:
    print(0)
elif x == -k*d:
    print(0)
else:
    s = 0
    y = abs(x)
    while y > 0:
        y -= d
        s += 1
    if (k - s) % 2 == 1:
        print((y + d))
    else:
        print(abs(y))

Submission status: Wrong Answer

Solution code:
T = input()
T = T.split(" ")
x = int(T[0])
k = int(T[1])
d = int(T[2])
val = (x+k*d)//(2*d)
if val>=0:
    print(min(abs(-2*d*val+x+k*d), abs(-2*d*(val+1)+x+k*d)))
else:
    print(x+k*d)

Submission status: Wrong Answer

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

    now = x
    i = 0
    if (now == 0):
        if k%2 == 0:
            print(0)
        else:
            print(d)
    elif (now > 0):
        if (now - d * k >=0):
            print(now - d * k)
            return
        i = int(now/d)
        now = now%d
        if (k-i)%2 == 0:
            print(now)
        else:
            print(d-now)
    elif (now < 0):
        if (now + d * k <=0):
            print(now + d * k)
            return
        i = int((-now)/d)
        now = -((-now)%d)
        if (k-i)%2 == 0:
            print(-now)
        else:
            print(now+d)
    
if __name__=='__main__':
    main2()

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

Submission status: Wrong Answer

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

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


print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def point(x):
    return (abs(x), x)

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

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

    else:
        k = X // D
        k = min(k, K)
        ans = X - D*k
        K -= k

        # print(ans, K)
        if K >= 1 and point(ans - D) < point(ans):
            ans -= D
            K -= 1
        # print(ans, K)
        if K % 2 == 1:
            ans = ans - D if ans > 0 else ans + D

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# Date [ 2020-08-15 21:13:42 ]
# Problem [ c.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 6 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    x, k, d = read_ints()
    x = abs(x)
    cnt = x // d
    if cnt < k:
        x -= cnt * d
    else:
        cnt = k
        x -= cnt * d
    if abs(x - d) < x and (k-cnt)%2 == 1:
        x = abs(x - d)
        cnt += 1
    
    if (k - cnt) % 2 == 0:
        print(x)
    elif x == 0:
        print(d)
    else:
        print(min(abs(x - d), x)) 

if __name__ ==  '__main__':
    Main()

Submission status: Wrong Answer

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

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

else:
    b = x - a*d
    c = k - a
    if c % 2 == 0:
        print(-b)
    else:
        print(b + d)

Submission status: Wrong Answer

Solution code:
X, K, D = (int(x) for x in input().split())
P = X
if abs(X) >= K * D:
    if X >= 0:
        P = X - K * D
    else:
        P = X + K * D
else:
    x = int(abs(X) / D)
    if X >= 0:
        if (K - x) % 2 == 0:
            P = X - D * x
        else:
            P = abs(X - D * (x + 1))
    else:
        if (K - x) % 2 == 0:
            P = abs(X + D * x)
        else:
            P = abs(X + D * (x + 1))
print(int(P))

Submission status: Wrong Answer

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

cand_n_1 = (x + k * d) // (2 * d)
cand_n_2 = (x + k * d) // (2 * d) + 1

best = 1e40
if cand_n_1 > k:
    best = min(best, abs(x + k * d - 2 * d * k))
    best = min(best, abs(x + k * d))
else:
    best = min(best, abs(x + k * d - 2 * d * cand_n_1))
    best = min(best, abs(x + k * d - 2 * d * cand_n_2))

print(best)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
t = K % 2
if t == 0:
    a = abs(X - (D * 2))
    b = abs(X + (D * 2))
else:
    a = abs(X - D)
    b = abs(X + D)

if a < b:
    print(a)
else:
    print(b)

Submission status: Wrong Answer

Solution code:
def solve(X, D, lb, ub):
    # min abs(X + t * D) (lb <= t <= ub)
    t = round(-X / D)
    ans = abs(X)
    for i in [-2, -1, 0, +1, +2]:
        tt = t + i
        tt = min(tt, ub)
        tt = max(tt, lb)
        y = X + tt * D
        ans = min(ans, abs(y))
    return ans

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

y1 = X - K * D
y2 = X + K * D
if (y1 > 0 and y2 > 0) or (y1 < 0 and y2 < 0): # same sign
    print(min(abs(y1), abs(y2)))
else:
    if K % 2 == 0:
        ans = solve(X, D * 2, -(K // 2), +(K // 2))
    else:
        ans1 = solve(X + K, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
        ans2 = solve(X - K, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
        ans = min(ans1, ans2)
    print(ans)

Submission status: Wrong Answer

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

r = x % d
m = x // d
if m > k:
    print(x - k * d)
elif (-m) > k:
    print(x + k * d)
elif (k - m) % 2 == 0:
    print(r)
else:
    print(d - r)

Submission status: Wrong Answer

Solution code:
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
N=X//D
Min=10**16
n=0
for i in range(N-1, N+2):
    M=X-i*D
    if M<Min:
        Min=M
        n=i
if (K-n)%2==0:
    print (abs(Min))
else:
    print(min(abs(Min+D), abs(Min-D)))

Submission status: Wrong Answer

Solution code:
#  a = (kd - x)/2d


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


pos = (k - (x//d))//2

if pos < 0:
    pos = 0
ans = x + pos * d - (k-pos) * d
fa = abs(ans) 


pos = ((x//d) - k)//2 
if pos < 0:
    pos = 0
ans = x + pos * d - (k-pos) * d 
fa = min(fa, abs(ans)) 

print(fa)

Submission status: Wrong Answer

Solution code:
def main(X, K, D):
    x = abs(X)
    k = K

    shou, amari = x // D, x % D
    if shou >= k:   # 0付近までたどり着かないとき
        x -= k * D
        k = 0
    else:
        k -= (shou + 1)
        x -= (shou + 1) * D

    if k:
        if k % 2:    # 奇数のとき
            print(abs(x + D))
        else:   # 偶数の時
            print(abs(x))
    else:
        print(min(abs(x), abs(x + D)))

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