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:
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
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif X == 0 :
        if limit == 10 :
            print('0')
        else :
            print(abs(D))
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            if (abs(val[i+1]) == abs(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:
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
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif D == 0 :
        print(abs(X))
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            if (abs(val[i+1]) == abs(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())

x_two_step_ago = x
x_one_step_ago = x

for i in range(k):
    x_two_step_ago = x_one_step_ago
    x_one_step_ago = x
    x = min(abs(x + d), abs(x - d))
    if i > 2 and x == x_two_step_ago and (k - i) % 2 == 1:
        break

print(x)

Submission status: Time Limit Exceeded

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

history = []
history.append(x)

for i in range(k):
    x = min(abs(x + d), abs(x - d))
    history.append(x)
    if i > 2 and history[i] == history[i - 2] and (k - i) % 2 == 1:
        break

print(x)

Submission status: Time Limit Exceeded

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

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

print(x)

Submission status: Time Limit Exceeded

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

while tmp_diff > d and k > 0:
    tmp_diff -= d
    k -= 1

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

Submission status: Time Limit Exceeded

Solution code:
s = list(map(int, input().split()))
q = 0
if s[0] > 0 and s[0] - s[1] * s[2] > 0:
    print(s[0] - s[1] * s[2])
elif s[0] < 0 and s[0] + s[1] * s[2] < 0:
    print(-s[0] - s[1] * s[2])
else:
    if s[0] > 0:
        while s[0] - s[2] > 0:
            s[0] = s[0] - s[2]
            q = q + 1
        if (s[1] - q) % 2 == 1:
            print(-s[0]+s[2])
        else:
            print(s[0])
    elif s[0] < 0:
        while s[0] + s[2] < 0:
            s[0] = s[0] + s[2]
            q = q + 1
        if (s[1] - q) % 2 == 1:
            print(s[0]+s[2])
        else:
            print(-s[0])
    else:
        if s[1] % 2 == 1:
            print(s[2])
        else:
            print(s[0])

Submission status: Time Limit Exceeded

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

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:
X,K,D=map(int,input().split())
now=X
for i in range(K):
    X_plus=now+D
    X_mi=now-D
    if abs(X_plus)<abs(X_mi):
        now=X_plus
    else :
        now=X_mi

print(abs(now))

Submission status: Time Limit Exceeded

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

count=0
while count<K:
    if X>=0:
        X-=D
        count+=1
    else:
        X+=D
        count+=1

print(abs(X))

Submission status: Time Limit Exceeded

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

zx=x
#zzx=zx
for i in range(k):
    xm=abs(x+d)
    xu=abs(x-d)
    x=min(xm,xu)
    k-=1
    if zx+x==d:
        if k%2!=0:
            x=zx
            break
        else:
#            x=zx
            break
#    zzx=zx
    zx=x
    
print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

else:
  Key=key-D
  if K%2==0:
    ans=abs(key)
  else:
    ans=abs(Key)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:

import sys
import math
# from collections import Counter
input = sys.stdin.readline
# import numpy as np

def inp():
    return(int(input()))

def inps():
    return str(input())
    
def inlt():
    return(list(map(int,input().split())))
    
def inlst():
    return(list(map(str,input().split())))
    
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split())) 

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

Submission status: Time Limit Exceeded

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

x=abs(x)

m=[]

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

print(min(m))

Submission status: Time Limit Exceeded

Solution code:


X, Y, Z= input().split(" ")
x=int(X)
k=int(Y)
d=int(Z)
n=1

if(x==d and k%2==0):
    print(x)
elif(x==d and k%2!=0):
    print("0")
else:
    while(n<=k):
        m=abs(x-d)
        p=abs(x+d)
        x=min(m, p)

        n+=1
    print(x)

Submission status: Time Limit Exceeded

Solution code:
X , K , D = map(int, input().split())
x = X
for trans in range(K):
    foward_X = x + D
    back_X = x - D
    if abs(foward_X) > abs(back_X):
        x = back_X
    else:
        x = foward_X
print(abs(x))

Submission status: Time Limit Exceeded

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

if X >= D * K:
  print(X - D * K)
else:
  K -= int(X / D)
  for i in range(0, int(X/D)):
    X -= D
  if K % 2:
    print(D - X)
  else:
    print(X)

Submission status: Time Limit Exceeded

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

posabs_list = []

for i in range(k+1):
        pos = x + d * (k-i) - d * i
        posabs = abs(pos)
        posabs_list.append(posabs)
print(min(posabs_list))

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  k=k-sa
  for i in range(k):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=1 if (k-i)%2==1 else bk1
        break
      elif ans==1:
        bk3=max(mansbk,pansbk) 
      elif 2*x==d:
        ans=0 if (k-i)%2==1 else x
        break
    else:
      ans=0 if (k-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  k=k-sa
  for i in range(k):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=1 if (k-i)%2==1 else bk1
        break
      elif ans==1:
        bk3=max(mansbk,pansbk) 
    else:
      ans=0 if (k-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if ans==1:
        bk3=max(mansbk,pansbk)
      elif bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    if ans==mbk:
      ans=mbk if (k-sa-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
mansbk,pansbk=0,0
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    mansbk=abs(ans-d)
    pansbk=abs(ans+d)
    if mansbk!=pansbk:
      ans=min(mansbk,pansbk)
      if ans==1:
        bk3=max(mansbk,pansbk)
      elif bk3!=1:
        bk1=min(mansbk,pansbk)
        bk2=max(mansbk,pansbk)
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    pbk=max(mansbk,pansbk)
    mbk=min(mansbk,pansbk)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
for i in range(xkd[0] // xkd[2]):
    xkd[0] -= xkd[2]
    xkd[1] -= 1
    if xkd[1] < 1:
      break
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if ans > xkd[0] + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      if bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=mbk if (k-sa-i)%2==0 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    l = list(map(int, input().split()))
    X = int(l[0])
    K = int(l[1])
    D = int(l[2])
    
    if D*K <= abs(X):
        minVal=abs(X) - D*K
    else:
        n=0
        while abs(X) - D*n > 0:
            n=n+1
        
        if (K-n)%2==0:
            minVal = abs(abs(X) - D*n)
        else:
            minVal = abs(abs(X) - D*(n-1))
        
    print(minVal)

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
sa=x//d
ans=x
pbk,mbk=0,0
bk1,bk2,bk3=1,1,1
if sa>=k:
  ans=x-d*k
else:
  ans=x-d*sa
  for i in range(k-sa):
    if abs(ans-d)!=abs(ans+d):
      ans=min(abs(ans-d),abs(ans+d))
      if ans==1:
        bk3=max(abs(ans-d),abs(ans+d))
      if bk3!=1:
        bk1=min(abs(ans-d),abs(ans+d))
        bk2=max(abs(ans-d),abs(ans+d))
        ans=1 if (k-sa-i)%2==1 else bk1
        break
    else:
      ans=0 if (k-sa-i)%2==0 else d
      break
    pbk=max(abs(ans-d),abs(ans+d))
    mbk=min(abs(ans-d),abs(ans+d))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
#0またがない
if X > K*D:
    ans = X - K*D
#0またぐ
else:
    while X >= 0 :
        X = X - D
        K -= 1
    if K % 2 == 0:
        ans = abs(X)
    if K % 2 != 0:
        ans = X+D

print(ans)

Submission status: Time Limit Exceeded

Solution code:
X,K,D = map(int,input().split())
pre1 = float('inf')
pre2 = float('inf')
ans = X
for i in range(K):
    if i >= 3 and ans == pre2:
        if ((K-i) % 2) == 0:
            break    
        else:
            if abs(ans - D) <= abs(ans + D):
                ans -= D
            else:
                ans += D
            break
    if i == 0:
        pre1 = ans
        if abs(ans - D) <= abs(ans + D):
            ans -= D
        else:
            ans += D
    else:
        pre2 = pre1
        pre1 = ans
        if abs(ans - D) <= abs(ans + D):
            ans -= D
        else:
                ans += D
print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
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:
x, k, d = map(int, input().split())

past1_x = 0
past2_x = 0

result = 0

for i in range(k):
	past2_x = past1_x
	past1_x = x
	if x > 0:
		x -= d
	else:
		x += d
	
	if (past2_x == x and i != 0) or i == k - 1:
		if (k - i) % 2 == 0:
			result = abs(past1_x)
		else:
			result = abs(x)
		break

print(result)

Submission status: Time Limit Exceeded

Solution code:
def WalkingTakahashi():
    
    data = list(map(int,input().split()))
    
    position = data[0]
    
    if data[0] == data[2] and data[1]%2 == 0:
        print(data[2])
    
    elif data[0] == data[2] and data[1]%2 == 1:
        print(0)
        
    else:
        for i in range(data[1]):
            
            if abs(position + data[2]) < abs(position - data[2]):
                position = position + data[2]
                
            else:
                position = position - data[2]
                
        print(abs(position))
        
if __name__ == '__main__':
    WalkingTakahashi()

Submission status: Time Limit Exceeded

Solution code:
a, b, c = map(int, input().split())
MAX = b * c
result = a
for i in range(b):
    if a < 0 and (a + MAX) <= 0:
        a += MAX
        break
    if a > 0 and (a - MAX) >= 0:
        a -= MAX
        break
    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())
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())

result = []
current = x
for i in range(k):
    current+=d
    result.append(abs(current))

for i in range(k):
    current-=2*d
    result.append(abs(current))

print(min(result))

Submission status: Time Limit Exceeded

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

if(x < 0):
  while(x < 0):
    k -= 1
    x = x + d
    if(k == 0):
      break
  if(abs(x) > abs(x - d)):
    k += 1
    x = x - d
elif(x > 0):
  while(x > 0):
    k -= 1
    x = x - d
    if(k == 0):
      break
  if(abs(x) > abs(x + d)):
    k += 1
    x = x + d

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

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
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    elif D == 0 :
        print(abs(X))
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            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())
mini=0
l1=x-d
l2=x+d
for i in range(k):
 if abs(l1)<abs(l2):
  mini=abs(l1)
  l1=mini-d
  l2=mini+d
 else:
  mini=abs(l2)
  l1=mini-d
  l2=mini+d
print(mini)

Submission status: Time Limit Exceeded

Solution code:
a, b, c = map(int, input().split())
MAX = b * c
result = a
for i in range(b):
    if a < 0 and (a + MAX) < 0:
        a += MAX
        break
    if a > 0 and (a - MAX) > 0:
        a -= MAX
        break
    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())
i = 0
while(i<K):
    if X > 0:
        X -= D
    else:
        X += D
    i += 1

print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
#ABC175 C
X,K,D = map(int,input().split())
min = X
a2 = 10 ** 13
a1 = 10 ** 14
a0 = 10 ** 15
while K:
  if abs(X - D) < abs(X + D):
    if K % 4 == 0:
      a2 = abs(X - D)
    elif K % 2 == 1:
      a1 = abs(X - D)
    elif K % 2 == 0:
      a0 = abs(X - D)
    min = abs(X - D)
    X = X - D
    K -= 1
  else:
    if K % 4 == 0:
      a2 = abs(X + D)
    elif K % 2 == 1:
      a1 = abs(X + D)
    elif K % 2 == 0:
      a0 = abs(X + D)
    min = abs(X + D)
    X = X + D
    K -= 1
  
  if (a2 == a0):
    min = a1
    break

print(min)

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=min(X,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, y, z = list(map(int, input().split()))
count=0
if(x<0):
    x=-x
if(z<0):
    z=-z
for i in range(0,y):
    x=x-z
    count+=1
    if(x<0):
        x=-x
    if(x<=z/2):
        if((y-count)%2==1):
            x=z-x
        break
print(x)

Submission status: Time Limit Exceeded

Solution code:
import sys
x,k,d=map(int,input().split())
f=k%2
x=abs(x)+d
for i in range(k+1):
    x-=d
    if f==i%2:
        if abs(x)<d:
            print(abs(x))
            sys.exit()
        elif abs(x)==d:
            print(abs(x))
            sys.exit()
print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int,input().split())
now = x
if x and k and d > 10000000 and x < k * d:
  k = k - (k - 10000000)
for _ in range(k):
  if abs(now - d) <= abs(now + d):
    now = now - d
  elif abs(now + d) < abs(now - d):
    now = now + d
print(abs(now))

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 - 2) % 2) == 0:
            if ans > 0:
                ans -= d
            else :
                ans += d
        break
print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
[X,K,D]=[int(_) for _ in input().split()]
X=abs(X)
Y=X//D
if K>Y:
  K-=Y
  X-=Y*D
#print(X,K,D)
X/=D
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:
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
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif abs(X) == K * D :
        print("0")
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            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())
if k%2 == 0 and abs(x)==abs(d):
  print(abs(x))
  exit()
res = []
i = 0
while i != k:
  res.append(abs(x+d*i-d*(k-i)))
print(min(res))

Submission status: Time Limit Exceeded

Solution code:
X,K,D=map(int, input().split())
min_list=[]
for k in range(K+1):
  current=abs(X+(K-2*k)*D)
  min_list.append(current)
print(min(min_list))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import math
x, k, d = list(map(int, input().split()))
minc = math.inf

if x > 0:
    # risou = min(abs(x % d) - d, x % d)
    current = x
    for ki in range(k):
        if abs(current - d) < abs(minc):
            minc = current - d
            minck = k - ki - 1
        current = current - d
        if current < x % d - d:
            break
else:
    current = x
    for ki in range(k):
        if abs(current + d) < abs(minc):
            minc = current + d
            minck = k - ki - 1
        current = current + d
        if current > -1 * (x % d) + d:
            break

if minck % 2 == 0:
    print(abs(minc))
else:
    if minc > 0:
        print(abs(minc - d))
    else:
        print(abs(minc + d))

Submission status: Time Limit Exceeded

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


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


    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:
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
    i = 0
    if abs(X) > K * D :
        if X > 0 :
            print(X - K * D)
        else :
            print(abs(X + K * D))
    elif X == K * D :
        print("0")
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            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())
while K > 0:
    if X > 0:
        X = X- D
    else:
        X = X+ D

    K = K-1

print(abs(X))

Submission status: Time Limit Exceeded

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

for i in range(0, 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())
List = []

if abs(X) > K*D:
    print(abs(X) - K*D)
elif 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 = map(int, input().split())
abs_x = abs(X)
hikukazu = abs(X) // D
count = K - (hikukazu)
if K > hikukazu:
    if count % 2 == 0:
        hikukazu = hikukazu
    else:
        hikukazu = hikukazu + 1
else:
    hikukazu = K


for i in range(hikukazu):
    
    abs_x = abs_x - D

    # print(X)

print(abs(abs_x))

Submission status: Time Limit Exceeded

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

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


num = 0

while k>0:
    s = abs(x)
    s = s-d
    if abs(s)>=abs(x):
        break
    x = abs(s)
    k -= 1


if k == 0:
    print(abs(s))
    exit()

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

Submission status: Time Limit Exceeded

Solution code:
def WalkingTakahashi():
    
    data = list(map(int,input().split()))
    
    position = data[0]
    
    for i in range(data[1]):
        
        if abs(position + data[2]) < abs(position - data[2]):
            position = position + data[2]
            
        else:
            position = position - data[2]
            
    print(abs(position))
        
if __name__ == '__main__':
    WalkingTakahashi()

Submission status: Time Limit Exceeded

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

now = X
visited = {}
left = K
prev = X
breaked = False

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

for i in range(K):
    if now >= D:
        k = now//D
    else:
        k=1
    left -= k
    prev = now
    next_p = now + k*D
    next_m = now - k*D
    if abs(next_p) > abs(next_m):
        now = next_m
    else:
        now = next_p
    visited.setdefault(str(now), 0)
    visited[str(now)] += 1
    if visited[str(now)] > 1:
        breaked = True
        break
    if left < 0:
        break
    # print(f"now:{now}")
if breaked:
    if left % 2 == 0:
        print(abs(now))
    else:
        print(abs(prev))
else:
    print(abs(now))

Submission status: Time Limit Exceeded

Solution code:
X,K,D = (int(x) for x in input().split())
if X >= -10**15 and X <= 10**15:
    if K >= 1 and K <= 10**15:
        if D >= 1 and D <= 10**15:
            i=1
            while i <= K:
                if X > 0:
                    X -= D
                    i += 1
                else:
                    X += D
                    i += 1
            else:
                X = abs(X)
                print(X)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(abs(X))

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
    i = 0
    if X > K * D :
        print(X - K * D)
    elif X == K * D :
        print("0")
    else :
        for j in val :
            #print(i)
            if abs(val[i] - D) < abs(val[i] + D) :
                val[i+1] = val[i] - D
            else :
                val[i+1] = val[i] + D
            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()