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
while True:
    try:
        x, k, d = map(int, input().split());
        if x == 0:
            k %= 2;
            if k:
                print(abs(x - d));
            else :
                print(0);
        elif abs(x) - k * d > 0:
            print(abs(x) - k * d);
        elif x > 0:
            k -= x // d;
            k %= 2;
            x -= x // d * d;
            if k:
                print(abs(x - d));
            else:
                print(x);
        else:
            num = abs(x) // d + 1;
            x += num * d;
            k -= num;
            if k:
                print(abs(x - d));
            else:
                print(x);
    except EOFError:
        break;

Submission status: Wrong Answer

Solution code:
import sys
X,K,D= map(int, input().split())
if X>0 and X-K*D>0:
    print(X-K*D)
    sys.exit()
if X<0 and X+K*D<0:
    print(K*D-X)
    sys.exit()
if X<0:
    X=-X
n=int(X/D)
if (K-n)%2==0:
    print(X-n*D)
else:
    print((n+1)*D-X)

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)
    print(min(a1, a2))
elif r == K:
    print(X - D*r)
else:
    print(X - D*K)

Submission status: Wrong Answer

Solution code:
import math
from collections import defaultdict,deque
from itertools import permutations
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
ips=lambda:input().split()

"""========main code==============="""

t=1
#t=ii()
for _ in range(t):
    a,b,c=ml()
    rem=a//c
    rem=abs(rem)
    if(b>=rem and b%2==rem%2):
        ans=abs(abs(a)-c*rem)
        print(ans)
    elif(b%2!=rem%2):    
        ans=abs(abs(a)-c*(rem+1))
        print(ans)
        
    else:
        print(abs(abs(a)-c*b))

Submission status: Wrong Answer

Solution code:
import math

X,K,D = map(int, input().split())
tm = round(X/D)
if K <= tm:
  print(abs(X-D*K))
else:
  if (K-tm)%2==1:
    if math.modf(X/D)[0]<0.5:
      print(abs(X-D*(tm+1)))
    else:
      print(abs(X-D*(tm-1)))
  else:
    print(abs(X-D*tm))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

q = X // D
mod = X % D

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def function(X, K, D):
    kai = int(abs(X)/D)
    if kai > K:
        if X > 0:
            return X-K*D
        else:
            return X+K*D
    else:
        K = K-kai
        if X > 0:
            X = X-kai*D
        else:
            X = X+kai*D
        if K % 2 == 0:
            return X
        else:
            if X > 0:
                if abs(X-D) < X:
                    return X-D
                else:
                    return X
            else:
                if abs(X+D) < abs(X):
                    return X+D
                else:
                    return X


if __name__ == '__main__':
    X, K, D = input().split()
    X = int(X)
    K = int(K)
    D = int(D)
    print(function(X, K, 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:
    tmp = abs(ans - d)
    print(min(ans, tmp))
else:
    t1 = abs(ans-d)
    t2 = abs(ans+d)
    print(min(t1, t2))

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 = 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())
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:
def main():
	X, K, D = map(int, input().split())

	
	if abs(X) > K * D:
		print(abs(abs(X) - K * D))
	elif abs(X) <= K * D:
		s = abs(X) // D
		if abs(X) % D == 0:
			if s % 2 == 0 and K % 2 == 0:
				print(0)
			elif s % 2 == 0 and K % 2 == 1:
				print(D)
			elif s % 2 == 1 and K % 2 == 0:
				print(D)
			elif s % 2 == 1 and K % 2 == 1:
				print(0)
		else:
			print(min(abs(abs(X) - s * D - D), abs(abs(X) - s * D)))

 
if __name__ == "__main__":
  	main()

Submission status: Wrong Answer

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

B, A = divmod(X, D)

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

else:
    print(X-K*D)

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 k:
		print(d-x%d)
	else:
		print(x%d)

Submission status: Wrong Answer

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

#最大まで移動するとき
if abs(X) >= K * D:
    print(abs(X) - K*D)
    exit()
if X >= 0:
    bef = X % D
    nex = bef - D
    
    if (K - X//D) % 2 == 0:
        print(abs(bef))
    else:
        print(abs(nex))
else:
    bef = abs(X) % D
    nex = bef + D

    if (K - X//D) % 2 == 0:
        print(abs(bef))
    else:
        print(abs(nex))

Submission status: Wrong Answer

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

if abs(X) >= K*D:
  print(abs(X)-K*D)
else:
  n = 1
  while abs(X) > n*D:
    n += 1
  if (K-n)%2 == 0:
    print(abs(X)-n*D)
  else:
    print(abs(X)-(n-1)*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(x + k * d)
else:
    a = x // d
    b = x % d
    if b <= abs(b-d):
        if a % 2 == k % 2:
            print(b)
        else:
            print(abs(b-d))
    else:
        if (a+1) % 2 == k % 2:
            print(abs(b-d))
        else:
            print(b)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
 
#偶数の場合
if K %2 == 0:
  if X//D < K:
    print(X%D)
  else:
    print(X%D + (X//D -K)*D)
else:
  if X//D < K:
    if abs(X%D-D) < abs(X%D +D):
      print(abs(X%D-D))
    else:
      print(abs(X%D +D))
  else:
    print(X%D + (X//D -K)*D)

Submission status: Wrong Answer

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

for i in range(k):
    if x < 0:
        x = x + d
    else:   
        x = x - d

print(x)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
#N = int(input())
X,K,D= list(map(int,input().split()))
cnt = 0
if X <= 0:
    X *=-1

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

while X >= 0 :
    X -= D
    cnt += 1

if ((K-cnt) % 2 == 0):
    print(X*-1)
else :
    print(X+K)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
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)%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)
    else:
      print(abs(X+D))

Submission status: Wrong Answer

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


def solve(x,k,d):
    di = int(x//d)
    if di >= k: 
        return 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())

k_min=x//d
if k_min>=k:
  k_min=k

if x>0:
  d_min=x-((k_min)*d)
else:
  d_min=x+(-(k_min)*d)
dk=k-k_min
if dk%2==0:
  print(d_min)
else:
  print(abs(min(d_min+d,d_min-d)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

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

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

X,K,D = LI()

sign = np.sign(X)
x = abs(X)

if x//D==0:
    if K%2==0:
        ans = X
    else:
        ans = -1 * sign * (x - D)
elif x//D>K:
    ans = sign * (x-K*D)
else:
    # 残り回数が偶数回
    if (K - x//D) % 2 ==0:
        # print('even')
        ans = sign * (x- (x//D)*D)
    else:
        # print('odd')
        ans = -1 * sign * (x - (x//D)*D -D)
print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = [int(p) for p in input().split()]
x=abs(x)
ans = min(x%d,d-x%d);
if(ans==0):
    p = (x+d-1)//d
    t = k;
    k-=p;
    if(k<0):
        print((x-t*d))
    elif(k%2==0):
        print(0)
    else:
        print(d)
elif(k*d>x):
    print(ans)
else:
    print((x-k*d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = (int(x) for x in input().split())
ans = 10**15
if x < 0:
    x *= -1
for i in range(k):
    x -= d
    if x <= 0:
        if (k-i) % 2 == 0:
            ans = x + d
            break
        else:
            ans = x * (-1)
            break

if ans == 10**15:
    print(x)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

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


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

Submission status: Wrong Answer

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

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

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(x - d)
    if y >= 1 and y < abs(x - a * d):
        ans = y
print(ans)

Submission status: Wrong Answer

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

cand_n_1 = (x + k * d) // (2 * d)
cand_n_2 = (x + k * d) // (2 * d) + 1
cand_n_3 = (x + k * d) // (2 * d) - 1

best = 1e40
if cand_n_1 > k:
    best = min(best, abs(x + k * d - 2 * d * k))
    best = min(best, abs(x + k * d))
else:
    best = min(best, abs(x + k * d - 2 * d * cand_n_1))
    if cand_n_2 <= k:
        best = min(best, abs(x + k * d - 2 * d * cand_n_2))
    if cand_n_3 >= 0:
        best = min(best, abs(x + k * d - 2 * d * cand_n_3))

print(best)

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

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

init = 10**15

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

print(init)

Submission status: Wrong Answer

Solution code:
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    #n = int(stdin.readline().rstrip())
    x,k,d = map(int, stdin.readline().rstrip().split())
    #L = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if abs(x) >= k*d:
        print(abs(x) - k*d)
        exit()
    else:
        a = x // d
        if abs(abs(x) - d*a) >= abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 0:
            print(abs(abs(x) - d*(a+1)))
            exit()
        elif abs(abs(x) - d*a) >= abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 1:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) <= abs(abs(x) - d*(a+1)) and (k-a)%2 == 0:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) <= abs(abs(x) - d*(a+1)) and (k-a)%2 == 1:
            print(abs(abs(x) - d*(a+1)))
            exit()

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

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

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 = X+D*(mv+1)
    posb = X+D*(mv-1)
    posc = X+D*(mv+2)
    posd = X+D*(mv-2)
    print(int(abs(min(posa, posb, posc, posd))))

Submission status: Wrong Answer

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


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

if X>=0:

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

Submission status: Wrong Answer

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

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


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 k == 2:
        print(pos1)
    elif k % 2 == 0:
        print(pos1)
    else:
        print(min(pos1, pos2))

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(X - (K * D))
    else:
        if d % 2:
            print(D - abs(m))
        else:
            print(m)


main()

Submission status: Wrong Answer

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

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:
    tmp = abs(ans - d)
    print(min(ans, tmp))
else:
    print(abs(ans-d))

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))
    else:
        print(min(abs(X-D), abs(X+D)))
elif X < 0:
    print(abs(X + D * K))
else:
    print(abs(X - D * K))

Submission status: Wrong Answer

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    X, K, D = il()
    X = abs(X)
    max_dist = K * D
    c = X // D
    if c < K:
        d = K - c
        if d % 2 == 0:
            print(abs(X - (d * D)))
        else:
            print(abs(X - ((d + 1) * D)))
    else:
        print(abs(X - max_dist))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())

if abs(a)>=abs(b*c):
    print(a-b*c)
    exit()

n=abs(a)
m=n//c
if abs(a-c*m)>abs(a-c*(m+1)):
    m=m+1
if (b-m)%2==0:
    print(abs(a-c*m))
else:
    print(min(abs(abs(a-c*m)+c),abs(abs(a-c*m)-c)))

Submission status: Wrong Answer

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

a = x // d
b = k - a

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

print(s)

Submission status: Wrong Answer

Solution code:
X,K,D = list(map(int , input().split()))
ans = 0
if (X >=0):
    a = K*D
    if (X >= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X -= D
            time += 1
            if X <= 0:
                break
        nokori = (K - time) % 2
        ans = X + D*nokori
else:
    a = -(K*D)
    if (X <= a):
        ans = X-a
    else:
        time = 0
        while (True):
            X += D
            time += 1
            if X >= 0:
                break
        nokori = (K - time) % 2
        ans = X - D*nokori
print(ans)

Submission status: Wrong Answer

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

k = X // D

if k > K:
    ans = min(abs(X - K * D),abs(X + K * D))
else:
    Xk = min(abs(X - k * D), abs(X + k * D))
    if (K - k) % 2 == 0:
        ans = Xk
    else:
        ans = min(abs(Xk-D),abs(Xk+D))

print(ans)

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 k==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:
xkd = input('').split()
x = int(xkd[0])
k = int(xkd[1])
d = int(xkd[2])

if x > k*d:
    print(x-(k*d))
else:
    a1 = x%d
    a2 = x%d -d
    if abs(a1) < abs(a2):
        k2 = k - (x%d)
        # 偶数ならぴったり着地、奇数ならひとつずれる
        if k2%2==0:
            print(abs(a2))
        else:
            print(abs(a1))
    else:
        k2 = k - (x%d) +1
        # 偶数ならぴったり着地、奇数ならひとつずれる
        if k2%2==0:
            print(abs(a1))
        else:
            print(abs(a2))

Submission status: Wrong Answer

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

minX = X % D

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
# x > 0のとき
left = abs(x - d * (x // d + 1))
right = abs(x - d * (x // d))

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

if x // d == 1:
    if x // d % 2 == k % 2:
        print(right)
    else:
        print(left)
else:
    if x // d % 2 == (k - (x // d)) % 2:
        print(right)
    else:
        print(left)

Submission status: Wrong Answer

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

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

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

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

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

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

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

#

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

Yes = 'Yes'
No = 'No'

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

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


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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