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:
#coding: utf-8

X, K, D = map(int, input().split())
# X: 現在地
# K: 移動回数
# D: 移動距離の絶対値
# ans: 移動後の座標の絶対値が最も小さくなるように動け！

mem = set()
mem_l = []

for i in range(K):
    p_move = X + D
    n_move = X - D

    if abs(p_move) <= abs(n_move):
        X = p_move
    else:
        X = n_move

    # 振動する場合を考えなきゃいけない -> 短縮のために
    mem.add(X)
    mem_l.append(X)

    # 同じものが現れた場合は振動開始している証拠
    if X in mem:
        if len(mem_l) >= 2:
            # 振動開始位置が偶数だった場合
            if i % 2 == 0 and K % 2 == 0:
                X = mem_l[-2]
            elif i % 2 == 0 and K % 2 != 0:
                break
            # 振動開始位置が奇数だった場合
            elif i % 2 == 1 and K % 2 == 0:
                break
            elif i % 2 == 1 and K % 2 != 0:
                X = mem_l[-2]

print(abs(X))

Submission status: Wrong Answer

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

m = math.floor(X/D)
if K >= m:
  if m==0:
    print(X)
    exit()
  if m%2 == K%2:
    print(abs(X%D))
  else:
    print(min(abs(X%D+D), abs(X%D-D)))
else:
  print( min( abs(X-D*K) , abs(X+D*K) ) )

Submission status: Wrong Answer

Solution code:
a = [int(i) for i in input().split()]
b = abs(a[0] // a[2])
c = a[1] - b
l = []
if c < 0:
  if a[0] >= 0:
    print(a[0] - a[1] * a[2])
  else:
    print(a[0] + a[1] * a[2])
else:
  if c % 2 == 0:
    if a[0] >= 0:
      print(a[0] - b * a[2])
    else:
      print(a[0] + b * a[2])
  else:
    if b + 1 <= a[1]:
      if a[0] >= 0:
        if abs(a[0] - (b + 1) * a[2]) < abs(a[0] - (b - 1) * a[2]):
          print(abs(a[0] - (b + 1) * a[2]))
        else:
          print(abs(a[0] - (b - 1) * a[2]))
      else:
        if abs(a[0] + (b + 1) * a[2]) < abs(a[0] + (b - 1) * a[2]):
          print(abs(a[0] + (b + 1) * a[2]))
        else:
          print(abs(a[0] + (b - 1) * a[2]))
    else:
      if a[0] >= 0:
        print(abs(a[0] - (b - 1) * a[2]))
      else:
        print(abs(a[0] + (b - 1) * a[2]))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
i=0
count=0
if X<0:
	if X+K*D<=0:
		print(abs(X+K*D))
		count+=1

	else:
		s=int(X/D)
		if abs(X+s*D)<=abs(X+(s+1)*D):
			L=abs(X+s*D)
		else:
			L=abs(X+(s+1)*D)
else:
	if X-K*D>=0:
		print(abs(X-K*D))
		count+=1

	else:
		s=int(X/D)
		if abs(X-s*D)<=abs(X-(s+1)*D):
			L=abs(X-s*D)
		else:
			L=abs(X-(s+1)*D)
            
if count==0:
	kaisuu=K-s
	if kaisuu%2==0:
		print(abs(L))

	else:
		a=abs(L+D)
		b=abs(L-D)
		if a<=b:
			print(abs(a))
		else:
			print(abs(b))

Submission status: Wrong Answer

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

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

elif x/d==1:
    print(x)

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

Submission status: Wrong Answer

Solution code:
ai=list(map(int,input().split()))
s = ai[0]
c = int(0)
n = int(ai[2])
for i in range(n):
  if abs (s + ai[1]) < abs(s - ai[1]):
    s += ai[1]
  else:
    s += -ai[1]
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
x,k,d = map(int, stdin.readline().rstrip().split())

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

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

Submission status: Wrong Answer

Solution code:
def isEven(num):
  if num%2 == 0:
  	return True
  return False

def isOdd(num):
  if num%2 == 1:
    return True
  return False

if __name__ == "__main__":
  x,k,d = map(int, input().split())
  div = x//d
  cmp = x
  defo = x
  if isOdd(k):
    if x > d:
      if k%div != 0:
        div = min(div,k%div)
      for i in [d,-d]:
        cmp = min(cmp,abs(defo+div*i))
      print(cmp)
    else:
      for i in [d,-d]:
        cmp = min(cmp,abs(defo+i))
      print(cmp)
  if isEven(k):
    div = x//(2*d)
    if x > d:
      div = min(div,k)
      for j in range(1,div):
        for i in [d,-d]:
          cmp = min(cmp,abs(defo+(2*j)*i))
      print(cmp)
    else:
      print(cmp)

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

Submission status: Wrong Answer

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

total = K*D
if(total > X):
    ans = (total-X-1) 
    ans -= D
elif total < X:
    ans = X - total
else:
    ans = X
print(ans)

Submission status: Wrong Answer

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

l = []

for i in range(k):
  a = x - d*i
  l.append(abs(a))
  if a<0:
    break

m = min(l)

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

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
Y = 0
p = 0
for i in range(1,K+1):
    if X > 0:
        Y = X - D
        if Y < 0:
            if K%2 == i%2:
                print(-Y)
            else:
                print(X)
            p = 1
            break
    elif X < 0:
        Y = X + D
        if Y > 0:
            if K%2 == i%2:
                print(Y)
            else:
                print(-X)
            p = 1
            break
    else:
        if K%2 == i%2:
            print(D)
        else:
            print(0)
        p = 1
        break
    X = Y
if p == 0:
    print(X)

Submission status: Wrong Answer

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

a = abs(X) // D
if a <= K:
  b = abs(X) - (D * a)
  c = abs(b - D)
else:
  b = abs(abs(X) - (D * K))
  c = 100000000000000000

if ((K - a)%2 == 0) and b <= c:
  print(b)
elif (K == a) and ((K - a)%2 == 1) and b <= c:
  print(abs(b - D))
elif (K != a) and ((K - a)%2 == 1) and b <= c:
  if b < abs(b - D):
    print(b)
  else:
    print(abs(b - D))
elif ((K - a)%2 == 0) and c < b:
  print(c)
elif (K == a) and ((K - a)%2 == 1) and c < b:
  print(abs(c - D))
elif (K != a) and ((K - a)%2 == 1) and c < b:
  if c < abs(c - D):
    print(c)
  else:
    print(abs(c - D))

Submission status: Wrong Answer

Solution code:
import math
import numpy


if __name__ == "__main__":
    X, K, D = [int(a) for a in input().split()]
    m_dis = abs(K*D)
    ans = 0
    mod_dis = m_dis - abs(X)
    ans = mod_dis % D
    if ((mod_dis - ans) /D) % 2 == 1:            
        ans += D
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    x, k, d = i_map()

    amari = x % d
    shou = x // d

    if k % 2 == 0:
        if amari == 0:
            if shou % 2 == 0:
                if shou <= k:
                    print(0)
                    exit()
                else:
                    print(abs(x-k*d))
                    exit()

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

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

    if shou <= k:
            print(abs(x-shou*d))
            # print('a')
    else:
        print(abs(x-k*d))  
        # print('b')



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)
elif x == k * d:
    print(0)
else:
    q = x // d
    mod = x % d
    k -= q
    x -= q * d

    r = abs(mod - d)
    if k == 0:
        print(abs(x))
    elif k == 1:
        print(abs(x-d))
    elif k % 2 == 0:
        print(mod)
    else:
        print(r)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if k%2 == 0 and abs(x)==abs(d):
  print(abs(x))
  exit()
res = []
i = 0
t = abs(x+d*k)
while i != k:
  res.append(t)
  i+=1
  t = t-k
print(min(res))

Submission status: Wrong Answer

Solution code:
X,K,D = list(map(int,input().split()))
count = K
ans = abs(X)
x = X
L = K//2
mx = X//D

if x>0:
    if mx<K:
        x = X-mx*D
        count -= mx
    else:
        x = X-(K-1)*D
        count -= K-1
    while x>0 and count>0:
        x -= D
        count -= 1
        if abs(x)<ans and count%2==0:
            ans = abs(x)
elif x<0:
    if mx<K:
        x = X+mx*D
        count -= mx
    else:
        x = X+(K-1)*D
        count -= K-1
    while x<0 and count>0:
        x += D
        count -= 1
        if abs(x)<ans and count%2==0:
            ans = abs(x)

print(ans)

Submission status: Wrong Answer

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

isReturn = False
i = 0

k = math.floor(x / d)


while i < k:
    if(x >= 0):
        x -= d
    else:
        x += d
        isReturn = True

    if isReturn:
        if((k-i) % 2 == 0):
            i = k-2
        else:
            i = k-1

    i += 1

print(abs(x))

Submission status: Wrong Answer

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

if ans_temp <= k:
    if ans_temp % 2 == 0:
        ans = x
    else:
        ans1 = x - d
        ans2 = x + d
        ans = min(abs(ans1),abs(ans2))
else:
    ans = x - k*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)
else:
    a = x % d
    b = a - d
    if (k - (x // d)) % 2 == 1:
        print(abs(x % d - d))
    else:
        print(min(a, b))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import itertools

if __name__ == '__main__':
    # 初期位置、移動回数、一回の移動量
    X, K, D = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split()))
    origX = X
    # oldX = X
    # lasti = 0
    # 7-3=4
    # 4-3=1
    # 1-3=-2

    # for i in range(K):
    #     X -= D
    #     if abs(X) > abs(oldX):
    #         X = oldX
    #         lasti = i
    #         break
    #     oldX = X
    # else:
    #     print(abs(X))
    #     exit()

    # 6 3 2
    # 9 3 4
    X = abs(X)
    lasti = X // D
    X = X - D*lasti

    # 割れないときはどうするの？
    # 3 5 7
    # マイナスの時がある

    if lasti >= K:
        print(abs(origX-D*K))
        exit()

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

Submission status: Wrong Answer

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

zx=x
zzx=zx
for i in range(k):
    xm=x+d
    xu=x-d
    x=min(abs(xm),abs(xu))
    k-=1
    if zx<x:
        if k%2==0:
            x=zzx
            break
        else:
            x=zx
            break
    zzx=zx
    zx=x
    
print(x)

Submission status: Wrong Answer

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

ans = 1000000000000007
prev = 1000000000000007
if abs(X) % D == 0:
  if abs(X) // D <= K:
    print(0)
    exit()

if X + D*K < 0:
  print(X+D*K)
  exit()

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


if K % 2 == 0:

  i = K // 2
  for k in range(0, i+1):
    mn = abs(X + k * D * 2)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

  prev = 1000000000000007
  for k in range(0, i+1):
    mn = abs(X - k * D * 2)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

else:
  for k in range(1, K+1, 2):
    mn = abs(X + k * D)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

  prev = 1000000000000007
  for k in range(1, K+1, 2):
    mn = abs(X - k * D)
    if prev < mn:
      break
    prev = mn
    ans = abs(min(mn, ans))

print(ans)

Submission status: Wrong Answer

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

if x <= 0:#開始座標が０か負
    if x + k*d <=0:#一方向にだけ動かせるとき
        ans= abs(x + k*d)
    else:
        kk=abs(x)//d
        tmp=x+kk*d#負の座標間で0に最大まで近づける
        tmp2=x+d#正の座標間で0に最大まで近づける
        if tmp==0:
            tmp=d
            tmp2=d

        if tmp==tmp2 :#4回移動でループ
            if (k-kk)%2==1:
                ans=abs(tmp)
            else:
                ans=0
        else:#値が違うとき2回移動でループ
            if (k-kk)%2==0:
                ans=abs(tmp)
            else:
                ans=abs(tmp2)

else:#開始座標が正
    if x - k*d >=0:#一方向にだけ動かせるとき
        ans=abs(x - k*d)
    else:
        kk=abs(x)//d
        tmp=x-kk*d#正の座標間で0に最大まで近づける
        tmp2=x-d#負の座標間で0に最大まで近づける
        if tmp==0:
            tmp=d
            tmp2=d

        if tmp==tmp2 :#4回移動でループ
            if (k-kk)%2==1:
                ans=abs(tmp)
            else:
                ans=0
        else:#値が違うとき2回移動でループ
            if (k-kk)%2==0:
                ans=abs(tmp)
            else:
                ans=abs(tmp2)

print(ans)

Submission status: Wrong Answer

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

if k%2==0:
  ans=0 #偶数
else:
  ans=1 #奇数

semin=x//d #最小に行くための個数
if semin%2==0:
  minb=0 #最小に行くための個数が偶数
else:
  minb=1 #最小に行くための個数が奇数

if semin>k:
  print(abs(x-k*d))
else:
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-d*semin))
    else:
      print(min(abs((x-(d*semin))-d),abs(x-d*semin)))
  else:#ｋが奇数の時
    if minb==0:
      print(abs((x-(d*semin))-d))
    else:
      print(min(abs((x-(d*semin))-d),abs(x-d*semin)))

Submission status: Wrong Answer

Solution code:
X, K, D  = map(int, input().split())
shou = X//D
amari = X%D 

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

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
a1=(abs(a)-0)%c
if(a==0):
    if(b%2==0):
        print(0)
    else:
        print(c)
if(a>0):
    a2=a1-c
    ma1=int(a/c)
    ma2=ma1+1
    a3=abs(a2)
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(a-(b*c))
if(a<0):
    a2=abs(a1-c)
    ma1=((a1-(2*a1))-(a))
    ma1=int(ma1/c)
    ma2=ma1+1
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(a+(b*c))

Submission status: Wrong Answer

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

N = X // D
if N >= K:
    res = X - K * D    
else: # N < K:
    base = X - N * D
    # print(base)
    if N % 2 == 0 and K % 2 == 0:
        res = base
    else:
        res = min(abs(base - D), base + D)

print(res)

Submission status: Wrong Answer

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

flag = False
res = 0

if X == D:
    res = X
    flag = True

for i in range(K):
    if flag:
        break
        
    n = res if res != 0 else X

    res_m = n - D
    res_p = n + D

    res_m *= -1 if res_m < 0 else 1

    res = res_m if res_m < res_p else res_p

print(res)

Submission status: Wrong Answer

Solution code:
a, b, c = map(int, input().split())
for i in range(b):
  if abs(a-c) < abs(a+c):
    a -= c
  else:
    a += c
print(a)

Submission status: Wrong Answer

Solution code:

li = list(map(int ,input().split()))

x = abs(li[0])
k = li[1]
d = li[2]

if x == d and k % 2 == 0:
    print(abs(x))
    exit()
if x == d and k & 2 == 1:
    print(0)
    exit()

if x < k * d:
    x = x % d
    print(abs(x))
    exit()

k_count = k

for _ in range(k):
    sum_result = x + d
    minus_result = x - d
    k_count -= 1
    if minus_result > 0:
        x = minus_result
    else:
        if minus_result + d > minus_result:
            print(minus_result + d)
            exit()
        else:
            print(minus_result)
            exit()

    if abs(x) < d and k_count % 2 == 0:
        print(abs(x))
        exit()
    if abs(x) < d and k_count % 2 == 1:
        if abs(abs(x) - d) < abs(abs(x) + d):
            print(abs(abs(x) - d))
            exit()
        else:
            print(abs(abs(x) + d))
            exit()

print(abs(x))

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
x=(abs(i[0]))

if x>=i[1]*i[2]:
    print(x-i[1]*i[2])
else:
    if x%i[2]>=(i[2]-x%i[2]):
        if (i[1]-x//i[2])%2 == 0:
            print(i[2]-x%i[2])
        else:
            print(x%i[2])
    else:
        if (i[1]-x//i[2])%2 == 1:
            print(i[2]-x%i[2])
        else:
            print(x%i[2])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

if(K%2 == 1):
    print(((X%D)-D))
    exit()

Submission status: Wrong Answer

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

def binary_search(asset):
    last_r = 0
    last = (asset["R"] - asset["L"]) * d + x
    min_ = abs(last)

    mid = asset["R"] + (k - asset["R"]) // 2 + asset["R"] % 2
    asset["R"] = mid
    asset["L"] = k -mid

    now = (asset["R"] - asset["L"]) * d + x

    while abs(last_r - mid) > 0 and abs(last) != abs(now) :
        if min_ > abs(now):
            min_ = abs(now)
        if abs(last_r - mid) == 1:
            return min(abs(last), abs(now))
        last = now
        last_r = mid
        assert asset["L"] + asset["R"] == k
        if now == 0:
            return now
        if abs(now) > abs(last):
            mid = asset["R"] - ((k - asset["R"]) // 2 + asset["R"] % 2)
            asset["R"] = mid
            asset["L"] = k -mid

        else:
            mid = asset["R"] + ((k - asset["R"]) // 2 + asset["R"] % 2)
            asset["R"] = mid
            asset["L"] = k -mid
        now = (asset["R"] - asset["L"]) * d + x
        
    return min_

#最初はL が K個
asset = {"L": k, "R": 0}
print(binary_search(asset))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if K <= 10:
    list_ = [abs((X - K * D) + (2* i * D)) for i in list(range(0,K+1))]
    result = min(list_)
    print(result)
    

else:
    
    it = int(K /2)
    tmp_result = abs((X - K * D) + (2* (it +1) * D))
    
    flag =True
    
    c = 0
    while flag:
        
        c += 1
        if c < 1000:
        
            if it == 0:
                result = abs((X - K * D) + (2* 0 * D))
                flag = False

            elif it == (K+1):
                result = abs((X - K * D) + (2* (K+1) * D))
                flag = False

            result = abs((X - K * D) + (2* it * D)) 
            result_m = abs((X - K * D) + (2* (it -1) * D))
            result_p = abs((X - K * D) + (2* (it +1) * D))

            if result <= result_m and result <= result_p:
                flag =False

            if result > result_m:
                if it == 4:
                    it = 3
                elif it == 3:
                    it = 2
                elif it == 2:
                    it =1
                elif it ==1:
                    it = 0
                else:
                    it = int(it/2)

            elif result > result_p:
                if (K+1- it) == 4:
                    it = K-2
                elif (K+1- it) == 3:
                    it = K-1
                elif (K+1- it) == 2:
                    it =K
                elif (K+1- it) ==1:
                    it = K+1
                else:
                    it = it +(int((K+1- it)/2))
        
        else:
            
            it = it - 500
            for z in range(1000):
                
                if (it + z) > (K+1) or (it + z) < 0:
                    continue
                tmp_result = abs((X - K * D) + (2* (it + z) * D)) 
                
                if tmp_result < result:
                    result = tmp_result
            
                
    print(result)

Submission status: Wrong Answer

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

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

    if X > 0 and (X - K * D) > 0:
        print(abs(X - K * D))
    elif X < 0 and (X + K * D) < 0:
        print(abs(X + K * D))
    elif X * (X - K * D) < 0 or X * (X + K * D) < 0:
        # if X % D == 0:
        #     if abs((X // D) % 2) == abs(K % 2):
        #         print(0)
        #     else:
        #         print(D)
        #     exit()
        if abs((X // D) % 2) == abs(K % 2):
            print(X % D)
        else:
            print(min(abs((X % D) + D), abs((X % D) - D)))


main()

Submission status: Wrong Answer

Solution code:
list = list(map(int, input().split())) 
hajime=list[0]
kaisuu=list[1]
hohaba=list[2]
if hajime>=0:
  ayumi=hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime-hohaba*kaisuu
  kouho_2=hajime-hohaba*ayumi
  kouho_3=-hajime+hohaba*(ayumi+1)

  
  
  if kaisuu <= ayumi:
    print(kouho_1)
  else:
    if nokori%2==0:
      print(kouho_2)
    else:
      print(kouho_3)
      
if hajime<0:  
  ayumi=-hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime+hohaba*kaisuu
  kouho_2=hajime+hohaba*ayumi
  kouho_3=hajime+hohaba*(ayumi+1)
  if kaisuu<=hohaba:
    
    print(-kouho_1)  
  else:
    if nokori%2==0:
      print(-kouho_2)
    else:
      print(kouho_3)

Submission status: Wrong Answer

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

while True:
    if count == k:
        break
    count += 1
    next1 = abs(x)+d
    next2 = abs(x)-d
    next = min(abs(next1),abs(next2))
    if abs(x) <= abs(next):
        break
    else:
        x=next

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

Submission status: Wrong Answer

Solution code:
list_in = list(map(int,input().split()))
xin = list_in[0]
kin = list_in[1]
din = list_in[2]

now_place = xin
zflag = 0

if (kin * din) > xin:
    chk = (xin // din)
    kin = kin - chk 
    xin = xin - (din * chk)

for i in range(kin):
    p_place = now_place + din
    m_place = now_place - din
    bef_place = now_place
    if (abs(p_place) < abs(m_place)):
        now_place = p_place
    else:
        now_place = m_place
    if(abs(bef_place)<abs(now_place)):
        zflag=1
    if (zflag == 1) and (0 == ((kin + 1 - i)%2)):
        break

print(abs(now_place))

Submission status: Wrong Answer

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

C =[]
if X == K and K == D:
    print(X)

else:
    for i in range(K):
        if abs(X - D) >= abs(X + D):
            X = X + D
            C.append(X)
        #print(X)
        else:
            X = X - D
            C.append(X)
        #print(X)

    ooo = C[K-1]
    print(abs(ooo))

Submission status: Wrong Answer

Solution code:
import sys

s = input()
a = s.split()

x = int(a[0])
k = int(a[1])
d = int(a[2])

for i in range(k):
    if 0 < x:
        x = x-d
    else :
        if((k-i+1)%2==0):
            break
        elif ((k-i+1)%2==1):
            x = x+d
            break

print(str(abs(x)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if X == D:
    print(X)
    exit()

ans = abs(X-D)
for i in range(2, K+1):
    num = abs(X-(D*i))
    if ans >= num:
        ans = num
    else:
        break
print(ans)
exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def my_sign(x):
    return (x > 0) - (x < 0)

if K%2==1:
  X=X+my_sign(X)*(-1)*D
  K=K-1
  
if K < abs(X//D):
  print(X+my_sign(X)*(-1)*D*K)
else:
  if (X//D)%2==1:
    print(min(abs(X%D+X), abs(X%D)))
  else:
    print(X%D)

Submission status: Wrong Answer

Solution code:
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    global x,k,d
    i = min(k,abs(x//d))
    if abs(x-d*i)<abs(x+d*i):
        x -= d*k
    else:
        x += d*i
    k -= i
    if k%2==1:
        if abs(x-d)<abs(x+d):
            print(abs(x-d))
        else:
            print(abs(x+d))
    else:
        print(abs(x))
    return

if __name__=='__main__':
    x,k,d = IL()
    solve()

Submission status: Wrong Answer

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

x=abs(x)
y=x
if x==k==d:print(x)
for i in range(k):
    y-=d
    if y<d:
        if i % 2 == k % 2:
            print(y)
            raise SystemExit(0)
        else:
            if y<abs(y-d):
                print(y)
                raise SystemExit(0)
            else:
                print(abs(y-d))
                raise SystemExit(0)
print(y)