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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(X, K, D):
    X = abs(X)
    n = X // D
    if n > K:
        return  abs(X-D*K)

    return abs(X-(n + (K-n) % 2) * D)


def main():
    X, K, D = read_int_n()
    print(slv(X, K, D))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

X = abs(X)

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

K -= X//D
X %= D
if K % 2 == 1:
    X = D-X
print(X)

Submission status: Accepted

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

k = (D*K-X)//(2*D)
ans = min(abs(X+D*K), abs(X-D*K))
for i in range(k-10, k+10):
  if 0<=i<=K:
    ans = min(ans, abs(X+i*D-(K-i)*D))
print(ans)

Submission status: Accepted

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

l_ast = (K*D-X) / (2*D)

def dist(l):
    return abs(X+2*l*D-K*D)

if l_ast > K:
    d = dist(K)
elif l_ast < 0:
    d = dist(0)
else:
    l_ = int(l_ast)
    if l_ast - l_ < 0.5:
        d = dist(l_)
    else:
        d = dist(l_+1)
print(d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def examA():
    S = SI()
    ans = 0
    if "R" in S:
        ans = 1
    if "RR" in S:
        ans = 2
    if "RRR" in S:
        ans = 3
    print(ans)
    return

def examB():
    N = I()
    L = LI()
    L.sort()
    cnt = 0
    for i in range(N):
        for j in range(i+1,N):
            for k in range(j+1,N):
                if L[i]==L[j]:
                    continue
                if L[j]==L[k]:
                    continue
                if L[i]+L[j]>L[k]:
                    cnt += 1
    ans = cnt
    print(ans)
    return

def examC():
    X, K, D = LI()
    if X<0:
        X = -X
    ans = inf
    if X>K*D:
        ans = X-K*D
        print(ans)
        return

    K -= X//D
    X -= D * (X//D)

    ans = X
    if K>0:
        if K%2==1:
            ans = abs(X-D)
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

def test():
    i = I()
    li = LI()
    lsi = LSI()
    si = LS()
    print(i)
    print(li)
    print(lsi)
    print(si)
    return

from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = [int(_) for _ in input().split()]
X = abs(X)
if X - K * D >= 0:
    ans = X - K * D
else:
    c1, ans = divmod(X, D)
    c2 = (K - c1) % 2
    if c2:
        ans = abs(ans - D)
print(ans)

Submission status: Accepted

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


if abs(X)-K*D>=0:
    print(abs(abs(X)-D*K))
else:
    cnt=ceil(abs(X)/D)

    dist_1= -(abs(X)-cnt*D)
    dist_2= abs(X)-(cnt-1)*D


    remain_hop=K-cnt

    if remain_hop%2==0:
        print(dist_1)
    else:
        print(dist_2)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    X = abs(X)
    q, r = divmod(X, D)
    if K <= q:
        ans = (q - K) * D + r
    else:
        if (K - q) % 2 == 0:
            ans = r
        else:
            ans = D - r

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

x,k,d = na()
x = abs(x)

y = x%d
yy = abs(y-d)

c = x//d

if c > k:
    print(x-d*k)
    exit()

if c%2 == k%2:
    print(y)
else:
    print(yy)

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    X, K, D = map(int, input().split())
    nearP = 0
    if abs(X) // D > K:
        if X >= 0: print(abs(X - K * D))
        else: print(abs(X + K * D))
    else:
        if X >= 0:
            move = X // D
            nearP = X % D
            if (K - move) % 2 == 0: print(nearP)
            else: print(abs(nearP - D))
        else:
            move = abs(X) // D
            nearM = X + move * D
            if (K - move) % 2 == 0: print(abs(nearM))
            else: print(abs(nearM + D))

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

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

num = abs(x)//d

dist = abs(x) - min(num,k)*d

if num >= k:
    print(dist)
else:
    if (k-num)%2 == 0:
        print(dist)
    else:
        print(abs(dist-d))

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(X, K, D):
    X = abs(X)
    k = X // D
    X -= D * min(K, k)
    K -= min(K, k)

    ans = X - D if K & 1 else X
    print(abs(ans))


if __name__ == '__main__':
    input = sys.stdin.readline
    X, K, D = map(int, input().split())
    main(X, K, D)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    X, K, D = read_ints()
    if X < 0:
        X = -X
    closest_K = X // D
    if K <= closest_K:
        return abs(X-K*D)
    if (K-closest_K)%2 == 0:
        return abs(X-closest_K*D)
    return abs(X-(closest_K+1)*D)


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
X,K,D = [int(_n) for _n in input().split()]
X = abs(X)
if (X // D)+1 > K:
    print(X - D * K)
    # print(X/D, K)
else:
    K -= (X // D)
    X -= (X // D) * D
    if K % 2 == 0:
        print(abs(X))
    else:
        print(abs(X - D))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

def f(x):
    return 2*D*x+X-K*D

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

if f(0)==0 or f(K)==0:
    print(0)
    exit()

if f(0)>0:
    print(f(0))
elif f(K)<0:
    print(abs(f(K)))
else:
    m = (K*D-X)//(2*D)
    ans = 10**18
    
    for i in range(m-100, m+100):
        if 0<=i<=K:
            ans = min(ans, abs(f(i)))
    
    print(ans)

Submission status: Accepted

Solution code:
def main():

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

    if X == 0:
        if K % 2 == 0: return 0
        else: return D
    else:
        X = abs(X)
        if X >= D*K:
            return X - D*K
        else:
            n = X // D
            l = X % D
            if (K - n) % 2 == 0:
                return l
            else:
                return D-l


if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

x=abs(x)
t=x//d

if k<=t:
    print(x-k*d)
else:
    last=k-t
    if last%2==1:
        print(abs(x-(t+1)*d))
    else:
        print(abs(x-t*d))

Submission status: Accepted

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

ok = 0
ng = k+1
while abs(ok-ng)>1:
    m = (ok+ng)//2
    if x-m*d+(k-m)*d>=0:
        ok = m
    else:
        ng = m
ans = min(ans, abs(x-ok*d+(k-ok)*d))
if ok<k:
    ok += 1
    ans = min(ans, abs(x-ok*d+(k-ok)*d))
print(ans)

Submission status: Accepted

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

    if k * d <= x:
        print(x - k*d)
        return

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

    print(abs(x))

main()

Submission status: Accepted

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

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

Submission status: Accepted

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



def main():
    x, k, d = map(int, input().split())
    
    x = abs(x)
    if abs(x) > k*d:
        ans = abs(x-k*d)
    else:
        if x == 0:
            if k%2:
                ans = d
            else:
                ans = 0
        else:
            key = x//d
            k -= key
            x -= key*d
            if k%2:
                ans = min(abs(x-d), abs(x+d))
            else:
                ans = abs(x)
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write('\n'.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7


x,k,d=mdata()
x=abs(x)
if x>=k*d:
    out(x-k*d)
else:
    t=x//d
    if (t+k)%2==0:
        out(x-t*d)
    else:
        out((t+1)*d-x)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))


x,k,d = l_in()

if x < 0:
    x = -x

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

k = k - x//d
r = x%d

if k%2 == 0:
    print(r)
else:
    print(d-r)

Submission status: Accepted

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

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

y=abs(x)
t=y//d
if k<t:
    print(y-d*k)
else:
    s=t-k
    if s%2==0:
        print(y-d*t)
    else:
        print(abs(y-d*(t+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 - K * D)
else:
    if (K - a) % 2:
        print(D - X % D)
    else:
        print(X % D)

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
P = X-D*K
Q = 2*D
if X-D*K<=0<=X+D*K:
    U=(X-D*K)%(Q)
    print(min(U,Q-U))
    
elif 0<X-D*K:
    print(X-D*K)
    
    
elif X+D*K<0:
    print(-X-D*K)

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:
    tmp1 = x - (x//d)*d
    tmp2 = k - (x//d)
    if tmp2 % 2 == 0:
        print(tmp1)
    else:
        print(abs(tmp1-d))

Submission status: Accepted

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

X = abs(X)

if X//D <= K:
    K -= X//D
    X %= D

    X -= D*(K%2)

    print(abs(X))
else:
    print(X - D*K)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
X,K,D = list(map(int,input().split()))
X = abs(X)
if X < K*D:
    if ((X-X%D)//D)%2 == K%2:
        print(X%D)
    else:
        print(-(X%D-D))
else:
    print(X-K*D)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

X,K,D = MI()
X = abs(X)

q = X//D
r = X % D

if K <= q:
    print(X-K*D)
    exit()

if (q-K) % 2 == 0:
    print(r)
else:
    print(D-r)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

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

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

X,K,D = IL()

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

D *= 2
K //= 2

best1 = X%D
best2 = X%D-D

if abs(best1)<abs(best2):
  best = best1
else:
  best = best2
  
if abs(X-best)//D < K:
  print(abs(best))
else:
  if X>best:
    print(abs(X-D*K))
  else:
    print(abs(X+D*K))

Submission status: Accepted

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

Submission status: Accepted

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

if x == 0:
    print(d*(k%2))

else:
    x = abs(x)
    sho = x//d
    amari = x%d
    if sho >= k:
        print(x-k*d)
    else:
        if (k-sho)%2==0:
            print(amari)
        else:
            print(abs(amari-d))

Submission status: Accepted

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

ma=x+k*d
mi=x-k*d

if ma*mi>0:
    print(min(abs(ma),abs(mi)))
else:
    a=x%d
    b=a-d
    if (x//d)%2==k%2:
        print(abs(a))
    else:
        print(abs(b))

Submission status: Accepted

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

Submission status: Accepted

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

if X < 0:
    X *= -1

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

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
a = x // d
a = min(k, a)
k -= a
x -= (d*a)
if k == 0:
    print(x)
else:
    y = abs(x - d)
    if k &1 == 0:
        print(x)
    else:
        print(y)

Submission status: Accepted

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

X = abs(X)
a = min(X//D, K)
X -= D * a
K -= a

if K == 0:
    print(X)
    exit()

if abs(X-D) < X:
    X = abs(X-D)
    K -= 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
l = x - k * d
r = x + k * d
if l <= 0 <= r:
  y = r % (2 * d)
  print(min(y, 2 * d - y))
elif r <= 0:
  print(-r)
else:
  print(l)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    if X < 0:
        X *= -1

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

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
x=abs(abs(X)%D)
y=abs(x-D)
if K<abs(X)//D:
    print(abs(X)-K*D)
elif (K-abs(X)//D)%2:
    print(y)
else:
    print(x)

Submission status: Accepted

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

Submission status: Accepted

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

def f(m):
    ret = x
    ret -= d * m
    ret += d * (k - m)
    return abs(ret)

l = 0
r = k

for _ in range(1000):
    c1 = (l * 2 + r) // 3
    c2 = (l + 2 * r) // 3
    if f(c1) > f(c2):
        l = c1
    else:
        r = c2
res = 10 ** 1000
for i in range(l - 1000, r + 1000):
    if i < 0 or i > k:
        continue
    res = min(res, f(i))
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ceil(a,b):
    return (a+b-1)//b
X,K,D = map(int,input().split())
n = ceil(D*K-X,2*D)
n = max(0,n)
cand = [0,K,n,n-1]
ans = 10**15
for c in cand:
    if c<0 or c>K:
        continue
    x = abs((2*c-K)*D+X)
    ans = min(ans,x)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

now = abs(X)
rest = K

step1 = min(now//D, K)
rest -= step1
now -= step1 * D

if rest <= 0:
    print(abs(now))
else:
    if rest % 2 == 1:
        now = abs(now - D)
        print(now)
    else:
        print(now)

Submission status: Accepted

Solution code:
def main():
    X, K, D = map( int, input().split())
    if X < 0:
        X = -X
    c = min(X//D, K)
    X -= c*D
    K -= c
    if K%2 == 0:
        print(X)
    else:
        print(D-X)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("## ",*x,file=sys.stderr)
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    #N = int(input())
    x,k,d = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列

    x=abs(x)
    if x-k*d>=0:
        print(x-k*d)
        return 
    ans=float("inf")
    if (x//d)%2==k%2:
        ans=min(ans,x%d)
    else:
        ans=min(ans,abs(x%d-d))
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
X = max([X,-X])
if X > K*D:
    print(X-K*D)
else:
    A = X//D
    k = K-A
    if k % 2 == 0:
        print(X%D)
    else:
        print(D-X%D)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  X,K,D=LI()

  if abs(X)>=K*D:
    return abs(X)-K*D

  x=abs(X)
  c1=(x+D-1)//D

  K-=c1
  x1=c1*D-x

  if K%2==0:
    return x1

  return D-x1

# main()
print(main())