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 = [int(x) for x in input().split()]
X = abs(X)
a = X//D
if a >= K:
    print(X-K*D)
    exit()
else:
    K = K-a
if K % 2 == 0:
    print(X%D)
else:
    print(D-X%D)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)
import math
import itertools
INF = float('inf')
from heapq import heapify, heappop, heappush

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
"""
XからK回移動しなければならない
Dは一回の移動距離
"""
kaisu=abs(X)//abs(D)

one=abs(abs(X)-abs(D)*kaisu)
two=abs(abs(X)-abs(D)*(kaisu+1))

#まず、たどりつけないとき
if K<kaisu:
    ans=abs(X)-abs(D)*K
elif K==kaisu:
    ans=one
elif K>kaisu:
    #kaisu,kaisu+2,kaisu+4 動かすとoneにたどり着く
    #kaisu+1,kaisu+3,kaisu+5 動かすとtwoにたどり着く
    if (K-kaisu)%2==0:
        ans=one
    elif (K-kaisu)%2!=0:
        ans=two
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    X, K, D = map(int, readline().split())
    X = abs(X)
    if X // D >= K:
        ans = X - D * K
    else:
        if (K - X // D) % 2 == 0:
            ans = X - X // D * D
        else:
            ans = abs(X - X // D * D - D)


    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#--c--#
import sys,math,collections,itertools
input = sys.stdin.readline


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

#-近い位置までに必要な移動回数 L-#
L = abs(X)//D
if L>=K:
    ans = abs(X) - K*D

else:
    if (K-L)%2 == 0:
        ans = abs(X) - L*D
    else:
        ans = abs(abs(X) -(L+1)*D)
print(ans)

Submission status: Accepted

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

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

ans = 0

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

print(ans)

Submission status: Accepted

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

ans_1 = abs(X - X//D * D)
ans_2 = abs(ans_1-D)

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

elif (K - X//D) % 2 == 0:
    print(ans_1)
else:
    print(ans_2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
    Satwik_Tiwari ;) .
    14th AUGUST , 2020  - FRIDAY
"""

#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function

from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase

from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================
#fast I/O region
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")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
  def wrapped_func(*args, **kwargs):
    if stack: return f(*args, **kwargs)
    to = f(*args, **kwargs)
    while True:
      if type(to) is GeneratorType:
        stack.append(to)
        to = next(to)
        continue
      stack.pop()
      if not stack: break
      to = stack[-1].send(to)
    return to
  return wrapped_func
#### END ITERATE RECURSION ####

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
def testcase(t):
    for p in range(t):
        solve()
def printlist(a) :
    for p in range(0,len(a)):
        out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
    res = 1     # Initialize result
    x = x % p  # Update x if it is more , than or equal to p
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : # If y is odd, multiply, x with result
            res = (res * x) % p

        y = y >> 1      # y = y/2
        x = (x * x) % p
    return res
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) :
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6
    return True

#===============================================================================================
# code here ;))
def chck(a,b,c):
    if(a+b>c and b+c>a and c+a>b):
        return True
    return False

def solve():
    x,k,d = sep()
    if(x==0):
        if(k%2==0):
            print(0)
        else:
            print(x+d)
        return

    x = abs(x)

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









testcase(1)
# testcase(int(inp()))

Submission status: Accepted

Solution code:
import sys


def solve(x, k, d):
    x = abs(x)
    if d * k <= x:
        return x - d * k
    p, q = divmod(x, d)
    if (k - p) % 2 == 0:
        return q
    else:
        return d - q


def main():
    input = sys.stdin.buffer.readline
    x, k, d = map(int, input().split())
    print(solve(x, k, d))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

x,k,d = ip()
if k*d <= abs(x):
    print(abs(x)-(k*d))
    exit()
if x < 0:
    n = abs(x)//d
    k -= n
    x += n*d
    if k%2:
        print(abs(x+d))
    else:
        print(abs(x))
elif x > 0:
    n = x//d
    x -= n*d
    k -= n
    if k%2:
        print(abs(x-d))
    else:
        print(abs(x))
else:
    if k%2:
        print(d)
    else:
        print(0)

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split())
x_max = x+k*d
x_min = x-k*d
if x_max<=0:print(-x_max)
elif x_min>=0:print(x_min)
else:
    t = x_max%(2*d)
    t2 = -t+2*d
    print(min(t,t2))

Submission status: Accepted

Solution code:
#import numpy as np
#import collections
#import math

#f = open('./input_C.txt')
#
#X,K,D=map(int, f.readline().split())

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

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


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

X = abs(X)
if (tmp := X - K * D) >= -D:
    print(abs(tmp))
else:
    tmp = (X - (K % 2) * D) % (2 * D)
    print(min(abs(tmp), abs(tmp - 2 * D)))

Submission status: Accepted

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

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

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(X - K*D)
else:
    if abs(X)//D%2 == K%2:
        print(abs(abs(X)%D))
    else:
        print(abs(D-abs(X)%D))

Submission status: Accepted

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

a, b = X//D, X%D
if a > K:
  print(X-D*K)
elif (K-a)%2:
  print(D-b)
else:
  print(b)

Submission status: Accepted

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

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

Submission status: Accepted

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

x, K, D = map(int, readline().split())
if x - K * D >= 0:
    print(abs(x - K * D))
elif x + K * D <= 0:
    print(abs(x + K * D))
else:
    low, high = 0, K
    while high - low > 1:
        mid = (high + low) // 2
        if x + (-K + 2 * mid) * D <= 0:
            low = mid
        else:
            high = mid
    print(min(abs(x + (-K + 2 * low) * D), abs(x + (-K + 2 * high) * D)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')



def solve():
    
    # for _ in range(ii()):

        


        
        x,k,d= mi()
        x = abs(x)
        c = x//d
        if c>=k:
            print(x-k*d)
            exit(0)
        k -= c
        c1 = x%d
        c2 = c1-d
        if k&1:
            print(abs(c2))
        else:
            print(abs(c1))
    # print(c1,c2,k) 



















        
if __name__ =="__main__":

    if path.exists('input.txt'):
        sys.stdin=open('input.txt', 'r')
        sys.stdout=open('output.txt','w')
    else:
        input=sys.stdin.readline
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10**9 + 7
def solve():
    x, k, d = map(int, input().split())
    t = min(abs(x) // d, k)
    if x < 0:
        x += t * d
    else:
        x -= t * d
    k -= t
    k %= 2
    ans = x
    if k > 0:
        ans = min(abs(x + d), abs(x - d))
    else:
        ans = abs(x)
    print(abs(ans))
t = 1
while t > 0:
    solve()
    t -= 1

Submission status: Accepted

Solution code:
import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

DIV = (10**9) + 7
#DIV = 998244353

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s += a
        yield s


sys.setrecursionlimit(200005)


def dijkstra(G,start=0):
    heap = []
    cost = [INF]*len(G)
    heappush(heap,(0,start))
    while len(heap)!=0:
        c,n = heappop(heap)
        if cost[n] !=INF:
            continue
        cost[n]=c
        for e in G[n]:
            ec,v = e
            if cost[v]!=INF:
                continue
            heappush(heap,(ec+c,v))
    return cost


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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
# まずは0に近づける

if X < 0:
    t = min(X//(-D), K)
    X = X + D*t
    K -= t
else:
    t = min(X//(D), K)
    X = X - D*t
    K -= t

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

print(abs(X))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# map(int, input().split())
X,K,D = map(int, input().split())

# 移動仕切れるか
max_move = K*D
if abs(X) >= max_move:
    print(abs(X)-max_move)
else:
    # ギリギリを狙う
    giri_kaisuu = int(abs(X)/D)
    nokori = K - giri_kaisuu
    if nokori%2 == 0:
        print(abs(X)-(giri_kaisuu*D))
    else:
        print(-(abs(X)-((giri_kaisuu+1)*D)))

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


x,k,d = list(map(int, input().split()))
x = abs(x)
if x>=k*d:
    ans = x-k*d
elif x<=-k*d:
    ans = x+k*d
else:
    v = x//d
    val = x%d
    val2 = abs(val-d)
    if (k-v)%2==0:
        ans = val
    else:
        ans = val2
print(ans)

Submission status: Accepted

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

cnt = abs(x) // d
x = abs(x)
if k % 2 == cnt % 2:
    ans = min(abs(x - d * min(k,cnt)), abs(x + d * min(k,cnt)))
elif k % 2 != cnt % 2:
    ans = min(abs(x - d * min(k,(cnt + 1))), abs(x - d * min(k,(cnt + 1))))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

x = max(x, -x)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())  # 座標X, ちょうどK回, 距離Dの移動
X = abs(X)
# 最適な回数
most_nearest_do = X//D

# K回移動しても最適な分近づけない場合はK回移動するだけ
if most_nearest_do >= K:
    X -= K*D
    print(abs(X))
    exit()
else:
    # 余る場合（Kの方が大きい場合）
    X -= most_nearest_do*D
    K -= most_nearest_do  # 余った回数が偶数なら反復横跳び
    if K % 2 == 0:
        print(abs(X))
    else:
        print(abs(X-D))

Submission status: Accepted

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

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

Submission status: Accepted

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

    if X >= 0:
        use = min(X // D, K)
        K -= use
        X -= use * D
        if K % 2:
            X -= D

    else:
        use = min((-X) // D, K)
        K -= use
        X += use * D
        if K % 2:
            X += D

    print(abs(X))


if __name__ == '__main__':
    main()

# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    X, K, D = map(int, sysread().split())
    ans = INF
    if abs(X) > K * D:
        ans = abs(X) - K * D
        print(ans)
        return

    i = abs(X) // D
    if not (K - i) % 2:
        ans = abs(X) % D
        print(ans)
        return
    else:
        ans = D - (abs(X) % D)
        print(ans)




if __name__ == "__main__":
    run()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if K * D < X:
    print(abs(X - K * D))
else:
    cnt1 = X // D
    ans1 = deka
    if cnt1 <= K:
        rem = K - cnt1
        if rem % 2 == 0:
            ans1 = X - cnt1 * D
        else:
            ans1 = abs(X - cnt1 * D - D)
    
    cnt2 = (X+D-1) // D
    ans2 = deka
    if cnt2 <= K:
        rem = K - cnt2
        if rem%2 == 0:
            ans2 = cnt2 * D - X
        else:
            ans2 = abs(cnt2 * D - X - D)
    
    print(min(ans1, ans2))

Submission status: Accepted

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


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

    X = abs(X)

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

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

    return


main()

Submission status: Accepted

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

if X > 0:
    p = math.ceil(X/D)
    if K < p:
        ans = X - K*D
    else:
        X -= p*D
        K -= p
        ans = X + (K % 2) * D
else:
    p = math.ceil(abs(X/D))
    if K < p:
        ans = X + K*D
    else:
        X += p*D
        K += p
        ans = X - (K % 2) * D

print(abs(ans))

Submission status: Accepted

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


def solve(X: int, K: int, D: int):
    X = abs(X)
    t = X // D
    if t <= K:
        if t % 2 == K % 2:
            print(abs(X - D * t))
            return
        else:
            print(abs(X - D * (t + 1)))
            return
    else:
        print(abs(X - D * K))
        return
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(X, K, D)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 21:12:05 2020

@author: NEC-PCuser
"""

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

if abs(X) > K * D:
	print(abs(X) - K * D)
else:
	#1000 / 3 333・・・1
	N = X // D
	if (N - K) % 2 == 0:
		print(X % D)
	else:
		print(D - (X % D))

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:
    d,m=divmod(X,D)
    if (K-d)%2==0:
        print(m)
    else:
        print(abs(m-D))

Submission status: Accepted

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

Submission status: Accepted

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

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


# x< k*d
ans1=x%d
ans2=abs((x%d)-d)

moved=x//d
nokori=k-moved
if nokori==0:
    print(ans1)
    exit()

if nokori%2==0:
    print(ans1)
else:
    print(ans2)

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

Submission status: Accepted

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

n = abs(X)//D

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

Submission status: Accepted

Solution code:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy
#n = int(input())
x, k, d = map(int, sys.stdin.readline().split())
#s = input()
#l = list(map(int, sys.stdin.readline().split()))

a = (abs(x)//d) + 1

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

print(abs(ans))

Submission status: Accepted

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