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:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    x,k,d = LI()
    if x == 0:
        if k % 2 == 1:
            print(d)
        else:
            print(0)
        return
    else:
        s = abs(x) // x
    c = abs(x) // d

    ans = 0
    if c >= k:
        ans = x - d * k * s
    else:
        ans = x - d * c * s
        if (k-c) % 2 == 1:
            ans = ans - d * s
    print(abs(ans))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
import re
import queue

input = stdin.readline

MOD = 1000000007
INF = 122337203685477580


def solve():
    x,k,d = map(int, input().split())
    move = k*d
    x = abs(x)
    if x > k*d:
        print(x -k*d)
    else:
        q = x//d
        if abs(q - k)%2 == 0:
            print(x%d)
        else:
            print(d - x%d)
            
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    x,k,d=map(int, readline().rstrip().split())
    if x < 0:
        x*= -1
    
    if x==0:
        if k%2==0:
            print(0)
        else:
            print(d)
        return
    
    aa = x // d

    if k <= aa:
        print(x-k*d)
        return
    x-=aa*d
    k-=aa
    if k%2==0:
        print(x)
    else:
        print(abs(x-d))


    #arr=list(map(int, readline().rstrip().split()))
    #n=int(readline())
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if X == 0:
    if K % 2 == 0:
        ans = 0
    else:
        ans = D
else:
    tmp1 = X % D
    tmp2 = tmp1 - D
    if abs(tmp1) <= abs(tmp2):
        goal = tmp1
    else:
        goal = tmp2
    cnt = abs(X - goal) // D
    if K < cnt:
        if X > 0:
            ans = X - K * D
        else:
            ans = X + K * D
    elif K == cnt:
        ans = goal
    elif K > cnt:
        if (K - cnt) % 2 == 0:
            ans = goal
        else:
            if goal >= 0:
                ans = goal - D
            else:
                ans = goal + D

print(abs(ans))

Submission status: Accepted

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

pos_min = X - K * D
pos_max = X + K * D

if X <= K * D and X >= -K * D:
    if K % 2 == 0:
        print(min([X % (2 * D), 2 * D - X % (2 * D)]))
    else:
        print(min([(X - D) % (2 * D), 2 * D - (X - D) % (2 * D)]))
elif pos_min > 0:
    print(pos_min)
else:
    print(-pos_max)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    X, K, D = MI()
    X = abs(X)
    z = X // D
    if z >= K:
        print(X - K * D)
        return
    # print(K, z)
    K -= z
    # print(K)
    X = X - z * D
    X2 = abs(X - D)
    # print(X, X2)
    if K % 2 == 0:
        print(X)
    else:
        print(X2)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

if k2%2 == 0:
    print(x%d)
else:
    print(abs(x%d-d))

Submission status: Accepted

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

    X = abs(X)

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

move = abs(x)//d
amari = abs(x)%d 

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

Submission status: Accepted

Solution code:
import sys

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

ans = 9999999
if abs(X) >= K * D:
    ans = abs(X) - K*D
else:
    A = abs(X) % D
    B = K - (abs(X) // D)

    if B % 2 == 0:
        ans = A
    else:
        ans = abs(A - D)

print(ans)

Submission status: Accepted

Solution code:
def abs(a):
	if a<0:
		return -a
	return a
x,k,d=map(int,input().split())
if x==0:
	if k%2==1:
		print(d)
	else:
		print(0)
if x>0:
	if k*d<=x:
		print(x-k*d)
		exit(0)
	r=x%d
	steps=(x-r)//d
	l=abs(r-d)

	if((k-steps)%2==0):
		print(r)
	else:
		print(l)
if x<0:
	if k*d<=abs(x):
		print(abs(x+k*d))
		exit(0)
	l=abs(x)%d
	steps=(abs(x)-abs(l))/d
	r=-l+d

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

Submission status: Accepted

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

X=abs(X)

q=X//D
r=X%D

if r < D-r:
    n=q
    ans1=r
    ans2=D-r
else:
    n=q+1
    ans1=D-r
    ans2=r

if K <= n:
    ans=X-K*D
    ans=abs(ans)
else:
    d=K-n
    if d%2==0:
        ans = ans1
    else:
        ans = ans2
print(ans)

Submission status: Accepted

Solution code:
x,k,d=(int(i) for i in input().split())
c=(abs(x)//d)
if(x<0):
    x+=(min(k,c)*d)
else:
    x-=(min(k,c)*d)
k=max(0,k-c)
if(k%2==1 and x<0):
    print(abs(x+d))
elif(k%2==1 and x>0):
    print(abs(x-d))
elif(k%2==1 and x==0):
    print(x+d)
else:
    print(abs(x))

Submission status: Accepted

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

Submission status: Accepted

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

p = X // D

if abs(p) > K:
    ans = min(abs(X - K * D), abs(X + K * D))

else:
    m0 = abs(X - D * p)
    m1 = abs(X - D * (p + 1))

    if abs(m0) < abs(m1):
        if (K - p) % 2 == 0:
            ans = m0
        else:
            ans = m1
    else:
        if (K - p - 1) % 2 == 0:
            ans = m1
        else:
            ans = m0
print(ans)

Submission status: Accepted

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

ans = 0
if K*D < abs(X):
    ans = abs(X) - K*D
else:
    a1 = X//D
    a2 = X%D
    K -= a1
    # 往復する
    if K%2 == 0:
        ans= a2
    else:
        ans = abs(a2-D)
print(ans)

# 1000000000000000 1000000000000000 1000000000000000

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
n = x//d
p = x - d*n
m = abs(p-d)

if(k <= n):
  print(x-d*k)
else:
  ama = n-k
  if(ama%2 == 0):
    print(p)
  else:
    print(m)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

n = abs(X) // D

if K <= n:
    if X < 0:
        print(abs(X + D*K))
        sys.exit()
    else:
        print(abs(X - D*K))
        sys.exit()
else:
    if (K-n) % 2 == 0:
        if X < 0:
            print(abs(X + n * D))
            sys.exit()
        else:
            print(abs(X - n * D))
            sys.exit()
    else:
        if X < 0:
            print(abs(X + (n+1) * D))
            sys.exit()
        else:
            print(abs(X - (n+1) * D))
            sys.exit()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)

wari = x//d
amari = k-wari
p = x - (d*wari)

if wari >= k:
    print(x-(d*k))
else:
    if amari%2 == 0:
        print(p)
    else:
        print(d-p)

Submission status: Accepted

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

def solve():
    if X > K*D:
        return X - K*D
    elif -1*X > K*D:
        return -X - K*D
    elif X >= 0:
        first = X // D
        rem = K - first
        if rem % 2 == 0:
            return X % D
        else:
            return D - (X % D)
    else: # X < 0
        first = (-1*X) // D
        rem = K - first
        if rem % 2 == 0:
            return (-1 * X) % D
        else:
            return D - (-1 * X) % D

print(solve())

Submission status: Accepted

Solution code:
import math
import sys

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

x = abs(x)

v = math.floor(x / d)

if v > k:
  x = abs(x - k * d)
  print(x)
  sys.exit()

x -= v * d
k -= v
x = abs(x)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]


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


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

Submission status: Accepted

Solution code:
x, k, d = [list(map(int, s.split())) for s in open(0)][0]
x = abs(x)

ans = x
if x > k * d:
    ans = x - k * d
else:
    m = x//d
    if m%2 == k%2:
        ans = x - m * d
    else:
        ans = (m+1) * d - x
print(ans)

Submission status: Accepted

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if X < 0:
    X *= -1

n = X // D
m = X % D

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

Submission status: Accepted

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

if temp > k:
    ans = x - d*k

else:
    ref1 = x - temp*d #0かマイナス
    ref2 = x - (temp - 1)*d #プラス
    k = k - temp
    if k%2 == 0:
        ans = ref1
    else:
        ans = ref2

print(abs(ans))

Submission status: Accepted

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    x, k, d = map(int, input().split())

    x = abs(x)
    d = abs(d)
    ans = 0

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

division, mod = divmod(abs(X), D)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ini=input("").split(" ")
x=abs(int(ini[0]))
k=int(ini[1])
d=int(ini[2])
if(k%2==1):
    k-=1
    x=abs(x-d)
if((x-k*d)>=0):
    print(x-k*d)
else:
    c=int(x/(2*d))
    if(abs(x-c*2*d)<abs(x-c*2*d-2*d)):
        print(x-c*2*d)
    else:
        print(abs(x-c*2*d-2*d))

Submission status: Accepted

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

t = X // D

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

K -= t
X -= t * D

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)

M = X // D
X = X - D * min(M, K)
K = max(0, 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())

jump=abs(x//d)

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

Submission status: Accepted

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

X = abs(X)
fl = X // D
cl = fl + 1

if fl > K:
    ans = X - K * D
elif K % 2 == fl % 2:
    ans = X - fl * D
else:
    ans = cl * D - X

print(ans)

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if X <= 0:
    X *= -1

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

Submission status: Accepted

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

sho = X // D
yo = X % D

if sho > K:
    print(X - K * D)
elif sho == K:
    print(yo)
else:
    if (K - sho) % 2 == 0:
        print(yo)
    else:
        print(abs(yo - D))

Submission status: Accepted

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

x = abs(x)

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

Submission status: Accepted

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

ans=0
if X==0:
    if K%2==0:
        ans=0
    else:
        ans=D
else:
    cnt=abs(X)//D
    goal1=abs(X)%D
    goal2=abs(goal1-D)

    if cnt>K:
        ans=abs(X)-D*K
    else:
        amari=K-cnt
        if amari%2==0:
            ans=goal1
        else:
            ans=goal2

print(ans)

Submission status: Accepted

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

if abs(K*D) <= abs(X):
    ans = abs(abs(X) - abs(K*D))
    print(ans)
    exit()
    
tmp1 = X%D
tmp2 = abs(tmp1-D)

cnt = X//D

check = K - cnt
if check%2 == 0:
    print(tmp1)
else:
    print(tmp2)

Submission status: Accepted

Solution code:
x,k,d=map(int, input().split())
x=abs(x)
k=abs(k)
d=abs(d)
ans=[]
if x<k*d:
    div=x//d
    if (k-div)%2==0:
        ans.append(abs(x-d*(div)))
    else:
        ans.append(abs(x-d*(div+1)))
    #ans.append(abs(x-d*(div+1)))
else:
    ans.append(x-k*d)

print(min(ans))

Submission status: Accepted

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

    kyori = abs(x)
    kaisuu = kyori // d
    sa = k - kaisuu
    amari = kyori % d

    if kaisuu >= k:
        print(kyori- k*d)
    else:
        if sa % 2 == 0:
            print(amari)
        else:
            print(d - amari)




if __name__ == "__main__":
    main()

Submission status: Accepted

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

#X = 10
#K = 1
#D = 2


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

N1 = startN + 1
N2 = startN
N3 = startN - 1

if N1 < 0:
    N1 = 0
if N2 < 0:
    N2 = 0
if N3 < 0:
    N3 = 0
if N1 > K:
    N1 = K
if N2 > K:
    N2 = K
if N3 > K:
    N3 = K

def getScore(n):
    score = X + n * D - D * (K - n)
    if score < 0:
        score = score * -1
    return score

score1 = getScore(N1)
score2 = getScore(N2)
score3 = getScore(N3)

minScore = score1
if minScore > score2:
    minScore = score2
if minScore > score3:
    minScore = score3

print(minScore)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

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

X, K, D = getNM()
X = abs(X)
time = X // D

if K < time:
    print(abs(X - (D * K)))
    exit()
elif (K - time) % 2 == 0:
    print(abs(X - (D * time)))
    exit()
else:
    print(abs((D * (time + 1)) - X))
    exit()

Submission status: Accepted

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

X = abs(X)

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

near0_X = X % D
after_K = K - X//D

if after_K % 2 == 0:
    print(near0_X)
else:
    print(D-near0_X)

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
if X < 0:
    X *= -1
if D < 0:
    D *= -1
if abs(K*D) > X:
    if K % 2 == 0:
        mini = X//(2*D)
        bigger = X//(2*D)+1
        ans = min(abs(X-(2*D*mini)),abs(X-(2*D*bigger)))
    else:
        smaller_start = X-D
        bigger_start = X+D
        smaller_mini = smaller_start//(2*D)
        smaller_big = smaller_start//(2*D)+1
        bigger_mini = bigger_start//(2*D)
        bigger_big = bigger_start//(2*D)+1
        ans = min(abs(smaller_start-2*D*smaller_mini),abs(smaller_start-2*D*smaller_big),abs(bigger_start-2*D*bigger_mini),abs(bigger_start-2*D*bigger_mini))
else:
    ans = abs(X-K*D)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
X, K, D = map(int, input().split())
if abs(X) > K*D:
    res = abs(X) - K*D
else:
    q, r = divmod(abs(X), D)
    if (K - q)%2==0:
        res = r
    else:
        res = D - r
print(res)

Submission status: Accepted

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

x = abs(x)

ans = x - r*d

if ans >=0:
    print(ans)
else:
    print(min(abs(ans%(d*2)),abs(d*2-ans%(d*2))))

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math
import bisect

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

x,k,d = readInts()

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

Submission status: Accepted

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

#n = int(input())
#l = list(map(int, input().split()))

'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''

x,k,d=map(int, input().split())
aaa=abs(x//d)
if aaa>=k:
    print(min(abs(x-k*d),abs(x+k*d)))
    sys.exit()
else:
    if (k-aaa)%2==0:
        print(min(abs(x-(aaa)*d),abs(x+(aaa)*d)))
        sys.exit()
    else:
        print(min([abs(x-(aaa+1)*d),abs(x+(aaa+1)*d),abs(x-(aaa-1)*d),abs(x+(aaa-1)*d)]))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

low = X // D

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    s = input().split(' ')
    x = int(s[0])
    k = int(s[1])
    d = int(s[2])

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
if DK <= abs(X):
    ans = abs(X) - DK
else:
    q = X // D
    r = X % D
    if (K - q) % 2 == 0:
        ans = r
    else:
        ans = D - r
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
X = max(X,X*(-1))
plusmin = X%D
minusmin = -plusmin+D
totalmin = min(plusmin,minusmin)
if totalmin == plusmin:
    num = X//D
else:
    num = X//D+1
    
if K<num:
    answer = X-D*K
elif K==num+1:
    answer = max(plusmin,minusmin)
elif K%2==num%2:
    answer = totalmin
else :
    answer = max(plusmin,minusmin)
    
print(answer)

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:
    K -= N
    X = X%D
    ans = D-X if K&1 else X
print(ans)

Submission status: Accepted

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

if x >= 0:
    cnt = x // d
    if cnt >= k:
        pos = x - k * d
        print(pos)
    else:
        pos1 = x - cnt * d # cnt-kai
        nokori = k - cnt
        if nokori % 2 == 0:
            print(pos1)
        else:
            pos2 = abs(pos1 - d)
            print(pos2)
else:
    x = -x
    cnt = x // d
    if cnt >= k:
        pos = x - k * d
        print(pos)
    else:
        pos1 = x - cnt * d # cnt-kai
        nokori = k - cnt
        if nokori % 2 == 0:
            print(pos1)
        else:
            pos2 = abs(pos1 - d)
            print(pos2)

Submission status: Accepted

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

kd = k * d

if(x + kd < 0):
    print(0 - x - kd)
elif(x - kd > 0):
    print(x - kd)
else:
    if(x < 0):
        x *= -1
    if(k%2 == 1):
        x += d
    xd = x % (d*2)
    xd2 = d*2 - xd
    print(min(xd,xd2))

Submission status: Accepted

Solution code:
l=input().split()
x=int(l[0])
k=int(l[1])
d=int(l[2])
x=abs(x)
l=[]
l.append(abs(x-k*(d)))
z=x-(k*d)
if(z<0):
    rem=(z%(2*d))
    l.append(rem)
    l.append(2*d-rem)
print(min(l))

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
th = abs(X) / D
# print(th)
# 足りない場合
if th >= K:
    print(abs(X) - K * D)
# 足りる場合
else:
    curX = abs(X) - int(th) * D
    # print(curX)
    if (K - int(th)) % 2 == 0:
        print(curX)
    else:
        print(abs(curX - D))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
@author: Saurav Sihag
"""

rr = lambda: input().strip()
rri = lambda: int(rr())
# rri = lambda: int(stdin.readline())
rrm = lambda: [int(x) for x in rr().split()]
# stdout.write(str()+'\n')

from sys import stdin, stdout, setrecursionlimit

def sol():
    x, k, d=rrm()
    x=abs(x)
    a=x//d
    if a>k:
        print(abs(x-k*d))
        return
    rk=k-a
    xa=x-a*d
    # print(x, k, d, a, rk, xa)
    if rk%2==1:
        xa-=d
    print(abs(xa))
    return

    
# sol()
def main():
    # T = rri()
    # for t in range(1, T + 1):
        ans=sol()
        # print("Case #{}: {}".format(t, ans))

main()