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

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

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
X=abs(X)
if X==0 and K%2==0:
    ans=0
elif X==0 and K%2==1:
    ans=D
elif 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 main():
    X, K, D = map(int, input().split())

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    if K % 2 == 0 :
        limit = 10
    else :
        limit = 11
    for i in range(K) :
        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 :
            break

    print(abs(val[i+1]))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
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:
X,K,D = map(int,input().split())
i = 0
while i < K:
  i = i + 1
  if X >= 0:
    X = X - abs(D)
  else:
    X = X + abs(D)
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

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 count == param_k:
    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())
lis = []
for i in range(0,k+1):
    a = x
    a += d*i
    a -= d*(k-i)
    lis.append(a)
for j,k in enumerate(lis):
    lis[j] = abs(k)
print(min(lis))

Submission status: Time Limit Exceeded

Solution code:
x,k,d = map(int, input().split())
x_min = abs(x)
for i in range(k):
    if x < 0:
        x += d
        if abs(x) < x_min:
            x_min = abs(x)
        else:#xが0以上
            if (k - i - 1) % 2 == 1:
                break
            else:
                x_min = d - x_min
                break
    else:
        x -= d
        if abs(x) < x_min:
            x_min = abs(x)
        else:#xが負
            if (k - i - 1) % 2 == 1:
                break
            else:
                x_min = d - x_min
                break
print(x_min)

Submission status: Time Limit Exceeded

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

if x > 0 and x - (k*d) >= 0:
  print(x - (k*d))
elif x < 0 and x + (k*d) <= 0:
  print(abs(x + (k*d)))
elif x == 0:
  if k % 2 == 0:
    print(0)
  else:
    print(d)
else:
  for i in range(k):
    if x > 0:
      x = x - d
      k -= 1
      if x <= 0:
        mid = True
        break
    else:
      x = x + d
      k -= 1
      if x >= 0:
        mid = True
        break

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

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
candidate  = []
for i in  range(K+1):
    goal = X + D*i -D*(K-i)
    candidate.append(goal)
ans = min(candidate,key = abs)
print(abs(ans))

Submission status: Time Limit Exceeded

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

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:
[X, K, D] = list(map(int, input().split()))
X = abs(X)
Y = K%2
for i in range(K):
    X -= D
    if X<0:
        if (i+1)%2 != Y:
            X += D
        else:
            X = abs(X)
        break
print(X)

Submission status: Time Limit Exceeded

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

for k in range(K):
    if min(abs(X+D), abs(X-D)) < abs(X):
        X = min(abs(X+D), abs(X-D))
        K -= 1
    else:
        if K%2 == 0:
            print(X)
            break
        else:
            X = min(abs(X+D), abs(X-D))
            print(X)
            break

if K == 0:
    print(X)

Submission status: Time Limit Exceeded

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

if(X > 0):
    if(X-K*D >= 0):
        print(X-K*D)
    else:
        while(X >= 0):
            X = X-D
            K -= 1
        if(K % 2 == 0):
            print(abs(X))
        else:
            print(abs(X+D))
elif(X < 0):
    if((X+K*D) <= 0):
        print(abs(X+K*D))
    else:
        while(X <= 0):
            X = X+D
            K -= 1
        if(K % 2 == 0):
            print(abs(X))
        else:
            print(abs(X-D))
else:
    if(K % 2 == 0):
        print(0)
    else:
        print(abs(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)):
        i += 1
else:
    while abs(X+D*i)<abs(X+D*(i-1)) and abs(X+D*i)>abs(X+D*(i+1)):
        i += 1
if i<K:
    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))))
else:
    print(abs(abs(X)-D*K))

Submission status: Time Limit Exceeded

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

x = X
done = False
while K:
    K -= 1
    pre = x
    if x >= 0:
        x -= D
    else:
        x += D
    if pre * x <=0:
        if K % 2 == 0:
            ans = abs(x)
        else:
            ans = abs(pre)
        done = True
        break

if not done:
    ans = abs(x)

print(ans)

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
    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
    k -= cnt
    if k%2 == 0:
        print(min)

    else:
        print(x)

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
m = x
for i in range(1, k+1):
    m1 = m + d
    m2 = m - d
    if abs(m1) < abs(m2):
        m = m1
    else:
        m = m2

print(abs(m))

Submission status: Time Limit Exceeded

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

    for i in range(k):
        current_x = min(abs(current_x + d), abs(current_x - d))

    print(current_x)


if __name__ == '__main__':
    abc175_c()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
p = input().split()
count = 0
sp = 0

x = int(p[0])
num = int(p[1])
D = int(p[2])

if(x != D):
  for i in range(num):
    if(x > 0):
      x = x -D
    elif(x < 0):
      x = x + D
    elif(x==0):
      x = x + D
  print(abs(x))
elif(x == D and num%2 == 0):
  print(x)
elif(x == D and num%2 == 1):
  print(0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

pre1 = 0
pre2 = 0
now = X

for k in range(K):
    pre2 = pre1
    pre1 = now
    sei = now + D
    hu = now - D
    if(abs(sei) < abs(hu)):
        now = sei
    elif(abs(sei) > abs(hu)):
        now = hu
    else:
        print(abs(sei))
        exit(0)
    if(pre2 == now):
        if((K+1-k) % 2 == 0):
            print(abs(now))
            exit(0)
        else:
            print(abs(pre1))
            exit(0)
print(abs(now))

Submission status: Time Limit Exceeded

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

now = X
for i in range(0, K):
    now = min(abs(now + D), abs(now - D))
print(now)

Submission status: Time Limit Exceeded

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

def solve(x, k, d):
    if k == 0:
        return x
    else:
        for i in range(k):
            x = min(map(abs, (x-d, x+d)))

        return x
print(solve(x,k,d))

Submission status: Time Limit Exceeded

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

if abs(x)//d <= k :
  z = k - (abs(x)//d)
  x = abs(x)%d
  if z % 2 == 0:
    print(x)
    exit()
  else:
    if abs(x) + d <= abs(abs(x) - d):
      print(abs(x)+d)
      exit()
    else:
      print(abs(abs(x)-d))
      exit()
      
else:
  for i in range(1,k+1):
    if abs(x) >= abs(abs(x) - d):
      x = abs(x) - d
    else:
      if (k-i-1)%2 == 0:
        print(abs(x))
        exit()
      else:
        if abs(x) + d <= abs(abs(x) - d):
          print(abs(x)+d)
          exit()
        else:
          print(abs(abs(x)-d))
          exit()
print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
X,K,D=map(int,input().split())
count=0
if abs(X)>=K*D :
  ans=abs(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-D),abs(X+D))
      break
    elif X>D :
      X-=D
    else :
      X+=D
    count+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

while i > 0:
  if x <= d and x >= -d:
    i = i % 2
  if i == 0:
    break
  if x >= 0:
    x -= d
  else:
    x += d
  i -= 1
  
print(abs(x))

Submission status: Time Limit Exceeded

Solution code:

X, K, D = map(int,input().split())
tmp = int(abs(X)/D)
if X > 0:
    if K > tmp:
        K = K - tmp
        X = X - D*tmp
    while True:
        X = X - D
        K -= 1
        if abs(X) < abs(X - D) or K == 0:
            break
    if K % 2 == 0:
        print(abs(X))
    else:
        print(min(abs(X+D), abs(X-D)))
elif X < 0:
    if K > tmp:
        K = K - tmp
        X = X + D*tmp
    while True:
        X = X + D
        K -= 1
        if abs(X) < abs(X + D) or K == 0:
            break
    if K % 2 == 0:
        print(abs(X))
    else:
        print(min(abs(X+D), abs(X-D)))
elif X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Time Limit Exceeded

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

# print(param_x, param_k, param_d)

param_x_tmp = param_x

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

# before cross 0
count = 0
while param_x_tmp*param_x > 0:
  
  param_x_tmp = param_x_tmp + param_d*int_times_nega_posi
  
  count = count + 1
  
  # 
  #print(count, param_x_tmp)
  
  if count == param_k:
    print(abs(param_x_tmp))

# after cross 0
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())
X = abs(X)
for i in range(K):
    if X >= D:
        X = X-D
    elif abs(X) < D and (K-i)%2==1:
        X = X-D
        break
    elif X < D and (K-i)%2==0:
        break
print(abs(X))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

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

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 = map(int,input().split())

A = abs(X)
while A > D:
    A -= D
    K -= 1
    if K == 0:
        break

if K > 0:
    if K%2 == 1:
        A = A - D

print(abs(A))

Submission status: Time Limit Exceeded

Solution code:
import sys
X, K, D = map(int, input().split())
if X == D and K%2 == 0:
    print(abs(X))
    sys.exit(0)
for _ in range(K):
    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())


if X>0:
    for k in range(K):
        bx = X
        X-=D
        if X<=0:
            break
else:
    for k in range(K):
        bx = X
        X+=D
        if X>=0:
            break

if K == k + 1 or (K-k-1)%2==0:
    print(abs(X))
else:
    print(abs(bx))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

X = abs(X)

final = -1

for n in range(K):
  X = abs(X - D)
  if X <= D / 2:
    if (K-n) % 2 == 0:
      final = abs(X-D)
      break
    else:
      final = X
      break

if final >= 0:
  print(final)
else:
  print(X)

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-=d
  elif 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())

c = X

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


print(abs(c))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
list_in = list(map(int,input().split()))
xin = list_in[0]
kin = list_in[1]
din = list_in[2]

now_place = xin

for i in range(kin):
    p_place = now_place + din
    m_place = now_place - din
    if (abs(p_place) < abs(m_place)):
        now_place = p_place
    else:
        now_place = m_place

print(abs(now_place))

Submission status: Time Limit Exceeded

Solution code:
X,K,D=map(int,input().split())
count=0
if abs(X)>=K*D :
  ans=abs(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: Time Limit Exceeded

Solution code:
X,K,D = map(int,input().split())
dp = [[0]*2 for i in range(K+1)]
dp[0][0] = X
dp[0][1] = X

for i in range(1,K+1):
    dp[i][0] = min(abs(dp[i-1][0] - D),abs(dp[i-1][1] - D))
    dp[i][1] = min(abs(dp[i-1][0] + D),abs(dp[i-1][1] + D))
    
print(min(dp[-1]))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
mind = 1000000000000000
c = 0
while k > 0:
    k -= 1
    if abs(x - d - 0) <= abs(x + d - 0):
        x -= d
    else:
        x += d
    if abs(x) == mind:
        break
    mind = min(mind, abs(x))

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

Submission status: Time Limit Exceeded

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

while i > 0:
  if x >= 0:
    x -= d
  else:
    x += d
  i -= 1
  if x <= d and x >= -d:
    i = i % 2  

print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
mind = 1000000000000000
c = 0
while k > 0:
    k -= 1
    if abs(x - d - 0) <= abs(x + d - 0):
        x -= d
    else:
        x += d
    if x == mind:
        break
    mind = min(mind, abs(x))

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

x=abs(x)

t=x

if k%2==1:
    t-=d
    k-=1

min=abs(t)

if x-k*d<0:
    k-=(x/(d*2))*2

while(-x<t and k>0):
    t-=d*2
    k-=2
    if min < abs(t):
        break
    else:
        min=abs(t)

print(min)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import math
# スペース区切りの整数の入力
X, K, D = map(int, input().split())

cross = abs(-(-X//D))

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

Submission status: Time Limit Exceeded

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

if x > 0 and x - (k*d) >= 0:
  print(x - (k*d))
elif x < 0 and x + (k*d) <= 0:
  print(abs(x + (k*d)))
else:
  for i in range(k):
    if x > 0:
      x = x - d
      k -= 1
      if x <= 0:
        mid = True
        break
    else:
      x = x + d
      k -= 1
      if x >= 0:
        mid = True
        break

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

Submission status: Time Limit Exceeded

Solution code:
a = list(input().split(" "))
x = int(a[0])
if x < 0:
    x *= -1
k = int(a[1])
d = int(a[2])

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

print(x)

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    if K * D < X:
        print(X - K * D)
    else:
        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:
X, K, D = map(int,input().split())
t = 0
Y = abs(X)
if Y >= K * D:
    Y -= K * D
    print(Y)
else:
    for i in range(1,K + 1):
        Z = Y
        Z = i * D
        if Z >= Y:
            t = i
            
            break
    if X >= 0:
        a = X - t * D
        b = X - (t - 1) * D
        if (K - t) % 2 == 0:
            print(abs(a))
        else:
            print(abs(b))
    else:
        a = X + (t - 1) * D
        b = X + t * D
        if (K - t) % 2 == 0:
            print(abs(b))
        else:
            print(abs(a))

Submission status: Time Limit Exceeded

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

if X>0:
    p = True
else:
    p = False
c = 0
while abs(X)> D:
    if p :
        X -= D
        K -= 1
    else:
        X += D
        K -= 1
    if K == 0:
        break

if K > 0:
    if K%2 == 1 and p == True:
        X = X - D
    if K%2 == 1 and p == False:
        X = X + D

print(abs(X))

Submission status: Time Limit Exceeded

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):
    xi -= d
    if xi <= 0:
      if (i+k) % 2 != 0:
        m = abs(xi)
      else:
        m = min(abs(xi+d),abs(xi-d))
      break
print(m)

Submission status: Time Limit Exceeded

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

while c < K :
    if c == 0 :
        NX = X
        OX = NX
    if abs(NX + D) < abs(NX - D) :
        NX = NX + D
    else :
        NX = NX - D
    if abs(OX) > abs(NX) :
        OX = NX
        c = c + 1
    else:
        break

if (K - c) % 2 == 1 :
    if abs(OX + D) < abs(OX - D) :
        OX = OX + D
    else:
        OX = OX - D


print(abs(OX))

Submission status: Time Limit Exceeded

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

ans = 1000000000000007
prev = 1000000000000007
if K % 2 == 0:
  i = K // 2
  for k in range(0, i+1):
    mn = abs(X + k * D * 2)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

  prev = 1000000000000007
  for k in range(0, i+1):
    mn = abs(X - k * D * 2)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

else:
  for k in range(1, K+1, 2):
    mn = abs(X + k * D)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

  prev = 1000000000000007
  for k in range(1, K+1, 2):
    mn = abs(X - k * D)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(0, k):
  if abs(x + d) < abs(x - d):
    x += d
  elif abs(x - d) <= abs(x + d):
    x -= d
print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
if k%2 == 0:
  if x>0 and abs(x)<=abs(x-d):
    print(abs(x))
    exit()
  elif x<0 and abs(x)<=abs(x+d):
    print(abs(x))
    exit()
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:
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):
    xi -= d
    if xi <= 0:
      if (i+k) % 2 != 0:
        m = min(abs(xi),abs(xi+2*d),abs(xi-2*d))
      else:
        m = min(abs(xi+d),abs(xi-d))
      break
print(m)

Submission status: Time Limit Exceeded

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

ans = 10**15+1
for i in range(K):
    if X >= 0:
        X -= D
    else:
        X += D
    if X <= abs(ans):
        ans = X

print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
s,k,d = map(int,input().split())
st = s
m = 0
while m<k:
  if abs(s-d)<abs(s+d):
    s=s-d
    m+=1
  elif abs(s+d)<abs(s-d):
    s=s+d
    m+=1
print(abs(s))

Submission status: Time Limit Exceeded

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

print(min_dist)

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
add_list = []
for i in range(k//2+1):
  num = k - 2*i
  add_list.append(d * num)
answer = float('inf')
for add in add_list:
  answer = min(abs(x + add), abs(x - add), answer)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
x_pre = x
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

print(abs(x_pre))

Submission status: Time Limit Exceeded

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

if X < 0:
    X *= -1
print(X)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
if X > 0:
    while True:
        X = X - D
        K -= 1
        if abs(X) < abs(X - D) or K == 0:
            break
    if K % 2 == 0:
        print(abs(X))
    else:
        print(min(abs(X+D), abs(X-D)))
elif X < 0:
    while True:
        X = X + D
        K -= 1
        if abs(X) < abs(X + D) or K == 0:
            break
    if K % 2 == 0:
        print(abs(X))
    else:
        print(min(abs(X+D), abs(X-D)))
elif X == 0:
    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
    else:
        x = x - d
        
print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
if k%2 == 0 and x>0 and abs(x)<=abs(x-d):
  print(abs(x))
  exit()
elif k%2 == 0 and x<0 and abs(x)<=abs(x+d):
  print(abs(x))
  exit()
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:
X, K, D = map(int, input().split())
if X == K and D % 2 == 0:
  print(abs(X))
  exit(0)
elif X == K and D % 2 == 1:
  print(0)
  exit(0)
for i in range(K):
  if abs(X - D) < abs(X + D):
    X -= D
  else:
    X += D
print(abs(X))