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: Accepted

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

hu = False
if X < 0:
    X = abs(X)

idou_kaisu = X//D # 移動回数
amari = X%D # あまり

if idou_kaisu >= K: # すべて一方方向
    print(X - (K*D))
    exit()
else:
    X = amari
    K = K - idou_kaisu

three_list = []

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
n=min(max(round(-x/(2*d)+k/2),0),k)
print(abs(x+(2*n-k)*d))

Submission status: Accepted

Solution code:
X,K,D=map(int, input().split())
n=(K*D-X)//(2*D)
#print(n)
if n<0:
    ans=abs(X-K*D)
elif n>=K:
    ans=abs(X+K*D)
else:
    ans1=abs(X+(2*n-K)*D)
    ans2=abs(X+(2*(n+1)-K)*D)
    ans=min(ans1,ans2)
    #print(n,ans1,ans2)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math
# スペース区切りの整数の入力
X, K, D = map(int, input().split())

cross = abs(-(-X//D))

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
n = X // D
if K <= n:
    ans = X - K*D
else:
    ans = abs([X - n*D, X - (n+1)*D][(K-n)%2])

print(ans)

Submission status: Accepted

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

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

if X < 0:
  X = -X

s = X//D

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

k = K - s
x = X - D*s
if k%2 == 0:
  print(x)
else:
  print(D-x)

Submission status: Accepted

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

a = abs(x) - k*d

if a>0:
    print(a)
elif a==0:
    print(0)
else:
    ap = int(abs(x) / d)
    if (k-ap) % 2 == 0:
        print(abs(x)-ap*d)
    else:
        print((ap+1)*d-abs(x))
exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
X, K, D = list(map(int, input().split()))
x = int(X)
D = int(D)
j = 0
j = abs(X//D)

if j > K:
    j = K


if x > 0:
    x = x - (D*j)
else:
    x = x + (D*j)

j = (K-j) % 2

if j==0:
    print(abs(x))
else:
    if x > 0:
        x = x - D
    else:
        x = x + D
    print(abs(x))

Submission status: Accepted

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

k_min=abs(x//d)
if k_min>=k:
  k_min=k
if x>0:
  d_min=x-((k_min)*d)
else:
  d_min=x+((k_min)*d)

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

Submission status: Accepted

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

X = abs(X)
aa = X // D

if K < aa :
    X -= K * D
else :
    if (K - aa) % 2 == 1 :
        X -= (aa+1) * D
    else :
        X -= aa * D

print(abs(X))

Submission status: Accepted

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

if d * k >= abs(x) and x != 0:
  # 0超える1つ手前は？
  near_0 = x // d
  if x < 0:
    near_0 += 1
  if (k - abs(near_0)) %2 == 0:
    print(abs(x - near_0 * d))
  else:
    hoge = x - near_0 * d + d
    huga = x - near_0 * d - d
    if abs(hoge) < abs(huga):
      print(abs(hoge))
    else:
      print(abs(huga))
else:
  if x == 0:
    if k%2 == 0:
      print(0)
    else:
      print(d)
  elif x > 0:
    print(abs(x-(d*k)))
  else:
    print(abs(x+(d*k)))

Submission status: Accepted

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

move_count = 0
D = abs(D)
X = abs(X)

q = X // D
mod = X % D
vi = X //2
if q > K:
  print(X - D*K)
else:
  if (K-q) % 2 == 0:
    print(X- D*q)
  else:
    print(abs(X-D*(q+1)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


n=X//D
if n<=K:
	if (K-n)%2==0:
		a=X%D
		b=abs(a-2*D)
	else:
		a=X%D+D
		b=abs(X%D-D)
else:
	a=X
	b=Y
print(min(a,b))

Submission status: Accepted

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

if x == 0:
    if k % 2 == 0:
        print(0)
    else:
        print(d)
else :
    if abs(x) > k * d:
        if x > 0:
            print(x - k * d)
        else:
            print(abs(x + k * d))
    else:
        if x > 0:
            aaa = x // d
            a = x - aaa * d
            b = x - (aaa + 1) * d
            if abs(a) >= abs(b):
                if (k - (aaa + 1))%2 == 0:
                    print(abs(b))
                else:
                    print(abs(a))
            else:
                if (k - aaa)%2 == 0:
                    print(abs(a))
                else:
                    print(abs(b))
                
        else:
            aaa = abs(x) // d
            a = x + aaa * d
            b = x + (aaa + 1) * d
            if abs(a) >= abs(b):
                if (k - (aaa + 1))%2 == 0:
                    print(abs(b))
                else:
                    print(abs(a))
            else:
                if (k - aaa)%2 == 0:
                    print(abs(a))
                else:
                    print(abs(b))

Submission status: Accepted

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

# K*Dが小さすぎる
if K * D < X:
    ans = X - (K * D)
else:
    # toutatu:何回で0を超えるか？(0ちょうどになってもよい)
    
    rest = X % D
    
    if rest==0:
        # 答えは 0 と　D　のどちらか
        # 偶奇で場合分け
        toutatu = X / D
        if (K - toutatu)%2==0:
            ans = 0
        else:
            ans = D
    
    else:
        toutatu = X // D + 1
        # 答えは |D-rest| と　|rest|　のどちらか
        # K - toutatuの偶奇で場合分け
        if (K - toutatu)%2==0:
            ans = D-rest
        # そうでないなら打ち消しあえない
        else:
            ans = rest

print(ans)

Submission status: Accepted

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

if abs(x) >= d * k:
  ans = min(abs(x-k*d), abs(x+k*d))
else:
  tmp_k = x // d
  if (k - tmp_k) % 2 == 0:
    ans = min(abs(x-tmp_k*d), abs(x+tmp_k*d))
  else:
    ans = min(abs(x-tmp_k*d), abs(x+tmp_k*d))
    ans = d - ans

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

x = abs(x)
a = x % d
b = x % d - d
ans = abs(x - k * d)
if (x - a) // d <= k and ((x - a) // d - k) % 2 == 0:
    ans = min(ans, a)
if (x - b) // d <= k and ((x - b) // d - k) % 2 == 0:
    ans = min(ans, -b)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    X, K, D = read_values()
    X = abs(X)
    k = X // D
    if k >= K:
        print(X - K * D)
    else:
        K -= k
        X -= k * D
        print(X if K % 2 == 0 else D - X)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

if abs(x) // d < k:
    if (k - abs(x) // d) % 2 == 0:
        print(abs(x) - abs(x) // d * d)
    elif (k - abs(x) // d) % 2 == 1:
        print(min(abs(abs(x) - (abs(x) // d - 1) * d),
                  abs(abs(x) - (abs(x) // d + 1) * d))
              )
elif abs(x) // d >= k:
    print(abs(x) - k*d)

Submission status: Accepted

Solution code:
x,k,d = [int(p) for p in input().split()]
x=abs(x)
ans = min(x%d,d-x%d);
if(ans==0):
    p = x//d
    t = k;
    k-=p;
    if(k<0):
        print((x-t*d))
    elif(k%2==0):
        print(0)
    else:
        print(d)
elif(k*d>x):
    p = x//d
    k-=p
    if(x%d==ans):
        if(k%2==0):
            print(ans)
        else:
            print(d-ans)
    else:
        k-=1
        if(k%2==0):
            print(ans)
        else:
            print(d-ans)
else:
    print((x-k*d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
if abs(X)>K*D:
    print(min(abs(X-K*D), abs(X+K*D)))
else:
    X=abs(X)
    N=X//D
    Min=10**16
    n=0
    for i in range(N-1, N+2):
        M=abs(X-i*D)
        if M<Min:
            Min=M
            n=i
    if (K-n)%2==0:
        print (Min)
    else:
        print(min(abs(Min+D), abs(Min-D)))

Submission status: Accepted

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


if K*D<=X:
    print(X-K*D)
else:
    x=X//D
    key=x%2
    if x==0:
        if K%2==0:
            print(X)
        else:
            print(abs(X-D))
    else:
        y=X-D*x
        if K%2==key:
            print(y)
        else:
            print(abs(y-D))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
nums = list(map(int, input().strip().split(' ')))
x = nums[0]
k = nums[1]
d = nums[2]
x = abs(x)
if x >= k * d:
    print(x - (k * d))
else:
    ans = []
    ans.append(x)
    m = x // d
    k1 = x - (m * d)
    left = k - m
    if(left % 2 == 0):
        ans.append(abs(k1))
    else:
        ans.append(abs(k1 - d))
    sorted(ans)
    print(ans[len(ans) - 1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#n = int(input())
x,k,d = map(int,input().split())
#l = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
v=abs(x)//d
if abs(x)>= d:
    if abs(x)//d >= k:
        if x >= 0:
            ans=abs(x-d*k)
        else:
            ans=abs(x+d*k)
    else:
        move=v*d
        dif=k-v
        if x >=0:
            #l=[]
            #l.append(x-v*d)
            x2=x-v*d
            #l.append(abs(x2-d))
            #ans=min(l)
            if dif%2==0:
                ans=abs(x-v*d)
            else:
                ans=abs(x2-d)
        else:
            x2=x+v*d
            if dif%2==0:
                ans=abs(x+v*d)
            else:
                ans=abs(x2+d)
else:
    if x>=0:
        x2=x-d
        dif=k-1
        if dif==0:
            ans=abs(x2)
        elif dif%2==0:
            ans=abs(x2)
        else:
            ans=abs(x2+d)
    else:
        x2=x+d
        dif=k-1
        if dif==0:
            ans=abs(x2)
        elif dif%2==0:
            ans=abs(x2)
        else:
            ans=abs(x2-d)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    x = kwargs["x"]
    k = kwargs["k"]
    d = kwargs["d"]
    if k * d <= abs(x):
        res = abs(x) - k * d
    else:
        res = abs(abs(x) % d - (k - abs(x) // d) % 2 * d)

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["x"], kwargs["k"], kwargs["d"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)

Submission status: Accepted

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

sign = 0
if x != 0:
  sign = int(x /abs(x))

if (abs(x) // d) < k:
  tmp = x - sign * d * (abs(x) // d)
  #print(tmp)
  if (k - (abs(x) // d)) % 2 == 0:
    print(abs(tmp))
  else:
    print(min(abs(tmp + d), abs(tmp - d)))
else:
  print(abs(x - sign * d * k))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	x, k, d = map(int, sys.stdin.readline().split())
	x = abs(x)
	if x == 0:
		if k % 2 == 0:
			print(0)
		else:
			print(d)
		exit()

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




main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

c = x//d

a = x - x//d * d
b = d - abs(a)

if x//d%2 == 0:
    odd = a
    even = b
else:
    odd = b
    even = a

if abs(x//d) > k:
    ans = abs(abs(x) - abs(d*k))
else:
    if k%2 ==0:
        ans = odd
    else:
        ans = even

# print(odd,even)
print(abs(ans))

Submission status: Accepted

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

num = 0

x = abs(x)

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

    
print(num)

Submission status: Accepted

Solution code:
# 2020/08/15
# AtCoder Beginner Contest 175 - C

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

# Calc
# d * k < x -> 足りない場合
if d * k < abs(x):
    # 足りない場合：
    ans = abs(x) - k * d
else:
    # 飛び越す場合：
    # 0に近い正の値までの回数
    wkc = x // d
    
    if k % 2 == 0:
        # kが偶数の場合：留まれるのは偶数回目
        if wkc % 2 == 0:
            ans = x % d
        else:
            ans = abs(x % d - d)
    else:
        # kが奇数の場合：留まれるのは奇数回目
        if wkc % 2 == 0:
            ans = abs(x % d - d)
        else:
            ans = x % d

# Output
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    x, k, d = map(int, input().split())

    sho, amari = divmod(abs(x), d)

    if sho > k:
        if x < 0:
            ans = x + (k * d)
        else:
            ans = x - (k * d)
        print(abs(ans))
        return

    if x < 0:
        x = -amari
    else:
        x = amari

    k -= sho

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

    print(abs(ans))


# テストクラス


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """6 2 4"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """7 4 3"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """10 1 2"""
        output = """8"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def test_1original_1(self):
        test_input = """7 3 3"""
        output = """2"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Accepted

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

x = abs(x)

a = x//d

y = x - a*d
b = k - a

if k >= a:
    if b % 2 == 0:
        print(abs(y))
    elif abs(y - d) > abs(y - 2*d):
        print(abs(y-2*d))
    else:
        print(abs(y-d))
else:
    print(abs(x - k*d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
ans=0
if x>k*d:
    ans=x-k*d
else:
    dis_zero=x//d
    #print(dis_zero)
    n=k-dis_zero
    a=x-(d*dis_zero)
    D=abs(a-d)
    if n%2==0:
        ans=a
    elif n%2!=0:
        ans=D
print(ans)

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
ans = 1000000000000000
m = x // d
if m > k:
    print(abs(x-k*d))
else:
    if (k%2==0 and m%2==1) or (k%2==1 and m%2==0):
        m += 1
    ans = min(ans,x-m*d)
    """
    if m+2 <= k:
        ans = min(ans,ans+2*d)
        ans = min(ans,ans-2*d)
        """
    print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

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

	if abs(X) > K * D:
		print(abs(X) - K * D)
	elif abs(X) <= K * D:
		s = abs(X) // D
		if abs(X) % D == 0:
			if s % 2 == 0 and K % 2 == 0:
				print(0)
			elif s % 2 == 0 and K % 2 == 1:
				print(D)
			elif s % 2 == 1 and K % 2 == 0:
				print(D)
			elif s % 2 == 1 and K % 2 == 1:
				print(0)
		else:
			if s % 2 == 0 and K % 2 == 0:
				print(abs(X) - s * D)
			elif s % 2 == 0 and K % 2 == 1:
				print(abs(abs(X) - s * D - D))
			elif s % 2 == 1 and K % 2 == 0:
				print(abs(abs(X) - s * D - D))
			elif s % 2 == 1 and K % 2 == 1:
				print(abs(X) - s * D)
			

 
if __name__ == "__main__":
  	main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 17:00:46 2020

@author: saito
"""

# %% import phase

# %% define phase

# %% input phase
x, k, d = list(map(int, input().split()))

# %% process phase
xabs = abs(x)
if k*d <= xabs:
    ans = xabs-k*d
else:
    innum = xabs // d
    if (k - innum)%2 == 0:
        ans = xabs%d
    else:
        ans = d - xabs%d


# %%output phase
print(ans)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

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

m = Decimal(str(x/d)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
m = abs(m)
if k < m:
    m = k

if x >= 0:
    x = x - m * d
else:
    x = x + m * d
k = k - m

#print("m=",m," k=",k, "x=",x)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

X_abs = abs(X)
p = X_abs // D

if ( p > K ):
  print( X_abs - D*K )
  
else: # ( -D < ans < D )
  K_p = K-p
  
  if ( K_p % 2 == 0 ):
    print( X_abs - D*p )
    
  else:
    print( - X_abs + D*(p+1) )

Submission status: Accepted

Solution code:
#!/usr/local/bin/python3

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

X = abs(X)
n = X // D
if K - n <= 0:
    n = K

K -= n
X -= D * n

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

Submission status: Accepted

Solution code:
l=list(map(int,raw_input().split()))
x=int(l[0])
k=int(l[1])
d=int(l[2])
s = 0
e = (k/2)

ans = int(1e32)

while s<=e :
    m = (s+e)/2
    curr = abs(x+(k-2*m)*d)
    ans = min(curr,ans)
    left = abs(x+(k-2*(m-1))*d)
    right = abs(x+(k-2*(m+1))*d)

    if(left < curr or right < curr):
        if(left<right):
            e=m-1
        else:
            s=m+1
    else:
        break 

s=0
e=(k/2)
ans2= int(1e32)
#negative
while s<=e :
    m = (s+e)/2
    curr = abs(x-(k-2*m)*d)
    ans2 = min(curr,ans2)
    left = abs(x-(k-2*(m-1))*d)
    right = abs(x-(k-2*(m+1))*d)

    if(left < curr or right < curr):
        if(left<right):
            e=m-1
        else: 
            s=m+1
    else:
        break 

ans = min(ans2,ans)

print(ans)

Submission status: Accepted

Solution code:
import math

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

X = abs(X)

max_distance = K * distance

if max_distance <= X:
    print(X - max_distance)
else:
    # Xをdistanceで割って、何回の移動で0を超えるのかを判断
    over_zero_movecount = math.ceil(X / distance)

    # 0を超えたときの座標の絶対値(1)
    over_zero_x = abs(X - over_zero_movecount * distance)
    # 1つ前の移動の絶対値(2)
    over_zero_x_before = abs(X - (over_zero_movecount - 1) * distance)

    # 0を超えた後の残りの移動回数が偶数回の場合
    if (K - over_zero_movecount) % 2 == 0:
        print(over_zero_x)
    # 0を超えた後の残りの移動回数が奇数回の場合
    else:
        print(over_zero_x_before)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
count=0
import math
c=abs(x)//d
amari=abs(x)%d
if c<=k:
    if (k-c)%2==0:
        print(amari)
    else:
        print(abs(amari-d))
else:
    print(abs(x)-d*k)

Submission status: Accepted

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

n, x= divmod(abs(X), D)
if n > K:
  print(min(abs(X - D * K), abs(X + D * K)))
  exit()
K = K - n

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

Submission status: Accepted

Solution code:
X,K,D = map(int, input().split())
X = abs(X)
if X - K*D > 0:
  print(X - K*D)
else:
  needCount = X//D
  limX = X - needCount * D
  remK = K - needCount
  minusX = limX - D
  if remK % 2 == 0:
    print(limX)
  else:
    print(abs(minusX))

Submission status: Accepted

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

Submission status: Accepted

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

mini = math.ceil(X/D)

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

Submission status: Accepted

Solution code:
import sys
import math
x,k,d = map(int,input().split())
if abs(x) >= k*d:
  ans = abs(x) - k*d
  print(ans)
  sys.exit()
else:
  n = math.floor(x/d)
  dist = x - n*d
  if (k - n)%2 == 0:
    print(dist)
    sys.exit()
  else:
    print(abs(d-dist))

Submission status: Accepted

Solution code:
import sys
import copy
input = sys.stdin.readline


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

if X>=0:

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
## Library
 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input
 
# N=int(input())
# A,B,C,D=map(int, input().split())
# S = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]
#    S = []
#    for _ in range(N):
#        S.append(list(input()))

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

X = abs(X)

if 0 <= X - K*D:
    print(X-K*D)
else:
    tmp = X%D
    tmp2 = tmp - D
    if (K - (X - tmp)//D)%2 == 0:
        print(abs(tmp))
    else:
        print(abs(tmp2))

Submission status: Accepted

Solution code:
x,k,d = map(int, input().split())
x = abs(x)
if x<k*d:
	ans = abs(k*d-x) % (2*d)
	print(min(ans, 2*d-ans))
else:
	print(x-k*d)
# eval("eval(\"print(\"hello\")\")")

# # a = "1234"
# # al = list(a)
# # al.insert(1,".")
# # print(al)


# str = input()
# l = list(str)
# n = len(str)
# i = 0;
# while i<n:
# 	if(l[i]=="("):
# 		l.insert(i,"*")
# 		l.insert(i,"*")
# 		n = n+2
# 		i=i+2
# 	i = i+1
# # print(l)
# print(eval("".join(l)))

Submission status: Accepted

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

if abs(X) >= K*D:
    print(abs(X)-K*D)
elif X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
else:
    d = abs(X) // D
    
    amari = abs(X) % D
    
    hantai = D - amari
    
    
    if (K - d) % 2 == 0:
        print(amari)
    else:
        print(hantai)