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: Accepted

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

print(abs(now))

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
if x < 0:
    x = -x
if x - k*d > 0:
    print(x-k*d)
    quit()

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# vim: fileencoding=utf-8

import math


def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    n = math.ceil(x / d)
    r = 0
    if n > k:
        r = x - (k * d)
    else:
        r = x - (n * d)
        k2 = k - n
        if k2 % 2 == 1:
            r += d
    print(abs(r))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

else:
	k-=x//d
	x-=(x//d)*d
	rem=k%2
	if rem:
		print(d-x%d)
	else:
		print(x%d)

Submission status: Accepted

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

if k%2 == 0: #偶数の場合
    flg = 1
else:
    flg = 0
x = abs(x) #対称性から
if 2*x < d:
    if flg: #元の場所
        print(x)
    else:
        print(d-x)
else:
    if x < d: #原点を超える
        if flg:
            print(x)
        else:
            print(d-x)
    else: #原点を超えない値がある
        tmp = x // d #移動した回数
        if tmp >= k:
            print(x-d*k)
        else:
            s = x - d*tmp
            t = abs(s-d)
            if tmp%2 == 0: #偶数
                if flg:
                    print(s)
                else:
                    print(t)
            else:
                if flg:
                    print(t)
                else:
                    print(s)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys


def resolve(in_):
    X, K, D = map(int, next(in_).split())

    c, a = X // D, X % D
    b = a - D

    if X >= 0:
        ans = X - K * D
        if ans <= b:
            if c % 2 == K % 2:
                ans = a
            else:
                ans = abs(b)
        elif ans <= a:
            ans = a
        else:
            pass
    else:
        ans = X + K * D
        if ans >= a:
            if c % 2 == K % 2:
                ans = a
            else:
                ans = abs(b)
        elif ans >= b:
            ans = abs(b)
        else:
            ans = abs(ans)
    return ans

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


if D * K < abs(X):
    #届かない場合
    print(abs(X) - D * K)
else:
    #届く場合、何回移動すると一番近くなるかを算出
    cnt = int(abs(X) / D) 
    distance = abs(X) % D
    if distance > (D/2):
        cnt = cnt + 1
        distance = abs(distance - D)
    #残り移動回数が奇数の場合は一個動く、偶数の場合は確定
    if (K - cnt) % 2 == 0:
        print(distance)
    else:
        print(abs(distance - D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

if X >= 0: 
    temp1 = X // D
    if temp1+1 > K:
        print(X - D * K)
        sys.exit()
    else:
        ite1 = (K - temp1) % 2
        if ite1 == 1:
            print(abs(X % D - D))
            sys.exit()
        else:
            print(X % D)
            sys.exit()
else:
    temp1 = abs(X // D) - 1
    if temp1+1 > K:
        print(abs(X + D * K))
        sys.exit()
    else:
        ite1 = (K - temp1) % 2
        if ite1 == 1:
            print(X % D)
        else:
            print(abs(X % D - D))

Submission status: Accepted

Solution code:
[x, k, d] = map(int , input().split())
t = ((k*d - x) / (2*d))
t = round(t)
if k < t:
	t = k
elif t < 0:
	t = 0
min = x - k*d + 2*t*d
print(abs(min))

Submission status: Accepted

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

if X // D <= K:
    now = X % D
    K -= X // D
else:
    now -= K * D
    K = 0

if K > 0:
    ans1 = now
    ans2 = abs(now - D)

    if ans2 < ans1 and K % 2 == 1 or ans2 > ans1 and K % 2 == 1:
        now = ans2
print(abs(now))

Submission status: Accepted

Solution code:
X, N, D = map(int, input().split())
X = abs(X)
maxD = N * D

if X > maxD :
  print(X - maxD)
else :
  N -= X // D
  num = N % 2
  if num == 0:
    print(X % D)
  else :
    print(D - X % D)

Submission status: Accepted

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


def main():
    [x, k, d] = [int(e) for e in si().split()]
    if x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(abs(d))
    elif x < 0:
        mul = (0-x)//d
        if k <= mul:
            x += d*k
            print(abs(x))
            return
        else:
            x += d*mul
            k -= mul
        while x < 0 and k:
            x += d
            k -= 1
        if k > 0:
            if k % 2:
                print(abs(x-d))
            else:
                print(x)
        else:
            print(abs(x))
    else:
        mul = (x-0)//d
        if k <= mul:
            x -= d*k
            print(x)
            return
        else:
            x -= d*mul
            k -= mul
        while x > 0 and k:
            x -= d
            k -= 1
        if k > 0:
            if k % 2:
                print(x+d)
            else:
                print(abs(x))
        else:
            print(abs(x))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if k * d >= x:
    # 右側
    ans1 = x % d
    # 左側
    ans2 = abs(ans1 - d)
    # x//d -> 右側に必要な最低限の手数
    # k-最低限の手数 が　偶数 -> そこへ着地
    # 奇数ならもう1つdマイナスのやつへ着地
    if (k - (x // d)) % 2 == 0:
        print(ans1)
    else:
        print(ans2)
else:
    print(x - (k * d))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
dis,num,move=map(int,input().rstrip().split())
if dis<0:
    dis=-dis
a=dis%move
b=move-dis%move
c=int((dis-a)//move)
if dis-num*move>0:
    result=dis-num*move
elif (num-c)%2==0:
    result=a
else:
    result=b
print(result)

Submission status: Accepted

Solution code:
x,k,d=map(int, input().split())
if x<0:
  x=-1*x

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

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
n = (K-X//D)//2
#print(n)
if X - K*D > 0:
    ans = X-K*D
elif X + D*K < 0:
    ans = abs(X+D*K)
else:
    ans = float('inf')
    for i in range(-1, 2):
        res = X + D*(K-2*(n+i))
        ans = min(ans, abs(res))
    for i in range(-1, 2):
        res = X + D*(2*(n+i)-K)
        ans = min(ans, abs(res))
        #print(res)

print(ans)

Submission status: Accepted

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

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

    if X - K * D >= 0:
        print(X - K * D)
    elif X + K * D <= 0:
        print(-D * K - X)
    else:
        y = (D * K - X) // (2 * D)
        yy = (D * K - X + 2 * D) // (2 * D)
        print(min(abs(2 * D * y + X - D * K), abs(2 * D * yy + X - D * K)))
main()

Submission status: Accepted

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

Submission status: Accepted

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

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

div = x // d
if k <= div:
    ans = x - k * d
else:
    if (k - div) % 2 == 1:
        ans = x - d * (div + 1)
    else:
        ans = x - d * div

print(abs(ans))

Submission status: Accepted

Solution code:
import sys

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

if X < 0:
    X = -X

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

temp_X = X
temp_K = K

#while temp_X-D >= 0:
#    temp_X -= D
#    temp_K -= 1

k = X//D

temp_X = X-k*D
temp_K = K-k

if temp_K%2==0:
    print(temp_X)
else:
    print( abs(temp_X-D) )

Submission status: Accepted

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

r = X % D

maxMoveLength = K * D

plusMin = r
minusMin = r - D

countForPlusMin = abs(X - plusMin) // D
countForMinusMin = abs(X - minusMin) // D

goal = 0
if K >= countForPlusMin and K % 2 == countForPlusMin % 2:
    goal = plusMin
elif K >= countForMinusMin and K % 2 == countForMinusMin % 2:
    goal = minusMin
elif K < countForMinusMin or K < countForPlusMin:
    if X < 0:
        goal = X + D * K
    else:
        goal = X - D * K

print(abs(goal))

Submission status: Accepted

Solution code:
import numpy as np
#import functools
#import operator
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with
#from itertools import permutations as perm
#import collections as C #most_common
import math
#import sympy

#N = int(input())
X,K,D= map(int,input().split())
#L = list(map(int,input().split()))
#S = str(input())
#T = str(input())
c,C=0,0

X=abs(X)

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

else:
    x = math.ceil(X/D)

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

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

Submission status: Accepted

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


if x == 0:

    if k % 2 == 0:
        ans = 0
    else:
        ans = d

elif x > 0:
    amari = x % d
    kouho1 = amari
    kouho2 = amari - d
    kyori1 = abs(x - kouho1)
    kyori2 = abs(x - kouho2)
    if kyori1 // d > k:
        ans = x - k * d

    elif (kyori1 // d) % 2 == 1:
        if k % 2 == 1:
            ans = abs(kouho1)
        else:
            ans = abs(kouho2)
    else:
        if k % 2 == 0:
            ans = abs(kouho1)
        else:
            ans = abs(kouho2)
else:
    x *= -1
    amari = x % d
    kyori1 = abs(x - amari)
    kyori2 = abs(x - (amari - d))
    if kyori1 // d > k:
        ans = abs(x - k * d)
    elif (kyori1 // d) % 2 == 1:
        if k % 2 == 1:
            ans = abs(amari)
        else:
            ans = abs(amari - d)
    else:
        if k % 2 == 0:
            ans = abs(amari)
        else:
            ans = abs(amari - d)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D = (abs(int(T)) for T in input().split())
if K<=(X//D):
    Now = X-K*D
    Count = 0
    print(Now)
else:
    Now = X-(X//D)*D
    Count = K-(X//D)
    if abs(Now)<abs(Now-D):
        if Count%2==0:
            print(abs(Now))
        else:
            print(abs(Now-D))
    else:
        if Count%2==1:
            print(abs(Now-D))
        else:
            print(abs(Now))

Submission status: Accepted

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

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

if k < tmp:
  result = abs(abs(x) - d*k)
elif tmp%2 == k%2:
  result = abs(abs(x) - d*tmp)
else:
  a = result = abs(abs(x) - d*tmp)
  result = abs(abs(a) - d)

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():

    x,k,d = tuple([int(t)for t in input().split()])

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

        else:
            print(d-ax%d)
    else:
        print(abs(x)-k*d)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
x, k, d = [int(i) for i in input().split()]
x = abs(x)
moves_to_base_without_exceeding = x // d
if(x - k * d >= 0):
    print(x - k * d)
elif(((moves_to_base_without_exceeding - k) % 2 == 0)):
    print(x % d)
else:
    print(d - (x % d))

Submission status: Accepted

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(abs(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: Accepted

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

X = abs(X)
Q, R = divmod(X, D)

if Q >= K:
    output = X - (K*D)
else :
    if (K - Q) % 2 == 1:
        output = abs(D - R)
    else :
        output = R
        
print(output)

Submission status: Accepted

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

def solve():
    if X // D >= K:
        return X - D*K
    if ((X//D)%2 == 0) ^ (K%2 == 0):
        return D - (X - D*(X//D))
    else:
        return X - D*(X//D)

if __name__ == "__main__":
    print(solve())

Submission status: Accepted

Solution code:
#  X+DP-D(K-P)
# =X-DK+2DP
# =C+2DP
# C>=0?
#   P=0
# C<0?
#   P=~(-C/2D)

i = list(map(int, input().split()))
x = i[0]
k = i[1]
d = i[2]
c=x-d*k
if c>=0:
    print(c)
else:
    p=-c//(2*d)
    ans = 10000000000000000000000000000000000000000
    for i in range(-100,100):
        if 0<=p+i and p+i<=k:
            ans=min(ans,abs(c+2*d*(p+i)))
    if ans == 10000000000000000000000000000000000000000:
        print(abs(c+2*d*k))
    else:
        print(ans)

Submission status: Accepted

Solution code:
import math
n , st , m = map(int , input().split())
n = abs(n)
t = abs(n)
n = (n-m*st)
tot = n//m
if n>=0:
	print(n)
else:
	rem = t%m
	tot = t//m
	if((st-tot)%2)!=0:
		print(abs(m-rem))
	else:
		print(abs(rem))

Submission status: Accepted

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

Submission status: Accepted

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

    if K*D > abs(X):
        temp = K - X // D

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

k = abs(X) // D

if k > K:
    ans = min(abs(X - K * D),abs(X + K * D))
else:
    Xk = min(abs(X - k * D), abs(X + k * D))
    if (K - k) % 2 == 0:
        ans = Xk
    else:
        ans = min(abs(Xk-D),abs(Xk+D))

print(ans)

Submission status: Accepted

Solution code:

number=list(map(int,input().split()))
X,K,D=number[0],number[1],number[2]
res=0
t=0
ans=0
if X<0:
    X=X*(-1)
if X>K*D:
    print(X-K*D)
elif X<=K*D:
    key=int(X/D)
    key+=1
    res=X-(D*key)
    t=K-key
    t=int(t%2)
    if t==0:
        ans=res*(-1)
    else:
        ans=res+D
        
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
x=input()
x0=x.split()
x=int(x0[0])
k=int(x0[1])
d=int(x0[2])
if abs(x)>abs(k*d):
    print(abs(x)-abs(k*d))
else:
    c=math.floor(abs(x)/d)
    if abs(x)-c*d<abs(abs(x)-(c+1)*d):
        if (k-c)%2==0:
            print(abs(x)-c*d)
        else:
            print(abs(abs(x)-(c+1)*d))
    else:
        if (k-c)%2==0:
            print(abs(x)-c*d)
        else:
            print(abs(abs(x)-(c+1)*d))

Submission status: Accepted

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

numbers = input().split(" ")
x = int(numbers[0])
k = int(numbers[1])
d = int(numbers[2])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def pm(x):
  if X>=0:
    return 1
  else:
    return -1
X,K,D=map(int,input().split())
if (abs(X))<D :
  if K%2==0:
    print(int(abs(X)))
  else:
    a=X-pm(X)*D
    print(int(abs(a)))
else:
  if abs(X)//D>=K:
    a=X-(pm(X))*D*K
    print(int(abs(a)))
  else:
    K=K-int(abs(X)//D)
    X=X-(pm(X))*D*int(abs(X)//D)
    if (abs(X))<D :
      if K%2==0:
        print(int(abs(X)))
      else:
        a=X-(pm(X))*D
        print(int(abs(a)))

Submission status: Accepted

Solution code:
from collections import Counter
import copy
from itertools import combinations
import math


def A(s):
  if s == 'RRR':
    return 3
  elif s == 'RRS':
    return 2
  elif s == 'RSR':
    return 1
  elif s == 'SRR':
    return 2
  elif s == 'RSS':
    return 1
  elif s == 'SRS':
    return 1
  elif s == 'SSR':
    return 1
  elif s == 'SSS':
    return 0
  l = 0
  max_l = 0
  for i in range(len(s)):
    if i == 0 and s[i] == 'R':
      l += 1
    elif s[i - 1] == s[i] and s[i] == 'R':
      l += 1
    else:
      l = 0
    if l > max_l:
      max_l = l
  return max_l


def B(N, Ls):

  def _can_tri(a, b, c):
    if a == b or a == c or b == c:
      return False
    tmp_a, tmp_b, tmp_c = sorted([a, b, c])
    if tmp_a + tmp_b > tmp_c:
      return True
    return False

  ans = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        if _can_tri(Ls[i], Ls[j], Ls[k]):
          # print(i + 1, j + 1, k + 1)
          ans += 1
  return ans


def C(X, K, D):
  abs_x = int(abs(X))
  if K * D <= abs_x:
    return abs_x - K * D
  else:
    a = K - math.floor(abs_x / D)
    # print(a, abs_x, D, math.floor(abs_x / D))
    if a % 2 == 0:
      return abs_x % D
    else:
      return D - abs_x % D


def main():
  # s = input()
  # print(A(s))
  # N = int(input())
  # Ls = [int(x) for x in input().split(' ')]
  # print(B(N, Ls))
  X, K, D = [int(x) for x in input().split(' ')]
  print(C(X, K, D))


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy


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

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

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
dk=d*k
if dk <= x:
  print(x-dk)
  exit()

dv,md=divmod(x,d)  
k-=dv
if k%2==0:
  print(md)
else:
  print(abs(md-d))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

minNeedCount = X // D
rightRest = X % D
leftRest = abs(rightRest - D)

if K < minNeedCount:
  res = X - K*D
else:
  if (K-minNeedCount) % 2 == 0:
    res = rightRest
  else:
    res = leftRest

print(res)

Submission status: Accepted

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

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

    cnt = abs(X) // D
    if X >= 0:
        X -= min(K, cnt) * D
    else:
        X += min(K, cnt) * D

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
kai = 0
xy = abs(X)
if xy >= K*D:
    kotae = abs(xy-(K*D))
if xy < K*D:
    kai, mod = divmod(xy, D)
    ds = kai*D
    if (K-kai)%2==0:
        kotae = abs(xy-ds)
    if (K-kai)%2!=0:
        kouho1 = abs(xy-ds-D)
        kouho2 = abs(xy-ds+D)
        if kouho1>=kouho2:
            kotae = kouho2
        if kouho1<kouho2:
            kotae = kouho1
print(kotae)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

A = list(map(int, input().split()))
x = A[0]
k = A[1]
d = A[2]
if x<0:
    x*=-1
if x==0:
    if k%2==0:
        print(0)
    else: print(d)
else:
    if x-k*d<0:
        m=x//d+1
        k-=m
        
        if k%2==0:
            print(abs(x-m*d))
        elif k%2==1:
            print (abs(x-(m-1)*d))
        
    else:
        print(x-k*d)

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

x,k,d = nm()

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

import math


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = list(map(int,input().split()))
x = abs(x)
minA = x%d
minB = -1 * (minA-d)
step = x//d
if k <= step:
    print(x - d*k)
else:
    if (k-step)%2 == 0:
        print(minA)
    else:
        print(minB)

Submission status: Accepted

Solution code:

import datetime
from decimal import Decimal, ROUND_DOWN
import heapq
from fractions import gcd
from functools import reduce
from collections import deque
# from math import factorial
import itertools
import collections
import math
import sys
sys.setrecursionlimit(2000000)
# import statistics
# import numpy as np
# n = int(input())
# n, m, p = list(map(int, input().split()))
# a = list(map(int, input().split()))
#
#  abc = [int(input()) for i in range(5)]
#n = int(input())
#n, m = list(map(int, input().split()))
x, k, d = list(map(int, input().split()))
#l = list(map(int, input().split()))
# data = [list(map(int, input().split())) for i in range(n)]
# n = int(input())
# a = list(map(int, input().split()))
# b = list(map(int, input().split()))
# ab_sorted = sorted(ab, key=lambda x: x[0])
# data = [list(map(int, input().split())) for i in range(n)]
# print(min(15*n, 100*(n//10+1), 100*(n//10)+15*(n % 10)))
# for i in range(1, int(n**0.5)+1):

shou = x//d
amari = x % d

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


# print(shou)
# print(amari)

print(ans)

Submission status: Accepted

Solution code:
v = input().split(" ")
x = int(v[0])
k = int(v[1])
d = int(v[2])

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

Submission status: Accepted

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

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

k -= x // d
k %= 2
x %= d
print(abs(x - k * d))

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if k*d<abs(x):
    print(abs(x)-k*d)
    exit()
n=abs(x)-abs(x)//d*d # 残りの距離
m=k-abs(x)//d  # 余った回数
if m%2==0:
    print(n)
else:
    print(abs(n-d))

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
n=x//d
if k<n:n=k
r=x-n*d
if (k-n)%2:r-=d
print(abs(r))

Submission status: Accepted

Solution code:
import math
while True:
    try:
        x, k, d = map(int, input().split());
        if x == 0:
            k %= 2;
            if k:
                print(abs(x - d));
            else :
                print(0);
        elif abs(x) - k * d > 0:
            print(abs(x) - k * d);
        elif x > 0:
            k -= x // d;
            k %= 2;
            x -= x // d * d;
            if k:
                print(abs(x - d));
            else:
                print(x);
        else:
            num = abs(x) // d + 1;
            x += num * d;
            k -= num;
            k %= 2;
            if k:
                print(abs(x - d));
            else:
                print(x);
    except EOFError:
        break;

Submission status: Accepted

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

first_move = abs(x)//d

if first_move >= k:
    print(abs(x)-d*k)
else:
    k -= first_move
    x_abs = abs(x) - first_move*d

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

Submission status: Accepted

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

x = abs(x)
#0に最も近い距離になる。
ans = x % d
#ansへの移動回数分、kから引く
if k- (x//d) < 0:
    print(x - k*d)
    exit()
else:
    k -= x//d

if k % 2 == 0:
    print(ans)
else:
    t1 = abs(ans-d)
    t2 = abs(ans+d)
    print(min(t1, t2))