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())
 
if (x - k*d > 0 and x > 0) or (x + k*d < 0 and x < 0):
    print(abs(x) - k*d )
    exit
else:
    if (x % d == 0) and (k % 2 == 0):
        print(abs(x))
    elif (x % d == 0) and (k % 2 == 1):
        print(0)
    elif (x - (x % d)) % d == k % 2:
        print(x % d)
    else:
        print(x - (x % d))

Submission status: Wrong Answer

Solution code:
A = list(map(int, input().split()))
X = A[0]
K = A[1]
D = A[2] 
for i in range(K):
    if X > 0:
        X -= D
    if X <= 0:
        X += D
print(abs(X))

Submission status: Wrong Answer

Solution code:
a=input()
x,m,d=map(int,a.split())
list=[x]
i=0
sol1=None
counter=True

while counter and i<m:
    
    list.append(x-((i+1)*d))
    
    if list[i+1]>list[i] or -1*list[i+1]>list[i]:
        
        if (m-i)%2==0:
            
            sol1=abs(list[i])
            counter=False
            
        else:
            
            sol1= abs(min(list[i-1],list[i],list[i+1]))
            counter=False
            
    sol =abs(min(list))                    
    i+=1        
if sol1==None:
    print(sol)
elif sol1!=sol:
    print(sol1)

Submission status: Wrong Answer

Solution code:
a=input()
x,m,d=map(int,a.split())
list=[x]
i=0
counter=True

while counter and i<m:
    
    list.append(x-((i+1)*d))
    
    if list[i+1]>list[i] or -1*list[i+1]>list[i]:
        
        if (m-i)%2==0:
            
            sol=abs(list[i])
            counter=False
            
        else:
            
            sol = abs(min(list[i-1],list[i],list[i+1]))
            counter=False
            
    sol =abs(min(list))                    
    i+=1        
print(sol)

Submission status: Wrong Answer

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

r = x % d
sho = x//d
ans = 0
if sho < k:
    if (k-sho) % 2 == 1:
        ans = x-r
    else:
        ans = r
else:
    ans = x-d
print(ans)

Submission status: Wrong Answer

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

r = x % d
sho = x//d
ans = 0
if sho < k:
    ans = min(r, d-r)
else:
    ans = x-d
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


X, K, D = zz()
if (abs(X) <= K * D):
    move_num = X // D
    if (X >= 0):
        pos = X - move_num*D
    else:
        pos = X + move_num*D
    K - move_num  # 残りこんだけ移動する
    if ((K - move_num) % 2 == 0):
        print(abs(pos))
    else:
        print(min(abs(pos-D), abs(pos+D)))
    # 近くにいける
    pass
else:
    # できるだけ近づくだけ
    if(X >= 0):
        print(abs(X - K*D))
    else:
        print(abs(X + K*D))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


X, K, D = zz()
if (X <= K * D):
    move_num = X // D
    if (X >= 0):
        pos = X - move_num*D
    else:
        pos = X + move_num*D
    K - move_num  # 残りこんだけ移動する
    if ((K - move_num) % 2 == 0):
        print(abs(pos))
    else:
        print(min(abs(pos-D), abs(pos+D)))
    # 近くにいける
    pass
else:
    # できるだけ近づくだけ
    if(X >= 0):
        print(abs(X - K*D))
    else:
        print(abs(X + K*D))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


X, K, D = zz()
if (X <= K * D):
    move_num = X // D
    if (X >= 0):
        pos = X - move_num*D
    else:
        pos = X + move_num*D
    K - move_num  # 残りこんだけ移動する
    if ((K - move_num) % 2 == 0):
        print(pos)
    else:
        print(min(abs(pos-D), abs(pos+D)))
    # 近くにいける
    pass
else:
    # できるだけ近づくだけ
    if(X >= 0):
        print(abs(X - K*D))
    else:
        print(abs(X + K*D))

Submission status: Wrong Answer

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

if X>K*D:
    ans=X-K*D
elif (X//D)%2==K%2:
    ans=X%D
else:
    ans=abs((X%D)-D)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if (x-(k*d))>0:
    print((x-(k*d)))
elif (x-(k*d))==0:
    print(0)
else:
    k1=0
    a=(x-(k1*d))
    while a>=0:
        k1+=1
        a=(x-(k1*d))
    print(abs(a-0))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if abs(X) - K * D < 0:
    d = abs(X) // D
    print(min(abs(abs(X) - d * D), abs(abs(X) - (d+1) * D)))
else:
    print(abs(X) - K * D)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if r<=0:
  print(-p)
elif 0<r<k:
  s=min(p-2*d*q,-p+2*d*(q+1))
  print(s)

Submission status: Wrong Answer

Solution code:
input_line = input()
data = input_line.split(" ")
x = abs(int(data[0]))
k = int(data[1])
d = int(data[2])
a = x // d
b = x % d
res = abs(a)
if res >= k :
  ans = x - k * d
else :
  num = k - res
  if num % 2 == 1 :
    ans = abs(b - d)
  else :
    ans = res
print(ans)

Submission status: Wrong Answer

Solution code:
input_line = input()
data = input_line.split(" ")
x = abs(int(data[0]))
k = int(data[1])
d = int(data[2])
a = x // d
b = x % d
res = abs(a)
if res >= k :
  ans = x - k * d
else :
  num = k - res
  if num % 2 == 1 :
    ans = abs(res - d)
  else :
    ans = res
print(ans)

Submission status: Wrong Answer

Solution code:
input_line = input()
data = input_line.split(" ")
x = abs(int(data[0]))
k = int(data[1])
d = int(data[2])
a = x // d
b = x % d
res = abs(a)
if res >= k :
  ans = x - k * d
else :
  num = k - res
  if num % 2 == 1 :
    ans = abs(res - d)
  else :
    ans = res
  print(ans)

Submission status: Wrong Answer

Solution code:
input_line = input()
data = input_line.split(" ")
x =abs(int(data[0]))
k = int(data[1])
d = int(data[2])
a = x // d
b = x % d
res = abs(a)
num = k - res
if num % 2 == 1 :
  ans = abs(b - d)
else :
  ans = res
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split(" "))
X = abs(X)
 
a = X // D
b = X % D
 
remain_count = K - a
 
if remain_count < 0:
    ans = X - K * D
else:
    if remain_count % 2 == 0:
        ans = b
    else:
        ans = abs(b - D)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
a = x
 
if x >= k*d or x <= -k*d:
    print(str(abs(x - k * d )))
else:
    for i in range(k):
        if abs(a+d) == abs(a-d):
            a = a+d
            break
        elif abs(a + d) > abs(a - d):
            a = a-d
        else:
            a = a+d
    print(str(abs(a)))

Submission status: Wrong Answer

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

def y(l): return abs(x + l * d - (k - l) * d)

lf = max(0, (k - x / d) / 2)
l = int(lf)
if lf - l > 0 and l + 1 <= k:
    print(min(y(l), y(l + 1)))
else:
    print(y(l))

Submission status: Wrong Answer

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

def y(l): return abs(x + l * d - (k - l) * d)

lf = max(0, (k - x / d) / 2)
l = int(lf)
if lf - l > 0:
    print(min(y(l), y(l + 1)))
else:
    print(y(l))

Submission status: Wrong Answer

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

if k * d < x:
  y = x - k * d
  
else:
  a = x - d * int(x/d)
  if (k - int(x/d)) % 2 == 0:
    y = a
  else:
    y = abs(a-d)

print(y)

Submission status: Wrong Answer

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

if k * d < x:
  y = x - k * d
  
else:
  a = abs(x - d * (int(x/d) + 1))
  if (k - int(x/d) + 1) % 2 == 0:
    y = a
  else:
    y = abs(a-d)

print(y)

Submission status: Wrong Answer

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

if x >= k*d or x <= -k*d:
    print(str(abs(x - k * d )))
else:
    for i in range(k):
        if abs(a + d) > abs(a - d):
            a = a-d
        else:
            a = a+d
    print(str(abs(a)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = X // D
b = X % D

remain_count = K - a

if remain_count < 0:
    ans = X - K * D
else:
    if remain_count % 2 == 0:
        ans = min(b, abs(b - D))
    else:
        ans = max(b, abs(b - D))
    
print(ans)

Submission status: Wrong Answer

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

a = X // D
b = X % D

remain_count = K - a

if remain_count < 0:
    ans = X - K * D
else:
    if remain_count % 2 == 0:
        ans = min(b, abs(b - D))
    else:
        ans = abs(b - D)
    
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split(" "))
K_s = str(K)
K2 = int(K_s[len(K_s) - 1])

if K2 % 2 == 1:
    if X < 0:
        ans = X + D
    else:
        ans = X - D
else:
        ans = X

print(ans)

Submission status: Wrong Answer

Solution code:
var = input()
x = int(var.split()[0])
k = int(var.split()[1])
d = int(var.split()[2])
cnt = x // d
if (k <= cnt) :
  print(x - d * k)
else :
  kk = k - cnt
  if (kk % 2 == 0) :
    print(x - d * cnt)
  else :
    print(d - (x - d * cnt))

Submission status: Wrong Answer

Solution code:
var = input()
x = int(var.split()[0])
k = int(var.split()[1])
d = int(var.split()[2])
cnt = x // d
if (k <= cnt) :
  print(int(x - d * k))
else :
  kk = k - cnt
  if (kk % 2 == 0) :
    print(int(x - d * cnt))
  else :
    print(int(d - (x - d * cnt)))

Submission status: Wrong Answer

Solution code:
A = list(map(int, input().split())) 

x = A[0]
k = A[1]
d = A[2]

first_k = abs(x) // d
if k < first_k:
  final_min = abs(x - k * d)
else:
  first_min = abs(x) % d
  second_min = abs(x) - d * (first_k + 1)

  if abs(first_min) < abs(second_min):
    first_k += 1
    final_min = second_min
  else:
    final_min = first_min
  remain_k = k - first_k
  if remain_k % 2 != 0:
    plus = final_min + d
    minus = final_min - d
    if abs(plus) > abs(minus):
      final_min = abs(minus)
    else:
      final_min = abs(plus)

print(abs(final_min))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
k = abs(X)//D
dk = K-k
dd = min(abs(X-k*D), abs(X+k*D))

if dk>=0:
    if dk%2:
        print(min(abs(dd-D), abs(dd+D)))
    else:
        print(abs(dd))
else:
    print(abs(X-K*D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
k = abs(X)//D
dk = K-k
dd = X-k*D

if dk>=0:
    if dk%2:
        print(min(abs(dd-D), abs(dd+D)))
    else:
        print(abs(dd))
else:
    print(abs(X-K*D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
k = X//D
dk = K-k
dd = X-k*D

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
k = X//D
dk = K-k
dd = X-k*D

if dk>0:
    if dk%2:
        print(abs(dd-D))
    else:
        print(abs(dd))
else:
    print(X-K*D)

Submission status: Wrong Answer

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

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


def main():
    x,k,d = map(int,input().split())
    
    while k:
        if x-d >0:
            x-=d

        
        elif abs(x-d)>=abs(x) and (k)%2==0:
            print(x)
            break
        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-d))
            break
        
        k-=1
        
    else:
        print(abs(x-d))


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

Solution code:
# import itertools
# import math
import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
X, K, D = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

X_abs = abs(X)

if X_abs > K * D:
    print(X_abs - (K * D))
    sys.exit()
else:
    j = X_abs // D
    i = X_abs - (j * D)
    if j % 2 == 0:
        print(i)
    else:
        print(abs(i - D))

Submission status: Wrong Answer

Solution code:
def c_walking_takahashi():
    X, K, D = [int(i) for i in input().split()]
    x = abs(X)
    need_move, r = divmod(x, D)  # 位置 r に到達するには need_move 回の移動が必要
    if need_move >= K:
        return X - K * D  # r には到達できない。動ける限り動いた結果がこれ
    can_move = K - need_move
    # 動ける回数が偶数回だと位置 r で、奇数回だと abs(r-D)=D-r で終了する
    if can_move % 2 == 0:
        return r
    return D - r

print(c_walking_takahashi())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if p<=0:
  print(-p)
elif 0 < p < 2*k*d:
  s=min(p-2*d*q,-p+2*d*(q+1))
  print(s)
else:
  print(x+k*d)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if r<=0:
  print(-p)
elif 0<r<k:
  s=min(p-2*d*q,-p+2*d*(q+1))
  print(s)
else:
  print(x+k*d)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if r<=0:
  print(-p)
elif 0<r<=k:
  s=min(p-2*d*q,-p+2*d*(q+1))
  print(s)
else:
  print(x+k*d)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())

if(k * d < abs(x)):
  x_ = x -(k * d) if x > 0 else x + (k * d) 

elif(k * d == abs(x)):
  x_ = 0 + d if x > 0 else 0 - d 
elif(x == 0):
  if k % 2 == 1:
    x_ = d
  else:
    x_ = 0
    
else:
  x_ = (abs(x) % d) if x > 0 else -(abs(x) % d)
  k_ = (abs(x) // d)
  if (k - k_) % 2 == 1:
    x_ = x_ - d if x > 0 else x_ + d
    
print(abs(x_))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())

if(k * d < abs(x)):
  x_ = x -(k * d) if x > 0 else x + (k * d) 

elif(k * d == abs(x)):
  x_ = 0 + d if x > 0 else 0 - d 
elif(x == 0):
  if k % 2 == 1:
    x_ = d
  else:
    x_ = d*2 
    
else:
  x_ = (abs(x) % d) if x > 0 else -(abs(x) % d)
  k_ = (abs(x) // d)
  if (k - k_) % 2 == 1:
    x_ = x_ - d if x > 0 else x_ + d
    
print(abs(x_))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())

if(k * d < abs(x)):
  x_ = x -(k * d) if x > 0 else x + (k * d) 

elif(k * d == abs(x)):
  x_ = 0 + d if x > 0 else 0 - d 
  
else:
  x_ = (abs(x) % d) if x > 0 else -(abs(x) % d)
  k_ = (abs(x) // d)
  if (k - k_) % 2 == 1:
    x_ = x_ - d if x > 0 else x_ + d
    
print(abs(x_))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p//(2*d)
if r<=0:
  print(-p)
elif 0<r<=k:
  s=min(p-2*d*q,-p+2*d*(q+1))
  print(s)
else:
  print(x+k*d)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if r<=0:
  print(-p)
elif 0<r<=k:
  s=min(p-2*d*q,-p+2*d*(q+1))
else:
  print(x+k*d)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
p=k*d-x
q=p//(2*d)
r=p/(2*d)
if r<=0:
  print(-p)
elif 0<r<=k:
  s=min(p-2*d*q,-p+2*d*(q+1))
else:
  print(x*k+d)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
x = abs(x)
x_ = x % d
k_ = x // d
if(x_ == 0):
  x_ = x_ + d

if(k < k_):
  x_ = x - (k * d)

print(x_)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())
x = abs(x)
x_ = x
for i in range(1, k+1):
  if x >= 0:
    x = x - d
    if abs(x) > 0 and abs(x) < x_:
       x_ = abs(x)

  if x < 0:
    break

print(x_)

Submission status: Wrong Answer

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

a = [abs(x // d), x % d]

if a[0] > k:
   ans = min(x+k*d ,x-k*d)
else:
    b = (k- a[0]) % 2
    ans = min(a[1]+b*d ,a[1]-b*d)

print(abs(ans))

Submission status: Wrong Answer

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

a = [abs(x // d), x % d]

if a[0] > k:
   ans = min(x+k*d ,x-k*d)
else:
    b = (k- a[0]) % 2
    ans = min(x+b*d ,x-b*d)

print(abs(ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
put = False
lst = []
for i in range(K):
    if abs(X+D) < abs(X-D):
        X += D
    else:
        X -= D
    if X in lst:
        check = lst[-2:]
        check = sorted(check)
        print(abs(check[0]))
        put = True
        break
    else:
        lst.append(X)
    K -= 1
if put == False:
    print(abs(X))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
print(max(abs(x)-k*d,(x%d,-x%d)[k-x//d&1]))

Submission status: Wrong Answer

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

for k in range(K):
    if X > 0:
        X = X - abs(D)
        a += 1
    elif X < 0:
        X = X + abs(D)
        b += 1
    else:
        if (k-K)%2 == 1:
            X = D
        else:
            X = 0
        break
    if a >= 1 and b >= 1:
        if (k-K)%2 == 1:
            k = K
        else:
            break

print(abs(X))

Submission status: Wrong Answer

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

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

if X>0:
    if (X-K*D)>=0:
        print(X-D*K)
    else:
        cnt = X//D
        val = X%D
        if (K-cnt)%2==0:
            print(val)
        else:
            print(abs(D-val))
else:
    if (K*D+X)<=0:
        print(X+K*D)
    else:
        cnt = abs(X)//D
        val = abs(X)%D
        if (K-cnt)%2==0:
            print(abs(-val))
        else:
            print(abs(-val+D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if X>0:
    if (X-K*D)>=0:
        print(X-D*K)
    else:
        cnt = X//D
        val = X%D
        if (K-cnt)%2==0:
            print(val)
        else:
            print(D-val)
else:
    if (K*D+X)<=0:
        print(X+K*D)
    else:
        cnt = abs(X)//D
        val = abs(X)%D
        if (K-cnt)%2==0:
            print(-val)
        else:
            print(-val+D)

Submission status: Wrong Answer

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

if D == X:
    if D % 2 == 1:
        X = 0
    else:
        X = abs(X)
else:
    for k in range(K):
        if X >= 0:
            X = X - abs(D)
            a += 1
        else:
            X = X + abs(D)
            b += 1

        if a >= 1 and b >= 1 and not k == K:
            print(k)
            if (K-k)%2 == 1:
                k = K
            else:
                break
        else:
            pass
print(abs(X))