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:
# -*- using:utf-8 -*-
class main:
  def function(x, k, d):
    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
      else:
        if (k - i) % 2 == 0:
          return abs(x)
          break
        else:
          return abs(x + d)
          break
    return abs(x)

if __name__ == "__main__":
  x, k, d = map(int, input().split())
  x = main.function(x, k, d)
  print(x)

Submission status: Time Limit Exceeded

Solution code:
# -*- using:utf-8 -*-
class main:
  def function(x, k, d):
    for i in range(k):
      if abs(x + d) >= abs(x - d):
        x = x - d
      else:
        x = x + d
    print(abs(x))

if __name__ == "__main__":
  x, k, d = map(int, input().split())
  main.function(x,k,d)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from io import BytesIO, IOBase


def main():
    x,k,d = map(int,input().split())
    t = x//d
    
    if t<=k and x<0:
        x+=t*d
        k-=t
    elif t<=k and x>0:
        x-=t*d
        k-=t
    elif t>k and x<0:
        x+=k*d
        k-=k
    elif t>k and x>0:
        x-=k*d
        k-=k
    
    while k:
        if x<0:
            if x+d<0:
                x+=d
            elif abs(x+d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x+d))
                break
            elif abs(x+d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 != 0:
                print(abs(x+d))
                break
        else:
            if x-d>0:
                x-=d
            elif abs(x-d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x-d))
                break
            elif abs(x-d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 != 0:
                print(abs(x-d))
                break
        k-=1
    else:
        print(abs(x))
            
        
    

# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion



import collections
if __name__=='__main__':
    main()
##    
##

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from io import BytesIO, IOBase


def main():
    x,k,d = map(int,input().split())
    t = x//d
    if t<=k and x<0:
        x+=t*d
        k-=t
    elif t<=k and x>0:
        x-=t*d
        k-=t
    if t>k and x<0:
        x+=k*d
        k-=k
    elif t>k and x>0:
        x-=k*d
        k-=k
    
    while k:
        if x<0:
            if x+d<0:
                x+=d
            elif abs(x+d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x+d))
                break
            elif abs(x+d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 != 0:
                print(abs(x+d))
                break
        else:
            if x-d>0:
                x-=d
            elif abs(x-d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x-d))
                break
            elif abs(x-d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 != 0:
                print(abs(x-d))
                break
        k-=1
    else:
        print(abs(x))
            
        
    

# region fastio

##BUFSIZE = 8192
##
##
##class FastIO(IOBase):
##    newlines = 0
##
##    def __init__(self, file):
##        self._fd = file.fileno()
##        self.buffer = BytesIO()
##        self.writable = "x" in file.mode or "r" not in file.mode
##        self.write = self.buffer.write if self.writable else None
##
##    def read(self):
##        while True:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            if not b:
##                break
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines = 0
##        return self.buffer.read()
##
##    def readline(self):
##        while self.newlines == 0:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            self.newlines = b.count(b"\n") + (not b)
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines -= 1
##        return self.buffer.readline()
##
##    def flush(self):
##        if self.writable:
##            os.write(self._fd, self.buffer.getvalue())
##            self.buffer.truncate(0), self.buffer.seek(0)
##
##
##class IOWrapper(IOBase):
##    def __init__(self, file):
##        self.buffer = FastIO(file)
##        self.flush = self.buffer.flush
##        self.writable = self.buffer.writable
##        self.write = lambda s: self.buffer.write(s.encode("ascii"))
##        self.read = lambda: self.buffer.read().decode("ascii")
##        self.readline = lambda: self.buffer.readline().decode("ascii")
##
##
##sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
##input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion



import collections
if __name__=='__main__':
    main()
##    
##

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from io import BytesIO, IOBase


def main():
    x,k,d = map(int,input().split())
    t = x//d
    if t<=k and x<0:
        x+=t*d
        k-=t
    elif t<=k and x>0:
        x-=t*d
        k-=t
    
    while k:
        if x<0:
            if x+d<0:
                x+=d
            elif abs(x+d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x+d))
                break
            elif abs(x+d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x+d)>=abs(x) and (k)%2 != 0:
                print(abs(x+d))
                break
        else:
            if x-d>0:
                x-=d
            elif abs(x-d)<=abs(x) and (k-1)%2 == 0:
                print(abs(x-d))
                break
            elif abs(x-d)<=abs(x) and (k-1)%2 != 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 == 0:
                print(abs(x))
                break
            elif abs(x-d)>=abs(x) and (k)%2 != 0:
                print(abs(x-d))
                break
        k-=1
    else:
        print(abs(x))
            
        
    

# region fastio

##BUFSIZE = 8192
##
##
##class FastIO(IOBase):
##    newlines = 0
##
##    def __init__(self, file):
##        self._fd = file.fileno()
##        self.buffer = BytesIO()
##        self.writable = "x" in file.mode or "r" not in file.mode
##        self.write = self.buffer.write if self.writable else None
##
##    def read(self):
##        while True:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            if not b:
##                break
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines = 0
##        return self.buffer.read()
##
##    def readline(self):
##        while self.newlines == 0:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            self.newlines = b.count(b"\n") + (not b)
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines -= 1
##        return self.buffer.readline()
##
##    def flush(self):
##        if self.writable:
##            os.write(self._fd, self.buffer.getvalue())
##            self.buffer.truncate(0), self.buffer.seek(0)
##
##
##class IOWrapper(IOBase):
##    def __init__(self, file):
##        self.buffer = FastIO(file)
##        self.flush = self.buffer.flush
##        self.writable = self.buffer.writable
##        self.write = lambda s: self.buffer.write(s.encode("ascii"))
##        self.read = lambda: self.buffer.read().decode("ascii")
##        self.readline = lambda: self.buffer.readline().decode("ascii")
##
##
##sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
##input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion



import collections
if __name__=='__main__':
    main()
##    
##

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from io import BytesIO, IOBase


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

# region fastio

##BUFSIZE = 8192
##
##
##class FastIO(IOBase):
##    newlines = 0
##
##    def __init__(self, file):
##        self._fd = file.fileno()
##        self.buffer = BytesIO()
##        self.writable = "x" in file.mode or "r" not in file.mode
##        self.write = self.buffer.write if self.writable else None
##
##    def read(self):
##        while True:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            if not b:
##                break
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines = 0
##        return self.buffer.read()
##
##    def readline(self):
##        while self.newlines == 0:
##            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
##            self.newlines = b.count(b"\n") + (not b)
##            ptr = self.buffer.tell()
##            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
##        self.newlines -= 1
##        return self.buffer.readline()
##
##    def flush(self):
##        if self.writable:
##            os.write(self._fd, self.buffer.getvalue())
##            self.buffer.truncate(0), self.buffer.seek(0)
##
##
##class IOWrapper(IOBase):
##    def __init__(self, file):
##        self.buffer = FastIO(file)
##        self.flush = self.buffer.flush
##        self.writable = self.buffer.writable
##        self.write = lambda s: self.buffer.write(s.encode("ascii"))
##        self.read = lambda: self.buffer.read().decode("ascii")
##        self.readline = lambda: self.buffer.readline().decode("ascii")
##
##
##sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
##input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion



import collections
if __name__=='__main__':
    main()
##    
##

Submission status: Time Limit Exceeded

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

e = -d

lists = [d, e]
lists2 = []

for n in range(k + 1):
    final_position = x + d * (k - n) + e * n
    lists2.append(abs(final_position))
    
print(min(lists2))

Submission status: Time Limit Exceeded

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

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

print(abs(X))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    def solve(x, k, d):
        to_l_max = x - (k * d)
        to_r_max = x + (k * d)

        if to_l_max > 0:
            return to_l_max
        if to_r_max < 0:
            return - to_r_max

        current_x = x

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

    ans = solve(x, k, d)
    print(ans)

if __name__ == '__main__':
    abc175_c_1()

Submission status: Time Limit Exceeded

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

if abs(X) <= D * K:
    for i in range(K):
        a = abs(X) - (D * (i + 1))
        b = abs(X) - (D * i)
        if a < 0:
            break
    if abs(a) < abs(b):
        c = K - (i + 1)
        if c % 2 == 0:
            if 0 <= X:
                goal = a
            else:
                goal = -a
        else:
            if 0 <= X:
                goal = b
            else:
                goal = -b

    if abs(b) <= abs(a):
        c = K - i
        if c % 2 == 0:
            if 0 <= X:
                goal = b
            else:
                goal = -b
        else:
            if 0 <= X:
                goal = a
            else:
                goal = -a
else:
    if 0 <= X:
        goal = X - D * K
    else:
        goal = X + D * K

print(abs(goal))

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
i=0
while i<k:
    pos=abs(x-d)
    neg=abs(x+d)
    if pos<=neg:
        x=x-d
    elif neg<pos:
        x=x+d
    else:
        break
    i+=1
print(abs(x))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit

setrecursionlimit(10 ** 8)


def main(x, k, d):
    for i in range(k):
        if abs(x + d) < abs(x - d):
            x = x + d
        else:
            x = x - d

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


a, b, c = map(int, input().split())

main(a, b, c)

Submission status: Time Limit Exceeded

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

sum = A
for i in range(0,L[1]):
    sum1 = abs(sum + L[2])
    sum2 = abs(sum - L[2])
    if sum1 < sum2:
        sum = sum1
    elif sum2 < sum1:
        sum = sum2
    elif sum2 == sum1:
        sum =sum1

print(sum)

Submission status: Time Limit Exceeded

Solution code:

    


import collections
if __name__=='__main__':
    x,k,d = map(int,input().split())
    while k:
        r = abs(x-d)
        s = abs(x+d)
        if r<s:
            x-=d
        else:
            x+=d
        k-=1
    print(abs(x))

Submission status: Time Limit Exceeded

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

a = True

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit

X,K,D = map(int,input().split())
i = 0
@njit
def solve(i,x,d,k):
  while i < k:
    i = i + 1
    if x >= 0:
      x = x - abs(d)
    else:
      x = x + abs(d)
  return x

print(abs(solve(0,X,D,K)))

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:

x, k, d = list(map(int, input().split()))
max = None
for i in range(k):
  if x >= 0:
    x -= d
  else:
    x += d

  abs_x = abs(x)
  if max is None:
    max = abs_x
  elif max > abs_x:
    max = abs_x

print(max)

Submission status: Time Limit Exceeded

Solution code:
pos,cnt , dis = map(int,input().split())
while cnt >0:
    if pos>0:
        pos =pos-dis
        cnt -=1
    else:
        pos = pos +dis
        cnt -=1
print(abs(pos))

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

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

tmp = None

if X < 0:
    X = -X

while K != 0:
    X -= D    
    if(X < D):
        if(K % 2 == 1):
            break
        elif(X < 0):
            X += D
            break
        else:
            X -= D
            break            
        
    K -= 1

print(abs(X))

Submission status: Time Limit Exceeded

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

tmp = None

while K != 0:
    if X >= 0:
        X -= D
    else:
        X += D
    
    if(abs(X) < D) & (K % 2 == 1):
        break
        
    K -= 1

print(abs(X))

Submission status: Time Limit Exceeded

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

tmp = None

while K != 0:
    if X >= 0:
        X -= D
    else:
        X += D
    
    if(K % 2 == 1):
        if tmp == X:
            break
        else:
            tmp = X
        
    
    K -= 1

X = abs(X)
print(X)

Submission status: Time Limit Exceeded

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

tmp =[None, None]

while K > 0:
    if X >= 0:
        X -= D
    else:
        X += D
    
    if (X == tmp[1]) & (K % 2 == 1):
        break
    
    tmp[1]= tmp[0]
    tmp[0]= X
    
    K -= 1

X = abs(X)
print(X)

Submission status: Time Limit Exceeded

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

X = abs(X) 
cnt = 0

if X == 0:
    if K%2==0:
        ans = 0
    elif K%2==1:
        ans = D
elif X >= K*D:
    ans = X-K*D
else:
    while K > cnt:
        X = X - D
        cnt += 1
        if X<=0:
            if (K-cnt)%2 == 0:
                ans = abs(X)
                break
            elif (K-cnt)%2 == 1:
                ans = abs(X+D)
                break
        ans = X

print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
L = []
if K % 2 == 0:
    if X < 0:
        for num in range(int(K/2+1)):
            L.append(abs(X +2* D*num)) 
        print(min(L))
    else:
        for num in range(int( K/2+1)):
            L.append(abs(X -2* D*num))
        print(min(L))
else:
    if X < 0:
        for num in range(int((K-1)/2+1)):
            L.append(abs(X + 2*D*num + D))   
    else:
        for num in range(int((K-1)/2+1)):
            L.append(abs(X - 2*D*num - D))
    print(min(L))

Submission status: Time Limit Exceeded

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

X = abs(X) 
cnt = 0

if X == 0:
    if K%2==0:
        ans = 0
    elif K%2==1:
        ans = D
else:
    while K > cnt:
        X = X - D
        cnt += 1
        if X<=0:
            if (K-cnt)%2 == 0:
                ans = abs(X)
                break
            elif (K-cnt)%2 == 1:
                ans = abs(X+D)
                break
        ans = X
               
print(abs(ans))

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
L = []
if K % 2 == 0:
   for num in range(int(K/2+1)):
        L.append(abs(X +2* D*num)) 
   for num in range(int( K/2+1)):
        L.append(abs(X -2* D*num))
   print(min(L))
else:
    for num in range(int((K-1)/2+1)):
        L.append(abs(X + 2*D*num + D))   
    for num in range(int((K-1)/2+1)):
        L.append(abs(X - 2*D*num - D))
    print(min(L))

Submission status: Time Limit Exceeded

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

ans = float("inf")
for i in range(k + 1):
    ans = min(ans, abs(x + (d * i) - (d * (k - i))))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10**8)

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

print(abs(X))

Submission status: Time Limit Exceeded

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

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



print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
while K > 0:
    if abs(X) < D:
        break

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
XKD = input().split()
X = int(XKD[0])
K = int(XKD[1])
D = int(XKD[2])
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())

if x<0:
    x = x * -1
tem_x = x

count = 0

if x-k*d>=0:
    result = x-k*d
else:
    while (tem_x > 0):
        tem_x -= d
        count += 1
    res = [count * d - x , x - count * d + d]

    if (abs(res[0]) < abs(res[1])):
        if ((k - count) % 2 == 0):
            result = res[0]
        else:
            result = res[1]
    elif(abs(res[0]) == abs(res[1])):
        result = res[0]
    else:
        if ((k - count + 1) % 2 == 0):
            result = res[1]
        else:
            result = res[0]

    result = abs(result)


print(result)

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 = 4
    else :
        limit = 5
    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 == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = X // D
        #print(syo)
        if syo > 1 :
            if X > 0 :
                val[0] = X - (syo-2) * D
            else :
                val[0] = X + (syo - 2) * D
            #print(val[0])
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 0
            else:
                limit = 1
        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
            #print(val[i+1])
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                #print("a")
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                #print("b")
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()

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()) 
mlist = []
for i in range(K+1):
    t = X + i*D - (K-i)*D
    if t < 0 :
        t = -t
    mlist.append(t)
print(min(mlist))

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 = 4
    else :
        limit = 5
    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 == 4 :
            print('0')
        else :
            print(abs(D))
    else :
        syo = X // D
        #print(syo)
        if syo > 1 :
            val[0] = X - (syo-2) * D
            #print(val[0])
            K = K -(syo-2)
            if K % 2 == 0:
                limit = 0
            else:
                limit = 1
        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
            #print(val[i+1])
            if (abs(val[i+1]) == abs(val[i-1])) and i > limit :
                #print("a")
                print(abs(val[i + 1]))
                break
            i += 1
            if i == K :
                #print("b")
                print(abs(val[i]))
                break


if __name__ == '__main__':
    main()