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

x = abs(x)

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

print(x)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def move(x, D):
  d1 = x - D
  d2 = x + D
  if abs(d1) >= abs(d2):
    return d2
  else:
    return d1

x ,K, D = map(int, input().split())
k = 0
while k < K:
  if abs(x) <= D:
    if K - k % 2 == 0:
      x = abs(x) -abs(D)
      break
    else:
      break
  else:
    move(x,D)
    k += 1
	
print(abs(x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if X < 0:
    X = -X

for i in range(K):
    X -= D    
    if(X < D):
        if(i % 2 == 1):
            break
        elif(X < 0):
            X += D
            break
        else:
            X -= D
            break 

print(abs(X))

Submission status: Wrong Answer

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

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

while K != 0:
    X -= D    
    if(X < D):
        if(K % 2 == 1):
            break
        elif(X < 0):
            X += D
            break
        else:
            X -= D
            break            
        
    K -= 1

print(abs(X))

Submission status: Wrong Answer

Solution code:
b=list(map(int,input().split()))
X=int(b[0])
K=int(b[1])
D=int(b[2])
X=abs(X)
a=10**15
for i in range(round(X/D),K+1):
    if abs(X-D*i+D*(K-i))<a:
        a=X-D*i+D*(K-i)

print(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

x,k,d = readints()

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

print(x)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

tmp = None

while K != 0:
    if X >= 0:
        X -= D
    else:
        X += D
    
    if(K % 2 == 1):
        tmp = X
        if tmp == X:
            break
    
    K -= 1

X = abs(X)
print(X)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x==0:
	if k%2==1:
		print(d)
		exit()
	if k%2==0:
		print(0)
		exit()
if x==d:
	if k%2==1:
		print(0)
		exit()
	if k%2==0:
		print(x)
		exit()
if x<d:
	print(min(x,abs(x-d)))
	exit()
if x>d:
	if x-(k*d)>0:
		print(x-(k*d))
		exit()
	if x-(k*d)<0:
		print(min(x%d,abs((x%d)-d)))
		exit()

Submission status: Wrong Answer

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

if x==0:
	if k%2==1:
		print(d)
		exit()
	if k%2==0:
		print(0)
		exit()
if x==d:
	if k%2==1:
		print(0)
		exit()
	if k%2==0:
		print(x)
		exit()
if x-(k*d)>0:
	print(x-(k*d))
	exit()
if x-(k*d)<0:
	print(min(x%d,abs((x%d)-d)))
	exit()

Submission status: Wrong Answer

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

if x==d:
	if k%2==1:
		print(0)
		exit()
	if k%2==0:
		print(x)
		exit()
if x-(k*d)>0:
	print(x-(k*d))
	exit()
if x-(k*d)<0:
	print(min(x%d,abs((x%d)-d)))
	exit()

Submission status: Wrong Answer

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

nokori = abs(X - (K * D)) #残りマス
q = abs(X // D) #商
p = abs(X % D) #余り
nokorik = K-q #残り回数
if K <= q:
    print(nokori)
else:
    if nokorik % 2 == 0:
        print(p)
    else:
        print(abs(p-D))

Submission status: Wrong Answer

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

nokori = abs(X - (K * D)) #残りマス
q = abs(X // D) #商
p = X % D #余り
nokorik = K-q #残り回数
if K <= q:
    print(nokori)
else:
    if nokorik % 2 == 0:
        print(p)
    else:
        print(abs(p-D))

Submission status: Wrong Answer

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

if X // D == K:
    print(0)
else:
    nokori = abs(X - (K * D)) #残りマス
    q = abs(X // D) #商
    p = X % D #余り
    nokorik = K-q #残り回数
    if K <= q:
        print(nokori)
    else:
        if nokorik % 2 == 0:
            print(p)
        else:
            print(abs(p-D))

Submission status: Wrong Answer

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

if X // D == K:
    print(0)
else:
    nokori = abs(X - (K * D)) #残りマス
    q = abs(X // D) #商
    p = X % D #余り
    nokorik = K-q #残り回数
    if K < q:
        print(nokori)
    else:
        if nokorik % 2 == 0:
            print(p)
        else:
            print(abs(p-D))

Submission status: Wrong Answer

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

if X / D == K:
    print(0)
else:
    nokori = abs(X - (K * D)) #残りマス
    q = abs(X // D) #商
    p = X % D #余り
    nokorik = K-q #残り回数
    if K < q:
        print(nokori)
    else:
        if nokorik % 2 == 0:
            print(p)
        else:
            print(abs(p-D))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int, input().split())
x=abs(x)
a=x%d
b=(x-a)//d
if b <= k:
  if (b ^ k) & 1:
    print(-x%d)
  else:
    print(x%d)
else:
  print(x-k*d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

for i in range(b):
    up = a - c 
    down = a + c
    if abs(down) < abs(up):
        a = down
    elif abs(up) < abs(down):
        a = up

print(abs(a))

Submission status: Wrong Answer

Solution code:
import math
import sys

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

num=math.floor(x/d)

if num<0:
  num*=-1

if num>k:
  if x>0:
    print(x-k*d)
    
  else:
    print(x+k*d)
  sys.exit()

if x>0:
  n=x-num*d
  
else:
  n=x+num*d
  
remaining=k-num

if remaining%2==0:  
  if n>=0:
    print(n)
  
  else:
    print(n*-1)
    
else:
  if n>0:
    n-=d
    if n>=0:
      print(n)
      
    else:
      print(n*-1)
      
  else:
    n+=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys

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

num=math.floor(x/d)

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

n=x-num*d
remaining=k-num

if remaining%2==0:
  if n>0:
    print(n)
    
  else:
    print(n*-1)
    
else:
  if n>0:
    n-=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)
      
  else:
    n+=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)

Submission status: Wrong Answer

Solution code:
def C():
    X, K, D = map(int, input().split())
    if X//D >= K:
        return X - K*D

    pos1 = abs(X - (X//D)*D)
    pos2 = abs(X - (X//D+1)*D)

    if pos1 > pos2:
        K -= X//D+1
    else:
        K -= X//D
    
    if K % 2 == 0:
        return min(pos1, pos2)
    else:
        return abs(min(pos1, pos2) - D)

print(C())

Submission status: Wrong Answer

Solution code:
def C():
    X, K, D = map(int, input().split())
    if X//D > K:
        return X - K*D

    pos1 = abs(X - (X//D)*D)
    pos2 = abs(X - (X//D+1)*D)

    if pos1 > pos2:
        K -= X//D+1
    else:
        K -= X//D
    
    if K % 2 == 0:
        return min(pos1, pos2)
    else:
        return abs(min(pos1, pos2) - D)

print(C())

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
x = abs(x)
num = x//d
if k >= num:
    x -= num*d
    cnt = k-num
else:
    x -= k*d
    print(x)
    exit()
if abs(x) > abs(x-d):
    x -= d
    cnt -= 1

if cnt%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)
if d>=x and k%2==0:
  ans=x
elif d>=x and k%2!=0:
  ans=abs(x-d)
elif k*d>x:
  ans=x%d
else:
  ans=abs(x)-k*d
print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
x = abs(x)
m = min(x//d,k)
x -= d*m
k -= m

print(x)

Submission status: Wrong Answer

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

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

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(abs(Xa-D*K))
elif (K-Xs) % 2 == 0:
    print(abs(Xm))
else:
    print(abs(D-Xm))

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10**8)

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
    if abs(X - D) > abs(X) and abs(X + D) > abs(X):
        if (K - i - 1) % 2 == 1:
            if abs(X - D) < abs(X + D):
                X = X - D
            else:
                X = X + D
        break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

p = x // d
if p > k:
  print(x - k * d)
else:
  min = [x, x]
  min[0] = x - p * d
  min[1] = abs(min[0] - d)
  print(min[k%2])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
q,mod = divmod(x,d)
if k <= q:
    ans = x - k*d
else:
    ans = mod
    ans -= (k-q)%2*d
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x=abs(x)

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


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

Submission status: Wrong Answer

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

x=abs(x)

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


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

Submission status: Wrong Answer

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

x=abs(x)

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


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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
q = x // d
r = x % d

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

Submission status: Wrong Answer

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

x=abs(x)

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

for i in range(k):
  tmp=x
  x-=d
  if abs(x) >= abs(tmp):
    if (k-i)%2==0:
      print(abs(tmp))
      exit()
    else:
      print(abs(x))
      exit()
else:
  print(abs(x))

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


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


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 = abs(x) // d
        print(min(abs(abs(x) - m * d), abs(abs(x) - (m + 1) * d)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k, d = map(int, input().split())
n = abs(n)
ok = n // d
left = k - (ok)
if left % 2:
	print(d - (n%d))
else:
    print(n%d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

for i in range(k):
  x = x - d
  if x < 0:
    k = k - i + 1
    break

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

Submission status: Wrong Answer

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

for i in range(k):
  x = x - d
  if x < 0:
    k = k - i + 1
    break

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

Submission status: Wrong Answer

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

X = abs(X)
time = X//D
rest = K - time

if rest <= 0:
    print(X-K*D)
elif rest%2 == 0:
    print(min(X-time*D, abs(X-(time+2)*D)))
else:
    print(min(X-time*D, abs(X-(time+1)*D)))

Submission status: Wrong Answer

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

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

if rest == 0:
    print(X-rest*D)
elif rest < 0:
    print(X-K*D)
elif rest%2 == 0:
    print(min(X-rest*D, abs(X-(rest+2)*D)))
else:
    print(min(X-rest*D, abs(X-(rest+1)*D)))

Submission status: Wrong Answer

Solution code:
import sys
XKD = input().split()
X = int(XKD[0])
K = int(XKD[1])
D = int(XKD[2])
KD = K*D
if abs(X)>KD:
    print(X-KD)
    sys.exit()
else:
    if X>0:
        for i in range(K):
            X-=D
            if X<=0:
                if (K-(int(XKD[0])-X)/D)%2 == 0:
                    print(abs(X))
                    sys.exit()
                else:
                    X+=D
                    print(X)
                    sys.exit()
    else:
        for i in range(K):
            X+=D
            if X>=0:
                if (K-(int(XKD[0])-X)/D)%2 == 0:
                    print(abs(X))
                    sys.exit()
                else:
                    X-=D
                    print(abs(X))

Submission status: Wrong Answer

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

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

if x < 0:
    px = x + d * zeroL
else:
    px = x - d * zeroL

nokori = k - zeroL
if nokori % 2 == 1:
    print(min(abs(px - d), abs(px + d)))
else:
    print(px)

Submission status: Wrong Answer

Solution code:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-08-16 22:37
# @url:https://atcoder.jp/contests/abc175/tasks/abc175_c

import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio

BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def main():
    x,k,d=map(int,input().split())
    n=x//d
    if k<=n:
        print (abs(x-k*d))
    else:
        l,r=abs(x-(n+1)*d),abs(x-n*d)
        if k%2==n%2:
            print (r)
        else:
            print (l)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cs = [x]
res = x
count = 0
if x<0:
    x=x*-1
tem_x = x
print(x)
if x-k*d>=0:
    result = x-k*d
else:
    while (tem_x > 0):
        tem_x -= d
        count += 1
    res = [count * d - x , x - count * d + d]

    if (abs(res[0]) < abs(res[1])):
        if ((k - count) % 2 == 0):
            result = res[0]
        else:
            result = res[1]
    elif(abs(res[0]) == abs(res[1])):
        result = res[0]
    else:
        if ((k - count + 1) % 2 == 0):
            result = res[1]
        else:
            result = res[0]

    result = abs(result)


print(result)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cs = [x]
res = x
tem_x = x
count = 0
if x<0:
    x=x*-1

if x-k*d>=0:
    result = x-k*d
else:
    while (tem_x > 0):
        tem_x -= d
        count += 1
    res = [count * d - x , x - count * d + d]

    if (abs(res[0]) < abs(res[1])):
        if ((k - count) % 2 == 0):
            result = res[0]
        else:
            result = res[1]
    elif(abs(res[0]) == abs(res[1])):
        result = res[0]
    else:
        if ((k - count + 1) % 2 == 0):
            result = res[1]
        else:
            result = res[0]

    result = abs(result)


print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cs = [x]
res = x
tem_x = x
count = 0
if x<0:
    x=x*-1

if x-k*d>=0:
    result = x-k*d
else:
    while (tem_x > 0):
        tem_x -= d
        count += 1
    res = [x - count * d, x - count * d + d]

    if (abs(res[0]) < abs(res[1])):
        if ((k - count) % 2 == 0):
            result = res[0]
        else:
            result = res[1]
    elif(abs(res[0]) == abs(res[1])):
        result = res[0]
    else:
        if ((k - count + 1) % 2 == 0):
            result = res[1]
        else:
            result = res[0]

    result = abs(result)


print(result)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cs = [x]
res = x
tem_x = x
count = 0
if x<0:
    x=x*-1

if x-k*d>=0:
    result = x-k*d
else:
    while (tem_x > 0):
        tem_x -= d
        count += 1
    res = [x - count * d, x - count * d + d]

    if (abs(res[0]) <= abs(res[1])):
        if ((k - count) % 2 == 0):
            result = res[0]
        else:
            result = res[1]
    else:
        if ((k - count + 1) % 2 == 0):
            result = res[1]
        else:
            result = res[0]

    result = abs(result)


print(result)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cs = [x]
res = x
tem_x = x
count = 0
if x<0:
    x=x*-1
    flag=1
else:
    flag=0

if x-k*d>=0:
    result = x-k*d
else:
    while (tem_x > 0):
        tem_x -= d
        count += 1
    res = [x - count * d, x - count * d + d]

    if (abs(res[0]) <= abs(res[1])):
        if ((k - count) % 2 == 0):
            result = res[0]
        else:
            result = res[1]
    else:
        if ((k - count + 1) % 2 == 0):
            result = res[1]
        else:
            result = res[0]

if flag == 1:
    result = result*-1


print(result)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cs = [x]
res = x
tem_x = x
count = 0
if x<0:
    x=x*-1
    flag=1
else:
    flag=0

if x-k*d>=0:
    result = x-k*d
else:
    while (tem_x > 0):
        tem_x -= d
        count += 1
    res = [x - count * d, x - count * d + d]

    if (abs(res[0]) < abs(res[1])):
        if ((k - count) % 2 == 0):
            result = res[0]
        else:
            result = res[1]
    else:
        if ((k - count + 1) % 2 == 0):
            result = res[1]
        else:
            result = res[0]

if flag == 1:
    result = result*-1


print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cs = [x]
res = x
tem_x = x
count = 0
if x<0:
    x=x*-1
    flag=1
else:
    flag=0
    
while(tem_x>0):
    tem_x -= d
    count += 1
res = [x-count*d,x-count*d+d]
if(abs(res[0]) < abs(res[1])):
    if((k-count)%2==0):
        result = res[0]
    else:
        result = res[1]
else:
    if((k-count+1)%2==0):
        result = res[1]
    else:
        result = res[0]

if flag == 1:
    result = result*-1    


print(res)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
scnt = abs(X) // D
if K <= scnt:
    print(abs(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(abs(X - scnt * D)))
        else:
            print(abs(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:
    print(abs(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:
    print(abs(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))