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: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
ans = 0
if x >= k * d:
	ans = x - k * d
else:
	step = x // d
	x -= d * step
	k -= step
	if step % 2 == 0:
		ans = abs(x)
	else:
		ans = abs(x - d)
print(ans)

Submission status: Wrong Answer

Solution code:
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

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

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi


def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)


def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y


for _ in range(int(input()) if not True else 1):
    #n = int(input())
    x, k, d = map(int, input().split())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    if x > 0:
        req = x // d
        if k < req:
            print(x - k*d)
        else:
            k -= req
            cur = x % d
            print(abs(cur) if k % 2 == 0 else abs(cur - d))
    else:
        req = (-x) // d
        if k < req:
            print(abs(x + k*d))
        else:
            k -= req
            cur = - (x%d)
            print(abs(cur) if k % 2 == 0 else abs(cur + d))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cnt = abs(x)//d
if x == 0:
    if k % 2 == 1:
        print(d)
    else:
        print(0)
    exit()
if cnt >= k:
    if x > 0:
        print(x-k*d)
    else:
        print(x+k*d)
else:
    x = abs(x)
    k -= cnt
    x %= d
    if k % 2 == 1:
        print(d-x)
    else:
        print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def solve(x, k, d):
    n, m = divmod(x, d)

    if n >= k:
        return x - k * d

    if n % 2 == k % 2:
        return m

    return abs(m - d)


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

print(solve(x, k, d))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if x >= k * d:
    print(x - d * k)
else:
    k -= x // d
    if k % 2 == 0:
        print(x % d)
    else:
        print(-(x % d - d))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
x = abs(x)
if x - k*d < 0:
  print(min(x%d, d - x%d))
else:
  print(x - k*d)

Submission status: Wrong Answer

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

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
  else:
    x=x+d
print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
x,k,d = list(map(int,input().split()))
dis = x
cnt = 0
def zet(x):
    if x >= 0:
        return x
    else:
        return -x
while cnt<k:
    if zet(dis)<d:
        recnt = k -cnt
        if recnt%2==0:
            pass
        else:
            dis = d-zet(dis)
        break
    elif dis>=0:
        dis = dis - d
    else:
        dis = dis + d
    cnt += 1
print(zet(dis))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
if abs(X) > D * K:
    abso = abs(X) - D * K
    print(abso)
    exit()
abso = abs(X)
mini = abs(X)
g = abs(X)
for i in range(K):
    abso =abs(abso - D)
    if abso < mini:
        mini = abso
print(abso)

Submission status: Time Limit Exceeded

Solution code:
input_list = input().split()
(x_int, k_int, d_int) = (int(input_list[0]), int(input_list[1]), int(input_list[2]))

x_current = x_int
for i in range(k_int):
    abs_1 = abs(x_current + d_int)
    abs_2 = abs(x_current - d_int)

    if abs_1 < abs_2:
        x_current = abs_1
    else:
        x_current = abs_2

print(x_current)

Submission status: Time Limit Exceeded

Solution code:

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


xx = abs(x)
count = k
l = []

while count > 0:
    xx = xx - d
    count -= 1
    l.append(abs(xx))
print(min(l))


pass

Submission status: Time Limit Exceeded

Solution code:
x,k,d = map(int,input().split())
cnt = 0
while cnt < k :
    l = [abs(x + d), abs(x - d)]
    a = min(l)
    if a == l[0] :
        x += d
    else :
        x -= d
    cnt += 1
print(a)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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())
for i in range(K):
    if X>=0:
        X -= D
    else:
        X += D
x = abs(X)
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 = abs(x - d)
    else:
        x = abs(x + d)
        
print(x)

Submission status: Time Limit Exceeded

Solution code:
import sys
X,K,D = map(int,input().split())
position = abs(X)
if abs(X) > K*D:
    print(abs(X) - K*D)
    sys.exit()
count = 0
while count <= K:
    position -= D
    count += 1
    if position <=0:
        break
if (K - count) % 2 == 1:
    print(position + D)
else:
    print(abs(position))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = min([abs(X+D*i) for i in range(-K, K+1, 2)])

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):
    x -=d
  else:
    x+=d

print(abs(x))

Submission status: Time Limit Exceeded

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

if x < 0:
    x = -x

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

Submission status: Time Limit Exceeded

Solution code:
import math
x,k,d=map(int,input().split())
def f(x,k,d,i):
    return abs(x+(k-2*i)*d)
a=0
b=k
m=int((a+b)/2)
if k==1:
    print(min(f(x,k,d,0),f(x,k,d,1)))
else:
    while f(x,k,d,m)>f(x,k,d,m-1) or f(x,k,d,m)>f(x,k,d,m+1):
        if f(x,k,d,m)<f(x,k,d,m+1):
            m=math.floor((a+m)/2)
            b=m
        else:
            m=math.ceil((m+b)/2)
            a=m
    print(f(x,k,d,m))

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
dis=float("inf")
for i in range(k+1):
    if abs(x+(k-2*i)*d)<dis:
        dis=abs(x+(k-2*i)*d)
print(dis)

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:
    flag = 0
    for i in range(1,k+1):
        p = x-i*d
        c = k-i
        if p <= 0:
            if c%2 != 0:
                print(abs(d+p))
                flag = 1
                break
            elif c%2 == 0:
                print(abs(p))
                flag = 1
                break
        
        if flag == 0:
            i2 = k+1-i
            p2 = x-i2*d
            if p2 > 0:
                if i%2 != 0:
                    print(abs(d-p2))
                    break
                elif i%2 == 0:
                    print(abs(p2))
                    break

Submission status: Time Limit Exceeded

Solution code:
x,k,d = map(int,input().split())
List=[]
Set =[]
if x>0:
  for i in range(k):
    List.append(x-d)
    x=x-d
  Set = list(map(abs,List))
  print(sorted(Set)[0])
else:
  for i in range(k):
    List.append(x+d)
    x=x+d
  Set=list(map(abs,List))
  print(sorted(Set)[0])

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:
    if x < k*d*1000000:
        for i in range(1,k+1):
            p = x-i*d
            c = k-i
            if p <= 0:
                if c%2 != 0:
                    print(abs(d+p))
                    break
                elif c%2 == 0:
                    print(abs(p))
                    break
    else:
        for i in range(1000000,k+1):
            p = x-i*d
            c = k-i
            if p <= 0:
                if c%2 != 0:
                    print(abs(d+p))
                    break
                elif c%2 == 0:
                    print(abs(p))
                    break

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:
    for i in range(1,k+1):
        p = x-i*d
        c = k-i
        if p <= 0:
            if c%2 != 0:
                print(abs(d+p))
                break
            elif c%2 == 0:
                print(abs(p))
                break

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())
goal = []
times = 0
pos = [0]
movement = []

while times < K:
    for i in pos:
        plus = i + 1
        minus = i - 1
        movement.append(plus)
        movement.append(minus)
    times += 1
    pos.clear()
    for j in movement:
        pos.append(j)
    movement.clear()

for i in pos:
    goal.append(i * D + X)

print(abs(min(goal, key=abs)))

Submission status: Time Limit Exceeded

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

if x == 0:
    if k % 2 == 1:
        x = d

elif x > 0:
    i = 0
    while (d < x)&(i != k-1):
        x -= d
        i += 1
    if (k-i)%2 ==1:
        x -= d
else:
    i = 0
    while (-d > x)&(i != k-1):
        x += d
        i += 1
    if (k-i)%2 ==1:
        x += d

print(abs(x))

Submission status: Time Limit Exceeded

Solution code:
def execute(X, K, D):
    for i in range(K):
        if X > 0:
            X -= D
        else:
            X += D

    return abs(X)

if __name__ == '__main__':
    X, K, D = map(int, input().split())
    print(execute(X, K, D))

Submission status: Time Limit Exceeded

Solution code:
x,k,d = map(int, input().split())
for i in range(k):
    a = abs(x+d)
    b = abs(x-d)
    if a>b:
        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 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):
  X1=X-D
  X2=X+D
  if abs(X1)>abs(X2):
    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:
  f=True
else:
  f=False
  
for i in range(k):
  if x > 0:
    if f==False and (k-i)%2==0:
      print(abs(x))
      exit()
    x -= d
    f=True
  else:
    if f==True and (k-i)%2==0:
      print(abs(x))
      exit()
    x += d
    f=False
print(abs(x))

Submission status: Time Limit Exceeded

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

def judge():
    if x > 0:
        for r in range(d):
            if (x - r)/d == (x - r)//d:
                i = (x - r)//d
                if (k - i)%2 == 0:
                    print(r)
                else:
                    print(d-r)
                exit()
    else:
        l = -x
        for r in range(d):
            if (l - r)/d == (l - r)//d:
                i = (l - r)//d
                if (k - i)%2 == 0:
                    print(r)
                else:
                    print(d-r)
                exit()


if x == 0:
    if k%2 == 0:
        print(0)
    else:
        print(d)
elif x < 0:
    if k*d + x > 0:
        judge()
    else:
        print(abs(k*d + x))
else:
    if x - k*d < 0:
        judge()
    else:
        print(abs(x - k*d))

Submission status: Time Limit Exceeded

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
a = int(X/D)
if a < K:
        K -= a
        X -= a*D

if K*D <= X:
    X -= K*D
else:
    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()]

s=X

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
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 _ in range(k):
  if x >= 0:
    x -= d
  elif x < 0:
    x += d

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

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()


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

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()


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

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

if X == 0:
    print(0 if K % 2 == 0 else D)
elif X < 0:

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

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def change_place(x, d):
    if x < 0:
        x += d
    else:
        x -= d
    return x

for i in range(k):
    x = change_place(x, d)

print(abs(x))

Submission status: Time Limit Exceeded

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

def change_place(x, d):
    if abs(x - d) > abs(x + d):
        x += d
    else:
        x -= d
    return x

for i in range(k):
    x = change_place(x, d)

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_ = min(X, abs(X-D))
    if X == X_:
        X = abs(X_ - (K - i)%2*D)
        break
    X = X_
print(X)

Submission status: Time Limit Exceeded

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

def func(i):
  if i >= 0: return i
  else: return -i

for _ in range(K):
  if func(X + D) <= func(X - D): X += D
  else: X -= D

print(func(X))

Submission status: Time Limit Exceeded

Solution code:
X,K,D=map(int,input().split())
flag=True
X=abs(X)
I=X//D
for i in range(I):
    X-=D
K-=I
if K<1:
    for i in range(abs(K)):
        X+=D
    flag=False
ans=X
if flag:
    if K%2!=0:
        ans-=D
        ans=abs(ans)
        if ans>abs(X+D):
            ans+=D
            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 = map(int, input().split())
x = abs(x)
count = 0

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

elif x - k*d == 0:
    print(0)

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

Submission status: Time Limit Exceeded

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

if abs(X) > K*D:
    print(abs(X)-K*D)
    sys.exit()
    
for t in range(K):
    # 絶対値の小さい方に移動
    if abs(temp - D) < abs(temp + D):
        temp = temp-D
    else:
        temp = temp+D

    # 移動幅より小さくなったとき
    if abs(temp) < D:
        if (K-t-1) % 2 == 0:
            print(abs(temp))
            sys.exit()
        else:
            if abs(temp - D) < abs(temp + D):
                temp = temp-D
                print(abs(temp))
                sys.exit()
            else:
                temp = temp+D
                print(abs(temp))
                sys.exit()

print(abs(temp))

Submission status: Time Limit Exceeded

Solution code:
import sys
X,K,D= map(int,input().split())
temp = X
for t in range(K):
    # 絶対値の小さい方に移動
    if abs(temp - D) < abs(temp + D):
        temp = temp-D
    else:
        temp = temp+D

    # 移動幅より小さくなったとき
    if abs(temp) < D:
        if (K-t-1) % 2 == 0:
            print(abs(temp))
            sys.exit()
        else:
            if abs(temp - D) < abs(temp + D):
                temp = temp-D
                print(abs(temp))
                sys.exit()
            else:
                temp = temp+D
                print(abs(temp))
                sys.exit()

print(abs(temp))

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
r=x
if k*d==x:
	print(0)
else:
	index=0
	while x>0:
		x=x-d
		index+=1
		if index==k:
			break
	p=k-index
	loc=r-index*d
	if p%2==1:
		l1=min(abs(loc+d),abs(loc-d))
	else:
		l1=abs(r-index*d)
	print(l1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
for n 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 -=d
    elif x < 0:
        x +=d
print(abs(x))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
input_str = input()
l = [int(x.strip()) for x in input_str.split(' ')]
X = int(l[0])
K = int(l[1])
D = int(l[2])
loc = 0
lis = []

def calucu_min(X,K,D):
    for num in range(K) :
        lis.append( abs(abs(2 *D *num -K *D) - abs(X)))
        lis_unique = set(lis)
    print (min(lis_unique))

calucu_min(X,K,D)

Submission status: Time Limit Exceeded

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

n=0

x_abs=abs(x)

if x_abs-(d*k)>=0:
    print(x_abs-(d*k))
    
elif x==d:
    if k%2==0:
        print(d)
    else:
        print(0)

else:
    for i in range(1,k+2):
        if n>=0:
            n=x_abs-(d*i)
        else:
            if (i-1)%2==k%2:
                print(abs(n))
                break
            else:
                print(x_abs-(d*(i-2)))
                break

Submission status: Time Limit Exceeded

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

n=0

x_abs=abs(x)

if x_abs-(d*k)>=0:
    print(x_abs-(d*k))

else:
    for i in range(1,k+2):
        if n>=0:
            n=x_abs-(d*i)
        else:
            if (i-1)%2==k%2:
                print(abs(n))
                break
            else:
                print(x_abs-(d*(i-2)))
                break

Submission status: Time Limit Exceeded

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
while k > 0:
    x = min(abs(x + d), abs(x - d))
    k -= 1
print(x)

Submission status: Time Limit Exceeded

Solution code:
X, K, D = (int(x) for x in input().split())

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

print(abs(X))

Submission status: Time Limit Exceeded

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

ogpos = True

if X > 0:
    ogpos = True
else:
    ogpos = False

old = X
newpos = ogpos


while newpos == ogpos and K>0:
    K -= 1
    if X > 0:
        X -= D
    elif X < 0:
        X += D
    else:
        if ogpos == True:
            X -= D
        else:
            X += D

    if X>0:
        newpos = True
    elif X<0:
        newpos = False
        


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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

# loopに入ったらおしまい
record = deque([X,])
finished = False
while K > 0:
    if X > 0:
        X -= D
    else:
        X += D
    record.append(X)
    if len(record) > 3:
        record.popleft()
    if record[0] == record[-1]:
        if K % 2 == 0:
            print(abs(record[1]))
        else:
            print(abs(record[0]))
        finished = True
        break
    K -= 1     

if not finished:
    print(abs(X))

Submission status: Time Limit Exceeded

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

poss = True

if X > 0:
    poss = True
else:
    poss = False

old = X

for i in range(K):
    if X > 0:
        if poss == False:
##            if abs(old) >= abs(X):
##                break
##            else:
##                print("false", i)
            if (K-i)%2 == 0:
                break
            else:
                X = abs(X-D)
                break
            
            
        old = X
        X -= D
    elif X < 0:

        if poss == True:
##            if abs(old) >= abs(X):
##                break
##            else:
##                
##                print("true", i, X, old)

            if (K-i)%2 == 0:
                break
            else:
                X = abs(X+D)
                break
        
        old = X
        X += D
    else:
        if (K-i)%2 == 0:
            break
        else:
            X = X+D
            break


print(abs(X))

Submission status: Time Limit Exceeded

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

    def solve(x, k, d):
        # Xを絶対値でプラスの値にし、0に近づける問題に置き換える
        x = abs(x)

        # (x // d)は0の手前まで移動するのに必要な回数。kの方が小さい場合はk回移動してしまえばよい
        if k < (x // d):
            return x - (k * d)
        # kのほうが大きい場合はそこから数回移動が必要
        else:
            # xからとりあえずx//d回は移動してOK
            current_x = x - d * (x // d)
            k = k - (x // d)
            
            # 結局ここが時間がかかるので終わらない。
            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())
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()]

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 = input().split()
X = int(X)
K = int(K)
D = int(D)
for i in range (K):
    if 0 <= X:
        X = X - D
    else:
        X = X + D
print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
import sys
sin = sys.stdin.readline
x, k, d = map(int, sin().split())
now = x

for i in range(k):
    add = abs(now + d)
    diff = abs(now - d)
    if add > diff:
        now -= d
    else:
        now += d

print(abs(now))