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

    if k % 2 == 0:
        ans = 0
    else:
        ans = d

elif x > 0:
    amari = x % d
    kouho1 = amari
    kouho2 = amari - d
    kyori1 = abs(x - kouho1)
    kyori2 = abs(x - kouho2)
    if kyori1 // d > k:
        ans = x - k * d

    elif (kyori1 // d) % 2 == 1:
        if k % 2 == 1:
            ans = abs(kouho2)
        else:
            ans = abs(kouho1)
    else:
        if k % 2 == 0:
            ans = abs(kouho1)
        else:
            ans = abs(kouho2)
else:
    x *= -1
    amari = x % d
    kyori1 = abs(x - amari)
    kyori2 = abs(x - (amari - d))
    if kyori1 // d > k:
        ans = abs(x - k * d)
    elif (kyori1 // d) % 2 == 1:
        if k % 2 == 1:
            ans = abs(amari)
        else:
            ans = abs(amari - d)
    else:
        if k % 2 == 0:
            ans = abs(amari)
        else:
            ans = abs(amari - d)
print(ans)

Submission status: Wrong Answer

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

ans = 10**15+1
if K % 2 == 0 and X == D:
    print(X)
    sys.exit()
elif K % 2 == 1 and X == D:
    print(-X)
    sys.exit()
else:
    for i in range(K):
        if X >= 0:
            X -= D
        else:
            X += D
        if X <= abs(ans):
            ans = X

print(abs(ans))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int, input().split())
min=X+K*D
for k in range(K):
  current=X-k*D+(K-k)*D
  if min>abs(current):
    min=abs(current)
print(min)

Submission status: Wrong Answer

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

if k*d<=x:
    print(x-k*d)
elif -k*d>=x:
    print(x+k*d)
else:
    a = (k*d-x)//(2*d)
    
    ans = -(2*d*a-k*d+x)
    _ans = (2*d*(a+1)-k*d+x)
    print(min(ans,_ans))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
if x>=k*d:
    print(x-k*d)
else:
    temp = x//d
    if temp%2==k%2:
        min_ = temp
        max_ = temp+2
    else:
        min_ = temp-1
        max_ = temp+1
    if max_ > k:
        max_ = min_
    #print(min_,max_)
    ans = min(abs(x-d*min_),abs(x-d*max_))
    print(ans)

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8

import math


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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math

X,K,D = map(int, input().split())
for i in range(K):
  if abs(X-D) < abs(X+D):
    X-=D
  else:
    X+=D
print(X)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

if 0 <= X:
    ans = X-D*K
    print(ans)
if X < 0:
    ans = X+D*K
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	x, k, d = map(int, sys.stdin.readline().split())
	x = abs(x)
	if x == 0:
		if k % 2 == 0:
			print(0)
		else:
			print(d)
		exit()
	if x % d == 0:
		if x // d >= k:
			print(x - d * k)
		else:
			if (x // d) % 2 == 0:
				print(0)
			else:
				print(d)
	else:
		if x // d >= k:
			print(x - d * k)
		else:
			if x >= d:
				if x % d < abs(x % d - d):
					if (k - (x // d)) % 2 == 0:
						print(x % d)
					else:
						print(abs(x % d - d))
				else:
					if (k - (x // d + 1)) % 2 == 0:
						print(abs(x % d - d))
					else:
						print(x % d)
			else:
				if k % 2 == 0:
					print(x)
				else:
					if abs(x - d) < x + d:
						print(abs(x - d))
					else:
						print(x + d)




main()

Submission status: Wrong Answer

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

x=abs(x)
min=x

smin=x

if k%2==1:
    smin-=d
    k-=1

while(-x<smin and k>0):
    smin-=d*2
    k-=2

for t in range(smin,x,d*2):
    tt=abs(t)
    if tt<min:
        min=tt

print(min)

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
    print(k, X)
else:
  if K % 2 == 0:
    pass
  else:
    X = 0
print(X)

Submission status: Wrong Answer

Solution code:

X, K, D = map(int, input().split())
ref = abs(X) // D
cnt = K - ref
if abs(X) > K*D:
    print(abs(X)-K*D)
elif cnt % 2 == 0:
    print(abs(abs(X-D*ref)))
else:
    print(abs(abs(X-D*(ref+1))))

Submission status: Wrong Answer

Solution code:
#C - Walking Takahashi

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

p = abs(x)

if p/d < k:
  k = k - (p // d)
  p = p % d

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

elif x >= 0:
  print(x - (k * d))
else:
  print(x + (k * d))

Submission status: Wrong Answer

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

print(abs(now))

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

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

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

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

k -= n

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
if x-k*d>=d:print(x-k*d)
else:
    num1,num2=x%d,d-x%d
    if num1==num2:print(num1)
    else:
        A,B=(x//d)%2,k%2
        if A==0 and B==0:
            print(num1)
        elif A==1 and B==1:
            print(num2)
        elif A==1 and B==0:
            print(num2)
        else:print(num1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import math
# スペース区切りの整数の入力
X, K, D = map(int, input().split())

cross = abs(math.ceil(X / D))

if cross >= K:
    for i in range(K):
        if X >= 0:
            X -= D
        else:
            X += D
    print(X)
else:
    for i in range(cross):
        if X >= 0:
            X -= D
        else:
            X += D
    
    if (K-cross)%2 == 0:
        print(X)
    else:
        if X >= 0:
            print(X-D)
        else:
            print(X+D)

Submission status: Wrong Answer

Solution code:
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()


s,k,d = Ri()
flag = False
if s < 0:
    flag = True

totk=  k
s = abs(s)
times = s//d

minn = s-k*d
if minn >= 0:
    print(s-k*d)
else:
    pos1 = s-times*d
    pos2 = INF
    k-=times
    if pos1 == 0:
        if k %2 == 1:
            print(d)
        else:
            print(0)
        exit()
    pos2 = abs(pos1 - d)
    if k == 1:
        print(pos2)
    elif totk % 2 == 0:
        print(pos1)
    else:
        print(min(pos1, pos2))

Submission status: Wrong Answer

Solution code:
x,k,d = input().split()
x = int(x)
k = int(k)
d = int(d)
# print(x,k,d)
if x<k*d:
	print(abs(x-k*d) % (2*d))
else:
	print(x-k*d)
# eval("eval(\"print(\"hello\")\")")

# # a = "1234"
# # al = list(a)
# # al.insert(1,".")
# # print(al)


# str = input()
# l = list(str)
# n = len(str)
# i = 0;
# while i<n:
# 	if(l[i]=="("):
# 		l.insert(i,"*")
# 		l.insert(i,"*")
# 		n = n+2
# 		i=i+2
# 	i = i+1
# # print(l)
# print(eval("".join(l)))

Submission status: Wrong Answer

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

sho_p, amari_p = abs(x)//d, abs(x)%d
sho_m, amari_m = sho_p+1, abs(amari_p - d)

if amari_p < amari_m:
  flag = sho_p%2
  sho = sho_p
  amari = amari_p
elif amari_p > amari_m:
  flag = sho_m%2
  sho = sho_m
  amari = amari_m
else:
  flag = 2
  sho = sho_p
  amari = amari_p
    
if (k%2)==flag:
  if sho<=k:
    print(amari)
  else:
    print(x-(k*d))
elif flag==2:
  if sho<=k:
    print(amari)
  else:
    print(x-(k*d))
else:
  if sho<=k:
    print(max(amari_m, amari_p))
  else:
    print(x-(k*d))

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(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())
if D>= X:
    if K%2 == 0:
        ans = max(X, -X)
    else:
        ans = max(X-D, D-X)
elif K<((max(X, -X))//D):
    ans = max(X, -X)-(D * K)
else:
    N = (max(X, -X))//D
    if (K-N)%2 == 0:
        ans = max(X, -X)-(D * N)
    else:
        ans = -(max(X, -X)-(D * (N+1)))
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 abs(x-d) > abs(x+d):
    x = x+d
  else:
    x = x-d
print(abs(x))

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP

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

X = abs(X)

max_distance = K * distance

if max_distance <= X:
    print(X - max_distance)
else:
    # Xをdistanceで割って、何回の移動で0を超えるのかを判断
    over_zero_movecount = Decimal(str(X / distance)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)

    # 0を超えたときの座標の絶対値(1)
    over_zero_x = abs(X - over_zero_movecount * distance)
    # 1つ前の移動の絶対値(2)
    over_zero_x_before = abs(X - (over_zero_movecount - 1) * distance)

    # 0を超えた後の残りの移動回数が偶数回の場合
    if (K - over_zero_movecount) % 2 == 0:
        print(over_zero_x)
    # 0を超えた後の残りの移動回数が奇数回の場合
    else:
        print(over_zero_x_before)

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split(' '))
def solve(x, k, d):
    if x%d == 0:
        if k%2 == 0:
            return x
        else:
            return 0
    for i in range(k):
        if abs(x-d) <= abs(x):
            x -= d
        else:
            x += d
    return abs(x)
print(solve(x, k, d))

Submission status: Wrong Answer

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

if x < 0:
    x *= -1

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:
#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):print(res-d)
        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):print(res-d)
        else:print(res)

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:
import math
X,K,D = map(int,input().split())
if(X>0):
    posneg = 0
else:
    posneg = 1

if(posneg == 0):
    Ki = math.ceil(X/D)
else:
    Ki = abs(math.floor(X/D))
D = D*(-1)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
mind = 1000000000000000
c = 0
while k > 0:
    k -= 1
    if abs(x - d - 0):
        x -= d
    else:
        x += d
    mind = min(mind, x)
    if x == mind:
        c += 1
    if c == 2:
        break
if k%2 == 0 or k == 0:
    print(abs(mind - 0))
else:
    mind = min(abs(mind + d - 0), abs(mind - d - 0))
    print(mind)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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)
    else:
        n = x//d
        print(min(abs(x-n*d), abs(x-(n-1)*d)))
else:
    if x+k*d <= 0:
        print(abs(x+k*d))
    else:
        n = ((-1)*x)//d
        print(min(abs(x+n*d), abs(x+(n-1)*d)))

Submission status: Wrong Answer

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

h = []
k = 0
for i in range(K):
    k = X + (2 * i - K) * D
    if k < 0:
        k = -k
    h.append(k)
print(min(h))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import math
def main():
    x,k,d=map(int, input().split())
    res = float('inf')

    # res = min(res, x-i*d)でk-iが偶数となるようなiを二分探索(0 < i < k)

    l = 0
    r = k+1

    while(l+1!=r):
        mid = (l+r)//2
        if((k-mid)%2==0):
            res = min(res, abs(abs(x)-mid*d))
            res = abs(res)
        if(abs(abs(x)-mid*d) > x):
            r = mid
        else:
            l = mid

    print(res)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
i = list(map(int,input().split()))
x = i[0]
k = i[1]
d = i[2]
ans = abs(x)
ans_n_1 = abs(x)
if abs(x)>abs(k)*abs(d):
    ans = abs(x)-abs(k)*abs(d)
else:
    for idx in range(k):
        ans = abs(ans-abs(d))
        if ans >= ans_n_1:
            ans = ans_n_1
            break
        else:
            ans_n_1 = ans
print(ans)

Submission status: Wrong Answer

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


B, A = divmod(abs(X), D)

if B <= K:
    print(abs((A - ((K-B)%2) * D )))

else:
    print(X-K*D)

Submission status: Wrong Answer

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

    if K*D > abs(X):
        temp = K - X // D

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

    lasti = X // D
    X = X - D*lasti

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

    if abs(X)-D < abs(X):
        X -= D
        lasti += 1


    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= [int(i) for i in input().split()]
l = x // d
if k >= l:
    k -= l
    if x >= 0:
        x -= d*l
    else:
        x += d*l
    k = k % 2
    if k:
        if x >= 0:
            x -= d
        else:
            x += d
else:
    if x >= 0:
        x -= d*k
    else:
        x += d*k
print(abs(x))

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

#x = abs(x)
#0に最も近い距離になる。
ans = x % d
#ansへの移動回数分、kから引く
if k- (x//d) < 0:
    print(x - k*d)
    exit()
else:
    k -= x//d

if k % 2 == 0:
    print(ans)
else:
    t1 = abs(ans-d)
    t2 = abs(ans+d)
    print(min(t1, t2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def solve():
    if X // D >= K:
        return X - D*K
    if ((X//D)%2 == 0) ^ (K%2 == 0):
        return D - (X - D*(X//D))
    else:
        return X - D*(X//D)


if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

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

mv = int(-X / D + 0.5)
if (abs(mv)>K): mv = mv * K / abs(mv)
residual_moves = K - abs(mv)

if residual_moves % 2 == 0:
    print (int(abs(X + D * mv)))
else:
    posa = abs(X+D*(mv+1))
    posb = abs(X+D*(mv-1))
    posc = abs(X+D*(mv+2))
    posd = abs(X+D*(mv-2))
    print(int(min(posa, posb, posc, posd)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = 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( X - A * D ),  d ) )

Submission status: Wrong Answer

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

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

if k != 0 and k % 2 == 1:
    b = abs(x-d)
    c = abs(x+d)
    x = min(b,c)

print(x)

Submission status: Wrong Answer

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

x=abs(x)

kai=x//d
right=x%d
left=abs(x%d-d)

if (kai+k)%2==0:
    print(right)
else:
    print(left)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(abs(now))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
if x>=k*d:
    print(x-k*d)
else:
    temp = x//d
    if temp%2==k%2:
        min_ = temp
        max_ = temp+2
    else:
        min_ = temp-1
        max_ = temp+1
    #print(min_,max_)
    ans = min(abs(x-d*min_),abs(x-d*max_))
    print(ans)

Submission status: Wrong Answer

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

if X < 0:
    X = -X

if K % 2 != 0:
    X -= D
    K -= 1

if X < 0:
    X = -X

count = X // D

if count > K:
    print(X - K * K)
else:
    K //= 2
    D *= 2
    count = X // D
    X -= count * D
    print(min(X, D - X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = abs(x)

for _ in range(k):
    if k % 2 == 0 and x == d:
        break

    pre = ans
    if x < 0:
        ans = min(ans, abs(ans+d))
    if x >= 0:
        ans = min(ans, abs(ans-d))

    if pre <= ans:
        break

print(ans)

Submission status: Wrong Answer

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

if a < b:
    print(a)
else:
    print(b)

Submission status: Wrong Answer

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

ans = 0
if x < 0:
    x = abs(x)
if x > (k*d):
    ans = (x - (k*d))
elif x <= d:
    if k % 2:
        ans = d - x
    else:
        ans = x
else:
    count = x
    for i in range(k):
        nex = abs(count- d)
        if nex < count:
            count = nex
        else:
            ans = count
            break

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

Submission status: Wrong Answer

Solution code:
a = input().split()
x = int(a[0])
k = int(a[1])
d = int(a[2])

if (x == 0):
    if (k % 2 == 0):
        res = 0
    else:
        res = d
else:
    if ((k * d) > abs(x)):
        y = abs(x)
        res = abs(abs(x) - y * d)
        if ((k - y) % 2 == 1):
            res = abs(d - res)
    else:
        res = abs(x) - k * d
print(res)

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&1 == K&1:
    print(abs(X-(D*a)))
else:
    print(abs(D*(a+1)-X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = [int(x) for x in input().split()]
if X // D < K:
    rest = K - (X // D)
    if rest % 2:
        if X > 0:
            X -= D
        elif X < 0:
            X += D
    print(min(X%D, abs(X%D - D)))
else:
    if X < 0:
        print(abs(X + K*D))
    else:
        print(abs(X - K*D))

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(abs(abs(a[0]) - x1 * a[2]))
        else:
            print(abs(abs(a[0]) - (x1+1) * a[2]))