Problem:


Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ans = abs(x)
min = ans // d
cnt = 0
while True:
    if cnt >= k: break
    if cnt < min: ans -= d
    else:
        if k - (cnt + 1) % 2 != 0: ans = abs(ans - d)
        break
    cnt += 1
print(ans)

Submission status: Wrong Answer

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

solve()

Submission status: Wrong Answer

Solution code:
import sys
import bisect
#t=int(input())
t=1
for _ in range(t):
    x,k,d=map(int,input().split())
    #l=list(map(int,input().split()))
    #l2=list(map(int,input().split()))
    k1=x//d
    if k1>=k:
        print(x-k*d)
    else:
        pos1=x-k1*d
        pos2=abs(x-(k1+1)*d)
        if (k-k1)%2:
            print(pos2)
        else:
            print(pos1)

Submission status: Wrong Answer

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

k_i = abs(x//d)

# l = [abs(abs(x)-d*k_i), abs(abs(x)-d*(k_i+1))]

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

Submission status: Wrong Answer

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

n = int(n)
k = int(k)
d = int(d)

# print(n, k, d)

x = (k*d - n)//(2*d);

# debug1(x);
x = max(0, x);
ans = abs(n+x*d - (k-x)*d);

if (x+1 <= k):
    x = x + 1
    ans = min(ans, abs(n+x*d - (k-x)*d));

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
ans=x
for i in range(k):
    a=2*i
    ans=min(abs(x-k*d+a*d),ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations
def main():
    x, k, d = map(int, input().split())
    if x == 0:
        r = (k % 2) * d
        print(r)
    elif x < d:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(abs(abs(x) - d))
    else:
        t1 = abs(x) // d
        if t1 > k:
            r = abs(x) - k * d
            print(r)
        else:
            k -= t1
            x = abs(x) - t1 * d
            if k % 2 == 0:
                print(x)
            else:
                print(abs(x - d))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

t = True
cnt = 0

while t == True:
  cnt += 1
  x -= d
  if cnt == k or x < d/2:
    break

if (k-cnt) % 2 == 1:
  x -= d

print(abs(x))

Submission status: Wrong Answer

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

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:
    pass
else:
  print(abs(x))

Submission status: Wrong Answer

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    X, K, D = readnums()
    if X // D >= K:
        print(X - K * D)
    else:
        a1 = X % D
        a2 = D - X % D
        if (K - X // D) % 2:
            print(a2)
        else:
            print(a1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

if x / d <= k:
  k -= x / d

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

Submission status: Wrong Answer

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

g = abs(X) % D

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

Submission status: Wrong Answer

Solution code:
def main():
    X, K, D = list(map(int, input().split()))
    ans = 0
    if K*D <= X:
        ans = X - K*D
    else:
        while X > 0:
            X = X - D
        if abs(X) < abs(X+D) :
            ans = abs(X)
        else:
            ans = abs(X+D)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def main():
    x, k, d = map(int, input().split())
    if x > 0:
        for i in range(1, k+1):
            tmp_x = x - d
            if tmp_x <= 0:
                break
            else:
                x = tmp_x
        k -= i
        if abs(tmp_x) < x:
            if k % 2 == 0:
                ans = abs(tmp_x)
            else:
                ans = x
        else:
            if k % 2 == 0:
                ans = abs(tmp_x)
            else:
                ans = x
    elif x < 0:
        for i in range(1, k+1):
            tmp_x = x + d
            if tmp_x >= 0:
                break
            else:
                x = tmp_x
        k -= i
        if abs(tmp_x) < x:
            if k % 2 == 0:
                ans = abs(tmp_x)
            else:
                ans = x
        else:
            if k % 2 == 0:
                ans = abs(tmp_x)
            else:
                ans = x
    else:
        if k % 2 == 0:
            ans = 0
        else:
            ans = d
    print(ans)

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def main(x,k,d):
    q=x//d
    r=x%d
    if k<q:
        ans=x-k*d
    else:
        if (k-q)%2==0:
            ans=r
        else:
            ans=d-r
    return ans

if __name__=='__main__':
    x,k,d=readinput()
    ans=main(x,k,d)
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = X % D
k = K - X//D
if k < 0:
    print(min(abs(X + K * D), abs(X - K * D)))
else:
    if k % 2 == 0:
        print(abs(x))
    else:
        print(abs(abs(x)-D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X < 0:
    X *= -1

res = X
for i in range(K):
    X -= D
    if (i + 1) % 2 != K % 2:
        continue
    if X < 0:
        if res > abs(X):
            res = abs(X)
        break
    res = X

print(res)

Submission status: Wrong Answer

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

minx=x-k*d
maxx=x+k*d
targetx=minx%(2*d)
if targetx>d:
    targetx-=2*d
if targetx==d:
    if x<0:
        targetx-=2*d

if minx>targetx:
    print(minx)
    exit()
if maxx<targetx:
    print(maxx)
    exit()
print(targetx)

Submission status: Wrong Answer

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

x = X
if X < 0: x = -x

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

Submission status: Wrong Answer

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

def mp(): return map(int, input().split())
def lmp(): return list(map(int, input().split()))
x,k,d = mp()
if x >= 0:
    if x - k*d >= 0:
        print(x-k*d)
    else:
        u = x % d
        l = abs(u - d)
        if (k - x//d) % 2 == 0:
            print(u)
        else:
            print(l)
else:
    if x + k*d <= 0:
        print(x+k*d)
    else:
        u = x % d
        l = abs(u - d)
        if (k - abs(x)//d) % 2 == 1:
            print(u)
        else:
            print(l)

Submission status: Wrong Answer

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

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	x, k, d = map(int, sys.stdin.readline().split())
	x = abs(x)
	if x % d == 0:
		if x // d >= k:
			print(x - d * k)
		else:
			if x // d % 2 == 0:
				print(0)
			else:
				print(d)
	else:
		if x // d >= k:
			print(x - d * k)
		else:
			if x % d < abs(x % d - d):
				if (x // d) % 2 == 0:
					print(x % d)
				else:
					print(abs(x % d - d))
			else:
				if (x // d + 1) % 2 == 0:
					print(abs(x % d - d))
				else:
					print(x % d)




main()

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
a = 0
b = k +  1
mid = 0
while b - a > 1:
    mid = (a + b) >> 1
    if x - d * mid >= 1:
        a = mid
    else:
        b = mid
print(x - a * d)

Submission status: Wrong Answer

Solution code:
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    return

if __name__=='__main__':
    x,k,d = IL()
    st = set()
    c = -1
    for i in range(k):
        if abs(x-d) < abs(x+d):
            x -= d
        else:
            x += d
        if x in st and c == -1:
            c = i
        elif c != -1:
            c = abs(c-i)
            break
        st.add(x)
    k = k-i+1
    k -= c*(k//c)
    for i in range(k):
        if abs(x-d) < abs(x+d):
            x -= d
        else:
            x += d
    print(abs(x))
    solve()

Submission status: Wrong Answer

Solution code:
## coding: UTF-8
X, K, D= map(int,input().split())


saidai = X + K * D
saisyou = X - K * D

if(saidai == 0):
    ans = 0
elif(saisyou == 0):
    ans = 0
else:
    if(saidai > 0 and saisyou > 0):
        ans = saisyou
    elif(saidai < 0 and saisyou < 0):
        ans = saidai
    else:
        L = X // D
        P = X - L * D
        #print(L)
        if(L % 2 == K % 2):
            l = [abs(P - 4*D), abs(P-2*D), abs(P), abs(P+2*D), abs(P+4*D)]
        else:
            l = [abs(P - 3*D), abs(P-D), abs(P+D), abs(P+3*D)]
        ans = min(l)
            
        '''
        a = X - L * D
        b = a + D
        c = b + D
        d = a - D
        e = d - D
        l = [abs(a), abs(b), abs(c), abs(d), abs(e)]
        ans = min(l)
        '''
        #ans = -1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def abs(a):
	if a<0:
		return -a
	return a
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(d)
	else:
		print(0)
if x>0:
	if k*d<=x:
		print(x-k*d)
		exit(0)
	r=x%d
	steps=(x-r)//d
	l=abs(r-d)

	if((k-steps)%2==0):
		print(r)
	else:
		print(l)
if x<0:
	if k*d<=abs(x):
		print(abs(x+k*d))
		exit(0)
	l=abs(x)%d
	steps=(abs(x)-abs(l))/d
	r=-l+d

	if (k-steps)%2==0:
		print(l)
	else:
		print(r)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
seen = set()
for i in range(k):
  if abs(x) in seen:
    print(abs(x))
    exit()
  seen.add(abs(x))
  if x>=0:
    x -= d
  else:
    x += d

seen.add(x)
print(abs(min(seen)))

Submission status: Wrong Answer

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

cnt=abs(x)//d

if cnt>k:
    cnt=k


if x>=0:
    p=x-d*cnt
else:
    p=x-d*cnt

k-=cnt

if p==0 and k%2==1:
    print(d)
else:
    print(p)

Submission status: Wrong Answer

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

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)&((abs(x)/d)%2==1):
    print(d)
else:
    print((abs(x)-k*d + 1000000000000000000000000*d )% d)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if ans>0:
  print(ans)
else:
  print((-1)*ans)

Submission status: Wrong Answer

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

r = x%d
l = r-d

lc = abs(x-l)//d
rc = abs(x-r)//d

ans = abs(x-d*k)
if(k>=lc):
    if((k-lc)%2): ans = min(ans, min(abs(l+d), abs(l-d)))
    else: ans = min(ans, abs(l))
if(k>=rc):
    if((k-rc)%2): ans = min(ans, min(abs(r+d), abs(r-d)))
    else: ans = min(ans, abs(r))
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import re
import queue

input = stdin.readline

MOD = 1000000007
INF = 122337203685477580


def solve():
    x,k,d = map(int, input().split())
    move = k*d
    x = abs(x)
    if x > k*d:
        print(x -k*d)
    else:
        q = x//d
        if abs(q - k)/2 == 0:
            print(x%d)
        else:
            print(d - x%d)
            
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# by the authority of GOD     author: manhar singh sachdev #

import os,sys
from io import BytesIO, IOBase

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

#Fast IO Region
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")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    X, K, D = [int(item) for item in input().split()]

    if K*D >= X:
        temp = K - X // D

        if temp % 2 == 1:
            print(D - X%D)
        else:
            print(X%D)
    else:
        print(X - K*D)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
t1 = x % d
t2 = t1 - d
n = (x - t1) // d
if k == n:
    print(t1)
elif k < n:
    print(x - k * d)
else:
    n = k - n
    if n % 2 == 0:
        print(t1)
    else:
        print(-t2)

Submission status: Wrong Answer

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

Y = X

if X < 0:
    X = -X

i = 0
posA = X % D
posB = posA - D
i = X // D + 1

if K >= i:
    if K % 2 == i % 2:
        print(abs(posB))
    else:
        print(abs(posA))
else:
    print(abs(Y-D*K))

Submission status: Wrong Answer

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

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

m = X % D
l = X // D


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

Submission status: Wrong Answer

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

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    X,K,D=mi()

    if X == 0:
        ans = D if K % 2 == 1 else 0
        print(ans)
    
    n = abs(X)//D
    ans = INF
    for n2 in [n-1,n,n+1]:
        m = K-n2

        if X > 0:
            res = X - D*n2
        else:
            res = X + D*n2

        if m <= 0:
            if X > 0:
                res = X - D*K
            else:
                res = X + D*K
            ans = min(ans,abs(res))
            continue


        if m % 2 == 1:
            res = min(abs(res + D), abs(res-D))
        else:
            res = res
        
        ans = min(ans,abs(res))
    
    print(ans)






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


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

d*=2
k/=2


v1=x-k*d
n=x/d
v2=x-n*d if (n<=k) else v1
v3=x-(n+1)*d if(n+1<=k) else v1

print min(abs(v1),abs(v2),abs(v3))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
if X>0 and X>K*D:
  print(X-K*D)
  exit()
elif X<0 and X<K*D:
  print(abs(X-K*D))
  exit()
d,r=divmod(abs(X),D)
if (K-d)%2==0:
  print(abs(r))
else:
  print(abs(D-r))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

#x：現在地
#k：回数
#d:移動距離

idou = x // d

if idou > k:
  idou = k

minus = abs(x - d * idou)
plus = abs(x + d * idou)


if minus < plus:
  x = minus
else:
  x = plus

nokori = k - idou

nokori = nokori % 2

minus = abs(x - d * nokori)
plus = abs(x + d * nokori)

if minus < plus:
  x = minus
else:
  x = plus
  
print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
for i in range(K):
    A = abs(X-D)
    B = abs(X+D)
    if min([A, B]) < X :
        if A > B:
            X += D 
        else:
            X -= D
    else:
        break
print(X)

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
x = i[0]
k = i[1]
d = i[2]
c=x-d*k
if c>=0:
    print(c)
else:
    p1=-c//(2*d)
    p2=(-c+2*d-1)//(2*d)
    print(min(abs(c+2*d*p1),abs(c+2*d*p2)))

Submission status: Wrong Answer

Solution code:
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    #n = int(stdin.readline().rstrip())
    x,k,d = map(int, stdin.readline().rstrip().split())
    #L = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if abs(x) > k*d:
        print(abs(x) - k*d)
        exit()
    else:
        a = x // d
        if abs(abs(x) - d*a) > abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 1:
            print(abs(abs(x) - d*(a+1)))
            exit()
        elif abs(abs(x) - d*a) > abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 0:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) < abs(abs(x) - d*(a+1)) and (k-a)%2 == 0:
            print(abs(abs(x) - d*a))
            exit()
        else:
            print(abs(abs(x) - d*(a+1)))
            exit()




if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

k = round(X / D)

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

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

    else:
        print(min(abs(abs(X) - (k + 1) * D), abs(abs(X) - (k - 1) * D)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
nums = input().split()
ans=1e18
x=int(nums[0])
k=int(nums[1])
d=int(nums[2])
mo=x%d
_k=(x-mo)/d
_k=min(_k,k)
x-=_k*d
k-=_k
k%=2
if(k==0):
    ans=min(ans,abs(x))
else:
    x-=d
    ans=min(ans,abs(x))
print(int(ans))

Submission status: Wrong Answer

Solution code:
import os
import sys
from io import BytesIO, IOBase

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 list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()


s,k,d = Ri()
flag = False
if s < 0:
    flag = True


s = abs(s)
times = s//d

minn = s-k*d
if minn >= 0:
    print(s-k*d)
else:
    pos1 = s-times*d
    pos2 = INF
    k-=times
    if k >= 1:
        pos2 = abs(pos1 - d)
        print(max(pos1, pos2))
    else:
        print(pos1)

Submission status: Wrong Answer

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

a = x//d
b = x%d

if k <= a:
    print(abs(x-k*d))

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

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations
def main():
    x, k, d = map(int, input().split())
    if x == 0:
        r = (k % 2) * d
        print(0)
    elif x < d:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(abs(abs(x) - d))
    else:
        t1 = abs(x) // d
        if t1 > k:
            r = abs(x) - k * d
            print(r)
        else:
            k -= t1
            x = abs(x) - t1 * d
            if k % 2 == 0:
                print(x)
            else:
                print(abs(x - d))

if __name__ == '__main__':
    main()