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: Wrong Answer

Solution code:
"""MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWO:,'..'xXWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .:kOo.,kNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .lNWWo..lXMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MNOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMMMMWKxoloxKWNc  .:xxd;  ,dxxxkXMXkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMM
MKc''''''''''''''''.  ..'''''.   .''''.  .''MMMWx..,:. .xXo''.  .''.  ..'';kMk;'''''.  .'.''.  .''''''.  ..''.   .''MM
MWNXNNNNNNNNNNNNNNNO' ;KNNNNNKo. cKNNNd. lXNMMMK; ,0Wx. :NWXXl .dNNO' ;KNNNWMWNNNNNXc .kNNNNx. cXNNNNNO' ;KNN0; 'ONNMM
MMMMMMMMMMMMMMMMMWM0' :NMMMMWMX: .OMWWx. oWMMMMWx. ';. .dNMWWo .xMM0' :NWWMMMMMMMMWWl .OMMMWk. lWWMMWM0' ;XMMX: ,KMMMM
MMMMMMWMWkccccccccc;. :NMWNNNKd. :XMMWx. oWMMMWKo. .',. .,ldx; .dMM0' :NMMMMMMMNNWMWl .OMMMWk. lWMMMMM0' ;XMMX: ,0MMMM
MMWXKNWMWxcc,. .;cc;. :XMk,''..'lKWMMWx. oWMNx:. .lONNKxl.     .xMM0' :NMMMMMM0:,oKO' ,KMMMMk. lWWNkoc,. .;::;. ,KMMMM
MMXc.lXMMWWWNd..xWM0' :NM0;  ;kXWMMMWNl  oWMK:.;xXWWMWXx:. ,c' .dMM0' :NMMMMMMKc. ...;kWMMMMk. lWWc .:;. .clc:. ,KMMMM
MWWx. lXMWMMMK, lWM0' :NMMNx'.,ldxxoc'   oWMWNKNWWWN0o' .:kNWo .dMM0' :NMMMMMMMNO;  ,OWWWMMMk. lWM' lXx. lNMMX: ,KMMMM
MMMWk'.,xKNX0c..kWM0' :NMMMMNkl;,'',:l:  oWMMMMMWMWO' 'l0WMWWo .xMM0' :NMMMMMMMMMNd. .oXWMMMk. lWMd.....;0WMMX: ,KMMMM
MMMMWKo'...'..:kWMM0' :NMMMMMMMWWNNWWWx. oWMMMMMMMMWOxXWMWMWWo .xMM0' :NMMMMMMMMMMMKl. ,kNMMk. lWMWKkxx0NMWMMX: ,KMMMM
MMMMMMMN0kxk0XNWMMWKl;xNMMMMMMMMMMMMWM0c;kWMMMMMMMMMMMWWMMMMWk;:OMMXl,dNMMMMMMMMMMMMWO; .dNM0c;kWWMMMMMMMMMMMNd,oXMMMM
MMMMMMMMWWMWMMMMMMMMMWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWWMMWWMMWMMMMMMMMMMMMMMNkxKWMMWWMMMMMMMMMMMMMMWMMWMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"""


#				 Author: Udit "luctivud" Gupta @ https://www.linkedin.com/in/udit-gupta-1b7863135/					 #


import math;   		from collections import *
import sys;   		from functools import reduce
import time;   		from itertools import groupby

# sys.setrecursionlimit(10**6)

def input()         : return sys.stdin.readline()
def get_ints()      : return map(int, input().strip().split())
def get_list()      : return list(get_ints())
def get_string()    : return list(input().strip().split())
def printxsp(*args) : return print(*args, end="")
def printsp(*args)  : return print(*args, end=" ")


DIRECTIONS = [(+0, +1), (+0, -1), (+1, +0), (+1, -1)] 
NEIGHBOURS = [(-1, -1), (-1, +0), (-1, +1), (+0, -1),\
              (+1, +1), (+1, +0), (+1, -1), (+0, +1)]



# MAIN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# for _test_ in range(int(input())): 

x, k, d = get_ints()
el_facto = min(k, (x + d-1) // d)
moves = []
if k & 1:
	if el_facto & 1:
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0) :
			moves.append(el_facto)
	else:
		el_facto -= 1
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto < k) :
			moves.append(el_facto)
else:
	if not (el_facto & 1):
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto < k) :
			moves.append(el_facto)
	else:
		el_facto -= 1
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto < k) :
			moves.append(el_facto)
ans = float('inf')
x = abs(x)
# print(moves)
for i in moves:
	ans = min(ans, abs(x - i * d))
print(ans)

Submission status: Wrong Answer

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

q = abs(x) // d

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

mod = abs(x) % d


if q % 2 == 0:
    print(mod)
else:
    print(abs(d) - mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X = max(X, -X)
if X-(K*D) >0:
  print(X-(K*D))
else:
  step, place, nokori = int(X/K), X-(int(X/K)*K), D-int(X/K)
  if nokori%2 ==0:
    print(place)
  else:
    print(-(place-K))

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X,K,D=(int(x) for x in input().rstrip('\n').split())
if X>0 and X-K*D>=0:
  print(X-K*D)
elif X<0 and X+K*D<=0:
  print(X+K*D)
else:
  now = X
  if X>0:
    for i in range(1,K+1):
      next = now-D
      if next<0:
        if (K-i)%2==0:
          print((-1)*next)
          break
        else:
          print(now)
          break
      else:
        now = next
  elif X<0:
    for i in range(1,K+1):
      next = now+D
      if next>0:
        if (K-i)%2==0:
          print(next)
          break
        else:
          print((-1)*now)
          break
      else:
        now = next
  else:
    if K%2==0:
      print(0)
    else:
      print(D)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
x = i[0]
k = i[1]
d = i[2]
c=x-d*k
if c>=0:
    print(c)
else:
    p=-c//(2*d)
    ans = 10000000000000000000000000000000000000000
    for i in range(-2,2):
        if 0<=p+i and p+i<=k:
            ans=min(ans,abs(c+2*d*(p+i)))
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
from collections import defaultdict,deque
from itertools import permutations
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
ips=lambda:input().split()

"""========main code==============="""

t=1
#t=ii()
for _ in range(t):
    a,b,c=ml()
    rem=a//c
    
    if(b>=rem and b%2==rem%2):
        ans=abs(a)-c*rem
        print(ans)
    elif(b>=rem+1 and b%2!=rem%2):    
        ans=abs(abs(a)-c*(rem+1))
        print(ans)
    else:
        print(abs(a)-c*b)

Submission status: Wrong Answer

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

k = X // D
if abs(X - D * k) < abs(X - D * (k + 1)):
    best_k = k
else:
    best_k = k + 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = abs(X) // D
if d >= K:
  if X >= 0:
    ans = X - K * D
  else:
    ans = X + K * D
  print(ans)
  exit()

if X >= 0:
  ans = X - d * D
else:
  ans = X + d * D

n = K - d
if n % 2 == 1:
  if ans >= 0:
    ans -= D
  else:
    ans += D

print(abs(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def main():

    x,k,d = tuple([int(t)for t in input().split()])

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

    return res

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

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

    cout = main(kwargs)
    print(cout)

Submission status: Wrong Answer

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

p = x

for i in range(k):
  if abs(p) > abs(x - d):
    p = x - d
  else:
    p = x + d

  if abs(p) <= x:
    k = k - 1 - i
    break

if k % 2 == 0:
  print(abs(p))
elif p < 0:
  print(abs(p + d))
else:
  print(abs(p - d))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

X, K, D = LI()

"""
X 
K回数 D移動
"""
if X >= 0:
    tmp = X - D * K
    if tmp <= 0:
        if (X - X % D) // D % 2 == K % 2:
            ans = X % D
        else:
            ans = abs(X % D - D)
    else:
        ans = tmp
else:
    tmp = X + D * K

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

print(ans)

Submission status: Wrong Answer

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

for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D
    if X >= 0:
        if X in s: break
        s.add(X)
    else:
        if -X in s: break
        s.add(-X)

print(min(s))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int, input().split())
x=abs(x)
if (x/d)>=k:
    print((x//d-k)*d)
    #print('here')
else:
    t=x//d
    rem=k-t
    r=x-t*d
    l=(t+1)*d-x
    if rem%2==0:
        print(r)
    else:
        print(l)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
"""
if k%2:
    min(d*k-x)
else:
    print(0)
"""
s=lambda i:abs(d*i-x)
l=0
r=k+1
while r-l>1:
    m=l+r>>1
    if s(m)<s(m+1):
        r=m
    else:
        l=m
m=10**18
for i in range(max(1,r-3),min(k,r+3)+1):
    m=min(m,s(i+(k-i)%2))
print(m)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	X,K,D = inputMap()
	X = abs(X)

	koko = abs(X-D)
	if koko == 0:
		if K % 2 == 0:
			print(abs(D))
			sys.exit()
		else:
			print(0)
			sys.exit()


	ima = abs(X-0)

	if ima == 0:
		if K % 2 == 0:
			print(0)
			sys.exit()
		else:
			print(abs(D))
			sys.exit()

	imaima = abs(X-K*D)

	if imaima == 0:
		print(0)
		sys.exit()

	hituyou = ima // D
	saidai = ima % D

	if hituyou > K:
		#print("dd")
		ido = K * D
		ans = ima - ido
		print(ans)
		sys.exit()

	else:
		tmp = K - hituyou
		if tmp % 2 == 0:
			#print("aa")
			print(saidai)
			sys.exit()
		else:
			#print("bb")
			print(saidai + D)
			sys.exit()




#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

if x == d:
    if k % 2 == 1:
        print(0)
    else:
        print(d)
elif x > d * k:
    print(abs(x-d*k))
else:
    if k % 2 == 1:
        print(abs(x - math.ceil(x/3) * d))
    else:
        print(abs(x - (math.ceil(x/3) - 1) * d))

Submission status: Wrong Answer

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

X = abs(X)
a = X / D
b = round(a)

c = K - b

if c < 0:
    print(X-(K*D))
elif c % 2 != 0:
    if a > b:
        b = b + 1
    else:
        b = b - 1
    d = abs(X - (D*b))
    print(d)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

m = X % D
l = X // D

if D - m < m:
    m = D - m
    l += 1

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
a=k*d
b=k//2
c=10**17
for i in range(b):
  c=min(c,abs(c-b))
  b-=2*d
print(c)

Submission status: Wrong Answer

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

if x > 0:
    d *= -1
    for i in range(k):
        x += d
        if x < 0:
            d *= -1
    print(x)
    exit()

if x < 0:
    for i in range(k):
        x += d
        if x > 0:
            d *= -1
    print(x)
    exit()

Submission status: Wrong Answer

Solution code:
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    #n = int(stdin.readline().rstrip())
    x,k,d = map(int, stdin.readline().rstrip().split())
    #L = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if abs(x) > k*d:
        print(abs(x) - k*d)
        exit()
    else:
        a = x // d
        if abs(abs(x) - d*a) > abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 0:
            print(abs(abs(x) - d*(a+1)))
            exit()
        elif abs(abs(x) - d*a) > abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 1:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) < abs(abs(x) - d*(a+1)) and (k-a)%2 == 0:
            print(abs(abs(x) - d*a))
            exit()
        else:
            print(abs(abs(x) - d*(a+1)))
            exit()




if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split()) 

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#Problem C

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

T_min = X//D
ans = 0

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


if T_min%2 ==0:
    ans = abs(X) - (T_min*D)
else:
    ans1 = abs(abs(X) - (T_min*D) + D)
    ans2 = abs(abs(X) - (T_min*D) - D)
    ans = min(ans1,ans2)
    
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ans = 0
if k * d < abs(x):
    if x >= 0:
        ans = x - k*d
    else:
        ans = x + k*d
    print(ans)
    exit()
    
xd = x // (2*d)
if k % 2:
    tmp = x - 2*d*xd
    l = [abs(tmp-d), abs(tmp+d), abs(tmp+3*d)]
    ans = min(l)
else:
    tmp = x - 2*d*xd
    l = [abs(tmp-2*d), abs(tmp), abs(tmp+2*d)]
    ans = min(l)
print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
sho =  x//d
amari = x%d
tmp = []
tmp.append([sho,abs(amari)])
tmp.append([sho+1,abs(amari-d)])
#print(tmp)
#print(sho,amari,amari-d)
if k>=sho:
    if (k-sho)%2==0:
        print(tmp[0][1])
    else:
        print(tmp[1][1])
else:
    print(x-k*d)

Submission status: Wrong Answer

Solution code:
# Date [ 2020-08-15 21:13:42 ]
# Problem [ c.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 6 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    x, k, d = read_ints()
    x = abs(x)
    cnt = x // d
    if cnt < k:
        x -= cnt * d
    else:
        cnt = k
        x -= cnt * d
    
    if (k - cnt) % 2 == 0:
        print(x)
    elif x == 0:
        print(d)
    else:
        print(min(abs(x - d), x)) 

if __name__ ==  '__main__':
    Main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x-k*d>=0 and x>0:print(x-k*d)
elif x+k*d<0 and x<0:print(abs(x+k*d))
else:
    num1,num2=x%d,d-x%d
    if num1==num2:print(num1)
    else:
        A,B=(x//d)%2,k%2
        if A==0 and B==0:
            print(num1)
        elif A==1 and B==1:
            print(num2)
        elif A==1 and B==0:
            print(num2)
        else:print(num1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x > k*d or -x > k*d:
    print(abs(abs(x) -k*d))
else:
    num = x // d
    rest = k - num
    if rest%2 == 1:
        print(abs(abs(x)%d -d))
    else:
        print(abs(abs(x)%d))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
def find_abs(x, k, d):
    if x == k == d:
        return x
    for i in range(k):
        if x > 0:
            x = x - d
        elif x < 0:
            x = x + d
    return abs(x)


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

Submission status: Wrong Answer

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

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

if abs(x)>=k*d:
    if x<0:
        print((abs(x)-k*d)*(-1))
        exit()
    else:
        print(x-k*d)
        exit()


xxx=abs(x)//d

yyy=abs(x)%d

zzz=k-xxx

if x>=0:
    if zzz%2==0:
        print(abs(yyy))
    else:
        print(abs(yyy-d))

else:
    if zzz%2==0:
        print(abs((-1)*yyy))

    else:
        print(abs((-1)*yyy+d))

Submission status: Wrong Answer

Solution code:
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))

def f(d, x, b):
    return abs(2 * d * x - b)

def main():
    x, k, d = ril()
    b = - x + k * d
    if b <= 0:
        ans = -b
    elif b % (2 * d) == 0:
        ans = 0
    else:
        x1 = b // (2 * d)
        x2 = x1 + 1
        ans = min(f(d, x1, b), f(d, x2, b))
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#  a = (kd - x)/2d


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

pos = (k - (x//d))//2

if pos < 0:
    pos = 0

ans = x + pos * d - (k-pos) * d

print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

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

if K*D <= abs(X):
    if 0 <= X:
        print(X - K*D)
    else:
        print(abs(X + K*D))
else:
    dist = abs(X)
    a = dist // D
    # 残りの移動回数
    r = K - a
    if 0 <= X:
        pos1 = X - a * D
        pos2 = pos1 - D
    else:
        pos1 = X + a * D
        pos2 = X + D

    if r % 2 == 1:
        print(abs(pos2))
    else:
        print(abs(pos1))

Submission status: Wrong Answer

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

a=x//d

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

Submission status: Wrong Answer

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

min=x

smin,smax=x-k*d,x+k*d
if abs(smin)>x:
    smin=-x
if abs(smax)>x:
    smax=x
for t in range(smin,smax,d*2):
    tt=abs(t)
    if tt<min:
        min=tt
print(min)

Submission status: Wrong Answer

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

if x<0:
    x*=(-1)

while k>0:
    if x>=0:
        x-=d
        k-=1
    else:
        break

if k%2==0 and k!=0:
    print((-1)*x)
elif k==0:
    print(x)
else:
    print(x+d)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if(x==k and k==d and d==x):
    print(k)
else:
    flag=0
    for i in range(k):
        if(x-d<1):
            print(x)
            flag=1
            break
        else:
            x-=d
    if(flag==0):
        print(x)

Submission status: Wrong Answer

Solution code:
X, K, D = (int(x) for x in input().split())
count = X // D
if K < count:
    count = K
X = abs(X) - D*count
rest = K - count
if rest%2 == 1:
    if abs(X+D) < abs(X-D):
        X += D
    else:
        X -= D
print(abs(X))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
if X>0 and abs(X)>abs(K*D):
  print(X-K*D)
  exit()
elif X<0 and abs(X)>abs(K*D):
  print(abs(X-K*D))
  exit()
d,r=divmod(abs(X),D)
if (K-d)%2==0:
  print(abs(r))
else:
  print(abs(D-r))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(x)//d < k:
  #最小までいく。
  if (k-abs(x)//d)%2==0:
    if x>=0:
      print(abs(x-(x//d)*d))
    else:
      print(abs(x+(x//d)*d))
  else:
    if x>=0:
      print(abs(x-(x//d)*d-d))
    else:
      print(abs(x+(x//d)*d+d))
else:
  if x>=0:
    print(abs(x-k*d))
  else:
    print(abs(x+k*d))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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