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(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:
# -*- coding: utf-8 -*-
X,K,D=map(int,input().split())

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if X < 0:
    X = -X

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

counter = 0

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

print(abs(X))

Submission status: Time Limit Exceeded

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

    y = x

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


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

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

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]

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

print(abs(current))

Submission status: Time Limit Exceeded

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

i = 0
nokori = 0

if X > 0 :
    while X > 0 and i < K:
        X -= D
        i += 1
    
    nokori = K - i
    if nokori % 2 == 1 :
        X += D

else :
    while X <= 0 and i < K:
        X += D
        i += 1
    nokori = K - i
    if nokori % 2 == 1 :
        X -= D

print(abs(X))

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 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())
if k%2 == 0 and x>0:
  if abs(x)<=abs(x-d):
    print(abs(x))
    exit()
elif k%2 == 0 and x<0:
  if abs(x)<=abs(x+d):
    print(abs(x))
    exit()
for i in range(k):
  """if abs(x + d) <= abs(x):
    x = x + d
  elif abs(x - d) <= abs(x):
    x = x - d
  else:
    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
print(abs(x))

Submission status: Time Limit Exceeded

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

three_list = []

for i in range(K):
    three_list.append(X)
    
    if len(three_list) >= 4:
        three_list.pop(0)
    if len(three_list) >= 3:
        if three_list[0] == three_list[2]: # 繰り返し
            if (K-i) % 2 == 0: # 偶数
                print(abs(three_list[0]))
            else:
                print(abs(three_list[1]))
            exit()
    if X >= 0:
        X -= D
    else:
        X +=D
print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
lst = input().split()

X = int(lst[0])
K = int(lst[1])
D = int(lst[2])

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

print(abs(X))

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

if abs(X)/D < K:
  if K % 2 == 0:
    if (abs(X)//D) % 2 == 0:
      K = abs(X)//D
    else:
      K = abs(X)//D + 1
  else:
    if (abs(X)//D) % 2 == 0:
      K = abs(X)//D + 1
    else:
      K = abs(X)//D

if abs(X) % D == 0:
  if abs(X)//D < K:
    if abs(X)//D % 2 == K % 2:
      print(0)
      exit()

if abs(X) <= D:
  if K % 2 == 0:
    print(min([abs(X+D+D), abs(X), abs(X-D-D)]))
    exit()
  else:
    print(min([abs(X+D), abs(X-D)]))
    exit()

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:
import math

X,K,D = map(int, 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:
x, k, d=map(int, input().split(' '))
for i in range(k):
  if x >= 0:
    x -= d
  else:
    x += d

if x < 0:
  x = -x
print(x)

Submission status: Time Limit Exceeded

Solution code:
X,K,D = list(map(int , input().split()))
ans = 0
if (X >=0):
    a = K*D
    if (X >= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X -= D
            time += 1
            if X <= 0:
                break
        nokori = (K - time) % 2
        ans = X + D*nokori
else:
    a = -(K*D)
    if (X <= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X += D
            time += 1
            if X >= 0:
                break
        nokori = (K - time) % 2
        ans = X - D*nokori
Answ = abs(ans)
print(Answ)

Submission status: Time Limit Exceeded

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

#print(x,k,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, L = map(int, input().split())
Distance = []
for p_num in range(K+1):
    dist = L*p_num + (-1)*L*(K-p_num)
    Distance.append(abs(X + dist))


print(min(Distance))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if k*d <= x:
    print(x-k*d)
else:
    while(d*i < x):
        i += 1
    #print(i)
    if (k-i)%2 == 0:
        print(abs(x-d*i))
    else:
        print(x-d*i + 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())
def walk(place,D):
    A=place-D
    B=place+D
    if abs(A)>abs(B):
        return B
    else: 
        return A

for i in range(K):
    X=walk(X,D)
print(abs(X))

Submission status: Time Limit Exceeded

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

for i in range(K):
    if place >= 0 and (place - D) >= 0:
        place -= D
    elif place >= 0 and (place - D) <= 0:
        place -= D
    else: place += D 

if place < 0:
    place = abs(place)
print(place)

Submission status: Time Limit Exceeded

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

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

for i in range(K):
    left -= 1
    prev = now
    next_p = now + D
    next_m = now - 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)] > 2:
        breaked = True
        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 = map(int, input().split())
  
x = abs(x)

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

  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())
line = [x]
if abs(x) >= k*d:
  print(abs(x) - k*d)
else:
  for i in range(k):
    if x > 0:
      x -= d
    else:
      x += d
    line.append(x)
    if i != 0:
      if line[i-1] == line[i+1]:
        if (k - i + 1) % 2 == 0:
          x = line[i+1]
          break
        else:
          x = line[i]
          break
  print(abs(x))

Submission status: Time Limit Exceeded

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

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())
ans=X
min_abs = 10**15
for i in range(K):
  ans = min(abs(ans+D),abs(ans-D))
  
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = abs(x)%d
k = k - int(abs(x)/d)

if k < 0:
    ans = x
    k = tmp

for i in range(k):
    if abs(ans) <= d and (k-i)%2 == 0:
        print(abs(ans))
        exit()
    elif ans >= 0:
        ans = ans - d
    elif ans < 0:
        ans = ans + d

print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

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

if X < 0:
    X = -X

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

temp_X = X
temp_K = K

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C
# CreatedDate:  2020-08-15 20:38:56 +0900
# LastModified: 2020-08-15 21:31:46 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    x, k, d = map(int, input().split())
    ans = 1e31
    for d_plus in range(k+1):
        if ans > abs(x+d*(2*d_plus-k)):
            ans = abs(x+d*(2*d_plus-k))
        else:
            break
    print(ans)
    


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
dp = [[float('inf'), float('inf')] for i in range(k+1)]
dp[0] = [x, x]
for i in range(k):
  if abs(dp[i][0] + k) < abs(dp[i][1] + k):
    dp[i+1][0] = dp[i][0] + d
  else:
    dp[i+1][0] = dp[i][1] + d
  if abs(dp[i][0] - k) < abs(dp[i][1] - k):
    dp[i+1][1] = dp[i][0] - d
  else:
    dp[i+1][1] = dp[i][1] - d
if abs(dp[k][0]) < abs(dp[k][1]):
  print(abs(dp[k][0]))
else:
  print(abs(dp[k][1]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if abs(X)/D < K:
  if K % 2 == 0:
    if (abs(X)//D) % 2 == 0:
      K = abs(X)//D
    else:
      K = abs(X)//D + 1
  else:
    if (abs(X)//D) % 2 == 0:
      K = abs(X)//D + 1
    else:
      K = abs(X)//D

if abs(X) <= D:
  if K % 2 == 0:
    print(min([abs(X+D+D), abs(X), abs(X-D-D)]))
    exit()
  else:
    print(min([abs(X+D), abs(X-D)]))
    exit()

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())
ans = float('inf')
for bit in range(2**K):
    ans_pre = X
    for i in range(K):
        if bit & (1<<i):
            ans_pre += D
        else:
            ans_pre -= D
            
    ans = min(ans, abs(ans_pre))
    
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
XKD=input().split(" ")
X=int(XKD[0])
K=int(XKD[1])
D=int(XKD[2])

X=abs(X)

ans=pow(10,15)+1
for i in range(K):
    minus_X=abs(X-D)
    plus_X=abs(X+D)
    if(minus_X<plus_X):
        X=minus_X
        ans=minus_X
    else:
        X=plus_X
        ans=plus_X

print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,k,d = map(int, input().split())
def  abs_calc(x):
    if  x <  0:
        xi =  (-1)* x
    else:
        xi  = x
    return xi
xi = abs_calc(x)
ans = xi + k * d
for i in range(k+1):
    xx  = x +  d*i + (-1)*d*(k-i)
    xxi = abs_calc(xx)
    if xxi < ans:
        ans = xxi
print(ans)

Submission status: Time Limit Exceeded

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

g = k - int(x / d)
if g > 0:
  if g % 2 == 1:
    g -= 1
  k = k - g

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())
def counta(x,k,d):
    #xcnt=0
    x=abs(x)
    if d<2*x:
        while x>=d:
            x-=d
            k-=1
            if k==0:
                print(abs(x))
                return
        if k%2==0:
            print(x)
            return
        else:
            print(abs(x-d))
            return
    else:
        if k%2==0:
            print(x)
            return
        else:
            print(abs(x-d))
            return
counta(x,k,d)

Submission status: Time Limit Exceeded

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

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())
kai = 0
xy = abs(X)
ds = D
if xy >= K*D:
    kotae = abs(xy-(K*D))
if xy < K*D:
    while xy >ds:
        ds=ds+D
        kai = kai+1
    ds = ds-D
    if (K-kai)%2==0:
        kotae = abs(xy-ds)
    if (K-kai)%2!=0:
        kouho1 = abs(xy-ds-D)
        kouho2 = abs(xy-ds+D)
        if kouho1>=kouho2:
            kotae = kouho2
        if kouho1<kouho2:
            kotae = kouho1
print(kotae)

Submission status: Time Limit Exceeded

Solution code:
x,k,d = map(int, input().split())
line = [x]
for i in range(k):
  if x > 0:
    x -= d
  else:
    x += d
  line.append(x)
  if i != 0:
    if line[i-1] == line[i+1]:
      if (k - i + 1) % 2 == 0:
        x = line[i+1]
        break
      else:
        x = line[i]
        break
print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
nowPoint, count, move = map(int, input().split())
for i in range(count):
  if 0 < nowPoint:
    nowPoint = nowPoint - move
  else:
    nowPoint = nowPoint + move
if nowPoint < 0:
  nowPoint *= -1
print(nowPoint)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

x=abs(x)

for i in range(k):
    x=abs(x-d)
print(x)

Submission status: Time Limit Exceeded

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


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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
m=0
p=0
eo=k
ans=x
for i in range(1,k+1):
  m,p=abs(ans-d),abs(ans+d)
  ans=min(m,p)
  if ans<=d:
    eo=i
    break
if (k-eo)%2==0 and eo<k:
  print(ans)
elif (k-eo)%2==1 and eo<k:
  print(abs(ans-d))
else:
   print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
sa=abs(x)//d
ans=x
if sa>=k:
  ans=abs(x)-d*k
else:
  for i in range(k-sa):
    if abs(ans-d)<abs(ans+d):
      ans=abs(ans-d)
    elif abs(ans-d)>abs(ans+d):
      ans=abs(ans+d)
    else:
      if (k-i)%2==1:
        ans=d
      break
print(ans)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

x = abs(X)
k = K
while x > 0 and k > 0:
    x -= D
    k -= 1

if k % 2 == 1:
    x += D

print(abs(x))

Submission status: Time Limit Exceeded

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

for i in range(k):
    if abs(ans) <= d and (k-i)%2 == 0:
        print(abs(ans))
        exit()
    elif ans >= 0:
        ans = ans - d
    elif ans < 0:
        ans = ans + d

print(abs(ans))

Submission status: Time Limit Exceeded

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

start = x

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

print(abs(start))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
if x==0:
    if d%2==0:
        print(0)
    else:
        print(d)
else:
    cou=0
    ok=True
    if x>0:
        while x>0:
            cou+=1
            x-=d
            if cou==k:
                ans=x
                ok=False
                break
        xx=x+d
        if ok:
            if cou%2==0 and k%2==0:
                ans=x
            elif cou%2==0 and k%2==1:
                ans=xx
            elif cou%2==1 and k%2==0:
                ans=xx
            else:
                ans=x
    
    if x<0:
        while x<0:
            cou+=1
            x+=d
            if cou==k:
                ans=x
                ok=False
                break
        xx=x-d
        if ok:
            if cou%2==0 and k%2==0:
                ans=x
            elif cou%2==0 and k%2==1:
                ans=xx
            elif cou%2==1 and k%2==0:
                ans=xx
            else:
                ans=x
    print(abs(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
x,k,d = list(map(int,input().split()))
#偶数回前後に移動したら0
#偶数回一方向に移動したら2D

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

  if x < 0:
    break



if (k-i+1) % 2 == 0:
  #そのまま
  ans = abs(x)
else:
  ans = min(abs(x-d),abs(x+d))

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

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

for _ 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())
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(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:
# input 現在座標ｘ, 移動回数k, 移動距離d
x, k, d = map(int, input().split())

# 現在座標が正ならば引き算、負なら足し算をする
for i in range(k):
    if x > 0:
        x = x - d;
    elif x <= 0:
        x = x + d;

# xが負の数の場合正の数に編集する
if x < 0:
    x = x * (-1);

print(x);

Submission status: Time Limit Exceeded

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


if X > 0:
    cur_abs = abs(X)
    while K > 0:
        X -= D
        if abs(X) > cur_abs:
            X += D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)
elif X < 0:
    cur_abs = abs(X)
    while K > 0:
        X += D
        if abs(X) > cur_abs:
            X -= D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)
else:   # X == 0
    if K % 2 == 0:
        print(0)
    else:
        print(min(abs(X + D), abs(X - D)))

Submission status: Time Limit Exceeded

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

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

print(abs(X))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if x-k*d>=0:
    print(x-k*d)
else:
    a=x
    while a>d:
        a-=d
        k-=1
    if k%2==0:
        print(min(a,2*d-a))
    else:
        print(min(d-a,a+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:
dates = [int(i) for i in input().split(" ")]
X = dates[0]
K = dates[1]
D = dates[2]

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(min(s))