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:
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = 'ONLINE_JUDGE' not in sys.argv

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)

def main():
    X,K,D=mis()
    X = abs(X)
    ZP = X%D
    if abs(X - ZP)//D > K:
        dprint('too far')
        print(X - D*K)
        return
    ZN = ZP - D
    X, K = ZP, K - abs(X - ZP)//D
    if K&1:
        dprint('odd')
        print(abs(ZN))
    else:
        dprint('even')
        print(abs(ZP))


main()

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
s=[x]
if x>k*d:
    ans=x-k*d
else:  
    for i in range(k):
        if x>0:
            x=x-d
            ans=x
        else:
            x=x+d
            ans=x
        s.append(x)
        if abs(s[i-2])==abs(s[i]) and i>=2:
            ans=s[i]
            break
        
print(abs(ans))

Submission status: Wrong Answer

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

#直線のみに進行すればいい場合の回数
s = x//d

#最短回数よりも移動可能回数の方が少ない場合
if s >= k:
    print(abs(x) - k*d)

#最短経路回数よりも多く移動する場合
elif (abs(x) - k*d) < d:
    if (k-s) %2 == 0:
        print(abs(x) - s*d)
    else:
        print(d-(abs(x) - s*d))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
for i in range(K):
    if 0 <= X:
      absX = abs(X - D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X - D*(i+1))
              print(ans)
              exit(0)
    if X < 0:
      absX = abs(X + D*i)
      if absX <= D:
         if (K-i)%2==0:
              ans = absX
              print(ans)
              exit(0)
         if (K-i)%2==1:
              ans = abs(X + D*(i+1))
              print(ans)
              exit(0)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	X,K,D = inputMap()
	X = abs(X)

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


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

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

	if X - K*D > 0:
		ans = X - K*D
		print(ans)
		sys.exit()
	else:
		tmp = X // D
		nokori = K - tmp
		if nokori % 2 == 0:
			ans = X % D
			print(ans)
			sys.exit()
		else:
			ans = X+D % D
			print(ans)
			sys.exit()





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

Submission status: Wrong Answer

Solution code:
import numpy as np

X,K,D = map(int,input().split())
if np.sign(X)== -1:
  X=-X
a = X//K
b = X%K
if D-a < 0:
  print(X-K*D)
  exit()

if b > K-b:
  b = K-b

if (D-a)%2 == 0:
  print(b)
else:
  print(K+b)

Submission status: Wrong Answer

Solution code:
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
with decimal.localcontext() as ctx:
    ctx.prec = 50
    t=decimal.Decimal(abs(x))//decimal.Decimal(d)
    

if (k<=t):
	if (x>0):
		print(abs(x-k*d))
	else:
		print(abs(x+k*d))
else:
	if (x>0):
		x=x-t*d
		k-=t
		if k%2==0:
			print(abs(x))
		else:
			print(abs(x-d))
	else:
		x=x+t*d
		k-=t
		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) < abs(X - D) and abs(X) < abs(X + D):
    if K % 2 == 0:
        print(abs(X))
    else:
        print(min(abs(X - D), abs(X + D)))
else:
    if abs(X - D) < abs(X + D):
        if K * D <= abs(X):
            print(X - (K * D))
        else:
            remain = K - (X // D)
            X = X - (X // D) * D
            if remain % 2 == 0:
                print(abs(X))
            else:
                print(abs(X - D))
    elif abs(X - D) >= abs(X + D):
        if K * D <= abs(X):
            print(X + (K * D))
        else:
            remain = K - (X // D)
            X = X + (X // D) * D
            if remain % 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) >= k*d:
  print(abs(x)-k*d)
else:
  if (k - abs(x)//d) % 2 == 0:
    print(x%d)
  else:
    print(abs(abs(x%d)-d))

Submission status: Wrong Answer

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

n, x= divmod(X, D)
if n > K:
  print(min(abs(X - D * K), abs(X + D * K)))
  exit()
K = K - n

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

Submission status: Wrong Answer

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

Y = X // D
K -= Y
X = X % D

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

print(abs(X))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
L=min(X+D*K,X-D*K)
R=max(X+D*K,X-D*K)

if 0<=L:
    print(L)
elif R<=0:
    print(R)
else:
    X=L
    start=0
    end=K
    while end-start>1:
        test=(end+start)//2
        if X+test*2*D<=0:
            start=test
        else:
            end=test
    ans1=abs(X+2*D*start)
    ans2=abs(X+2*D*end)
    print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
xx, kk, dd= list(map(int,input().split()))

xx = abs(xx)

aa_double = (xx/dd)

if aa_double >= kk:
    ans = xx - dd*kk
else:
    aa_int = int(aa_double)
    remaining_move = kk - aa_int
    if (aa_int % 2) == 0:
        ans = xx - dd*aa_int
    else:
        ans = xx - dd*(aa_int+1)

print(abs(ans))

Submission status: Wrong Answer

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


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

if X>=0:

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

else:
    if x < 0:
        x *= -1
    for i in range(k):
        x -= d
        if x <= 0:
            if (k-i) % 2 == 0:
                print(x + d)
                break
            else:
                print(x * (-1))
                break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

k = K - (X // D)
x = X % D
while True:
    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()

    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
input_line = input().split(' ')
X = int(input_line[0])
K = int(input_line[1])
D = int(input_line[2])
for i in range(K):
    K -= 1
    if X >= 0:
        X -= D
        if X < 0:
            if K % 2 == 1:
                X += D
            break
    else:
        X += D
        if X > 0:
            if K % 2 == 1:
                X -= D
                
if X < 0:
    X *= -1
print(X)

Submission status: Wrong Answer

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

N = abs(X//D)

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
mx = x + d*k
mn = x - d*k

if (x < 0 and mx > 0) or (x > 0 and mn < 0):
  if k%2 == 0:
    if x < 0:
      b = min(abs(x%(2*d)),abs(x%(2*d)+2*d))
    else:
      b = min(abs(x%(2*d)),abs(x%(2*d)-2*d))
    b = min(abs(b),abs(mx),abs(mn))
  else:
    if x > 0:
      b = min(abs((x-d)%(2*d)),abs((x-d)%(2*d)-2*d))
    else:
      b = min(abs((x+d)%(2*d)),abs((x+d)%(2*d)+2*d))
  ans = min(abs(b),abs(mx),abs(mn))
else:
  ans = min(abs(mx),abs(mn))
print (ans)

Submission status: Wrong Answer

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

if K <= X//D:
    print(abs(X-D*K))

else:
    if X%D == 0:
        print(abs(D))
    else:
        a = abs(D//2)
        b = abs(abs(D)-a)
        print(min(a,b))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
	# print(x,k,d)
if x<k*d:
	ans = abs(x-k*d) % (2*d)
	print(min(ans, 2*d-ans))
else:
	print(x-k*d)
# eval("eval(\"print(\"hello\")\")")

# # a = "1234"
# # al = list(a)
# # al.insert(1,".")
# # print(al)


# str = input()
# l = list(str)
# n = len(str)
# i = 0;
# while i<n:
# 	if(l[i]=="("):
# 		l.insert(i,"*")
# 		l.insert(i,"*")
# 		n = n+2
# 		i=i+2
# 	i = i+1
# # print(l)
# print(eval("".join(l)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X > 0 and X-K*D > 0:
  print(abs(X-K*D))
elif X < 0 and X+K*D < 0:
  print(abs(X+K*D))
else:
  if X > 0:
    A = X//D
    B = X%D
    if (K-A)%2 == 1:
      print(abs(B-D))
    else:
      print(abs(B))
  if X < 0:
    A = (-X)//D
    B = (X%D)-D
    if (K-A)%2 == 1:
      print(abs(B+D))
    else:
      print(abs(B))

Submission status: Wrong Answer

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

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

m = X % D
if m != 0:
    a = X // D
    if (K - a) % 2 == 0:
        print(m)
    else:
        print(D - m)
else:
    a = X // D
    if (K - a) % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
# x > 0のとき
left = abs(x - d * (x // d + 1))
right = abs(x - d * (x // d))

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

if x % d == 0:
    if x // d % 2 == (k - (x // d) + 1) % 2:
        print(right)
    else:
        print(left)
else:
    if x // d % 2 == (k - (x // d)) % 2:
        print(right)
    else:
        print(left)

Submission status: Wrong Answer

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

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 = abs(x) // d
        print(a)
        if abs(abs(x) - d*a) >= abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 0:
            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 == 1:
            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()
        elif abs(abs(x) - d*a) <= abs(abs(x) - d*(a+1)) and (k-a)%2 == 1:
            print(abs(abs(x) - d*(a+1)))
            exit()

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 0
tmp = 0
if X >= K*D:
  print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
with decimal.localcontext() as ctx:
    ctx.prec = 50
    t=decimal.Decimal(abs(x))//decimal.Decimal(d)
    

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

y = x % d
l = x // d

m = k - l
if m < 0:
    if x > 0:
        x -= d * k
    else:
        x += d * k
else:
    if m % 2 == 0:
        x = y
    else:
        x = y - d

print(abs(x))

Submission status: Wrong Answer

Solution code:
import math

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


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

Submission status: Wrong Answer

Solution code:
L = list(map(int, input().split()))
X = L[0]
K = L[1]
D = L[2]

if X >D*K:
    print(abs(X-D*K))
elif X< -D*K:
    print(abs(X+D*K))
    
if X>=0 and X<D*K:
    N = int(abs(X)/D)
    e = X - N*D
    if abs(e) > abs(e-D):
        print(abs(e))
    else:
        print(abs(e-D))

if X<0 and X>-D*K:
    N = int(abs(X)/D)
    e = -X - N*D
    if abs(e) > abs(e-D):
        print(abs(e))
    else:
        print(abs(e-D))

Submission status: Wrong Answer

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

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

	print(x)

Submission status: Wrong Answer

Solution code:
def mini(X, K, D):
    for i in range(K):
        X -= D
        if X <= 0:
            K -= i
            break
        if i == K-1:
            return abs(X)
    if K%2 == 0:
        return abs(X + D)
    else:
        return abs(X)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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


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

if X>=0:

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

Submission status: Wrong Answer

Solution code:

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

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

mod = X % D

min_step = int((X - mod) / D)

if K % 2 == min_step % 2:
    print(mod)
else:
    print(D - mod)

Submission status: Wrong Answer

Solution code:

def main():
    x,k,d = map(int,input().split())
    x = abs(x)
    first_move = abs(x // d)
    if k <= first_move:
        print(x - d * k)
        return

    if abs(x - d * first_move) > abs(x - d * first_move -d):
        first_move += 1
        x = abs(x-d * first_move - d)
    else:
        x = abs(x-d*first_move)

    left_move = k - first_move

    if left_move % 2 == 0:
        print(x)
        return
    else:
        print(min(abs(x-d),abs(x+d)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k < a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = x - (a * d)
    else:
        x = x - (a * d)
        if x > 0:
            x = x - abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
x2 = x
for i in range(k + 1):
    if x < 0:
        ans = min(abs(x),abs(x + d))
        i2 = i
        if i2 % 2 == k % 2:
            #print("a")
            print(abs(x))
            exit()
        else:
            #print("b")
            print(x + d)
            exit()
    x = x - d
    
print(x2 - k * d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x < 0:
    x *= -1

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

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

Submission status: Wrong Answer

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

a = x//d

y = x - a*d
b = k - a

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if x>=0:
    dummy=x
    br=False
    for i in range(int(k/2)):
        if dummy>=0 and dummy-2*d<0:
            br=True
            print(min(dummy,2*d-dummy))
            break
        dummy=dummy-2*d
    if br==False:
        print(dummy-k*d)
else:
    dummy=x
    br=False
    for i in range(int(k/2)):
        if dummy<=0 and dummy+2*d>0:
            br=True
            print(min(-dummy,dummy+2*d))
            break
        dummy=dummy+2*d
    if br==False:
        print(dummy+k*d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def zet(num):
    if num<0:
        return -num
    else:
        return num


x,k,d = map(int, input().split())
x = zet(x)
amari = x%d
syou = x//d
if syou > k:
    syou = k
else:
    syou = syou
sa = k-syou
genzai = zet(x) - syou * d

if sa % 2 == 0:
    if amari*2 < d:
        print(genzai)
    else:
        print(zet(genzai - d))
else:
    print(zet(genzai - d))

Submission status: Wrong Answer

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

if abs(a) > 0:
    if abs(a) >= b*c:
        print(abs(a)-b*c)
    else:
        d = abs(a)//c
        e = b -d
        if e%2== 0:
            print(abs(a)-c*d)
        else:
            print(c*(d+1)-abs(a))
if a == 0:
    if b%2==0:
        print(c)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import numpy as np

x,k,d = np.array(list(map(int,input().split())))
#偶数回前後に移動したら0
#偶数回一方向に移動したら2D

capa_iter = np.abs(x) // np.abs(d)
if capa_iter < k:
  if (k - capa_iter) % 2 == 0:
    print(x - capa_iter*d)
  else:
    print(max([np.abs(x - capa_iter*d-d),np.abs(x - capa_iter*d+d)]))
  exit()


ans = min([np.abs(x-d),np.abs(x+d)])

print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X=abs(X)
q=X//D
mod=X%D

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

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

X,K,D = LI()

sign = np.sign(X)
x = abs(X)

# if x//D==0:
#     if K%2==0:
#         ans = X
#     else:
#         ans = -1 * sign * (x - D)
if x//D>K:
    ans = sign * (x-K*D)
# ちょうど0で振動する場合
elif x%D==0:
    if (K - x//D) % 2 ==0:
        ans = 0
    else:
        ans = -1 * D
else:
    # 残り回数が偶数回
    if (K - x//D) % 2 ==0:
        # print('even')
        ans = sign * (x- (x//D)*D)
    else:
        # print('odd')
        ans = -1 * sign * (x - (x//D)*D -D)
print(ans)

Submission status: Wrong Answer

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

r, s = divmod(x, d)
if r < 0:
    r = abs(r)
    if k < r:
        print(x + k * d)
    elif r%2 == k%2:
        print(s)
    elif r%2 != k%2:
        print(abs(s-d))
else:
    if k <= r:
        print(x - k * d)
    elif r%2 == k%2:
        print(s)
    elif r%2 != k%2:
        print(abs(s-d))