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: Runtime Error

Solution code:
from sys import stdin
from functools import lru_cache


@lru_cache(maxsize=1000)
def f(x, k, d):
    if k == 0:
        return x

    if x == 0:
        if k % 2 == 0:
            return 0
        if k % 2 == 1:
            return d
    if x > 0:
        return f(x-d, k-1, d)
    if x < 0:
        return f(x+d, k-1, d)


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(abs(f(X, K, D)))

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))

for i in range(k):
    if abs(x - d) > abs(x + d):
        x = x + d
    else:
        x = x - d
print(x)

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    a = (X+K*D)//(2*D)
    for i in range(-1,2):
        if a+i <= K and a+i >= 0:
            ans_list.append(abs(X+(K-(a+i))*D - (a+i)*D))
    ans = min(ans_list)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

x = abs(x)

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

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

Submission status: Runtime Error

Solution code:
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()
if abs(x)/d >= k:
    print(abs(x)-k*d))
elif x > 0:
    if (k - math.floor(x/d))%2 == 0:
        print(x%d))
    else:
        print(x%d-d))
elif x <= 0:
    if (k - math.ceil(x/d))%2 == 0:
        print(x%d))
    else:
        print(x%d+d))

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    a = (X+K*D)//(2*D)
    for i in range(-2,3):
        if a+i <= K and a+i >= 0:
            ans_list.append(abs(X+(K-(a+i))*D - (a+i)*D))
    ans = min(ans_list)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
while True:
    if x-d<=0:
        print(min(x),min(abs(x-d))
        break
    else:
        if k:
            x-=d
            k-=1
        else:
            print(x)
            break

Submission status: Runtime Error

Solution code:
[x, k, d]=list(map(int, input().split()))
low=1
high=k
mini=1000000000000000000
curr=abs(cal(x, k , k, d))
while low<=high:
    mid=low+(high-low)//2
    new=cal(x, mid, k ,d)
    if abs(new)>abs(curr):
        low=mid+1
    else:
        high=mid-1
    curr=abs(new)
    mini=min(abs(new), mini)
print(mini)

Submission status: Runtime Error

Solution code:
import math

def move(x,k,d):
    global kouho

    if len(kouho) != 0:
        if x > 0 and round(math.sqrt(min(kouho))) < x-k*d:
            return 0
        elif x < 0 and round(math.sqrt(min(kouho))) < x+k*d:
            return 0
    
    if k == 0:
        kouho.append(x*x)
        return 0
    elif k >= 1:
        move(x+d,k-1,d)

        move(x-d,k-1,d)
    



kouho = []
dt = input()
l = dt.split()
dt1 = [0]*3
for i in range(3):
    dt1[i] = int(l[i])

move(dt1[0],dt1[1],dt1[2])
print(round(math.sqrt(min(kouho))))

Submission status: Runtime Error

Solution code:
import math

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

a = X - K * D
if a > 0:
    print(a)
    return
if a + 2 * K * D < 0:
    print(abs(a + 2 * K * D))
    return
else:
    b = a / (-2 * D)
    p = math.floor(b)
    q = math.ceil(b)
    r = -(a + 2 * D * p)
    s = a + 2 * D * q
    print(min(r,s))
    return

Submission status: Runtime Error

Solution code:
import math

def move(x,k,d):
    global kouho

    if len(kouho) != 0:
        if x > 0 and round(math.sqrt(min(kouho))) < x-k*d:
            return 0
        elif x < 0 and round(math.sqrt(min(kouho))) < x+k*d:
            return 0
        elif x in kouho:
            return 0
    
    if k == 0:
        kouho.append(x*x)
        return 0
    elif k >= 1:
        move(x+d,k-1,d)

        move(x-d,k-1,d)
    



kouho = []
dt = input()
l = dt.split()
dt1 = [0]*3
for i in range(3):
    dt1[i] = int(l[i])

move(dt1[0],dt1[1],dt1[2])
print(round(math.sqrt(min(kouho))))

Submission status: Runtime Error

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

ans = 0
count = X // D
if X >= 0:
    if K <= count:
        ans = X - D * K
    else:
        ans = X - D * count
        if ((K - count) % 2 == 1):
            ans = ans - D
else:
    if K <= -1 * count:
        ans = X + D * K
    else:
        ans = X + D * xount
        if ((K - count) % 2 == 1):
            ans = ans + D

if ans < 0:
    ans = ans * (-1)

print(ans)

Submission status: Runtime Error

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

if (k * d) % x == 0:
    if (k * d) % 2 == 0:
        print(x)
    else:
        print(0)
elif k * d <= x:
    print(abs(abs(x) - abs(k * d)))
else:
    amari = (k * d) % x
    while (amari > d):
        amari = amari % d
    anslist = [abs(amari), abs(d - amari)]
    check = 0
    for i in range(k):
        if d * i > abs(x):
            check = k - i
            continue
    if check % 2 == 0:
        print(amari)
    else:
        print(abs(d - amari))

Submission status: Runtime Error

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
import math
x, k, d = map(int, input().split())

num = k*d

x = abs(x)

if x >= num:
    print(x - num)
else:
    j = int(x / d)
    b = x % (d*j)
    min = min(b, d - b)
    max = max(b, d - b)
    if min == d - b:
        j += 1

    if (k - j) % 2 == 0:
        print(min)
    else:
        print(max)

Submission status: Runtime Error

Solution code:
import sys

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

X,K,D = LI()

sho = X // D
amari = X % D

if (sho>=K):
    saigo = X - (K*D)
    print(abs(saigo))
else:
    nokori = K - ans(sho)
    if nokori % 2 == 0:
        print(abs(amari))
    else:
        ans = min(amari+D,amari-D)
        print(abs(ans))

Submission status: Runtime Error

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

for i in range(k):
  if abs(x-d)<abs(x+d):
    x-=di
  else:
    x+=d
print(abs(x))

Submission status: Runtime Error

Solution code:
[X,K,D]=list(map(int, input().split()))
abx=int(abs(X))
n=abx//D
if n>K:
    if X>0:
        ans=X-K*D
    else:
        ans=X+K*D
else:
    x2=abx-n*D
    x1=(n+1)*D-abx
    if x2>=x1:
        if n+1<=K:
            if (K-n-1)%2==0:
                ans=x1
            else:
                ans=x2
    else:
        if (K-n)%2==0:
            ans=x2
        else:
            ans=x1
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    X, K, D = map(int, input().split())
    if abs(X) == D:
        if K % 2 == 0:
            print(abs(X))
        else:
            print(0)
        return
    for i in range(K):
        plus = abs(X + D)
        minus = abs(X - D)
        if plus < minus:
            X += D
        else:
            X -= D
        print(X)
    print(abs(X)) 
            
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
[X,K,D]=[int(_) for _ in input().split()]
X/=D
for _ in [0]*K:
  X = X-1 if abs(X-1) < abs(X+1) else X+1
print(abs(int(X*D)))

Submission status: Runtime Error

Solution code:
import math

def move(x,k,d):
    global kouho

    if len(kouho) != 0:
        if x > 0 and round(math.sqrt(min(kouho))) < x-k*d:
            return 0
        elif x < 0 and round(math.sqrt(min(kouho))) < x+k*d:
            return 0
    
    if k == 0:
        kouho.append(x*x)
        return 0
    elif k >= 1:
        move(x+d,k-1,d)

        move(x-d,k-1,d)
    



kouho = []
dt = input()
l = dt.split()
dt1 = [0]*3
for i in range(3):
    dt1[i] = int(l[i])

move(dt1[0],dt1[1],dt1[2])
print(round(math.sqrt(min(kouho))))

Submission status: Runtime Error

Solution code:
[X,K,D]=list(map(int, input().split()))
n=abs(X)//D
abx=abs(X)
if n>K:
    if X>0:
        ans=X-K*D
    else:
        ans=X+K*D
else:
    x2=abx-n*D
    x1=(n+1)*D-abx
    if x2>=x1:
        if n+1<=K:
            if (K-n-1)%2==0:
                ans=x1
            else:
                ans=x2
    else:
        if (K-n)%2==0:
            ans=x2
        else:
            ans=x1
print(ans)

Submission status: Runtime Error

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

min_x = 10 ** 16

x = abs(x)

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

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

Submission status: Runtime Error

Solution code:
import numpy as np

X,K,D=map(int,input().split())
for i in np.arange(K):
    if(X>=0):
        X -= D
    if(X<0):
        X += D
        
print(np.abs(X))

Submission status: Runtime Error

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

sign = int(x /abs(x))

if (abs(x) // d) < k:
  tmp = x - sign * d * (abs(x) // d)
  #print(tmp)
  if (k - (abs(x) // d)) % 2 == 0:
    print(abs(tmp))
  else:
    print(min(abs(tmp + d), abs(tmp - d)))
else:
  print(abs(x - sign * d * k))

Submission status: Runtime Error

Solution code:
import sys
import math
X,K,D = map(int, input().split()) 
X1=abs(X)
syou=math.floor(X/D)
amari=math.floor(K/syou)
ans=0
if amari%2==0 and amari!=0:
    ans=X-syou*D
elif amari==0:
    ans=X-D*K
else:
    ans=X-syou*D
    if abs(ans+D)>abs(ans-D):
        ans=ans-D
    else:
        ans=ans+D
print(abs(ans))

Submission status: Runtime Error

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

xi = abs(x)
if abs(x) > d*k:
  m = min(abs(x+d*k),abs(x-d*k))
else:
  for i in range(k):
    if xi - d*i <= 0:
      if (i+k) % 2 != 0:
        m = abs(xi)
      else:
        m = min(abs(xi+d),abs(xi-d))
      break
    break
print(m)

Submission status: Runtime Error

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

if y > 0:
    print(y)
    exit()

while k > 0:
    if x >= 0:
        x -= d
    else:
        x += d

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

Submission status: Runtime Error

Solution code:
X,K,D=map(int,input().split())
count=0
if X>=K*D :
  ans=X-K*D
else :
  for i in range(K+1) :
    if abs(X)<=D :
      if (K-count)%2==0 :
        ans=abs(X)
      else :
        ans=min(abs(X-D),abs(X+D))
      break
    elif X>D :
      X-=D
    else :
      X+=D
    count+=1
print(ans)

Submission status: Runtime Error

Solution code:
[X,K,D]=[int(_) for _ in input().split()]
for _ in [0]*K:
  X = X-D if abs(X-D) < abs(X+D) else X+D
print(abs(X))

Submission status: Runtime Error

Solution code:
import numpy as np

X,K,D=map(int,input().split())
for i in np.arange(K):
    if(X>0):
        X -= D
    if(X<0):
        X += D
        
print(np.abs(X))

Submission status: Runtime Error

Solution code:
def abc175c_walking_takahashi():
    x, k, d = map(int, input().split())
    x = -x if x < 0 else x
    num = math.ceil(x / d)
    if num > k:
        print(x-k*d)
        return
    else:
        k -= num
        x -= num*d
        if k % 2 == 0:
            print(abs(x))
        else:
            print(x+d)
abc175c_walking_takahashi()

Submission status: Runtime Error

Solution code:
def main():
  from sys import stdin
  x,k,d = map(int, stdin.readline().split())
  for _ in [0] * k:
    x = x - d if x > 0 else x + d
  print(abs(x))
main()

Submission status: Runtime Error

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
import math
x, k, d = map(int, input().split())

num = k*d

x = abs(x)

if x >= num:
    print(x - num)
else:
    j = int(x / d)
    min = min(x % (d*j), d - (x % (d*j)))
    if min == d - (x % (d*j)):
        j += 1

    if (k - j) % 2 == 0:
        print(min)
    else:
        print(max(x % (d*j), d - (x % (d*j))))

Submission status: Runtime Error

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

if x < 0:
    x *= -1

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

Submission status: Runtime Error

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


while(K>0 and X-D>=0):
    c=X
    X=X-D
    r=X
    a=min(r,c)
    K=K-1
cc=a

cc=cc+(K//2)*(D)
cc=cc-(K-(K//2))*D
print(abs(cc))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import bisect

N = int(input())
L = list(map(int, input().split()))

cnt = 0
L = list(set(L))
L = sorted(L)


if N >= 3:
    for i in range(len(L)-2):
        for j in range(i+1, len(L)-1):
            ind = bisect.bisect_left(L, L[i]+L[j])
            cnt += max(0, ind-j)
    print(cnt)

else:
    print(0)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    a = [int(i) for i in input().split()]
    x1 = a[0] // a[2]

    if x1 > a[1]:
        print(abs(abs(a[0])-a[1]*a[2]))
    else:
        od = (a[1] -x1)%2
        if od == 0:
            if x1 - a[1] >2:
                print(min(
                    abs(abs(a[0]) - (x1-2) * a[2]),abs(abs(a[0]) - x1 * a[2]),abs(abs(a[0]) - (x1+2) * a[2])
                ))
            else:
                print(min(
                    abs(abs(a[0]) - x1 * a[2]),abs(abs(a[0]) - (x1-2) * a[2])
                ))
        else:
            if x1 - a[1] >3:
                print(min(
                    abs(abs(a[0]) - (x1-1) * a[2]),abs(abs(a[0]) - (x1+1) * a[2]),abs(abs(a[0]) - (x1+3) * a[2])
                ))
            elif x1 - a[1] >1:
                print(min(
                    abs(abs(a[0]) - (x1-1) * a[2]),abs(abs(a[0]) - (x1+1) * a[2])
                ))
            else:
                print(min(
                    abs(abs(a[0]) - (x1-1) * a[2])
                ))

Submission status: Runtime Error

Solution code:
import numpy as np

X, K, D = (list(map(int, input('').split(' '))))
dX = np.arange(-K,K+1,2)
new_X = X + D * dX
print(np.min(np.abs(new_X)))

Submission status: Runtime Error

Solution code:
import math

def move(x,k,d):
    global kouho
    if k == 0:
        kouho.append(x*x)
        return 0
    elif k >= 1:
        move(x+d,k-1,d)

        move(x-d,k-1,d)
    



kouho = []
dt = input()
l = dt.split()
dt1 = [0]*3
for i in range(3):
    dt1[i] = int(l[i])

move(dt1[0],dt1[1],dt1[2])
print(round(math.sqrt(min(kouho))))

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
tmp2 = []
tmp2.append(x)
tmp = x
ans = 0
frag_break = 0
frag = 0
if x < 0:
    frag = x + k * d
else:
    frag = x - k * d
if abs(frag) > abs(x):
    if tmp < 0:
        tmp += d
        tmp2.append(tmp)
    else:
        tmp -= d
        tmp2.append(tmp)
for i in range(2, k + 1):
    if tmp < 0:
        tmp += d
        tmp2.append(tmp)
    else:
        tmp -= d
        tmp2.append(tmp)
    if tmp2[i - 2] == tmp2[i]:
        if i % 2 == k % 2:
            ans = tmp2[i]
        else:
            ans = tmp2[i - 1]
        frag_break = 1
        break
else:
    tmp = frag
    if tmp < 0:
        tmp += d
        tmp2.append(tmp)
    else:
        tmp -= d
        tmp2.append(tmp)
for i in range(2, k + 1):
    if tmp < 0:
        tmp += d
        tmp2.append(tmp)
    else:
        tmp -= d
        tmp2.append(tmp)
    if tmp2[i - 2] == tmp2[i]:
        if i % 2 == k % 2:
            ans = tmp2[i]
        else:
            ans = tmp2[i - 1]
        frag_break = 1
        break
if frag_break == 0:
    ans = tmp
print(abs(ans))

Submission status: Runtime Error

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

distList = []
minV = X
for i in range(0, K+1):
    value = abs(X-i*D)
    if minV < value:
        break
    if (K-i) % 2 == 0:
        minV = X-i*D
        distList.append(value)
print(distList[-1])

Submission status: Runtime Error

Solution code:
ans = 0

if X > 0:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X - D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X- D*tmp
        X2 = X1 - D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
elif X < 0:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X + D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X + D*tmp
        X2 = X1 + D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
else:
    if K %2 == 0:
        ans = 0
    else :
        ans = D
print(ans)

Submission status: Runtime Error

Solution code:
x,k,d = map(int, input().split())
for n in range(k):
  if x > 0:
    if x > d:
      x = x - d
    elif x <= d:
      if (k-n) % 2 == 0:
        print(x)
      else:
        print(-(x-d))
  elif x < 0:
    if -x < -d:
      x = x + d
    elif x >= d:
      if (k-n) % 2 == 0:
        print(=x)
      else:
        print(x+d)

Submission status: Runtime Error

Solution code:
X, K, D = (list(map(int, input('').split(' '))))
dX = np.arange(-K,K+1,2)
new_X = X + D * dX
print(np.min(np.abs(new_X)))

Submission status: Runtime Error

Solution code:
import math;

# input 現在座標ｘ, 移動回数k, 移動距離d
x, k, d = map(int, input().split())


a = math.floor(x / d);

if a > k:
    for i in range(k):
        if x > 0:
            x = x - d;
        elif x <= 0:
            x = x + d;

elif a <= k:
    # あまり
    s = k%a;
    
    for i in range(a+s):
        if x > 0:
            x = x - d;
        elif x <= 0:
            x = x + d;
            
# xが負の数の場合正の数に編集する
if x < 0:
    x = x * (-1);

print(x);

Submission status: Runtime Error

Solution code:
X,K,D=map(int,input().split())
a=round(abs(X/D))
if K<a:
  print(abs(abs(X)-K*abs(D)))
elif (K-a)%2==0:
  l=[]
  l.append(abs(abs(X)-a*abs(D)))
  l.append(abs(abs(X)-(a+2)*abs(D)))
  l.append(abs(abs(X)-(a-2)*abs(D)))
  print(min(l))
elif (K-a)%2!=0:
  l=[]
    l.append(abs(abs(X)-(a+1)*abs(D)))
    l.append(abs(abs(X)-(a-1)*abs(D)))  
  print(min(l))

Submission status: Runtime Error

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


def is_even(num):
    if num % 2 == 0:
        return True
    else:
        return False


ans = 0

if 2 * x < d:
    ans = x
else:
    t = x // d
    if k > t - 2:
        if (k - t) / 2 == 0:
            t0 = abs(x - (t - 2) * d)
            t1 = abs(x - (t) * d)
            t2 = abs(x - (t + 2) * d)
            ans = min(t0, min(t1, t2))
        else:
            t0 = abs(x - (t - 1) * d)
            t2 = abs(x - (t + 1) * d)
            ans = min(t0, min(t1, t2))
    else:
      ans = abs(x- (k) * d)

print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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


def solve(X: int, K: int, D: int):
    if X<0:
        X*=-1

    mostl = X - K * D
    mostr = X + K * D
    if mostl*mostr>0:
        print(min(mostl, mostr))
        exit()

    ans=abs(X)
    if K%2 == 1:
        X -= D*flg
        K -= 1
        ans=min(ans, abs(X))

    N = X // (D*2)
    ans=min(ans, abs(X-D*2*N))
    if N < K//2:
        ans = min(ans, abs(X - D * 2 * (N+1)))

    print(ans)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(X, K, D)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
X,K,D=map(int,input().split())
count=0
if X>=K*D :
  ans=X-K*D
else :
  for i in range(K) :
    if abs(X)<=D :
      if (K-count)%2==0 :
        ans=abs(X)
      else :
        ans=min(abs(X),abs(X-D),abs(X+D))
      break
    elif X>D :
      X-=D
    else :
      X+=D
    count+=1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
X, K, L = map(int, input().split())
min_dist = inf
for p_num in range(K+1):
    dist = L*p_num + (-1)*L*(K-p_num)
    if dist < min_dist:
        min_dist = dist
print(min_dist)

Submission status: Runtime Error

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

sign = int(x /abs(x))

if (abs(x) // d) < k:
  tmp = x - sign * d * (abs(x) // d)
  #print(tmp)
  if (k -(abs(x) // d)) % 2 == 0:
    print(tmp)
  else:
    print(min(abs(tmp + d), abs(tmp - d)))
else:
  print(abs(x - sign * d * k))

Submission status: Runtime Error

Solution code:

x,k,d=map(int,input().split())
for i in range(k-1):
    if x>0:
        x-=d
        if x<=0:
            flag=-1
            break
    elif x<0:
        x+=d
        if x>=0:
            flag=1
            break
    else:
        flag=0
        break
if flag==0:
    print(0)
elif flag==-1:
    print(min(abs(x),x+d))
else:
    print(min(abs(x-d),x))

Submission status: Runtime Error

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

for i in range(K):
  if X >= 0:
    X -= D
    if X < 0 and abs(X) < X + D:
      if (K-i+1) % 2 == 0:
        print(abs(x))
        exit()
      else:
        print(X + D)
        exit()
  else:
    X += D
    if X >= 0 and X < abs(X-D):
      if (K-i+1) % 2 == 0:
        print(X)
        exit()
      else:
        print(abs(X-D))
        exit()
print(abs(X))

Submission status: Runtime Error

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

ans = abs(x)

for _ in range(k):
    if k % 2 == 0 and x == d:
        break

    pre = ans
    if x < 0:
        ans = min(ans, abs(ans+d))
    if x >= 0:
        ans = min(ans, abs(ans-d))

    if pre <= ans:
        break

print(pre)

Submission status: Runtime Error

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

def d(h):
    return X + (h - (K-h))*D

if d(s) >= 0:
    ans = d(s)
elif d(g) <= 0:
    ans = g(g)
else:
    half = (s + g)//2
    while s < half:
        if d(half) < 0:
            s = half
        else:
            g = half
        half = (s + g)//2

    ans = min(abs(d(s)) , abs(d(g)) )
print(ans)

Submission status: Runtime Error

Solution code:
x, k, d=map(int,input().split())
if x == k and k == b and k % 2 == 0:
    print(x)
else:
    for i in range(k):
        if x >= 0:
            x = x - d
        elif x < 0:
            x = x + d
    print(abs(x))

Submission status: Runtime Error

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

print(ans)

Submission status: Runtime Error

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

ans = 0

if x > 0:
    for i in range(x,-x,-d):
        l.append(i)
        if i < 0:
            break

else:
    for i in range(x,-x,d):
        l.append(i)
        if i > 0:
            break
    

if len(l)-1 > k:
    ans = l[k]
else:
    if len(l)%2 ==0:
        odd = l[len(l)-1]
        even = l[len(l)-2]
    else :
        odd = l[len(l)-2]
        even = l[len(l)-1]
    if k%2 == 0:
        ans = even
    else:
        ans = odd

print(abs(ans))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

xi = abs(x)
if abs(x) > d*k:
  m = min(abs(x+d*k),abs(x-d*k))
else:
  for _ in range(k):
    xi -= d
    if xi < 0:
      m = min(abs(xi),abs(xi+d),abs(xi-d))
      break
print(m)

Submission status: Runtime Error

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.5:
      print(abs(X-D*(tm+1)))
    else:
      print(abs(X-D*(tm-1)))
  else:
    print(abs(X-D*tm))

Submission status: Runtime Error

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

sign = int(x /abs(x))

if (abs(x) // d) < k:
  tmp = x - sign * d * (abs(x) // d)
  if (abs(x) // d) % 2 == 0:
    print(tmp)
  else:
    print(min(abs(tmp + d), abs(tmp - d)))
else:
  print(abs(x - sign * d * k))