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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
X, K, D = (int(x) for x in input().split())
min = abs(X)
count = 0
for _ in range(K):
    preX = X
    if abs(X+D) < abs(X-D):
        preX += D
    else:
        preX -= D

    if abs(X) <= abs(preX):
        break
    else:
        X = preX
        count += 1
rest = K - count
if rest%2 == 1:
    if abs(X+D) < abs(X-D):
        X += D
    else:
        X -= D
print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
s = abs(x-(1*d*0)+(1*d*(k-0)))
for i in range(k+1):
    s =min(abs(x-(1*d*i)+(1*d*(k-i))),s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
X,K,D=map(int,input().split())
for i in range(1,K+1):
  if X>=0 :
    X-=D
  else :
    X+=D
print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
X, K, D = map(int, input().split())
for i in range(K):
    X = min(abs(X+D), abs(X-D))
print(X)

Submission status: Time Limit Exceeded

Solution code:
import math
X, K, D = map(int,input().split())
for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D
print(abs(X))

Submission status: Time Limit Exceeded

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

for i in range(K):
    if (abs(X - D) < abs(X + D)):
        X = X - D
    else:
        X = X + D

    if (abs(X) <= D):
        if ((K - i - 1) % 2 == 0):
            break
        else:
            if (abs(X - D) < abs(X + D)):
                X = X - D
            else:
                X = X + D
            break


print(abs(X))

Submission status: Time Limit Exceeded

Solution code:
rule=input().split()
X=int(rule[0])
K=int(rule[1])
D=int(rule[2])
ans_li=[]
import math
for i in range(K+1):
    ans_pre=X+D*i-D*(K-i)
    ans_li.append(int(math.fabs(ans_pre)))
print(min(ans_li))

Submission status: Time Limit Exceeded

Solution code:
x,k,d=map(int,input().split())
n=x
for i in range(k):
    n1=n-d
    n2=n+d
    n=min(abs(n1),abs(n2))
print(n)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
x,k,d=map(int,input().split())
for i in range(k):
    v1 = x-d
    v2 = x+d
    x=min(abs(v1),v2)
print(abs(x))

Submission status: Accepted

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


def sign(x):
    return 1 if x >= 0 else -1


if abs(X) >= D * K:
    ans = X - sign(X) * D * K
else:
    l = abs(X) // D
    mod = abs(X) % D

    residual = K - l

    if residual % 2 == 0:
        ans = mod
    else:
        ans = mod - sign(mod) * D

print(abs(ans))

Submission status: Accepted

Solution code:
def A():
    s = input()
    if s == 'SSS': print(0)
    elif s=='RRR': print(3)
    elif s=='RRS' or s=='SRR': print(2)
    else: print(1)

def B():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    for i in range(n):
        for j in range(i+1, n):
            if a[i] == a[j]: continue
            for k in range(j+1, n):
                if a[i]==a[k] or a[j] == a[k]: continue
                s = a[i] + a[j] + a[k]
                if s > 2 * max(a[i], a[j], a[k]):
                    cnt += 1
    print(cnt)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = map(int, input().split())
lists = []
x = abs(x)
 
step = int(x/d) 
 
if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = min(abs(x % d - d), abs(x % d + d))
 
print(answer)

Submission status: Accepted

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

step = int(x/d) 

if step >= k:
  answer = x - k * d
elif step < k:
  k_left = k - int(step)
  if k_left % 2 == 0:
    answer = x % d
  if k_left % 2 == 1:
    answer = abs(x - step * d - d)

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

if K > step:
  extra = - D * ((K - step) % 2)
else:
  extra = 0

print(abs(absX - step * D + extra))

Submission status: Accepted

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

Submission status: Accepted

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

N = abs(X) // D
O = abs(X) - N * D

if O <= (D - O):
    pass
else:
    N += 1
    O = D - O
    
if N >= K:
    print(abs(X) - K * D)
else:
    if (K - N) % 2 == 0:
        print (O)
    else:
        print(D - O)

Submission status: Accepted

Solution code:
x, k, d=map(int,input().split())
x=abs(x)
#k回すべて戻っても0に達せない場合  k-x//d
if x//d>=k:
  print(x-k*d)
elif (k-x//d)%2==0:
  print(x%d)
else:
  print(abs(x-x//d*d-d))

Submission status: Accepted

Solution code:
def main():
    x, k, d = map(int,input().split())
    abs_x = abs(x)
    times = abs_x // d
    if(times > k):
        print(abs_x - k * d)
    else:
        if((k - times) % 2 == 0):
            print(abs_x - times * d)
        else:
            print(abs(abs_x - (times + 1) * d))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

    if abs(x) % d == 0:
        exceed_num = (abs(x) // d)
    else:
        exceed_num = (abs(x) // d) + 1


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



if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    x, k, d = map(int, input().split())
    cycle = abs(x) // d     # cycle:距離ｘを距離ｄ移動で詰めるための必要移動回数。
    if cycle >= k:          # xまでの移動でｋが尽きる場合。
        print(abs(abs(x) - k * d))
    else:                   # xまで移動してｋが余る場合。
        k -= cycle          # ｋ＝余る移動回数、にする
        if k&1:             # kが奇数なら「ｘを飛び越えた地点」に留まるのが最適解。
            print(abs(abs(x) - (cycle + 1) * d))
        else:
            print(abs(abs(x) - (cycle) * d))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#今Xにいる、K回 D移動する
[X, K, D] = [int(i) for i in input().split()]
#正負で答え変化しないので、正に変える
X = abs(X)
if X - K*D > 0:
#原点に到達しないとき
    print(X-K*D)
#原点に到達するとき
else:
    #往復始める正の位置を求める
    time = int(X/D)
    start = X - D * time
    #残り移動回数を計算
    time_re = K - time
    #残り回数の偶奇で場合分け
    if time_re % 2 == 0:
        print(start)
    else:
        print(abs(start-D))

Submission status: Accepted

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

t = X // D
if abs(t) >= K:
  if X > 0:
  	print(abs(X-(D*K)))
  elif X < 0:
    print(abs(X+(D*K)))
  exit(0)

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

Submission status: Accepted

Solution code:
import math
import collections
import itertools

def YesNo(Bool):
    if(Bool):
        print("Yes")
    else:
        print("No")
    return

def resolve():

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

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

    print(ans)
resolve()

Submission status: Accepted

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

cur  = abs(x)
rem = k

cnt = min(cur // d, k)
cur = cur - d * cnt
rem = rem - cnt

if rem  > 0:
        if rem % 2 == 1:
                cur = cur - d
                
ans = abs(cur)
print(ans)

Submission status: Accepted

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

x = abs(x)
straight = min(k, x//d)
k -= straight
x -= straight*d

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if abs(X) > D * K:
    abso = abs(X) - D * K
    print(abso)
    exit()
div = abs(X)//D
mod = abs(X)%D #+側の最小値
if (K - div)%2 == 0: #K-div:+側での絶対値の最小値までたどり着く回数
    print(mod)
else:
    print(D-mod)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 175 C
X, K, D = list(map(int, input().split()))
div = abs(X)//D
mod = abs(X)%D
if abs(X) > K*D:
    print(abs(X) - K*D)
elif (K-div)%2 == 0:
    print(mod)
else:
    print(D-mod)

Submission status: Accepted

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

if X - K * D >= 0:
    ans = abs(X - K * D)
elif X + K * D <= 0:
    ans = abs(X + K * D)
else:
    n = int(abs(X - K * D) / (2 * D))
    ans = min(abs(X - K * D + n * 2 * D), abs(X - K * D + (n + 1) * 2 * D))
    
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


X, K, D = MI()

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

Submission status: Accepted

Solution code:
import sys

# x = abs(int(input()))
# k = int(input())
# d = int(input())

array = list(map(int, input().strip().split()))
x = abs(array[0])
k = array[1]
d = array[2]
if (d == 0):
    amari = abs(x)

else:
    move_cnt = int(x / d)
    if (move_cnt <= k):
        k -= move_cnt
        amari = x - move_cnt * d

    else:
        amari = x - k * d
        k = 0

    if (k > 0):
        if (k % 2 == 1):
            if (amari > 0):
                amari -= d
            else:
                amari += d

print(abs(amari))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if X<0:
    X=-X

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

Submission status: Accepted

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

x = abs(x)
mincount = x // d


if mincount < k:
  k = (k-mincount)%2
  result = x - d*mincount -d*k
else:
  result = x - k*d
print(abs(result))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys

INF = float('inf')


def solve(X: int, K: int, D: int):
    return abs(X) - K * D if abs(X) > K * D else X % D if (K - X//D) % 2 == 0 else abs(X % D - D)


def main():

    sys.setrecursionlimit(10 ** 6)

    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
    print(f'{solve(X, K, D)}')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ax = abs(x)

kd = k*d

if(ax>kd):
    print(ax-kd)
else:
    num = ax//d
    if((num%2)==(k%2)):
        print(ax-(d*num))
    else:
        if(num==k):
            print(ax-d*num)
        else:
            print(abs(ax-d*num-d))

Submission status: Accepted

Solution code:
input_line = input().split()
x = int(input_line[0])
k = int(input_line[1])
d = int(input_line[2])

x = abs(x)
y = int(x/d)

if y < k:
  x -= y * d
  if (k-y) % 2 == 1:
    x -= d

else:
  x -= k*d
    
print(abs(x))

Submission status: Accepted

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

Submission status: Accepted

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

if x < 0:
    x = abs(x)

if x - maxi >= 0:
    res = x - maxi
else:
    p = x % d
    m = p - d
    rest = k - (x // d)
    if rest % 2 == 1:
        res = m
    else:
        res = p

print(abs(res))

Submission status: Accepted

Solution code:
# author:  Taichicchi
# created: 15.09.2020 21:22:26

import sys

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

X = abs(X)

if X >= K * D:
    print(X - (K * D))
else:
    k = K - X // D
    x = X - D * (X // 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)
if X - K * D >= 0:
    print(X - K * D)
    exit()
else:
    n = X // D
    x = X - n * D
    if (K - n) % 2 == 0:
        print(x)
    else:
        print(min(x + D, D - x))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8
x,k,d = map(int,input().split())
x = abs(x)
straight = min(k,x//d)
k -= straight
x -= straight*d
if k%2==0:
    print(int(x))
else:
    print(int(d-x))

Submission status: Accepted

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

if x < 0:
    x = -x

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

Submission status: Accepted

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

if x < 0:
    x *= -1
    
m = x // d

if k <= m:
    print(x - k*d)
    
else:
    x %= d
    y = abs(x-d)
    
    print([x,y][(k-m)%2])

Submission status: Accepted

Solution code:
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

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

count = min(k,x//d)
k -= count
x -= d*count

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

Submission status: Accepted

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


X = abs(X)
a = K*D
if X >= a:
    result = X-a
else:
    b = X//D
    c = (X-b*D)
    d = abs(X-(b+1)*D)
    if c < d:
        if b%2==K%2:
            result = c
        else:
            result = d
    else:
        if (b+1)%2==K%2:
            result=d
        else:
            result=c

print(result)

Submission status: Accepted

Solution code:
#C問題
import numpy as np
X,K,D = map(int, input().split())
ans = 0
if X >= 0 :
    if int(X/D)<=K:
        if int(X/D)%2==K%2:
            ans = X%D
        else:
            ans = D-X%D
    else:
        ans = abs(X-K*D)       
if X < 0 :
    if abs(int(X/D))<=K:
        if abs(int(X/D))%2==K%2:
            ans = abs(X)%D
        else:
            ans = D-abs(X)%D
    else:
        ans = abs(X+K*D)          
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = [int(s) for s in input().split()]
x = abs(x)
q, m = divmod(x, d)
if q >= k:
    x -= k * d
else:
    x = m
    if (k - q) & 1:
        x = d - x
print(x)

Submission status: Accepted

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

cur = abs(x)  # 座標を正にしてしまいます
rem = k  # 残りの移動回数です

cnt = min(cur // d, k)  # 0に向かって移動する回数です
cur -= d * cnt
rem -= cnt

# 0 % 2 = 0なので、残り回数の場合分けをする必要はないのですが、一応
if rem > 0:
    if rem % 2 == 1:
        cur = cur - d

ans = abs(cur)  # 絶対値を出力することに気をつけてください
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

XX = abs(X)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a=input().split(" ")
b=[x,k,d]= [int(a[0]),int(a[1]),int(a[2])]  
ans = 0
if x - k*d > 0:
  ans = x - k*d
elif x + k*d < 0:
  ans = - k*d -x
else:
  if k%2==0:
    ans= x%(2*d)
    if d<x%(2*d):
      ans= 2*d-x%(2*d)
  elif k%2==1:
    ans= (x+d)%(2*d)
    if d<(x+d)%(2*d):
      ans= 2*d-(x+d)%(2*d)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = -x if x <= 0 else x

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

Submission status: Accepted

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

Submission status: Accepted

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

def sign(x):
    return 1 if x >= 0 else -1


ans = abs(X) - K * D

if abs(X) - K * D < 0:
    divs = abs(X) // D
    mods = abs(X) % D
    nums = K - divs

    ans = abs(mods - (nums%2) * D)

print(ans)


# ans = min([abs(X+D*i) for i in range(-K, K+1, 2)])
# 
# print(ans)

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
if X > 0:
    if K*D <= X:
        ans = X - K*D
    else:
        n = X // D
        if X % D != 0:
            n += 1
        if (K - n) % 2 == 0:
            ans = abs(X-D*n)
        else:
            ans = abs(X-D*(n-1))
if X < 0:
    if K*D + X <= 0:
        ans = abs(K*D+X)
    else:
        n = -X // D
        if -X % D != 0:
            n += 1
        if (K-n) % 2 == 0:
            ans = abs(X+n*D)
        else:
            ans = abs(X+(n-1)*D)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if X < 0:
    X = -X

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

Submission status: Accepted

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

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

Submission status: Accepted

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




if __name__ == '__main__':
    main()

Submission status: Accepted

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

if k*d<=x:
  print(x-k*d)
else:
  e = math.floor(x/d)
  k -= e
  x = x-e*d
  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) >= d * k:
    print(abs(x) - d * k)
else:
    if (k - abs(x)//d) % 2 == 0:
        print(abs(x) - d * (abs(x)//d))
    else:
        print(abs(abs(x) - d * (abs(x)//d + 1)))

Submission status: Accepted

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

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

if k%2==1:
    x=x-d

kaisu=int(x/(2*d))
ans=abs(x-kaisu*d*2)
if ans >=d:
    ans=2*d-ans

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X,K,D = list(map(int,input().split()))
max_number = X + K*D
min_number = X - K*D
if min_number >= 0:
    ans = min_number
elif max_number <= 0:
    ans = -max_number
else:
    q = max_number // (2*D)
    ans1 = max_number - 2*q*D
    ans2 = max_number - 2*(q+1)*D
    if ans1 > -ans2:
        ans = -ans2
    else:
        ans = ans1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

    # Xは0以上と仮定する
    X = X if X >= 0 else -X

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

    # 以下は X < K * D
    K -= X // D
    X %= D

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

main()

Submission status: Accepted

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

if x >= k*d:
    ans = x - k*d
elif k&1:
    ans = min((x-d)%(2*d), 2*d - (x-d)%(2*d) )
else:
    ans = min(x%(2*d), 2*d - x%(2*d))

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)
count = X // D

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

print(answer)

Submission status: Accepted

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

X = abs(X)

straight = min(K, X // D)

K -= straight;
X -= straight * D;

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if x > k*d:
    print(x-k*d)
else:
    p = x//d
    c = k-p
    if (c%2 == 0):
        print(abs(x-p*d))
    else:
        print(abs(d-abs(x-p*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:
    p = x//d
    c = k-p
    if p == 0 or p==1:
          for i in range(1,k+1):
            p2 = x-i*d
            c = k-i
            if p2 <= 0:
                if c%2 != 0:
                    print(abs(d+p2))
                    break
                elif c%2 == 0:
                    print(abs(p2))
                    break
    elif (c%2 == 0):
        print(abs(x-p*d))
    else:
        print(abs(d-abs(x-p*d)))

Submission status: Accepted

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

m = x // d
p = k - m if m <= k else 0

if p == 0:
  print(x - (k*d))
else:
  x -= m * d
  if p & 1:
    print(abs(x - d))
  else:
    print(x)

Submission status: Accepted

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