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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for k in range(move):

    if np.sign(x) == 1:
        x = x-d
    else:
        x = x+d

    if abs(x) <= d and (move-(k+1))%2==0:
        break


print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
X,K,D = map(int,input().split())
Y,Z = X+0.1,X+0.2

    
if K> 10000000 and abs(X) - 10000000*D > 0:
    X = abs(X) - 10000000 * D
    K = K - 10000000

for i in range(K):
    #for TLE
    
    Z = Y
    Y = X

    if X >= 0:
        X = X - D
    else:
        X = X + D
    if X == Z: #roop
        if (K-i)%2 == 0: #even
            ans = Y
        else: #odd
            ans = X
        break
    if i == K-1:#last
        ans = X
print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
a, b, c = map(int, input().split())
result = a
for i in range(b):
    if abs(a + c) < abs(a - c):
        if abs(a + c) < abs(a + c * 2) and ((b - i) % 2 != 0):
            a += c
            break
        a += c
        if abs(result) < abs(a):
            break
        result = a
    else:
        if abs(a - c) < abs(a - c * 2) and ((b - i) % 2 != 0):
            a -= c
            break
        a -= c
        if abs(result) < abs(a):
            a += c*2
            break
        result = a
print(abs(a))

Submission status: Time Limit Exceeded

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

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

elif abs(x) == k * d:
    print(0)
    exit()

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

elif x < 0:
    while a < k:
        a += 1
        x = x + d
        if x == 0:
            if (k - a) % 2 == 0:
                print(x)
                exit()
            else:
                print(d)
                exit()
        elif x > 0:
            if (k - a) % 2 == 0:
                print(x)
                exit()
            else:
                print(abs(x - d))
                exit()

else:
    while a < k:
        a += 1
        x = x - d
        if x == 0:
            if (k - a) % 2 == 0:
                print(x)
                exit()
            else:
                print(d)
                exit()
        elif x < 0:
            if (k - a) % 2 == 0:
                print(abs(x))
                exit()
            else:
                print(x + d)
                exit()

exit()

Submission status: Time Limit Exceeded

Solution code:
L = list(map(int,input().split()))
X=L[0]
K=L[1]
D=L[2]
for i in range(K):
    if X >= 0:
        X= X-D
    else:
        X =X+D
print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math

# 入力値取得
x, k, d = map(int, input().split())

# 移動する
posi_min = 0
nega_min = 0
for i in range(k):
  # xが正の数なら引く、負の数なら足す
  if (x>=0):
    x -= d
    # 0をまたいだら終わり判定（残りが偶数回なら終了）
    if (x<=0 and (k-i+1)%2==0):
      break
  else:
    x += d
    # 0をまたいだら終わり判定（残りが偶数回なら終了）
    if (x>=0 and (k-i+1)%2==0):
      break

print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
import sys

L = list(map(int,input().split()))
x = L[0]
k = L[1]
d = L[2]

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



print(abs(x))

Submission status: Time Limit Exceeded

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

for i in range(K):
    if abs((X-D)-0) > abs((X+D)-0):
        X += D
    else:
        X -= D

print(abs(X-0))

Submission status: Time Limit Exceeded

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

if K == 1:
    print(min(abs(X-D),abs(X+D)))
elif K == 2:
    print(min(abs(X+D+D),abs(X-D-D),abs(X)))
elif K == 3:
    print(min(abs(X+D+D+D),abs(X+D),abs(X-D),abs(X-D-D-D)))
else:
    for i in range(3):
        if abs(X - D) >= abs(X+D):
            List.append(abs(X+D))
            X = X+D
        else:
            List.append(abs(X-D))
            X = X-D

    for i in range(K-3):
        if List[i] == List[i+2]:
            if K%2 == i%2:
                print(List[i+1])
            else:
                print(List[i])
            break
        else:
            if abs(X - D) >= abs(X+D):
                List.append(abs(X+D))
                X = X+D
            else:
                List.append(abs(X-D))
                X = X-D

    if len(List) == K:
        print(List[K-1])
    else:
        pass

Submission status: Time Limit Exceeded

Solution code:
x, k, d = [int(i) for i in input().split()]
x = abs(x)
if k * d <= x:
    print(x - k*d)
else:
    while x - d > 0:
        x -= d
        k -= 1
    if k & 1:
        print(d - x)
    else:
        print(x)

Submission status: Time Limit Exceeded

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

i = 0

while(i < K):
    if(abs(X + D) < abs(X - D)):
        X = abs(X + D)
    else:
        X = abs(X - D)
    
    i += 1

print(X)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

    val = [0] * 100000000
    A = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 10
    else :
        limit = 11
    i = 0
    for j in A :
        #print(i)
        if abs(val[i] - D) < abs(val[i] + D) :
            val[i+1] = val[i] - D
        else :
            val[i+1] = val[i] + D
        #val[i+1] = min(abs(val[i] - D), abs(val[i] + D))
        #val_new = min(abs(val - D), abs(val + D))
        #val = val_new
        if (val[i+1] == val[i-1]) and i > limit :
            print(abs(val[i + 1]))
            break
        i += 1
        if i == K :
            print(abs(val[i]))
            break




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

elif abs(x) == k * d:
    print(0)
    exit()

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

elif x < 0:
    while a < k:
        a += 1
        x = x + d
        if x == 0:
            if (k - a) % 2 == 0:
                print(x)
                exit()
            else:
                print(d)
                exit()
        elif x > 0:
            if (k - a) % 2 == 0:
                print(x)
                exit()
            else:
                print(abs(x - d))
                exit()

else:
    while a < k:
        a += 1
        x = x - d
        if x == 0:
            if (k - a) % 2 == 0:
                print(x)
                exit()
            else:
                print(d)
                exit()
        elif x < 0:
            if (k - a) % 2 == 0:
                print(abs(x))
                exit()
            else:
                print(x + d)
                exit()










for i in range(k):

    if x + d >= 0 and x < 0:
        break
    elif x - d <= 0 and x > 0:
        break

    elif abs(x + d) < abs(x - d):
        x = x + d
        a += 1
    else:
        x = x - d
        a += 1

if k == a:
    print(abs(x))

elif (k - a) % 2 == 0:
    print(abs(x))

else:
    if x == 0:
        print(d)
    elif x < 0:
        print(x + d)
    else:
        print(x - d)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
x,k,d=map(int,input().split())
m=abs(x)
n=0
for i in range(k):
  if x<0:
    x+=d
  else:
    x-=d
  if abs(x)<m:
    m=abs(x)
  else:
    n=k-i
    break
if n%2==0:
  print(m)
else:
  print(abs(x))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(0,k+1):
    y = x + 2*i*d - k*d
    if y < 0:
        y = -y
    list_y.append(y)

list_y.sort()
print(list_y[0])

Submission status: Time Limit Exceeded

Solution code:
[X,K,D]=[int(_) for _ in input().split()]
X=abs(X)
X/=D
#print(k1,k2)
for _ in range(K//2):
  X = min([abs(X-2), abs(X), abs(X+2)])
  #print(X)
if K%2 == 1:
  X = min([abs(X-1), abs(X+1)])
  #print(X)
print(abs(int(X*D)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for k in range(move):

    if np.sign(x) == 1 or np.sign(x) == 0:
        x = x-d
    else:
        x = x+d

    if abs(x) <= d/2 and (move-(k+1))%2==0:
        break
    elif abs(x) <= d/2 and (move-(k+1))%2!=0:
        if np.sign(x) == 1 or np.sign(x) == 0:
            x = x-d
        else:
            x = x+d

        break


print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
def main():
    X, K, D = list(map(int, input().split()))
    for i in range(K):
        if abs(X - D) > abs(X + D):
            X = X + D
        else:
            X = X - D
            if X <= 0:
                if (i % 2 == 1 and K % 2 == 0) or (i % 2 == 0 and K % 2 == 1):
                    print(abs(X))
                    return
                else:
                    if abs(X - D) < X:
                        print(abs(X - D))
                        return
                    elif abs(X + D) < X:
                        print(abs(X + D))
                        return
    print(abs(X))
main()

Submission status: Time Limit Exceeded

Solution code:
c,K,M=list(map(int,input().split()))
h=[]
for i in range(K):
  c=min(abs(c-M),abs(c+M))
  if c in h and (K+1-i)%2==0:
    print(c)
    exit()
  if c in h and (K+1-i)%2==1:
    print(min(abs(c-M),abs(c+M)))
    exit()
  else:
    h.append(c)
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math

# 入力値取得
x, k, d = map(int, input().split())

# 移動する
posi_min = 0
nega_min = 0
for i in range(k):
  # xが正の数なら引く、負の数なら足す
  end_flag = False
  if (x>=0):
    x -= d
    if (x<0):
      end_flag = True
  else:
    x += d
    if (x>=0):
      end_flag = True

  if (end_flag and (k-i+1)%2==0):
    break

print(abs(x))

Submission status: Time Limit Exceeded

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

if(X > 0):
    while(X - n * D >= 0):
        if(n == K):
            break
            print(X - n * D)
            exit()
        n += 1
    a = K - n
    if(a % 2 == 0):
        print(abs(X - n * D))
    else:
        print(abs(X - (n-1)*D))
elif(X < 0):
    while(n * D + X <= 0):
        if(n == K):
            break
            print(X - n * D)
            exit()
        n += 1
    a = K - n
    if(a % 2 == 0):
        print(abs(X + n * D))
    else:
        print(abs(X + (n-1)*D))
else:
    if(K % 2 == 0):
        print(0)
    else:
        print(D)

Submission status: Time Limit Exceeded

Solution code:
X, K, D  = map(int, input().split())
for i in range(K):
  if abs(X+D) > abs(X-D):
    X = X-D
  elif abs(X+D) <= abs(X-D):
    X = X+D
print(abs(X))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
param_x, param_k, param_d = map(int, input().split())

param_x_tmp = param_x

if param_x > 0:
  int_times_nega_posi = -1
else:
  int_times_nega_posi =  1
  
if (param_x*int_times_nega_posi*-1) - (param_k*param_d) > 0:
  print((param_x*int_times_nega_posi*-1) - (param_k*param_d))
  
else:
  count = 0
  while param_x_tmp*param_x > 0:
    
    param_x_tmp = param_x_tmp + param_d*int_times_nega_posi
    
    count = count + 1
    
    if param_k - count == 0:
      print(abs(param_x_tmp))
  
  if count < param_k:
    int_move = param_k - count #move left
    int_move_devide_2_no_amari = int_move % 2
    print(abs(param_x_tmp + param_d*int_move_devide_2_no_amari*int_times_nega_posi*-1))

Submission status: Time Limit Exceeded

Solution code:
X,K,D = map(int,input().split())
Y,Z = X+0.1,X+0.2

for i in range(K):
    Z = Y
    Y = X
    if X >= 0:
        X = X - D
    else:
        X = X + D
    if X == Z: #roop
        if (K-i)%2 == 0: #even
            ans = Y
        else: #odd
            ans = X
        break
    if i == K-1:#last
        ans = X
print(abs(ans))

Submission status: Time Limit Exceeded

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

    y = x

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

        if abs(x-d) < abs(x+d):
            x = x - d
        else:
            x = x + d
        if y == x:
            break

        y = x

    if k % 2 == 1:
        if abs(x-d) < abs(x+d):
            x = x - d
        else:
            x = x + d


    print(abs(x))
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for k in range(move):
    move_plus = x + d
    move_minus = x - d

    if abs(move_plus) > abs(move_minus):
        x = move_minus
    else:
        x = move_plus

    if abs(x) <= d/2 and (move-(k+1))%2==0:
        break
    elif abs(x) <= d/2 and (move-(k+1))%2!=0:
        if np.sign(x) == 1 or np.sign(x) == 0:
            x = x-d
        else:
            x = x+d

        break

print(abs(x))

Submission status: Time Limit Exceeded

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

ans = x
dic = {str(x):1}
for i in range(k):
    if ans >= 0:
        ans -= d
    else:
        ans += d

    str_ans = str(ans)   
    if str_ans in dic:
        dic[str_ans] += 1
    else:
        dic[str_ans] = 1
    
    if dic[str_ans] > 1:
        if ((k - i + 1) % 2) != 0:
            if ans >= 0:
                ans -= d
            else:
                ans += d
        break
print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
X, K ,D = input().split()
X = int(X)
K = int(K)
D = int(D)
if (K == 1):
  if (X >= 0):
    print(abs(X -D))
  if (int(X) < 0):
      print(abs(X + D))
  exit()
if (X == 0):
  print(abs( - (K % 2) *D))
elif (X > 0):
  if (X -K*D > 0):
    print(abs(X - K*D))
    exit()
  while ((X > 0) and (K != 0)):
         X = X - D
         K -= 1
  if (K==0):
    print(abs(X))
    exit()
  print(abs(abs(X) - (K % 2) *D))
elif (X < 0):
  if (X + K*D < 0):
    print(abs(X + K*D))
    exit()
  while ((X < 0) and (K != 0)):
         X = X + D
         K -= 1
  if (K==0):
    print(abs(X))
    exit()
  print(abs(abs(X) - (K % 2) *D))

Submission status: Time Limit Exceeded

Solution code:
X,K,D=map(int,input().split())
i=0
count=0
if X<0:
	if X+K*D<=0:
		print(abs(X+K*D))
		count+=1
	else:
		L=X+i*D
		while L<=0:
			i+=1
			L=X+i*D
		if abs(L)>=abs(X+(i+1)*D):
			i+=1
			L=abs(X+i*D)
else:
	if X-K*D>=0:
		print(abs(X-K*D))
		count+=1
	else:
		L=X-i*D
		while L>=0:
			i+=1
			L=X-i*D
		if abs(L)>=abs(X-(i-1)*D):
			i-=1
			L=abs(X-i*D)
if count==0:
	kaisuu=K-i
	if kaisuu%2==0:
		print(abs(L))

	else:
		a=abs(L+D)
		b=abs(L-D)
		if a<=b:
			print(a)
		else:
			print(b)

Submission status: Time Limit Exceeded

Solution code:
c,K,M=list(map(int,input().split()))
h=[]
for i in range(K):
  c=min(abs(c-M),abs(c+M))
  if c in h and (K+1-i)%2==0:
    print(c)
    exit()
  else:
    h.append(c)
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = x
dic = {'0':0}
for i in range(k):
    if ans >= 0:
        ans -= d
    else:
        ans += d

    str_ans = str(ans)   
    if str_ans in dic:
        dic[str_ans] += 1
    else:
        dic[str_ans] = 1
    
    if dic[str_ans] > 1:
        if ((k - i + 1) % 2) != 0:
            if ans >= 0:
                ans -= d
            else:
                ans += d
        break
print(abs(ans))

Submission status: Time Limit Exceeded

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    else:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, abs(X), K-i)
        break

print(min_score)

Submission status: Time Limit Exceeded

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

m=[]
n=[]

for i in range(1,k+1):
    a=d*i
    b=-d*i
    m.append(a)
    n.append(b)

mx=[]
for i in m:
    m1=x+i
    m2=abs(m1)
    mx.append(m2)

nx=[]
for i in n:
    n1=-1*(x+i)
    n2=abs(n1)
    nx.append(n2)

mxmin=min(mx)
nxmin=min(nx)

ans=min(mxmin,nxmin)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

X, K, D = map(int, input().split())
K2 = K
#X=１に近いものにおきかえる
if X >= 0:
  a= 1
else:
  a = -1
for i in range(K2):
  Y = X - a *D
  if a*Y< 0:
    break
  X=Y
  K-=1
  
#print(X, K, D)
      


flg = 0
minx = abs(X)
Xnow = X
Xprepre = 0
for i in range(K):
  if i != 0:
    Xprepre = Xpre
  Xpre = Xnow #前回
  X1 = Xnow -D
  X2 = Xnow +D
  if abs(X1) < abs(X2):
    Xnow = X1
  else:
    Xnow = X2
  #繰り返したらおわり
  if Xprepre == Xnow:
    flg = 1
    if (K-i+1)%2 == 0:
      print(abs(Xnow))
    else:
      print(abs(Xpre))
    break
if flg == 0:
  print(abs(Xnow))

Submission status: Time Limit Exceeded

Solution code:
X, K ,D = input().split()
X = int(X)
K = int(K)
D = int(D)
if (K == 1):
  if (X >= 0):
    print(abs(X -D))
  if (int(X) < 0):
      print(abs(X + D))
  exit()
if (X == 0):
  print(abs( - (K % 2) *D))
elif (X > 0):
  while ((X > 0) and (K != 0)):
         X = X - D
         K -= 1
  if (K==0):
    print(abs(X))
    exit()
  print(abs(abs(X) - (K % 2) *D))
elif (X < 0):
  while ((X < 0) and (K != 0)):
         X = X + D
         K -= 1
  if (K==0):
    print(abs(X))
    exit()
  print(abs(abs(X) - (K % 2) *D))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

p = X
_p = 0
k = 0

if abs(p) > K*D:
    print(abs(p)-K*D)
    sys.exit()

if p < 0:
    for i in range(K):
        _p = p
        p += D
        k += 1
        if p > 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
elif p > 0:
    for i in range(K):
        _p = p
        p -= D
        k += 1
        if p < 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Time Limit Exceeded

Solution code:
l=list(map(int,input().split()))
x=l[0]
k=l[1]
d=l[2]
x1=abs(x)
cou=0
while x1>0:
  x1-=d
  cou+=1
if k<cou:
  x2=abs(x)-k*d
  print(x2)
  # if x<=0:
  #   x3=x2*(-1)
  #   print(x3)
  # else:
  #   print(x2)

else:
  a=cou%2
  b=k%2
  x4=x1*(-1)
  if a==b:
    print(abs(x1))
  if a!=b:
    print(x1+d)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def abs_min(current, move):
    f = current+move
    b = current-move
    if abs(f) <= abs(b):
        return f
    else:
        return b
    
nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]

for i in range(num):
    current = abs_min(current, move)

print(abs(current))

Submission status: Time Limit Exceeded

Solution code:
[_x, _k, _d] = list(map(int, input().split(" ")))
_x = abs(_x)

while _k > 0:
  if _x >= _d:
    _x = abs(_x - _d)
    _k -= 1
  else:
    if _k % 2 == 1:
      _x = abs(_x - _d)
    _k = 0

print(_x)

Submission status: Time Limit Exceeded

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

X = abs(X)
now = X
over_zero_step = 0
over_zero = 0
not_over_zero = now
flg = 0
for i in range(1, K+1):
    if X < abs(K * D):
        nxt = now - D
    #    print(nxt, i)
        if nxt < 0:
            over_zero = nxt
            not_over_zero = now
            over_zero_step = i
            break
        now = nxt
    else:
        remaining_step = 0
        nearest_point = X - K * D
        flg = 1

if flg == 0:
        
    if abs(over_zero) <= not_over_zero:
        nearest_point = over_zero
        nearest_point_step = over_zero_step
        next_nearest_point = not_over_zero
        remaining_step = K - nearest_point_step
    else:
        nearest_point = not_over_zero
        nearest_point_step = over_zero_step - 1
        next_nearest_point = over_zero
        remaining_step = K - nearest_point_step

#print("rem", remaining_step)
if remaining_step % 2 == 0:
    print(abs(nearest_point))
else:
    print(abs(next_nearest_point))

Submission status: Time Limit Exceeded

Solution code:
import sys

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

p = X
_p = 0
k = 0

if abs(p) > K*D:
    print(abs(p)-K*D)
    sys.exit()

if p < 0:
    while 1:
        _p = p
        p += D
        k += 1
        if p > 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
elif p > 0:
    while 1:
        _p = p
        p -= D
        k += 1
        if p < 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
if x == 0:
    if k % 2 == 0:
        print(0)
    else:
        print(k)
elif k == 0 or d == 0:
    print(x)
elif abs(x) - (k * d) == 0:
    print(0)
elif abs(x) - (k * d) >= 0:
    print(abs(x) - (k * d))
elif x > 0:
    mini = 1000000000000000
    while x > 0 and k > 0:
        x -= d
        k -= 1
        mini = abs(x)
    if k % 2 == 0:
        print(mini)
    else:
        print(x + d)
else:
    mini = -1000000000000000
    while x < 0 and k > 0:
        x += d
        k -= 1
        mini = abs(x)
    if k % 2 == 0:
        print(mini)
    else:
        print(abs(x - d))

Submission status: Time Limit Exceeded

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

tmp = [abs(X)]
min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    else:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, abs(X), K-i)
        break

    if abs(X) in tmp:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, min_score, K-i)
        break
    tmp.append(abs(X))
print(min_score)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
min = 1000000000000000
cnt = 0
if x > 0:
    for i in range(k):
        if abs(x-d) < min:
            min = abs(x-d)
            x-=d
            cnt += 1
        else:
            if abs(x-d)>abs(x+d):
                x = abs(x+d)
            else:
                x = abs(x-d)
            break
    if cnt == 0:
        cnt += 1
    k -= cnt
    if k%2 == 0:
        print(min)

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


    else:
        print(x)

Submission status: Time Limit Exceeded

Solution code:
param_x, param_k, param_d = map(int, input().split())

param_x_tmp = param_x

if param_x > 0:
  int_times_nega_posi = -1
else:
  int_times_nega_posi =  1

count = 0
while param_x_tmp*param_x > 0:
  
  param_x_tmp = param_x_tmp + param_d*int_times_nega_posi
  
  count = count + 1
  
  if param_k - count == 0:
    print(abs(param_x_tmp))

if count < param_k:
  int_move = param_k - count #move left
  int_move_devide_2_no_amari = int_move % 2
  print(abs(param_x_tmp + param_d*int_move_devide_2_no_amari*int_times_nega_posi*-1))

Submission status: Time Limit Exceeded

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

# count = 0

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

Submission status: Time Limit Exceeded

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

if x > 0 :
    y = int(x / d)
    x -= min(k,y) * d
else:
    y = int(abs(x / d))
    x += min(k,y) * d
if(k != y):
    for i in range(int(k-y)):
        if abs(x - d) > abs(x + d):
            x += d
        else:
            x -= d
print(abs(x))

Submission status: Time Limit Exceeded

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

tmp = [X]
min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    else:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, abs(X), K-i)
        break

    if X in tmp:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, min_score, K-i)
        break
    tmp.append(X)
print(min_score)