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:
from sys import stdin


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

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


main()

Submission status: Wrong Answer

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

init = x

for i in range(round(abs(x) // abs(d))+1):
    tempx = x + i*d - (k-i)*d
    init = min(init, abs(tempx))

print(init)

Submission status: Wrong Answer

Solution code:
import sys

line = sys.stdin.readline()

arr = line.rstrip().split(" ")
x = int(arr[0])
k = int(arr[1])
d = int(arr[2])

max = x // d

if k < max:
    print( x - ( k * d ) )
    sys.exit()

maxv = x % d

if 0 == (k - max) % 2:
    print(maxv)
    sys.exit()


maxnv = d - maxv
print(maxnv)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
x, k, d = map(int, input().split())
ans = x


for bit in range(2**k):
    point = x
    for i in range(k):
        if ((bit >> i) & 1):
            point += d
        else:
            point -= d
    ans = min(abs(ans), abs(point))

print(ans)

Submission status: Wrong Answer

Solution code:
def is_odd(n):
    if n % 2:
        return True
    return False

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

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

Submission status: Wrong Answer

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

if x < 0:
    x *= -1

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if x>=0:
    while x > 0:
        x = x-d
        # print('x',x)
        k-=1
        if k == 0:
            # print('a',x)
            break
        if x==0:
            if d%2==0: 
                x=d
                d=0
                # print('aa',d,x)
            else: 
                x=0
                d=0
            break
    # print(abs(x),abs(x+d))
    print(min(abs(x),abs(x+d)))
else:
    while x < 0:
        x = x+d
        # print('x',x)
        k-=1
        if k == 0:
            # print('a',x)
            break
        if x==0:
            if d%2==0: 
                x=d
                d=0
                # print('aa',d,x)
            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())
jud = (x//d)-1
if(k<=jud):
  print(abs(abs(x)-d*k))
else:
  if((k-jud)%2 == 1):
    print(abs(abs(x)-(jud+1)*d))
  else:
    print(abs(abs(x)-(jud)*d))

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int, input().split())
n=(K*D-X)//(2*D)
if n<0:
    ans=abs(X-K*D)
else:
    ans1=abs(X+(2*n-K)*D)
    ans2=abs(X+(2*(n+1)-K)*D)
    ans=min(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
if abs(x) >= k*d:
  print(abs(x)-k*d)
else:
  if (k - abs(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())


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

Submission status: Wrong Answer

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

tmp1 = x - d * (x // d + 1)
tmp2 = tmp1 + d

if x >= k * d:
    print(x - k * d)
elif x == d and k % 2 == 0:
    print(x)
elif k % 2 == 0:
    print(min(abs(tmp1), abs(tmp2)))
else:
    print(max(abs(tmp1), abs(tmp2)))

Submission status: Wrong Answer

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

q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    if K%2 != q%2:
        a1 = mod - D
        a2 = mod + D
        ans = min(abs(a1),abs(a2))
    else:
        ans = mod

print(ans)

Submission status: Wrong Answer

Solution code:
#from math import abs
x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
if(x<=0):
    if(x+k*d<=0):print(x+k*d)
    else:
        res=x%d
        k-=abs(res-x)//d
        if(k%2):
            ans1=res+d
            ans2=res-d
            if(abs(ans1)>abs(ans2)):print(ans2)
            else:print(ans1)
        else:print(res)
else:
    if(x-k*d>=0):print(x-k*d)
    else:
        res=x%d
        k-=abs(res-x)//d
        if(k%2):
            ans1=res+d
            ans2=res-d
            if(abs(ans1)>abs(ans2)):print(ans2)
            else:print(ans1)
        else:print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if k % 2 != 0:
  x -= d    

print(abs(x))

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
i = math.floor((d*k - x)/(2*d))
print(min([abs(x-d*k+2*d*j) for j in range(i-1, i+2)]))

Submission status: Wrong Answer

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

import numpy as np

if X >= K*D:
    print(X-K*D)
else:
    a = X // D
    b = a + 1
    if a % 2 != K % 2:
        print(np.abs(b*D - X))
    else:
        print(np.abs(X - a*D))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ch = k%2
ans = abs(x)
cnt = 0
while True:
    if x == 0:
        if ((cnt-1)%2) == ch:
            print(0)
            break
        else:
            print(abs(d))
            break
    if x//d > k:
        if x > 0:
            print(abs(x-d*k))
        elif x < 0:
            print(abs(x+d*k))
        break
    if x > 0:
        cnt += x//d
        x -= d*(x//d)
        xx = x - d
        if cnt%2 == ch:
            print(abs(x))
        else:
            print(abs(xx))
        break
    elif x < 0:
        cnt += abs(x)//d
        x += d*(abs(x)//d)
        xx = x + d
        if cnt%2 == ch:
            print(abs(x))
        else:
            print(abs(xx))
        break

Submission status: Wrong Answer

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

if x>0:
    if x>k*d:
        ans=x-k*d
    else:
        X = x-round(x//d)*d
        if (k-round(x//d))%2==0:
            ans=abs(X)
        else:
            ans=abs(X-d)
else:
    if abs(x)>k*d:
        ans=x+k*d
    else:
        X=x+round(abs(x)//d)*d
        if (k-round(abs(x)//d))%2==0:
            ans=abs(X)
        else:
            ans=abs(X+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:
    if (k-x//d)%2==0:
        print(min(x%d,abs(x%d-d)))
    else:
        print(max(x%d,abs(x%d-d)))

Submission status: Wrong Answer

Solution code:
X, K, D = map( int, input().split())
d = D
x = X
while True:
  R = x % d
  x, d = d, R
  if ( R == 0 ):
    break
Y = X // D
if Y % 2 != 0:
  Y += 1
A = min( K, Y )
print( max( abs( abs( X ) - abs( A * D )),  d ) )

Submission status: Wrong Answer

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

for i in range(K):
  if 0<X:
    X -= D
  elif X<0:
    X += D
  else:
    if i%2==0:
      break
    else:
      X += D
      break

print(abs(X))

Submission status: Wrong Answer

Solution code:
import numpy as np
[X, K, D] = list(map(int, input().split()))
X = np.abs(X)
if X != D:
  for k in range(1, K + 1):
    if X > D:
      X = X - D
    else:
      X = X + D
else:
  if K % 2 == 0:
    pass
  else:
    X = 0
print(X)

Submission status: Wrong Answer

Solution code:

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

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


main()

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
s=[x]
for i in range(k):
    if x>0:
        x=x-d
        ans=x
    else:
        x=x+d
        ans=x
    s.append(x)
    if abs(s[i-2])==abs(s[i]) and i>=2:
        ans=s[i]
        break
        
print(abs(ans))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if X > 0:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X - D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X- D*tmp
        X2 = X1 - D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
else:
    tmp = abs(X) % D
    if tmp  >= K:
        ans = X + D*K
    elif tmp < K:
        nK = K - tmp
        X1 = X + D*tmp
        X2 = X1 + D
        if nK %2 == 0:
            ans = X1
        else :
            ans = X2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n = abs(x)//d
if n <= k :
    x -= d*n
    count = k-n+2
    if count % 2 == 0 :
        now = abs(x)
    else :
        now = d-abs(x)
else :
    now = abs(x) - k*d

print(abs(now))

Submission status: Wrong Answer

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

def d(h):
    return X + (h - (K-h))*D

if d(s) >= 0:
    ans = d(s)
elif d(g) <= 0:
    ans = d(g)
else:
    half = (s + g)//2
    while s < half:
        if d(half) < 0:
            s = half
        else:
            g = half
        half = (s + g)//2

    ans = min(abs(d(s)) , abs(d(g)) )
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

k = K
x = X
while True:
    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()

    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

Submission status: Wrong Answer

Solution code:
def zet(num):
    if num<0:
        return -num
    else:
        return num


x,k,d = map(int, input().split())
amari = x%d
syou = x//d
if syou > k:
    syou = k
else:
    syou = syou
sa = k-syou
genzai = zet(x) - syou * d

if sa % 2 == 0:
    if amari*2 < d:
        print(genzai)
    else:
        print(zet(genzai - d))
else:
    print(zet(genzai - d))

Submission status: Wrong Answer

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

if x-k*d>=0:
    print(x-k*d);exit()
p=x%d
kai=(x-p)//d
if kai%2==k%2:
    print(p)
else:
    print(abs(d-p))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
import collections

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

X,K,D = MI()
flag = 1
if(X<0):
  flag = -1

n = int(X/D)
if(n < K):
  X = X-n*D
  K = K - n
else:
  X = X -K*D*flag
  print(abs(X))
  exit()

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

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    a = [int(i) for i in input().split()]
    x1 = a[0] // a[2]

    if x1 > a[1]:
        print(abs(abs(a[0])-a[1]*a[2]))
    else:
        od = (a[1] -x1)%2
        if od == 0:
            print(min(
                abs(abs(a[0]) - (x1-2) * a[2]),abs(abs(a[0]) - x1 * a[2]),abs(abs(a[0]) - (x1+2) * a[2])
            ))
        else:
            print(min(
                abs(abs(a[0]) - (x1-1) * a[2]),abs(abs(a[0]) - (x1+1) * a[2]),abs(abs(a[0]) - (x1+3) * a[2])
            ))

Submission status: Wrong Answer

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

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

else:
	k-=x//d
	x-=(x//d)*d
	rem=k%2
	if rem:
		print(d-x%d)
	else:
		print(x%d)

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
t=a//c
if a%c==0:

        if t<=b and (b-t)%2==0:
            print(0)
        elif t<b and (b-t)%2!=0:
            print(c)
        else:
            print(a-b*c)
else:
    if t<=b and (b-t)%2==0:
        print(a%c)
    elif t<b and (b-t)%2!=0:
        print(c-a%c)
    else:
        print(a-b*c)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
div,rem = divmod(X,D)
if div<=K and (div-K)%2 == 0:
    print(rem)
elif div<=K and (div-K)%2 == 1:
    print(min(abs(rem-D),abs(rem+D)))
else:
    print(X-K*D)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 17:44:21 2020

@author: Paras
"""

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

moves = abs(x)//d
if moves>k:
    print(abs(x)-(k*d))
    
elif moves==k:
    print("0")
    
else:
    closest1 = abs(x)-moves*d
    closest2 = closest1-d
    if closest1==closest2:
        if closest1==0:
            leftmoves = k-moves
            if leftmoves%2==0:
                print("0")
            else:
                print(closest1+d)
        else:
            print(closest1)
    else:
        if (k-moves)%2==0:
            print(abs(closest1))
        else:
            print(abs(closest2))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
R=[]
for i in range(1,k+1):
    res=abs(x-i*d)
    R.append(res)
print(min(R))

Submission status: Wrong Answer

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

if (K-q)%2==0:
    ans=mod
else:
    ans=D-mod

print(ans)

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 = abs(int(x//d))
    if di >= k: 
        return abs(x) - (k*d)
    x = x - (di*d)
    k -= di
    if k % 2 == 0:
        return abs(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())
a = 0
b = k + 1
mid = 0
if x < 0:
    x *= -1
while b - a > 1:
    mid = (a + b) >> 1
    if x - d * mid >= 1:
        a = mid
    else:
        b = mid

ans = x - a * d
if a < k:
    y = abs(ans - d)
    if y >= 1 and y < ans:
        ans = y
print(ans)

Submission status: Wrong Answer

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

print(x,k,d)

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

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


# 実装を行う関数
def resolve(test_def_name=""):
    x, k, d = 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:
        ans = min(abs(x + k), abs(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 = """7 1 3"""
        output = """4"""
        self.assertIO(test_input, output)

        test_input = """7 2 3"""
        output = """1"""
        self.assertIO(test_input, output)

        test_input = """7 3 3"""
        output = """-2"""
        self.assertIO(test_input, output)

        test_input = """7 5 3"""
        output = """-2"""
        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:
def main2():
    X, K, D = map(int, input().split())

    r, m = divmod(X, D)
    mi = m
    K -= r

    K = K % 2

    if K == 0:
        print(mi)
    else:
        print(min(abs(mi-D), abs(mi+D)))

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

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

    y = x

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


    print(abs(x))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
playerinput = input()
lst = playerinput.split(" ")

X = int(lst[0])
K = int(lst[1])
D = int(lst[2])

lowest_value = X
while K != 0:
    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1
    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
    else:
        K -= 1
print(lowest_value)

Submission status: Wrong Answer

Solution code:
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

X,K,D = LI()

sho = X // D
amari = X % D

if (sho>=K):
    saigo = X - (K*D)
    print(saigo)
else:
    nokori = K - sho
    if nokori % 2 == 0:
        print(amari)
    else:
        ans = min(amari+D,amari-D)
        print(abs(ans))

Submission status: Wrong Answer

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

a=X//D
if X<0:
    b=a-1
else:
    b=a+1
if b<=K:
    if b%2==K%2:
        s=abs(X-D*b)
    else:
        s=abs(X-D*a)
else:
    s=abs(X-D*K)
print(s)

Submission status: Wrong Answer

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

move_count = 0
D = abs(D)
while True:
  if X >0:
    if X - D >0:
      X = X-D
      move_count += 1
    else:
      break
  else:
    if X + D < 0:
      X = X + D
      move_count += 1
    else:
      break
  if K - move_count ==0:
    print(X)
    break
if not K - move_count == 0:  
  if (K - move_count)%2 == 0:
    print(X)
  else:
    if X > 0:
      if X < abs(X - D):
        print(X)
      else:
        print(abs(X-D))

    else:
      if abs(X) < abs(X+D):
        print(X)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
 
#偶数の場合
if X%D ==0:
    print(X)
elif 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:
X,K,D=map(int,input().split())
a=round(abs(X/D))
if K<a:
  print(abs(X-K*D))
elif (K-a)%2==0:
  l=[]
  l.append(abs(X-a*D))
  l.append(abs(X-(a+2)*D))
  l.append(abs(X-(a-2)*D))
  print(min(l))
elif (K-a)%2!=0:
  l=[]
  l.append(abs(X-(a+1)*D))
  l.append(abs(X-(a+3)*D))
  l.append(abs(X-(a-1)*D))
  print(min(l))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
goal=X-K*D
y1=X%D
y2=y1-D

if y1<goal:
    print(int(goal))
else:
    if y1<abs(y2):
        L=(X-y1)/D
    else:
        L=(X-y2)/D
    if L%2==0:
        print(int(min(y1,abs(y2))))
    else:
        print(int(max(y1,abs(y2))))

Submission status: Wrong Answer

Solution code:
n,k,m = map(int,input().split())
#print(k*m,n)
if k*m > n:
    #print(min(n%m, n-(n%m)))
    x = n//m;
    left = n - x*m;
    right = abs(left - m);
    k = k - x;
    if k&1:
        print(right)
    else:
        print(left)
    
else:
    print(n - k*m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int, input().split())
if x>d*k:
	ans=x-k*d
else:
	k=k-x//d
	x=x%d
	if k%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())

t = X // D
if X < 0: sign = -1
else: sign = 1

if K < abs(t):
    x = X - sign*K*D
else:
    x = X - t*D

if (K - abs(t)) % 2 == 1:
    x = D - abs(x)

print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D= list(map(int, input().split()))
a, b = X // D, X % D
ans = X
if a < 0:
    a = abs(a + 1)
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:
L = list(map(int, input().split()))
X = L[0]
K = L[1]
D = L[2]

if X >D*K:
    print(abs(X-D*K))
elif X< -D*K:
    print(abs(X+D*K))
    
if X>=0 and X<D*K:
    N = int(abs(X)/D)
    e = X - N*D
    if abs(e) > abs(e-D):
        print(abs(e))
    else:
        print(abs(e-D))

if X<0 and X>-D*K:
    N = int(abs(X)/D)
    e = X - N*D
    if abs(e) > abs(e-D):
        print(abs(e))
    else:
        print(abs(e-D))

Submission status: Wrong Answer

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

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

print(abs(ans))

Submission status: Wrong Answer

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

k, x = divmod(X, D)
if K >= k:
    X = x
    K -= k
    if k % 2 == 0:
        print(abs(X))
    elif X < 0:
        print(abs(X + D))
    else:
        print(abs(X - D))
elif X < 0:
    print(abs(X + D * K))
else:
    print(abs(X - D * K))

Submission status: Wrong Answer

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

r = X // D

if r <= K+1:
    a1 = X - D*r
    a2 = (X - D*(r+1))*(-1)
    print(a1, a2)
    print(min(a1, a2))
elif r == K:
    print(X - D*r)
else:
    print(X - D*K)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    X, K, D = map(int, input().split())

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

x = abs(X)
# 相殺するために必要な回数
n = round(X / D)

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

Submission status: Wrong Answer

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

if x >= 0:
  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))

if x < 0:
  if abs(x)//d >= k:
    print(x + k*d)

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