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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
def getCount(X, D, K):
    x = X
    for i in range(D):
        if abs(x - K) < abs(x + K):
            x -= K
        else:
            x += K
    return x
line = input().split(' ')
X = int(line[0])
D = int(line[1])
K = int(line[2])
x = X

a = int(X / K)
b = X % K
if a % 2 == 0 and D % 2 == 0:
    if a > D:
        print(getCount(X, D, K))
    else:
        print(b)
else:
    if a > D:
        print(getCount(X, D, K))
    else:
        print(K - b)

Submission status: Wrong Answer

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

def calc(M):
	return ( -1 * D * M ) + ( D * ( K - M ) )


MAX_X = 1e15
L = 0
R = K
curMin = MAX_X + 1
sumTmp = 0
while True:
	M = ((L+R)//2)
	sumTmp = calc( M )
	# print(M, L,R, sumTmp, X+sumTmp)

	if abs(curMin - abs(X+sumTmp)) < 1:
		# print("curMin:", curMin, abs(X+sumTmp))
		break

	if 0 < X + sumTmp:
		# increase left range
		# (decrease current)
		L = M + 1
		# print("left inc",L,R)
	elif X == sumTmp:
		L = R = M
		curMin = 0
		break
	else:
		# sumTmp < X
		# decrease right range
		# (increase current)
		# print("right dec",L,R)
		R = M - 1

	if R < L:
		# FIXME: これ微妙では。。。?
		# 短調増加でなくなってしまっているため、これがありえる？
		L,R = R,L

	curMin = min(curMin, abs(X+sumTmp))


# print("X:",X,"K:",K, "L:",L, "D:", D)
# print(calc(L) )
print(abs( X + calc(L)))

Submission status: Wrong Answer

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = abs(X) // D
        #print(syo)
        if syo > 4 :
            if X > 0 :
                val[0] = X - (syo-2) * D
            else :
                val[0] = X + (syo - 2) * D
            #print(val[0])
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 0
            else:
                limit = 1
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            #print(val[i+1])
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit and (abs(val[i+1] < abs(val[i]))):
                #print("a")
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                #print("b")
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = abs(X) // D
        #print(syo)
        if syo > 5 :
            if X > 0 :
                val[0] = X - (syo-2) * D
            else :
                val[0] = X + (syo - 2) * D
            #print(val[0])
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 0
            else:
                limit = 1
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            #print(val[i+1])
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                #print("a")
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                #print("b")
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = abs(X) // D
        #print(syo)
        if syo > 1 :
            if X > 0 :
                val[0] = X - (syo-2) * D
            else :
                val[0] = X + (syo - 2) * D
            #print(val[0])
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 0
            else:
                limit = 1
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            #print(val[i+1])
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                #print("a")
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                #print("b")
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
line = input()

strL = line.split()
X = abs(int(strL[0]))
K = int(strL[1])
D = int(strL[2])

mod = X % D
div = int(X / D)

if div > K:
    ans = X - K * D
elif (K - div) % 2 != 0:
    ans = mod - D
else:
    ans = mod

print(int(ans))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

line = input().split(' ')
X = int(line[0])
D = int(line[1])
K = int(line[2])
x = X
odd_X = 0
eve_X = 0
count = 0
for i in range(D):
    if abs(x - K) < abs(x + K):
        x -= K
    else:
        x += K
    if count > 2:
        if eve_X == X:
            break
        elif odd_X == X:
            break
    if count % 2 == 0:
        eve_X = x
    else:
        odd_X = x
   
    count += 1
print(abs(x))

Submission status: Wrong Answer

Solution code:
line = input()

strL = line.split()
X = int(strL[0])
K = int(strL[1])
D = int(strL[2])

mod = X % D
div = int(X / D)

if div > K:
    if X > 0:
        ans = abs(X - K * D)
    else:
        ans = abs(X + K * D)
    
elif (K - div) % 2 != 0:
    if mod > 0:
        ans = mod - D
    else:
        ans = mod + D
else:
    ans = mod

print(int(abs(ans)))

Submission status: Wrong Answer

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

a = abs(x % d)
a_sho = abs(x // d)
b = abs(a-d)

if k <= a_sho:
    ans = abs(x - (k*d))
else:
    #if a < b:
    if (k - a_sho) %2 == 0:
        ans = a
    else:
        ans = b
print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split()) 
mlist = []
for i in range(K+1):
    t = X + i*D - (K-i)*D
    if t < 0 :
        t = -t
    mlist.append(t)
print(mlist)
print(min(mlist))

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

Submission status: Wrong Answer

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

X=abs(X)

q=X//D
r=X%D

if K<=q:
    print(X-q*D)
    exit()

else :
    if (K-q)%2==0:
        print(r)
    else :
        print(abs(r-D))

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10


    x,k,d = LMIIS()
    sign = -1 if x < 0 else 1
    absX = abs(x)
    if k*d <= abs(x):
        print(sign*(absX-k*d))
        return
    num = absX // d
    k -= num
    if k & 1:
        num += 1
    print(sign*(absX-num*d))
     
    



    
main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10


    x,k,d = LMIIS()
    sign = -1 if x < 0 else 1
    absX = abs(x)
    if k*d <= abs(x):
        print(sign*(absX-k*d))
        return
    num = absX // d
    k -= num
    if k & 1:
        num += 1
    print(sign*abs(absX-num*d))
     
    



    
main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# atcoder 
# Türkler var mı?
# Herkese memnün oldum
import sys
import math

readline = sys.stdin.buffer.readline
iimr = lambda: map(int, sys.stdin.buffer.readline().rstrip().split())
#sys.setrecursionlimit(10 ** 8)

def debug(*x):
    print(*x, file=sys.stderr)


class atcoder():
    def __init__(self):
        pass

    def çözmek(self):
        
        N, K, D = map(int, input().split())
        N = abs(N)
        res = []
        res.append(self.関数(N, K, D))
        print(*res, "\n")	
    
    def 関数(self, n, k, d):
       
        if n == 0:
            return n
        if (n % d == 0) and (n // d == k):
            return 0
        else:
            karpayi = n // d
            if karpayi <= k:
                ost1  = n % d
                if ((karpayi - k) % 2) == 0:
                    return ost1 
                else:
                    return(abs(ost1 - d))

            else:
                return n - math.floor(k * d)
            
        return -1

if __name__ == "__main__":
    ima = atcoder()
    ima.çözmek()

Submission status: Wrong Answer

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = X // D
        #print(syo)
        if syo != 0 :
            val[0] = X - (syo-1) * D
            K = K -(syo-1)
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = X // D
        #print(syo)
        val[0] = X - (syo-1) * D
        K = K -(syo-1)
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

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

Submission status: Wrong Answer

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

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 4
    else :
        limit = 5
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 10 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = X // D
        #print(syo)
        val[0] = X - (syo-1) * D
        K = K -(syo-1)
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    import sys
    input = sys.stdin.readline

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

    if 0 < kd <= abs(x):
        print(abs(x - kd))
    elif x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(d)
    elif x == d:
        if k % 2 == 0:
            print(d)
        else:
            print(0)
    else:
        m = x // d
        print(min(abs(x - d * m), abs(x - (m + 1) * d)))


if __name__ == '__main__':
    main()

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

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
total_distance = D * K
if X >= total_distance:
    print(X - total_distance)
else:
    to_zero_times = X // D
    from_zero_distance1 = X % D
    from_zero_distance2 = D - from_zero_distance1
    left_times = K - to_zero_times
    if left_times % 2 == 0:
        print(from_zero_distance1)
    else:
        print(from_zero_distance2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x < 0:
    flag = -1
else:
    flag = 1

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

print(abs(ans))

Submission status: Wrong Answer

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

if x < 0:
    flag = -1
else:
    flag = 1

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

print(abs(ans))

Submission status: Wrong Answer

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

if x < 0:
    flag = -1
else:
    flag = 1

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

Submission status: Wrong Answer

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

n = min( X // D , K )

remain = math.fabs(X) - D * n

if (K - n) % 2 == 0:
    print( int(remain) )
else:
    print( int(D - remain ) )

Submission status: Wrong Answer

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

if a - ( b * c ) >= 0:
    print(a - ( b * c ))
else:
    l = 0
    r = b

    while r - l > 1:
        mid = (r + l) // 2

        if a - ( mid * c ) >= 0:
            l = mid
        else:
            r = mid

    A = a - ( l * c )
    B = abs( a - ( ( l + 1 ) * c ) )

    k = b - l

    if k % 2 == 0:
        print(A)
    else:
        print(B)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if a - ( b * c ) >= 0:
    print(a - ( b * c ))
else:
    l = 0
    r = b

    while r - l > 1:
        mid = (r + l) // 2

        if a - ( mid * c ) > 0:
            l = mid
        else:
            r = mid

    A = a - ( l * c )
    B = abs( a - ( ( l + 1 ) * c ) )

    k = b - l

    if k % 2 == 0:
        print(A)
    else:
        print(B)

Submission status: Wrong Answer

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

if a - ( b * c ) >= 0:
    print(a - ( b * c ))
else:
    l = 0
    r = b

    while r - l > 1:
        mid = (r + l) // 2

        if( a - ( mid * c ) > 0 ):
            l = mid
        else:
            r = mid

    mn = a - ( l * c )
    mn = min( mn, abs( a - ( ( l + 1 ) * c ) ) )

    print(mn)

Submission status: Wrong Answer

Solution code:
l=list(map(int,input().split()))
X=l[0]
D=l[1]
K=l[2]
for i in range(K):
  if abs(X+D)<=abs(X-D):
    X=abs(X+D)
  else:
    X=abs(X-D)
print(X)

Submission status: Wrong Answer

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

a = (k * d - x) / (2 * d)

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

elif x < 0:
   print(x + k * d)
else:
   print(x - k * d)

Submission status: Wrong Answer

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

history = []
history.append(x)

for i in range(k):
    x = min(abs(x + d), abs(x - d))
    history.append(x)
    if i > 2 and history[i] == history[i - 2] and (d - i) % 2 == 0:
        break

print(x)

Submission status: Wrong Answer

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

history = []
history.append(x)

for i in range(k):
    x = min(abs(x + d), abs(x - d))
    history.append(x)
    if i > 2 and history[i] == history[i - 2]:
        break

print(x)

Submission status: Wrong Answer

Solution code:
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
x = xkd[0] // xkd[2]
xkd[0] -= xkd[2] * x
xkd[1] -= x
if xkd[1] < 1:
  xkd[0] += abs(xkd[1]) * xkd[2]
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if abs(ans) > abs(xkd[0]) + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))

Submission status: Wrong Answer

Solution code:
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
x = xkd[0] // xkd[2]
x = abs(x)
xkd[0] -= xkd[2] * x
xkd[1] -= x
if xkd[1] < 1:
  xkd[0] += abs(xkd[1]) * xkd[2]
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if abs(ans) > abs(xkd[0]) + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
x = xkd[0] // xkd[2]
x = abs(x)
xkd[0] -= xkd[2] * x
xkd[1] -= x
if xkd[1] < 1:
  xkd[0] += abs(xkd[1]) * xkd[2]
  print(xkd[0])
else:
    ans = xkd[0]
    if xkd[1] % 2 != 0:
        ans -= xkd[2]
        if ans > xkd[0] + xkd[2]:
            ans += xkd[2] * 2
            print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    X, K, D = LI()
    X = abs(X)
    if X - K * D > 0:
        ans = X - K * D
    else:
        a1 = X % D
        _a1 = a1 - D
        if abs(a1) > abs(_a1):
            a1 = _a1
        n_move = (X - a1) // D
        if n_move % 2 != K % 2:
            ans = min(abs(a1 + D), abs(a1 - D))
        else:
            a2 = abs(a1 - D)
            ans = min(a1, a2)
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    X, K, D = LI()
    if X - K * D > 0:
        ans = X - K * D
    elif X + K * D < 0:
        ans = X + K * D
    else:
        a1 = abs(X) % D
        n_move = (abs(X) - a1) // D
        if n_move % 2 != K % 2:
            ans = min(abs(a1 + D), abs(a1 - D))
        else:
            a2 = abs(a1 - D)
            ans = min(a1, a2)
    print(ans)
main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

zahyo=x%d
nokori=k-(x//d)

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

Submission status: Wrong Answer

Solution code:
a = [int(s) for s in input().split()]
if a[0] < 0:
  b = abs(a[0]) // a[2]
  c = a[0] + a[2] * int(b)
  d = int(c) + a[2]
  if a[1] <= int(b):
    print(abs(a[0] + a[1] * a[2]))
  elif a[1] == int(b) + 1:
    print(abs(a[0] + a[1] * a[2]))
  else:
    if abs(int(c)) < abs(int(d)):
      if (a[1] - int(b)) % 2 == 0:
        print(abs(c))
      else:
        print(abs(d))
    else:
      if (a[1] - int(b) - 1) % 2 == 0:
        print(abs(c))
      else:
        print(abs(d))
else:
  b = a[0] // a[2]
  c = a[0] - a[2] * int(b)
  d = int(c) - a[2]
  if a[1] <= int(b):
    print(abs(a[0] - a[1] * a[2]))
  elif a[1] == int(b) + 1:
    print(abs(a[0] - a[1] * a[2]))
  else:
    if abs(int(c)) < abs(int(d)):
      if (a[1] - int(b)) % 2 == 0:
        print(abs(c))
      else:
        print(abs(d))
    else:
      if (a[1] - int(b) - 1) % 2 == 0:
        print(abs(c))
      else:
        print(abs(d))

Submission status: Wrong Answer

Solution code:
# coding:utf-8
x, k, d = map(int, input().split())

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding:utf-8
x, k, d = map(int, input().split())

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

Submission status: Wrong Answer

Solution code:
# coding:utf-8
x, k, d = map(int, input().split())

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

Submission status: Wrong Answer

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

x, k, d = map(int, input().split())
x = abs(x)
if (x + d - 1) // d > k:
	print(x - d * k)
else:
	k -= (x + d - 1) // d
	if k & 1:
		print(x % d)
	else:
		print(d - (x % d))