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:
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    x, d, k = map(int, input().split())

    sho, amari = divmod(abs(x), k)

    if sho > k:
        if x < 0:
            ans = x + (k * d)
        else:
            ans = x - (k * d)
        print(abs(ans))
        return

    if x < 0:
        x = -amari
    else:
        x = amari

    d = sho

    if d % 2 == 1:
        if x < 0:
            ans = x + k
        else:
            ans = x - k
    else:
        ans = x

    print(abs(ans))


# テストクラス


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """6 2 4"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """7 4 3"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """10 1 2"""
        output = """8"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
#import functools
#import operator
from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with
#from itertools import permutations as perm
#import collections as C #most_common
import math
#import sympy

#N = int(input())
X,K,D= map(int,input().split())
#L = list(map(int,input().split()))
#S = str(input())
#T = str(input())
c,C=0,0


if X-D*K>=0:
    print(X-D*K)
else:
    x = math.ceil(X/D)

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

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

Submission status: Wrong Answer

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

if X < 0:
    X = -X
else:
    cnt = 0
    for i in range(K):
        X -= D
        cnt += 1
        if X <= 0:
            break
        else:
            continue
    
    remain = K-cnt
    
    if remain == 0:
        print(abs(X))
    else:
        if remain % 2 == 0:
            print(abs(X))
        else:
            print(abs(X+D))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
s=set()
while True:
    if x-d<=0:
        print(x)
        break
    else:
        if k:
            x-=d
            k-=1
        else:
            print(x)
            break

Submission status: Wrong Answer

Solution code:
def main():
    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 < 0:
            a = X % D
            b = a - D
            k = X // D
            if (K - k) % 2 == 0:
                ans = abs(a)
            else:
                ans = min(abs(b), abs(a + D))
        else:
            ans = X - K*D
    else:
        if X + K*D > 0:
            _X = -X
            a = _X % D
            b = -a + D
            k = _X // D
            if (K - k) % 2 == 0:
                ans = abs(a)
            else:
                ans = min(abs(b), abs(-a - D))
        else:
            ans = X + K*D

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
import math
a1, a2 = min(K, math.ceil(X / D)), min(K, math.floor(X / D))
if abs(X - (a1 * D)) <= abs(X - (a2 * D)):
	K -= a1
	X -= (a1 * D)
else:
	K -= a2
	X -= (a2 * D)
if K % 2 == 0:
	print(abs(X))
else:
	print(abs(abs(X) - D))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
n=max(round(k/2-x/(2*d)),0)
print(min(abs(x+(2*n-k)*d),abs((k-2*n)*d-x)))

Submission status: Wrong Answer

Solution code:
n, k, d = input().split()

n = int(n)
k = int(k)
d = int(d)

# print(n, k, d)

x = (k*d - n)//(2*d)

# debug1(x);
x = max(0, x-100)
y = min(k, x+100)

ans = 1e15;
for i in range (x, y+1):
    ans = min(ans, abs(n+i*d - (k-i)*d));

print(ans)

Submission status: Wrong Answer

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

g = abs(abs(X) % D)

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

Submission status: Wrong Answer

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


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

if X>=0:

    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))
    
else:
    
    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 k * d <= x:
    ans = x - (k*d)
elif k % 2 == 0:
    t = x // d
    if t % 2 == 1:
        t += 1
    ans = abs(x - t * d)
elif k % 2 == 1:
    t = x // d
    if t % 2 == 0:
        t += 1
    ans = abs(x - t * d)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8

import math


def main():
    X, K, D = map(int, input().split())
    n = math.ceil(X / D)
    r = 0
    if n >= K:
        r = X - (K * D)
    else:
        r = X - (n * D)
        k2 = K - n
        if k2 % 2 == 1:
            r += D
    print(abs(r))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

num_moved = x // d
num_remained = k - num_moved
if num_remained >= 0:
  now = x % d
else:
  now = x - d*k

#now = x % d
if num_remained % 2 == 0:
  result = abs(now)
else:
  tmpa = now + d
  tmpb = now - d
  if abs(tmpa) > abs(tmpb):
    result = abs(tmpb)
  else:
    result = abs(tmpa)
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
t=floor(decimal.Decimal(abs(x))/decimal.Decimal(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(abs(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 = list(map(int, input().split()))
L = X + D * K

if L >= 0:
   if L / (2 * D) - int(L / (2 * D)) > 0.5:
      ans = int(L / (2 * D)) + 1
   else:
      ans = int(L / (2 * D))

elif L < 0:
   if L / (2 * D) - int(L / (2 * D)) < -0.5:
      ans = int(L / (2 * D)) - 1
   else:
      ans = int(L / (2 * D))
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

for i in range(k):
  if x > 0:
    x = x - d
    k -= 1
    if x <= 0:
      mid = True
      break
  else:
    x = x + d
    k -= 1
    if x >= 0:
      mid = True
      break
      
if mid:
  if k % 2 == 0:
    print(x)
  else:
    if abs(x + d) > abs(x - d):
      print(abs(x - d))
    else:
      print(abs(x + d))
else:
  print(x)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

X,K,D = I()
if X == 0:
    print(0)
    exit()
if X > 0:
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        f = 0
    else:
        f = 1
    if (X//D)%2 != (K-X//D)%2:
        if f == 0:
            f = 1
        else:
            f = 0
    if f == 0:
        print(abs(num1))
    else:
        print(abs(num2))
else:
    X *= -1
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        f = 0
    else:
        f = 1
    if (X//D)%2 != (K-X//D)%2:
        if f == 0:
            f = 1
        else:
            f = 0
    if f == 0:
        print(abs(num1))
    else:
        print(abs(num2))

Submission status: Wrong Answer

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

if X // D <= K:
    now = X % D
    K -= X // D
else:
    now -= K * D
    K = 0

if K > 0:
    ans1 = now
    ans2 = abs(now - D)

    if ans1 < ans2 and K % 2 == 0 or ans1 > ans2 and K % 2 == 1:
        now = ans1
    else:
        now = ans2
print(abs(now))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# 移動制限がないときの最小値
m = x % d

# mを得るために必要な最小の移動回数
n = x // d

if k < n:
    print(x - k * d)
    exit()

n -= k

if n % 2 == 0:
    print(m)
else:
    print(min(abs(m - d), m + d))

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout,stderr
import time,math
from collections import defaultdict as dd, Counter
 
def pn(ob):stdout.write(str(ob))
def inp():return stdin.readline()
 
def main():
	x, k, d = map(int, inp().split())
	
	if(x == d):
		if(k % 2 == 0):
			print(abs(x))
		else:
			print(0)	
	elif(x <= 0):
		if(-x >= k * d):
			print(-x + (k * d))
		else:
			n = x // d
			res = abs(x - n * d)
			if(x % d != 0):
				n -= 1
			res = min(res, abs(x - n * d))
			print(res)

	else:
		if(x >= k * d):
			print(x - k * d)
		else:
			n = x // d
			res = abs(x - n * d)
			if(x % d != 0):
				n += 1
			res = min(res, abs(x - n * d))
			print(res)
		
  
if __name__ == '__main__':
	main()
	
#n,m=map(int,input().split()) 
# for i in range(n):
#     t=int(stdin.readline()) # input using fast i/p method 
#     if t%m == 0:
#         stdout.write(str(t)+'\n') #input using fast o/p method
 
# take line of integers as input
# 	arr = list(map(int, inp().split()))

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    X, K, D = map(int, input().split())

    d, m = divmod(X, D)
    if d > K:
        print(X - (K * D))
    else:
        if d % 2:
            print(D - abs(m))
        else:
            print(m)


main()

Submission status: Wrong Answer

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

if k*d <= abs(x):
    if x > 0:
        x -= k*d
    else:
        x += k*d
    res = abs(x)
else:
    moves = abs(x)/d
    moves = int(moves)
    x -= moves*d
    left = k-moves
    k = left
    if k%2 == 0: #k == 0 or k == 2:
        res = x
    else: # k == 1:
        if x > 0:
            res = x-d
        else:
            res = x+d
    # elif k > 2:
    #     if x > 0:
    #         res = min(abs(x), abs(x-d))
    #     else:
    #         res = min(abs(x), abs(x+d))

    res = abs(res)

print(res)

Submission status: Wrong Answer

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

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

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:

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

l=0
if x==0:
    print(0)
elif x<0:
    if x+d*k>=0:
        x=abs(x)
        a=x%d
        b=d-a
        e=x//d
        if e&1==k&1:
            print(a)
        else:
            print(b)
    else:
        print(x+d*k)
        
        
else:
    if x-d*k<=0:
        a=x%d
        b=d-a
        e=x//d
        if e&1==k&1:
            print(a)
        else:
            print(b)
    else:
        print(x-d*k)

Submission status: Wrong Answer

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def findMaximum(low, high):
    # Base Case: Only one element is present in arr[low..high]*/
    if low == high:
        return fun(low)

        # If there are two elements and first is greater then
    # the first element is maximum */
    if high == low + 1 and fun(low) >= fun(high):
        return fun(high);

        # If there are two elements and second is greater then
    # the second element is maximum */
    if high == low + 1 and fun(low) < fun(high):
        return fun(low)

    mid = (low + high) // 2  # low + (high - low)/2;*/

    # If we reach a point where arr[mid] is greater than both of
    # its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid]
    # is the maximum element*/
    if fun(mid) < fun(mid+1) and fun(mid) < fun(mid-1):
        return fun(mid)

        # If arr[mid] is greater than the next element and smaller than the previous
    # element then maximum lies on left side of mid */
    if fun(mid) > fun(mid+1) and fun(mid) < fun(mid-1):
        return findMaximum(mid+1,high)
    else:  # when arr[mid] is greater than arr[mid-1] and smaller than arr[mid+1]
        return findMaximum(low, mid-1)

def fun(zz):
    return abs(x+d*(2*zz-k))

x,k,d = MAP()

l = 0
r = k

print(findMaximum(l, r))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
x,k,d = map(int,input().split())
ans = float('inf')

if x/k < d:
    x = x - math.floor(x/d) *d
    for i in range(10**5):
        ans = min(abs(x-k), abs(x+k))
        if abs(x-k) < abs(x+k):
            x = x - k
        else:
            x = x + k
else:
    if x > 0:
        ans = x - k*d
    else:
        ans = x + k*d
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

if d * k >= abs(x):
  a = x % d
  fuck = False
  if a == 0 and k % 2 == 0 and d >= abs(x):
    fuck = True
  elif math.ceil(x / d) != k:
    b = a - d
  else:
    b = a
  if fuck:
    print(d)
  else:
    if a > abs(b):
      ans = b
    else:
      ans = a
    print(ans)
else:
  if x == 0:
    if k%2 == 0:
      print(0)
    else:
      print(d)
  elif x > 0:
    print(x-(d*k))
  else:
    print(x+(d*k))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
first = X - K*D
last = X + K*D

if first >= 0:
    ans = first
elif last <= 0:
    ans = last
else:
    s = 0
    g = K
    half = (s + g)//2
    while s < half:
        if X + (half - (K-half))*D < 0:
            s = half
        else:
            g = half
        half = (s + g)//2

    ans = min(abs(X + (half - (K - half))*D) , abs(X + (g - (K - g))* D ))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
for _ in range(1):#int(stdin.readline())):
    # n=int(stdin.readline())
    x,k,d=list(map(int,stdin.readline().split()))
    '''if x==k==d:
        print(0 if k&1 else x)
        continue'''
    des=x%d
    op=x//d
    a=[des,des-d]
    if k<=op:#check for eq
        print(x-k*d)
    else:
        print(abs(a[(k-op)%2]))

Submission status: Wrong Answer

Solution code:
from sys import stdin
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]


def solve(x,k,d):
    di = int(x//d)
    if di >= k: 
        return x - (k*d)
    x = x - (di*d)
    k = di
    if k % 2 == 0:
        return x
    else:
        return abs(x - d)

x,k,d = ip()
print(solve(x,k,d))

Submission status: Wrong Answer

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


if abs(x)<=d:
    if k%2==0:
        p=abs(x)
    else:
        if x>=0:
            p=abs(x-d)
        else:
            p=abs(x+d)
    print(p)
    exit(0)
else:
    cnt=abs(x)//d

    if cnt>k:
        cnt=k


    if x>=0:
        p=x-d*cnt
    else:
        p=x+d*cnt

    k-=cnt

    if p==0 and k%2==1:
        print(d)
    else:
        print(abs(p))

Submission status: Wrong Answer

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

if x==k and k==d:
  print(x)
  exit()

semin=x//d
humin=semin+1

if semin>=k:
  print(abs(x-k*d))
  exit()
else:
  print(min(abs(x-k*semin),abs(x-k*humin)))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys, copy,itertools
input = sys.stdin.readline

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

q = x // d
mod = x % d

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

print(abs(ans))

Submission status: Wrong Answer

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

if k%2==0:
    if x>d:
        print(x%d)
    else:
        print(x)
else:
    D=1

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

#偶数の場合
if K %2 == 0:
  if X//D > K:
    print(X%D)
  else:
    print(X%D + (X//D -K)*D)
else:
  if X//D > K:
    if abs(X%D-D) < abs(X%D +D):
      print(abs(X%D-D))
    else:
      print(abs(X%D +D))
  else:
    print(X%D + (X//D -K)*D)

Submission status: Wrong Answer

Solution code:
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
if X>K*D:
    print(X-K*D)
else:
    N=X//D
    Min=10**16
    n=0
    for i in range(N-1, N+2):
        M=X-i*D
        if M<Min:
            Min=M
            n=i
    if (K-n)%2==0:
        print (abs(Min))
    else:
        print(min(abs(Min+D), abs(Min-D)))

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 = (-1)*(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:
x,k,d = map(int,input().split())

need = x//d
if k>=need:
    x -= need*d
    k -= need
else:
    x -= k*d
    k = 0

while x>0 and k>0:
    x-=d
    k-=1

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

Submission status: Wrong Answer

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

while k>0:
    s = s-d
    num += 1
    k -= 1
    if abs(s)>=abs(x):
        break
    x = s

if k == 0:
    print(abs(s))
    exit()

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

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    X, K, D = [int(item) for item in input().split()]

    if K*D > X:
        temp = K - X // D

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def main():
    X, K, D = list(map(int, input().split()))
    ans = 0
    is_pos = 1
    if X < 0:
        is_pos = -1
    if K * D <= X * is_pos:
        ans = abs(X - K * D * is_pos)
    else:
        while X * is_pos > 0:
            X = X - D * is_pos
        if abs(X) < abs(X + D * is_pos) :
            ans = abs(X)
        else:
            ans = abs(X + D * is_pos)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = not (len(sys.argv)>=2 and sys.argv[1]=='ONLINE_JUDGE')

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)

def main():
    X,K,D=mis()
    X = abs(X)
    ZP = X%D
    if abs(X - ZP)//D > K:
        dprint('too far')
        print(X - D*K)
        return
    ZN = ZP - D
    X, K = ZP, K - abs(X - ZP)//D
    if K&1:
        dprint('odd')
        print(abs(ZN))
    else:
        dprint('even')
        print(abs(ZP))


main()