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:
##C - Walking Takahash
##正負が反転しないなら単調増加、減少
##反転するなら、反転した瞬間で残りの回数が偶数なら、その値、奇数なら、一つ前にの値
X,K,D = map(int,input().split())
R = K - X // D
##X1の正負フラグがあれば簡潔に書けるかも
if X < 0:
    X = -X
if X > 0 and X-K*D >= 0:
    print(X-K*D)
elif X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
else:
    if R % 2 == 0:
        print(X - D*(X//D))
    else:
        print(X - D*((X//D)+1))

Submission status: Wrong Answer

Solution code:
##C - Walking Takahash
##正負が反転しないなら単調増加、減少
##反転するなら、反転した瞬間で残りの回数が偶数なら、その値、奇数なら、一つ前にの値
X,K,D = map(int,input().split())
R = K - X // D
##X1の正負フラグがあれば簡潔に書けるかも
if X > 0 and X-K*D >= 0:
    print(X-K*D)
elif X < 0 and X+K*D <= 0:
    print(-X-K*D)
elif X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
else:
    if R % 2 == 0:
        print(abs(X - D*(X//D)))
    else:
        if X > 0:
            print(abs(X - D*((X//D)+1)))
        else:
            print(abs(X - D*((X//D)-1)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = abs(x)

mod = x % d

if mod >= k:
    ans = x - (d*k)

elif (k-mod) % 2 == 0:
    ans = x - (d*mod)

else:
    ans = x - (d*mod) - d

print(abs(ans))

Submission status: Wrong Answer

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

x = abs(x)

mod = x % d

if mod >= k:
    ans = x - (d*k)

elif (k-mod) % 2 == 0:
    ans = x - (d*mod)

else:
    ans = x - (d*mod) - d

print(ans)

Submission status: Wrong Answer

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

# 移動できる距離の max は KD

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

print(ans)

Submission status: Wrong Answer

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

# 移動できる距離の max は KD

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

print(abs(ans))

Submission status: Wrong Answer

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

# 移動できる距離の max は KD

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

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
a=abs(x)//d
b,c=abs(x%d),abs(-x%d)
mi=min(b,c)
if k<a:
    print(min(abs(x-k*d),abs(x+k*d)))
else:
    if a%2==k%2:
        print(mi)
    else:
        print(min(abs(mi-d),abs(mi+d)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split());xx=abs(x)
tmp=xx;tmp=(xx//d);xx-=(d*min(tmp,k))
if (k-tmp)&1:
	if(xx>0):xx-=d
	else:xx+=d
print(abs(xx))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
tmp=x;tmp=(x//d);x-=(d*min(tmp,k))
if (k-tmp)&1:
	if(x>0):x-=d
	else:x+=d
print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
tmp=x;tmp=(x//d);x-=(d*min(tmp,k))
if (k-tmp)&1:x-=d
print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k,d=map(int,input().split())
q=n//d
r=n%d
if(k<=q):
    print(n-k*d)
else:
    rem=k-q
    if(rem%2==0):
        print(r)
    else:
        print(min(abs(r-d),abs(r+d)))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

x = abs(x)

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

bai = x//d

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

Submission status: Wrong Answer

Solution code:
n,k,d=map(int,input().split())
q=n//d
r=n%d
if(k<=q):
    print(n-k*d)
else:
    rem=k-q
    if(rem%2==0):
        print(r)
    else:
        print(abs(r-d))

Submission status: Wrong Answer

Solution code:
def main():
    x,k,d = map(int,input().split())
    if x < k*d:
        # minimum = min(d-(x%d),x % d)
        cnt, amari = divmod(x,d)
        if abs(amari-d) < amari:
            amari = abs(amari - d)
            cnt += 1
        if (k-cnt)%2==0:
            print(amari)
        else:
            print(abs(amari-d))

    else:
        print(x-k*d)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if x < 0 :
  x = -x

l = x // D

if K > l:
  if abs(x - D*l) < abs(x - D*(l+1)) :
    if (l % 2) == (K % 2):
      print(abs(x - D*l))
    else:
      print(abs(x - D*(l+1) ))
  else:
    if (l % 2) == (K % 2) :
      print(abs(x - D*(l+1) ))
    else:
      print(abs(x - D*l ))
else:
  print(abs(x - D*K))

Submission status: Wrong Answer

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

l = x // D

if K > l:
  if abs(x - D*l) < abs(x - D*(l+1)) :
    if (l % 2) == (K % 2) :
      print(abs(x - D*l))
    else:
      print(abs(x - D*(l+1) ))
  else:
    if (l % 2) == (K % 2) :
      print(abs(x - D*(l+1) ))
    else:
      print(abs(x - D*l ))
else:
  print(abs(x - D*K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:


startX, countK, distanceD = map(int, input().split())
n = int(startX / distanceD)
ans = startX % distanceD
if n > countK:
    ans += distanceD * (n - countK)
else:
    if (countK - n) % 2 == 1:
        ans= abs(ans - distanceD)
print(ans)

Submission status: Wrong Answer

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


maxremove = abs(X)//D

if maxremove > K:
    print(abs(X)-K*D)
else:

    low = abs(X) - (abs(X)//D) * D

    K = K-maxremove

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

c = abs(X) // D
k = min(c, K)
K -= k
if K == 0:
    if X > 0:
        print(X-D*k)
        exit()
    elif X < 0:
        print(X+D*k)
        exit()

if X > 0:
    X -= D*k
    if abs(X-D) < abs(X):
        X -= D
        K -= 1
elif X < 0:
    X += D*k
    if abs(X+D) < abs(X):
        X += D
        K -= 1

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

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    X, K, D = map(lambda x: abs(int(x)), input().split())

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

Submission status: Wrong Answer

Solution code:
import sys

def input():
    return sys.stdin.readline().rstrip('\n')


def calc(X, K, D):
    n = X // D
    if K <= n:
        return abs(X - K * D)
    if (K - n) % 2 == 0:
        return abs(X - n * D)
    else:
        return abs(X - (n + 1) * D)


(X, K, D) = tuple([int(s) for s in input().split()])
print(calc(X, K, D))

Submission status: Wrong Answer

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

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

print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
 
if X <= K * D :
  if abs(X % D -D) < X % D :
    answer = abs(X % D - D)
  else :
    answer = X % D
else :
  answer = X - K * D
  
print(answer)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
answer = abs(X)
break_count = 0
while K > 0 :
    if X > 0 :
        X = X - D
    else :
        X = X + D

    K = K - 1        
    if answer > abs(X) :
        answer = abs(X)  
        break_count =0
    else:
        break_count = break_count +1
    if break_count > 10 or answer < 1:
        break

print(answer)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
answer = abs(X)
break_count = 0
while K > 0 :
    if X > 0 :
        X = X - D
    else :
        X = X + D

    K = K - 1        
    if answer > abs(X) :
        answer = abs(X)  
        break_count =0
    else:
        break_count = break_count +1
    if break_count > 5:
        break

print(answer)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
answer = abs(X)
break_count = 0
while K > 0 :
    if X > 0 :
        X = X - D
    else :
        X = X + D

    K = K - 1        
    if answer > abs(X) :
        answer = abs(X)  
        break_count =0
    else:
        break_count = break_count +1
    if break_count > 1:
        break

print(answer)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
answer = abs(X)
while K > 0 :
    if X > 0 :
        X = X - D
    else :
        X = X + D

    K = K - 1        
    if answer > abs(X) :
        answer = abs(X)  

print(answer)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
answer = X
while K > 0 :
    if X > 0 :
        X = X - D
    else :
        X = X + D

    K = K - 1        
    if answer > abs(X) :
        answer = abs(X)  

print(answer)

Submission status: Wrong Answer

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

    if X + K*D <= 0:
        print(abs(X + K*D))
        exit(0)
    if X - K*D >= 0:
        print(abs(X - K*D))
        exit(0)

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

Submission status: Wrong Answer

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

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
    
print(X)

Submission status: Wrong Answer

Solution code:

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

for i in range(K):
    if X > 0:
        X = X - D
    if X < 0:
        X = X + D
print(abs(X))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
N = (K-X/D)/2
n = round((K-X/D)/2)
if N < 0:
    print(abs(abs(X)-K*D))
else:
    l = [abs(abs(X)-(K-2*n)*D),abs(abs(X)-(K-2*(n-1))*D),abs(abs(X)-(K-2*(n+1))*D)]
    ans = min(l)
    print(ans)

Submission status: Wrong Answer

Solution code:
from __future__ import division, print_function
# import threading
# threading.stack_size(2**27)
# import sys
# sys.setrecursionlimit(10**7)
# sys.stdin = open('inpy.txt', 'r')
# sys.stdout = open('outpy.txt', 'w')
from sys import stdin, stdout
import bisect            #c++ upperbound
import math
import heapq
i_m=9223372036854775807
def inin():
    return int(input())
def stin():
    return input()
def spin():
    return map(int,stin().split())
def lin():                           #takes array as input
    return list(map(int,stin().split()))
def matrix(n):
    #matrix input
    return [list(map(int,input().split()))for i in range(n)]

################################################
def calculate_sum(a, N): #sum of a to N
    # Number of multiples 
    m = N / a 
    # sum of first m natural numbers 
    sum = m * (m + 1) / 2
    # sum of multiples 
    ans = a * sum
    return ans
def series(N):
    return (N*(N+1))//2

def count2Dmatrix(i,list):
    return sum(c.count(i) for c in list)

def modinv(n,p):
    return pow(n,p-2,p)

def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x
def LCM (x, y):
    return (x * y) // GCD(x, y)
    
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
def isprime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            # print(f)
    return j
# d = {}
 
"""*******************************************************"""
x, k, d = spin()

x = abs(x)
if k%2!=0:
    d1 = math.fabs(x - k*d)
    d2 = math.fabs(x - k//2*d + k//2*d)
    m = min(d1, d2)
else:
    d1 = math.fabs(x - k*d)
    d2 = math.fabs(x - (k//2+1)*d + (k//2-1)*d)
    m = min(d1, d2)
print(int(m))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
a = 0
b = K
N = (K-X/D)/2
n = round((K-X/D)/2)
if N < 0:
    print(abs(abs(X)-K*D))
else:
    print(abs(abs(X)-(K-2*n)*D))

Submission status: Wrong Answer

Solution code:
from __future__ import division, print_function
# import threading
# threading.stack_size(2**27)
# import sys
# sys.setrecursionlimit(10**7)
# sys.stdin = open('inpy.txt', 'r')
# sys.stdout = open('outpy.txt', 'w')
from sys import stdin, stdout
import bisect            #c++ upperbound
import math
import heapq
i_m=9223372036854775807
def inin():
    return int(input())
def stin():
    return input()
def spin():
    return map(int,stin().split())
def lin():                           #takes array as input
    return list(map(int,stin().split()))
def matrix(n):
    #matrix input
    return [list(map(int,input().split()))for i in range(n)]

################################################
def calculate_sum(a, N): #sum of a to N
    # Number of multiples 
    m = N / a 
    # sum of first m natural numbers 
    sum = m * (m + 1) / 2
    # sum of multiples 
    ans = a * sum
    return ans
def series(N):
    return (N*(N+1))//2

def count2Dmatrix(i,list):
    return sum(c.count(i) for c in list)

def modinv(n,p):
    return pow(n,p-2,p)

def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x
def LCM (x, y):
    return (x * y) // GCD(x, y)
    
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
def isprime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            # print(f)
    return j
# d = {}
 
"""*******************************************************"""
x, k, d = spin()

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
a = 0
b = K
n = round((K-X/D)/2)
if n < 0:
        print(abs(abs(X)-K*D))
else:
    print(abs(abs(X)-(K-2*n)*D))

Submission status: Wrong Answer

Solution code:
from __future__ import division, print_function
# import threading
# threading.stack_size(2**27)
# import sys
# sys.setrecursionlimit(10**7)
# sys.stdin = open('inpy.txt', 'r')
# sys.stdout = open('outpy.txt', 'w')
from sys import stdin, stdout
import bisect            #c++ upperbound
import math
import heapq
i_m=9223372036854775807
def inin():
    return int(input())
def stin():
    return input()
def spin():
    return map(int,stin().split())
def lin():                           #takes array as input
    return list(map(int,stin().split()))
def matrix(n):
    #matrix input
    return [list(map(int,input().split()))for i in range(n)]

################################################
def calculate_sum(a, N): #sum of a to N
    # Number of multiples 
    m = N / a 
    # sum of first m natural numbers 
    sum = m * (m + 1) / 2
    # sum of multiples 
    ans = a * sum
    return ans
def series(N):
    return (N*(N+1))//2

def count2Dmatrix(i,list):
    return sum(c.count(i) for c in list)

def modinv(n,p):
    return pow(n,p-2,p)

def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x
def LCM (x, y):
    return (x * y) // GCD(x, y)
    
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
def isprime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            # print(f)
    return j
# d = {}
 
"""*******************************************************"""
x, k, d = spin()

if k%2!=0:
    d1 = math.fabs(x - k*d)
    d2 = math.fabs(x - k//2*d + k//2*d)
    m = min(d1, d2)
else:
    d1 = math.fabs(x - k*d)
    d2 = math.fabs(x - (k//2+1)*d + (k//2-1)*d)
    m = min(d1, d2)
print(int(m))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
a = 0
b = K
n = round((K-X/D)/2)
print(abs(abs(X)-(K-2*n)*D))

Submission status: Wrong Answer

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())


x,k,d=input2()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
X,K,D = map(int,input().split())
x = abs(X)
s = np.sign(X)
q,r = divmod(x,D)
if q > K:
    print((x - (K * D)) * s)
else:
    k = K - q
    if k % 2 == 0:
        print(r)
    else:
        print(abs(r - D))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import math

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

s = math.ceil(x/d)
if s > k:
    if x > 0:
        print(x-k*d)
    else:
        print(x+k*d)
else:
    circa0 = x-s*d
    if s % 2 == k % 2:
        print(abs(circa0))
    else:
        print(min(abs(circa0-d),abs(circa0+d)))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
for _ in range(K):
  X = X - D
  if X <= 0 :
    print(min(abs(X), abs(X+D)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
x = abs(x)
t = x // d
if t >= k:
    answer = x - (k * d)
else:
    if (k - t) % 2 == 0:
        answer = x - (t * d)
    else:
        answer = x - (t - 1) * d
        
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
x = abs(x)
t = x // d
if t >= k:
    answer = x - (t * d)
else:
    if (k - t) % 2 == 0:
        answer = x - (t * d)
    else:
        answer = x - (t - 1) * d
        
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- using:utf-8 -*-
class main:
  def function(x, k, d):
    for i in range(k):
      if abs(x + d) >= abs(x - d):
        x = x - d
      elif abs(x + d) < abs(x - d):
        x = x + d
    print(x)

if __name__ == "__main__":
  x, k, d = map(int, input().split())
  main.function(x,k,d)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# -*- using:utf-8 -*-
class main:
  def function(x, k, d):
    for i in range(k):
      if abs(x + d) >= abs(x - d):
        x = x - d
      if abs(x + d) < abs(x - d):
        x = x + d
    print(x)

if __name__ == "__main__":
  x, k, d = map(int, input().split())
  main.function(x,k,d)

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
#K回繰り返すことを考慮すること
i = min(X//D, K) #移動回数が最小の方をとる
k = X%D

if k == 0:
    #print("1")
    print(abs(X - i*D))
else:
    n = K - i
    if n%2==0:
        #print("2")
        print(abs(X - i*D))
    else:
        #print("3")
        print(abs(X - i*D - D))

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
#K回繰り返すことを考慮すること
i = min(X//D, K) #移動回数が最小の方をとる
k = X%D

if k == 0:
    #print("1")
    print(X - i*D)
else:
    n = K - i
    if n%2==0:
        #print("2")
        print(X - i*D)
    else:
        #print("3")
        print(X - i*D - D)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
s=abs(d)
n=x//s
#print(n)
if k<=n:
    r=x-k*d
    print(r)
else:
    x-=n*s
    t=x-d
    t=abs(t)
    k-=n
    #print(k,x,t)
    if t==x:
        print(t)
    elif t<x:
        if k%2==1:
            print(t)
        else:
            print(x)
    else:
        if k%2==0:
            print(x)
        else:
            print(t)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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