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 d*k>=x:
	f=x//d
	x=x-d*f
	if f%2==0:
		ans=x
	else:
		ans=-(x-d)
else:
	ans=x-(d*k)
	
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
n=max(round((k*d-x)/(2*d)),0)
print(abs(x+2*n*d-k*d))

Submission status: Wrong Answer

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

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

        if(times%2 == K%2):
            print(abs(min_l))
        else:
            print(abs(min_r))
    else:
        print(abs(X+D*K))

Submission status: Wrong Answer

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

if K*D<=X:
    print(abs(X)-K*D)
    exit()
move_num_before = abs(X)//D
move_num_after = move_num_before+1
if (K - move_num_after)%2 == 0:
    print(abs(abs(X)-move_num_after*D))
    exit()
print(abs(X)-move_num_before*D)

Submission status: Wrong Answer

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

if X>=K*D:
    print(abs(X-K*D))
else:
    a=math.ceil(X/D)
    b=X-a*D
    K-=a
    if K%2==0:
        print(abs(b))
    else:
        print(abs(b+D))

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


def main():
    X, K, D = map(int, input().split())
    r = X
    for i in range(D):
        if r > 0:
            r -= K
        else:
            if (D - i - 1) % 2 == 0:
                r = K
            break
    print(abs(r))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    now = x
    if (now==0):
        if k%2 == 0:
            print(now)
        else:
            print(d-now)
    elif (now > 0):
        i = 0
        while now - d > 0:
            now -= d
            i += 1
            if (i==k):
                print(now)
                return
        #i回移動して、ギリギリ正
        #この時点で候補は now or now-d
        if (k-i)%2 == 0:
            print(now)
        else:
            print(d-now)
    elif (now < 0):
        i = 0
        while now + d < 0:
            now += d
            i += 1
            if (i==k):
                print(now)
                return
        #i回移動して、ギリギリ正
        #この時点で候補は now or now-d
        if (k-i)%2 == 0:
            print(-now)
        else:
            print(d+now)
    
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(X, K, D):
    # X + t * D (-K <= t <= +K)
    t = round(-X / D)
    ans = abs(X)
    for i in [-2, -1, 0, +1, +2]:
        tt = t + i
        tt = min(tt, +K)
        tt = max(tt, -K)
        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 * y2 > 0: # same sign
    print(min(abs(y1), abs(y2)))
else:
    if K % 2 == 0:
        ans = solve(X, K // 2, D * 2)
    else:
        ans1 = solve(X + K, (K - 1) // 2, D * 2)
        ans2 = solve(X - K, (K - 1) // 2, D * 2)
        ans = min(ans1, ans2)
    print(ans)

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 = x // d
    rest = 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 = abs(X)
for i in range(K):
    if X <= abs(X-D):
        if (K-i)%2==0:
            break
        else:
            if i+1 != K:
                X = max(abs(X+D), abs(X-D))
            break
    X -= D
print(X)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
a = x//d
k2 = k-a
if x-k*d > 0:
    print(x-k*d)
    exit()

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

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

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 * D)
    else:
        print(abs(X - K * D - D))

Submission status: Wrong Answer

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    x,k,d = map(int, input().split())
    n = x//d
    ans = x
    for i in range(n):
        ans -= d
        k -= 1
        if k == 0:
            break
    ans_2 = abs(ans - d)
    if k%2 ==0:
        print(ans)
    else:
        print(ans_2)

if __name__=="__main__":
    main()

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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

X, K, D = MAP()

if X == 0:
	if K%2 == 0:
		print(0)
	else:
		print(D)
	exit()

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


K -= abs(X)//D
if X < 0:
	X += D*(abs(X)//D)
else:
	X -= D*(abs(X)//D)


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

temp = (0 - -x//d)

if temp > k:
    ans = x - d*k

else:
    ref1 = x - temp*d #0かマイナス
    ref2 = x - (temp - 1)*d #プラス
    k = k - temp
    if k%2 == 0:
        ans = ref1
    else:
        ans = ref2

print(abs(ans))

Submission status: Wrong Answer

Solution code:
def main():
    x, k, d = list(map(int, input().split(" ")))
    dk = d*k

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

if __name__=="__main__":
    main()

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(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 = map(int, input().split())
if abs(x)>k*d:
  print(abs(x)-k*d)
  quit()

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if x >= 0:
    if x - k * d >= 0:
        print(x - k * d)
    else:
        # 最初に負になってしまうところとその直前を巡回する
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ok + ng) // 2
            if x - mid * d <= 0:
                # いきすぎ
                ok = mid
            else:
                ng = mid
        # ok回目で負にたどり着く？
        rest = k - ok
        if rest % 2 == 0:
            # いまいるところに戻ってこれる
            print(abs(x - ok * d))
        else:
            # 一回正方向にいどうしたもの
            print(x - (ok - 1) * d)
else:
    if x + k * d <= 0:
        print(abs(x + k * d))
    else:
        # 最初に正になるところ
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ng + ok) // 2
            if x + mid * k >= 0:
                ok = mid
            else:
                ng = mid
        rest = k - ok
        if rest % 2 == 0:
            print(x + ok * d)
        else:
            print(abs(x + ok * d - d))

Submission status: Wrong Answer

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

cand1 = (k * d - abs(x)) % d
cand2 = abs(x) % d
ans = min(cand1, cand2)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = [int(n) for n in input().split(" ")]

#X = 10
#K = 1
#D = 2


startN = (D * K - X) // (2 * D)

N1 = startN + 1
N2 = startN
N3 = startN - 1

if N1 < 0:
    N1 = 0
if N2 < 0:
    N2 = 0
if N3 < 0:
    N3 = 0

def getScore(n):
    score = X + n * D - D * (K - n)
    if score < 0:
        score = score * -1
    return score

score1 = getScore(N1)
score2 = getScore(N2)
score3 = getScore(N3)

minScore = score1
if minScore > score2:
    minScore = score2
if minScore > score3:
    minScore = score3

print(minScore)

Submission status: Wrong Answer

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

n = x // d

if n > k:
    print(x-k*d)
    exit()

if abs(x-d*n) > abs(x-d*(n+1)):
    a = abs(x-d*(n+1))
    b = n+1
else:
    a = abs(x-d*n)
    b = n

if (k-b) % 2 == 0:
    print(a)
else:
    print(min(abs(a-d),abs(a+d)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    X, K, D = map(int, input().split())
    M = X%D
    S = X//D
    C = K - S 
    if C < 0:
        ans = abs(X) - K*D
    else:
        if C%2 == 0:
            ans = M
        else:
            ans = abs(M - D)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


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

    pmin = x - k * d
    if pmin >= 0:
        return pmin

    pmax = x + k * d
    if pmax <= 0:
        return pmax

    dd, dm = divmod(x, d)
    remains = k - dd
    if remains & 1:
        return abs(dm - d)
    else:
        return dm


def main():
    print(solve())


if __name__ == "__main__":
    main()

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

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

	hituyou = ima // D
	saidai = ima % D

	if hituyou == K:
		print(saidai)
		sys.exit()

	elif hituyou > K:
		ido = K * D
		ans = ima - ido
		print(ans)
		sys.exit()

	else:
		tmp = K - hituyou
		if tmp % 2 == 0:
			print(saidai)
			sys.exit()
		else:
			print(saidai + D)
			sys.exit()




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

Submission status: Wrong Answer

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

def do(x, k, d):
    q = round(x/d)
    x -= q*d
    k -= q
    # print(q, x, k)
    if k%2 == 0:
        print(abs(x))
    else:
        print(min(abs(x + d), abs(x - d)))

if x > 0:
    if x - k*d < 0:
        do(x, k, d)
    else:
        print(x - k * d)

elif x < 0:
    if x + k*d > 0:
        do(abs(x), k, d)
    else:
        print(x + k*d)

else:
    if k%2:
        print(d)
    else:
        print(0)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:

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

x = abs(x)

decim = x/d

if (decim > k):
    print(abs(x - k*d))
    exit(0)
else:
    if (x % d == 0):
        print(0)
    else:
        lower = x // d
        upper = lower + 1
        print(min(abs(x - lower*d), abs(x - upper*d)))

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

Submission status: Wrong Answer

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

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

near0_X = X % D
after_K = K - X//D

if after_K % 2 == 0:
    print(near0_X)
else:
    print(D-near0_X)

Submission status: Wrong Answer

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

r = x%d
l = r-d

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

ans = min(abs(x-d*k), x)
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:
x, k, d = map(int, input().split())

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    x, k, d = ns()
    x = abs(x)
    tmp = x // d
    ans = INF
    base = 0
    if tmp <= k:
        if tmp <= k - 1:
            if x % d < abs(x % d - d):
                ans = x % d
                base = tmp
                if base % 2:
                    ans = abs(x % d - d)
            else:
                ans = abs(x % d - d)
                base = tmp + 1
                if base % 2:
                    ans = x % d
        else:
            ans = x % d
            base = 0
    else:
        ans = x - k * d
        base = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(X) > abs(D * K):
    print(abs(X-(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:
import sys
import math

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

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

if(a >= 0):
	print(abs(a))
elif(b <= 0):
	print(b)
else:
	b = b % (d * 2)
	print(min(abs(b), abs(b - d * 2)))

Submission status: Wrong Answer

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

pos_min = X - K * D
pos_max = X + K * D

if pos_min <= 0 and pos_max >= 0:
    print(min([X % (2 * D), 2 * D - X % (2 * D)]))
elif pos_min > 0:
    print(pos_min)
else:
    print(abs(pos_max))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = X // D
ans1 = X - cnt * D
ans2 = abs(ans1 - D)

if cnt < K:
  if (K - cnt)%2 == 0:
    print(ans1)
  else:
    print(ans2)
else:
  if X >= 0:
    print(X - K * D)
  else:
    print(X + K * D)

Submission status: Wrong Answer

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

ans = None

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

Submission status: Wrong Answer

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

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

        if(times%2 == K%2):
            print(abs(min_l))
        else:
            print(abs(min_r))
    else:
        print(X+D*K)

Submission status: Wrong Answer

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

x = abs(x)
ans = x
n = x // d
if n <= k and n % 2 == k % 2:
    ans = min(ans, x % d)
if k < n:
    ans = min(ans, x - k * d)
if n < k and n % 2 != k % 2:
    ans = min(ans, d - x % d)
print(ans)

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(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:
from math import ceil
x,k,d=map(int,input().split())
x=abs(x)
n=ceil(x/d)
if n>=k:
    ans=abs(x-k*d)
else:
    a=abs(x-n+d)
    b=abs(x-(n-1)*d)
    ans=min(a,b)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

    kyori = abs(x)
    kaisuu = kyori // d
    sa = k - kaisuu
    amari = kyori % d

    if kaisuu >= k:
        print(kyori- k*d)
    else:
        if amari <= d/2:
            if sa % 2 == 0:
                print(amari)
            else:
                print(d - amari)
        else:
            if sa % 2 == 0:
                print(d - amari)
            else:
                print(amari)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

ans = 0
if K*D < X:
    ans = X - K*D
else:
    a1 = X//D
    a2 = X%D
    K -= a1
    # 往復する
    if K%2 == 0:
        ans= a2
    else:
        ans = abs(a2-D)
print(ans)

# 1000000000000000 1000000000000000 1000000000000000

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import math
#input=stdin.readline
#def print(x):stdout.write(str(x)+'\n')
x,n,d=map(int,input().split())
ans=x
if x-n*d<0:
    c=x//d
    n=n-c
    ans=x-c*d
    a=abs(ans-d)
    b=ans
    if n%2==0:
        print(b)
    else:
        print(a)
else:
    print(x-n*d)

Submission status: Wrong Answer

Solution code:
import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    X, K, D = MI()


    if D*K > abs(X):
        ans = min([X%D,D-X%D])
    
    else:
        ans = abs(X) - D*K



    
    print(ans)

if __name__ == "__main__":
	main()

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

Submission status: Wrong Answer

Solution code:
x,k,d=(int(i) for i in input().split())
c=(abs(x)//d)
if(x<0):
    x+=(min(k,c)*d)
else:
    x-=(min(k,c)*d)
k=max(0,k-c)
if(k%2==1 and x<0):
    print(abs(x+d))
elif(k%2==1 and x>0):
    print(abs(x-d))
elif(k%2==1 and x==0):
    print(x+d)
else:
    print(x)

Submission status: Wrong Answer

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

a=x//d

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

Submission status: Wrong Answer

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

Y = X

if X < 0:
    X = -X

i = 0
while X > 0:
    X -= D
    i += 1

pos = [[-X, i], [X+D, i+1]]

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if x >= 0:
    if x - k * d >= 0:
        print(x - k * d)
    else:
        # 最初に負になってしまうところとその直前を巡回する
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ok + ng) // 2
            if x - mid * d <= 0:
                # いきすぎ
                ok = mid
            else:
                ng = mid
        # ok回目で負にたどり着く？
        rest = k - ok
        if rest % 2 == 0:
            # いまいるところに戻ってこれる
            print(abs(x - ok * d))
        else:
            # 一回正方向にいどうしたもの
            print(x - (ok - 1) * d)
else:
    if x + k * d <= 0:
        print(abs(x + k * d))
    else:
        # 最初に正になるところ
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ng + ok) // 2
            if x + mid * k >= 0:
                ok = mid
            else:
                ng = mid
        rest = k - ok
        if rest % 2 == 0:
            print(x + ok * d)
        else:
            print(abs(x + (ok - 1) * d))

Submission status: Wrong Answer

Solution code:

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

x = abs(x)

decim = x/d

if (decim > k):
    print(x - k*d)
    exit(0)
else:
    if (x % d == 0):
        print(0)
    else:
        lower = x // d
        upper = lower + 1
        print(min(abs(x - lower*d), abs(x - upper*d)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


def read():
    X, K, D = map(int, input().strip().split())
    return X, K, D


def solve(X, K, D):
    m = (-X + K*D) / (2*D)
    mc = max(0, math.ceil(m))
    mf = max(0, math.floor(m))
    ans = min(abs(X - K*D + 2 * mc * D), abs(X - K*D + 2 * mf * D))
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Wrong Answer

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

if (abs(X)>K*D):
    ans = abs(abs(X)-K*D)
else:
    
    m = abs(X)//K
    remk = K - m

    now = abs(X)-m*K

    if remk%2==0:
        ans = now
    else:
        ans = abs(now-K)



    
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = abs(x)

k -= x//d
x %= d

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