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 math

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

ans = 0
if x == 0:
    if k%2 == 0:
        ans = 0
    elif k%2 == 0:
        ans = abs(d)
else :
    if abs(x) - k*d >= 0:
        ans = abs(abs(x) - k*d)
        #if abs(x) - k*d == 0:
            #if k - (abs(x) / (k * d)) % 2 == 0:
                #ans = 0
            #else:
                #ans = abs(x)
    elif abs(x) - k*d < 0:
        if  abs(x) - d*(abs(x) // d) != 0:
            if (k - (math.ceil(abs(x) / d) )) % 2 == 1:
                ans = abs(abs(x) - d*(abs(x) // d))
            else:
                ans = abs(abs(x) - d*(abs(x) // d + 1))
        else:
            if (k - (math.ceil(abs(x) // d) ) ) % 2 == 1:
                ans = d
            else:
                ans = 0
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
data1 = [int(x) for x in input().split()]
x = data1[0]
k = data1[1]
d = data1[2]
a = x//d
if k % 2 == a % 2:
    if k >=abs(a):
        takahashi = [x-d*a,x-d*(a+1)]
        t = np.abs(takahashi)
        t_min = min(t)
    else:
        takahashi = [x-d*k,x+k*d]
        t = np.abs(takahashi)
        t_min = min(t)
else:
    if k >=max([abs(a-1),abs(a+1)]):
        takahashi = [x-d*(a-1),x-d*(a+1)]
        t = np.abs(takahashi)
        t_min = min(t)
    else:
        takahashi = [x-d*k,x+k*d]
        t = np.abs(takahashi)
        t_min = min(t)
print(t_min)

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(abs(x)-k*d))
                    exit()

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

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

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



if __name__ == '__main__':
    main()

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 - shou % 2 == 0:
    print(amari)
else:
    print(abs(amari - D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

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

	if x > 0:
		for i in range(k):
			#-進める
			x = x - d
			if x <= 0:
				#判定
				if i != k - 1:
					x = min(abs(x+d),abs(x-d))
					print(x)
					exit()
				else:
					x = abs(x)
	else:
		for j in range(k):
			#+進める
			x = x + d
			if x >= 0:
				#判定
				if j != k - 1:
					x = min(abs(x+d),abs(x-d))
					print(x)
					exit()
				else:
					x = abs(x)
	#判定
	print(abs(x))

Submission status: Wrong Answer

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

KOE=K%2

A=abs(X)//D
M=abs(X)%D

# print(A)

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

else :
    if A%2==KOE:
        print(min(M,abs(M-D),abs(M+D)))
    else :
        print(min(abs(M+D),abs(M-D)))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
a=divmod(abs(x),d)  
if x==0:
  if k%2==0:
    print(0)
  else:
    print(d)
else:
  if a[0]==0:
    if k%2==0:
      print(x)
    else:
      print(d-x)
  else:
    if a[0]<k:
      if a[1]==0 and a[0]%2==1:
        print(d)
      elif a[1]==0 and a[0]%2==0:
        print(0)
      elif a[1]!=0 and a[0]%2!=0:
        print(d-a[1])
      
      else:
        print(a[0])
    else:
      print(abs(x)-d*k)

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
temp=a//c
if(temp>b):
    temp=temp-b
    if(a%c==0):
        if(temp>b):
            print(a-(b*c))
        else:
            print(temp*c)
    else:
        print(a%c+(temp*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))

Submission status: Wrong Answer

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

c = X

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

Submission status: Wrong Answer

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

m = X//D
if K >= m:
  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:
x, k, d=map(int, input().split(' '))
if x - k*d >= 0:
  print(x - k*d)
else:
  l=2
  for i in range(k):
    if i*d > x:
      l=i
      break
  if (k-l) % 2 == 0:
    x = -x + l*d
  else:
    x = -(l-1)*d + x
  if x < 0:
    x = -x
  print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

s = x // d
a = x % d
ans = 0

if (s <= k):
    if (x < 0):
        ans = x + (d * s)
    else:
        ans = x - (d * s)

    '''
    if ((k % 2) == 0):
        if (x < 0):
            ans += d
        else:
            ans -= d
    '''

elif (s > k):
    if (x < 0):
        ans = x + (d * k)
    else:
        ans = x - (d * k)

print(ans)

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(x-m*d)
    else:
        print(x-m*d+d)

Submission status: Wrong Answer

Solution code:
[X,K,D]=[int(_) for _ in input().split()]
X=abs(X)
Y=X//D
if K>Y:
  K-=Y
  X-=Y*D
print(X,K,D)
X/=D
for _ in range(K//2):
  X = min([abs(X-2), abs(X), abs(X+2)])
  print(X)
if K%2 == 1:
  X = min([abs(X-1), abs(X+1)])
  print(X)
print(abs(int(X*D)))

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
    r = abs(mod - d)
    if k % 2 == 0:
        print(mod)
    else:
        print(r)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
flag = True
res = abs(X)%D
r = abs(X)//D

if abs(X)-K*D>=D:
  ans = abs(abs(X)-K*D)
  flag = False
else:
  ans = X

if flag:
  if r%2 == K%2:
    ans = min(ans, res)
  else:
    ans = min(ans, abs(res-D))
else:
  if r%2 != K%2:
    ans = abs(res)
print(ans)

Submission status: Wrong Answer

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

i = 0

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

print(X)

Submission status: Wrong Answer

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

if k % 2 == 0:
    start = 0
else:
    start = 1

if x <=d:
    if start == 0:
        print(x)
    if start == 1:
        print(min([abs(x) for x in [x+d, x-d]]))
    
    exit()

candidates = []
for i in range(start, k+1, 2):
    a = x + (i*d)
    b = x - (i*d)

    candidates.append(a)
    candidates.append(b)

ans = min([abs(x) for x in candidates])
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(min(abs((x-(d*semin))-d),abs(x-d*semin)))
    else:
      print(abs(x-d*semin))

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(abs((-X) % D))


main()

Submission status: Wrong Answer

Solution code:
a,b,c=map(int, input().split())
if(a<0):
    aa=-a
else:
    aa=a
if(aa>b*c):
    ans=aa-b*c
    print(ans)
    exit()
else:
    if(aa<c):
        if(b%2==0):
            print(aa)
            exit()
        else:
            print(c-aa)
            exit()
    if(aa==c):
        if(b%2==0):
            print(aa)
            exit()
        else:
            print(0)
            exit()
    if(aa==0):
        if(b%2==0):
            print(aa)
            exit()
        else:
            print(c)
            exit()
    else:
        amari=aa%c
        amarioruta=abs(amari-c)
        if(amari==0):
            hantei=aa/c
            if(hantei%2==0):       
                bb=b%2
                if(bb==0):
                    print(0)
                    exit()
                else:
                    print(c)
                    exit()
            else:
                bb=b%2
                if(bb==0):
                    print(c)
                    exit()
                else:
                    print(0)
                    exit()
        else:
            if(aa<b*c<aa+c):
                muri1=b*c-aa
                print(muri1)
                exit()
            else:
                if(amari>amarioruta):
                    print(amarioruta)
                    exit()
                else:
                    print(amari)
                    exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
x_min = abs(x)
num = 0
if x < 0:
    for j in range(16):
        if x + d * int(k/(10**j)) < 0:
            x += d * int(k/(10**j))
            num = int(k/(10**j))
            x_min = abs(x)
            break
else:
    for j in range(16):
        if x - d * int(k/(10**j)) >= 0:
            x -= d * int(k/(10**j))
            num = int(k/(10**j))
            x_min = abs(x)
            break
for i in range(k - num):
    if x < 0:
        x += d
        if abs(x) < x_min:
            x_min = abs(x)
        else:#xが0以上
            if (k - i - 1) % 2 == 1:
                break
            else:
                x_min = d - x_min
                break
    else:
        x -= d
        if abs(x) < x_min:
            x_min = abs(x)
        else:#xが負
            if (k - i - 1) % 2 == 1:
                break
            else:
                x_min = d - x_min
                break
print(x_min)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
a=divmod(abs(x),d)  
if x==0:
  if k%2==0:
    print(0)
  else:
    print(d)
else:
  if a[0]==0:
    if k%2==0:
      print(x)
    else:
      print(d-x)
  else:
    if a[0]<k:
      if a[1]==0 and a[0]%2==1:
        print(d)
      elif a[1]==0 and a[0]%2==0:
        print(0)
      elif a[1]!=0 and a[0]%2!=0:
        print(d-a[1])
      
      else:
        print(a[1])
    else:
      print(abs(x))

Submission status: Wrong Answer

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

p = K
q = K

if X >= 0:
    toDown = True
else :
    toDown = False

if toDown:
    for i in range(K+1):
        now = now + D*(p-q)
        if abs(now) < m:
            m = abs(now)
        if abs(prev) < abs(now):
            break
        else:
            q = q+1
            prev = now
            now = X
else:
    for i in range(K+1):
        now = now + D*(p-q)
        if abs(now) < m:
            m = abs(now)
        if abs(prev) > abs(now):
            break
        else:
            p = p+1
            prev = now
            now = X

print(m)

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:
a,b,c=map(int,input().split())
temp=int(a/c)
if(temp>b):
    temp=temp-b
    if(a%c==0):
        if(temp>b):
            print(a-(b*c))
        else:
            print(temp*c)
    else:
        print(a%c+(temp*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))

Submission status: Wrong Answer

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

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 - shou % 2 == 0:
    print(amari)
else:
    print(abs(amari - D))

Submission status: Wrong Answer

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

m = X//D
if K > m:
  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:
x, k, d = map(int, input().split())
if x>=0:
    while x > 0:
        if x > k*d:
            x= x-k*d
            d=0
            break
        x = x-d
        k-=1
        
        if x==0:
            if k%2!=0: 
                x=d
                d=0
            else: 
                x=0
                d=0
            break
    # print(abs(x),abs(x+d))
    print(min(abs(x),abs(x+d)))
else:
    while x < 0:
        if abs(x) > k*d:
            x= x+k*d
            d=0
            break
        x = x+d
        k-=1
        if d:
            break
        if x==0:
            if k%2!=0:
                x=d
                d=0
            else: 
                x=0
                d=0
            break
    print(min(abs(x),abs(x-d)))

Submission status: Wrong Answer

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

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())

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

print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
L = []
for num in range(1, D+1):
    L.append( abs(X + K*num) )
for num in range(1, D+1):
    L.append(abs(X - K*num))
print(min(L))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

c = X

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

Submission status: Wrong Answer

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

KOE=K%2

A=abs(X)//D
M=abs(X)%D

# print(A)

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

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

        print(min(abs(M+D),abs(M-D)))

Submission status: Wrong Answer

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

k = round(X / D)

if k > K:
    if k == 1:
        print(abs(abs(X) - K * D))
    else:
        print(min(abs(abs(X) - K * D), abs(abs(X) - (K - 2) * D)))

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

    else:
        print(min(abs(abs(X) - (k + 1) * D), abs(abs(X) - (k - 1) * D), abs(abs(X) - (k + 3) * D), abs(abs(X) - (k - 3) * D)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
x,k,d=map(int,input().split())
m=abs(x)
n=m//d
j=k-n
if x<0:
  if j%2==0:
    x+=d*n
  else:
    x+=d*(n+1)
else:
  if j%2==0:
    x-=d*n
  else:
    x-=d*(n+1)
print(abs(x))

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:
    if K * D < X:
        print(X - K * D)
    else:
        if X > D and K % 2 == 0:
            span = int(X / D)
            val = min(X - span * D, abs(X - (span + 1) * D))
            print(val)
        else:
            for i in range(K):
                if X >= 0:
                    X = X - D
                else:
                    X = X + D
            print(abs(X))

Submission status: Wrong Answer

Solution code:
import sys

L = list(map(int,input().split()))
x = L[0]
k = L[1]
d = L[2]
#print(k)

for i in range(k):
    if abs(x-d) == abs(x+d):
        x = abs(x-d)
        break
    elif abs(x-d)<abs(x+d):
        x = x-d
    else:
        x = x+d
    #print(x)



print(abs(x))

Submission status: Wrong Answer

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

ab=X/D

if ab.is_integer():
    if ab<=K:
        ab=int(ab)
    else:
        ab=K
        
else:
    if ab<=K:
        ab=int(ab)+1
    else:
        ab=K
X=X-ab*D

cc=X
cc1=X
cc11=X

K=K-ab
K1=K
K11=K


if K>1:
    asa=K//2
    cc=cc+(asa)*(D)
    K=K-asa
    cc=cc-K*D
    aw=abs(cc)
else:
    cc=cc+K*D
    aw=abs(cc)

if K1>1:
    
    asa1=K1//2
    cc1=cc1-(asa1)*(D)
    K1=K1-asa1
    cc1=cc1+K1*D
    aw1=abs(cc1)
else:
    cc1=cc1-K1*D
    aw1=abs(cc1)

if K11>1:
    
    asa11=K11//2
    cc11=cc11-(asa11)*(D)
    K11=K11-asa11
    cc11=cc11+K11*D
    aw11=abs(cc11)
else:
    cc11=cc11+K11*D
    aw11=abs(cc11)
    



AAAA=min(aw1,aw)

print(min(AAAA,aw11))

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 i % 2 == 0 and K % 2 == 0:
            X = mem_l[-1]
        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[-1]

print(abs(X))

Submission status: Wrong Answer

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

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:
    if x%d == 0:
        s = 1
    l = (d-r+s)%2
    if l==1:
        ans = abs(x)-abs(d*r)
    else:
        ans = abs(abs(x)-abs(d*(r+1)))
print(ans)

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))-(a1))
    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())

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

Submission status: Wrong Answer

Solution code:

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

ans = x

if 0 < x :

    # 理想の数字を追求
    if abs(x - (x // d * d)) < abs(x - (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x - (x // d * d))
        s_ans = abs(x - (((x // d) + 1) * d))
    else :
        # print('2採用')
        i_ans = abs(x - (((x // d) + 1) * d))
        s_ans = abs(x - (x // d * d))

    # print("回数制限なしの理想の答え :  {0}".format(i_ans))
    # print("回数制限なしの次点の答え :  {0}".format(s_ans))

    # print("最適な答えにたどりつく回数 :  {0}".format(x // d))
    # print("動かせる回数 　　　　　　　:  {0}".format(k))

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

            if (x // d) % 2== 0 :
                # print("aaa")
                ans = i_ans
            else :
                # print("bbb")
                ans = s_ans
        else :
            if (x // d) % 2 == 0:
                ans = s_ans
            else :
                ans == i_ans
else :
    if abs(x + (x // d * d)) < abs(x + (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x + (x // d * d))
        s_ans = abs(x + (((x // d) + 1) * d))
    else :
        # print('2採用')
        i_ans = abs(x + (((x // d) + 1) * d))
        s_ans = abs(x + (x // d * d))

    if k <= (x // d) :
        ans = x + (k * d)
    else : 
        if k % 2 == 0 :

            if (x // d) % 2 == 0 :
                ans = i_ans
            else :
                ans = s_ans
        else :
            if (x // d) % 2 == 0:
                ans = s_ans
            else :
                ans == i_ans

print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
s = X//D
a = X%D
z = K-s
if z % 2 == 0:
    print(a)
elif z % 2 == 1:
    if D / 2 < a:
        print(D - a)
    elif D / 2 == a:
        print(D-a)
    else:
        print(D-a)

Submission status: Wrong Answer

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

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())
mini = 10**16

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
flag = 0
times = 0
if x > d:
    # print("a")
    for i in range(k):
        if x - i * d <= 0:
            times = i - 1
            flag = 1
            break
        else:
            flag = 0
    # print(flag)
    if flag == 1:
        
        k = k - times
        # print(times, k, x)
        x = x - times * d
        if k % 2 == 0:
            print(abs(x))
        else:
            print(abs(x - d))
    else:
        print(abs(x - d))
else:
    if k % 2 == 0:
        print(abs(x))
    else:
        print(abs(x - d))