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:
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
    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 (k-n)%2==0:
        ans=a
    else:
        ans=D
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X,K,D=(int(x) for x in input().rstrip('\n').split())
if X>0 and X-K*D>=0:
  print(X-K*D)
elif X<0 and X+K*D<=0:
  print(X+K*D)
else:
  if X>0:
    x = X%D
    y=X//D
    if (K-y)%2==0:
      print(x)
    else:
      print(D-x)
  elif X<0:
    a = (-1)*X
    x = a%D
    y = a//D
    if (K-y)%2==0:
      print(x)
    else:
      print(D-x)
  else:
    if K%2==0:
      print(0)
    else:
      print(D)

Submission status: Wrong Answer

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

a = (K * D - X) / (2*D)
a1, a2 = math.floor(a), math.ceil(a)

left = 0
right = K
def f(a):
    return X - K * D + 2 * a * D

mid = None
while left < right:
    mid = (left + right) // 2

    if f(mid) == 0:
        break
    elif f(mid) < 0:
        left = mid + 1
    else:
        right = mid - 1

print(abs(X - K * D + 2 * left * D))

Submission status: Wrong Answer

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

if p//d >= k:
    print(abs(x)-k*d)
else:
    a = p%d
    b = abs((p%d)-d)
    print(min(a,b))

Submission status: Wrong Answer

Solution code:
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


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

    pmin = x - k * d
    if pmin >= 0:
        return pmin

    pmax = x + k * d
    if pmax <= 0:
        return pmax

    dd, dm = divmod(x, d)
    dd = abs(dd)
    remains = k - dd
    if remains & 1:
        return abs(dm - d)
    else:
        return dm


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
x, k, d = [int(i) for i in input().split()]
x = abs(x)
moves_to_base_without_exceeding = x // d
if(((moves_to_base_without_exceeding - k) % 2 == 0)):
    print(x % d)
else:
    print(d - x % d)

Submission status: Wrong Answer

Solution code:
X, K, D = (int(x) for x in input().split())
P = X
if abs(X) >= K * D:
    if X >= 0:
        P = X - K * D
    else:
        P = X + K * D
else:
    x = int(abs(X) / D)
    if X >= 0:
        if (K - x) % 2 == 0:
            P = X - D * x
        else:
            P = abs(X - D * (x + 1))
    else:
        if (K - x) % 2 == 0:
            P = abs(X + D * x)
        else:
            P = X + D * (x + 1)
print(int(P))

Submission status: Wrong Answer

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

shoki = X // D
if X >= K * D:
  print(abs(X - K * D))
elif (K-shoki) % 2 == 1:
  ans = X - (D * shoki) - D
  print(abs(ans))
else:
  ans = X - (D * shoki)
  print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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



a = abs(x//d)
b = x%d

if k <= a:
    print(abs(abs(x)-k*d))

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

Submission status: Wrong Answer

Solution code:
# by the authority of GOD     author: manhar singh sachdev #

import os,sys
from io import BytesIO, IOBase

def main():
    x,k,d = map(int,input().split())
    if x < 0:
        y = x+k*d
    else:
        y = x-k*d
    if x==0:
        if k%2:
            print(d)
        else:
            print(0)
    elif y*x >= 0:
        print(abs(y))
    else:
        z = abs(x)%d
        z1 = d-abs(x)%d
        if ((abs(x)-z)//d)%2 == k%2:
            print(x%d)
        else:
            print(z1)

#Fast IO Region
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")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

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

if(abs(x%d)<abs(x%d-d)):
  min_num=x%d
  min_num2=x%d-d
else:
  min_num=x%d-d
  min_num2=x%d

kaisuu=(x-min_num)/d

if(kaisuu>k):
  print(abs(x-d*k))
  sys.exit()
if((k%2==0 and kaisuu%2==0)or(k%2==1 and kaisuu%2==1)):
  print(abs(min_num))
else:
  print(abs(min_num2))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
q, r = divmod(X, D)
if X < 0:
    q += 1
    q *= -1
    r -= D
if q >= K:
    print(X - K * D)
else:
    if (K - q) % 2 == 0:
        print(r)
    else:
        print(min(abs(r - D), abs(r + 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:
    for i in range(k):
        if x >= 0:
            x -= d
        else:
            x += d
print(abs(x))

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

X,K,D = I()
if X == 0:
    print(0)
    exit()
if X > 0:
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        print(abs(num1))
    else:
        print(abs(num2))
else:
    X *= -1
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        print(abs(num1))
    else:
        print(abs(num2))

Submission status: Wrong Answer

Solution code:
# = int(input())
# = str(input())
X, K, D = map(int, input().split())
# = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]

if X == 0:
    ans = [D, 0]
    exit()

elif X>0:
    if X == D:
        ans = [0, D]
    elif X-D<0:
        ans = [D-X, X]
    else:
        if X-D*K>=0:
            print(X-D*K)
            exit()
        else:
            plus = X%D
            minus = D-X%D
            if (X//D)%2 == 1:
                ans = [plus, minus]
            else:
                ans = [minus, plus]

else:
    T = -1*X
    if T == D:
        ans = [0, T]
    elif T-D<0:
        ans = [D-T, T]
    else:
        if T-D*K>=0:
            print(T-D*K)
            exit()
        else:
            plus = T%D
            minus = D-T%D
            if (T//D)%2 == 1:
                ans = [plus, minus]
            else:
                ans = [minus, plus]

if K%2 == 1:
    print(ans[0])
else:
    print(ans[1])

Submission status: Wrong Answer

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

if K*D<abs(X):
  if X<0:
    print(abs(X+K*D))
  else:
    print(abs(X-K*D))
else:
  if X%D==0:
    ans = [0,D]
    if X//D%2==0:
      print(ans[K%2])
    else:
      print(ans[1-K%2])
  else:
    ans = [-X%D,X%D]
    if X//D%2==X%D:
      print(ans[K%2])
    else:
      print(ans[1-K%2])

Submission status: Wrong Answer

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

x = abs(x)
ans = [x]
n = x // d
if k <= n + 1:
    ans.append(abs(x - k * d))
if n <= k and n % 2 == k % 2:
    ans.append(x % d)
elif n < k and n % 2 != k % 2:
    ans.append(d - x % d)
print(min(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
XKD = list(map(int, input().split()))

posi = XKD[0]

count = 0
factor = 0
if (XKD[1] > int(abs(posi) / XKD[2])):
    time = XKD[1] - int(abs(posi) / XKD[2])
    factor = int(abs(posi) / XKD[2])
else:
    time = 0
    factor = XKD[1]

if (posi >= 0):
    posi -= XKD[2] * factor
else:
    posi += XKD[2] * factor

if (posi >= 0):
    if (time % 2 == 0):
        print(posi)
    else:
        print(abs(posi - XKD[2]))
else:
    if (time % 2 == 0):
        print(posi)
    else:
        print(abs(posi + XKD[2]))

Submission status: Wrong Answer

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

if x==k and x==d:
  print(x)
  exit()

if (x//d)<=k:
  x-=(x//d)*d
  k-=x//d

for i in range(k):
  tmp=x
  x-=d
  if abs(x) >= abs(tmp):
    if (k-i)%2==0:
      print(abs(tmp))
      exit()
    else:
      print(abs(x))
      exit()
  else:
    pass
else:
  print(abs(x))

Submission status: Wrong Answer

Solution code:

import datetime
from decimal import Decimal, ROUND_DOWN
import heapq
from fractions import gcd
from functools import reduce
from collections import deque
# from math import factorial
import itertools
import collections
import math
import sys
sys.setrecursionlimit(2000000)
# import statistics
# import numpy as np
# n = int(input())
# n, m, p = list(map(int, input().split()))
# a = list(map(int, input().split()))
#
#  abc = [int(input()) for i in range(5)]
#n = int(input())
#n, m = list(map(int, input().split()))
x, k, d = list(map(int, input().split()))
#l = list(map(int, input().split()))
# data = [list(map(int, input().split())) for i in range(n)]
# n = int(input())
# a = list(map(int, input().split()))
# b = list(map(int, input().split()))
# ab_sorted = sorted(ab, key=lambda x: x[0])
# data = [list(map(int, input().split())) for i in range(n)]
# print(min(15*n, 100*(n//10+1), 100*(n//10)+15*(n % 10)))
# for i in range(1, int(n**0.5)+1):

shou = x//d
amari = x % d

if shou <= k:
    k -= shou
    if k % 2 == 0:
        ans = amari
    else:
        ans = min(abs(amari+d), abs(amari-d))
else:
    ans = x-d*k

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
str= str(input())
if len(str.split())==3:
    s=[int(x)  for x in str.split()]
x=s[0]
k=s[1]
d=s[2]
cnt=int(x/d)
if cnt>=k:
    print(abs(x)-k*d)
else:
    num=k-cnt
    num=num%2
    if num>0 :
        ans1=abs(x)-cnt*d-d
        ans2=abs(x)-cnt*d+d;
        print(int(min(abs(ans1),abs(ans2))));
    else:
        print(int(abs(x)-cnt*d));

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
XKD = input().split()

X = int(XKD[0])
K = int(XKD[1])
D = int(XKD[2])
mini_X = X
count = 0
flag = 0
for i in range(K):
    if X >= 0:
        X -= D
    else:
        X += D
    count += 1


    if abs(mini_X) > abs(X):
        mini_X = abs(X)
    elif abs(mini_X) == abs(X):
        flag = 1
        break
    else:
        count -= 1
        break

if (K - count) % 2 == 0:
    print(abs(mini_X))
elif flag == 1:
    print(abs(X))
else:
    print(abs(mini_X) + D)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
n=max(round((k*d-x)/(2*d)),0)
ans=10**15+1
for i in range(0,n+10):
    ans=min(ans,abs(x+2*i*d-k*d))
print(ans)

Submission status: Wrong Answer

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

k = X // D
if abs(X - k * D) <= abs(X - (k + 1) * D):
    best_k = k
else:
    best_k = k + 1

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    X, K, D = map(int, input().split())
    M = X%D
    S = X//D
    C = K - S 
    if C < 0:
        ans = abs(X) - K*D
    else:
        if M <= abs(M-D):
            if C%2 == 0:
                ans = M
            else:
                ans = abs(M - D)
        else:
            if C%2 != 0:
                ans = M
            else:
                ans = abs(M - D)
    print(ans)

if __name__ == '__main__':
    main()

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


if x<0:
  x=(-1)*x
if d<0:
  d=(-1)*d
  
s=math.floor(x/d)

if k<s:
  print(x-d*k)
else:
  w=k-s
  v=x-d*s
  if w%2==0:
    print(v)
  else:
    print(v-d)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
s = X//D
if s == K:
    print(X%D)
elif s > K:
    print(abs(X-D*K))
elif s % 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 X > 0:
  v = 1
else:
  v = 0

for i in range(K):
  if X > 0:
    if v==0 and (K-i)%2==1:
      print(abs(X-D))
      v=2
      break
    elif v==0 and (K-i)%2==1:
      print(abs(X))
      v=2
      break
    X -= D
  else:
    if v==1 and (K-i)%2==1:
      print(abs(X+D))
      v=2
      break
    elif v==1 and (K-i)%2==0:
      print(abs(X))
      v=2
      break
    X += D
if v!=2:
  print(X)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

X, K, D = MAP()

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

flg = X//abs(X)
if K - abs(X)//D <= 0:
	if X < 0:
		print(X+D*K)
	else:
		print(X-D*K)
	exit()


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


if K%2 == 0:
	print(abs(X))
else:
	if X <= 0:
		print(X+D)
	else:
		print(abs(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)
else:
    mod = x % d
    if (x // d) //2 == 1:
        print(mod)
    else:
        print(abs(mod-d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
"""MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWO:,'..'xXWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .:kOo.,kNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .lNWWo..lXMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MNOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMMMMWKxoloxKWNc  .:xxd;  ,dxxxkXMXkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMM
MKc''''''''''''''''.  ..'''''.   .''''.  .''MMMWx..,:. .xXo''.  .''.  ..'';kMk;'''''.  .'.''.  .''''''.  ..''.   .''MM
MWNXNNNNNNNNNNNNNNNO' ;KNNNNNKo. cKNNNd. lXNMMMK; ,0Wx. :NWXXl .dNNO' ;KNNNWMWNNNNNXc .kNNNNx. cXNNNNNO' ;KNN0; 'ONNMM
MMMMMMMMMMMMMMMMMWM0' :NMMMMWMX: .OMWWx. oWMMMMWx. ';. .dNMWWo .xMM0' :NWWMMMMMMMMWWl .OMMMWk. lWWMMWM0' ;XMMX: ,KMMMM
MMMMMMWMWkccccccccc;. :NMWNNNKd. :XMMWx. oWMMMWKo. .',. .,ldx; .dMM0' :NMMMMMMMNNWMWl .OMMMWk. lWMMMMM0' ;XMMX: ,0MMMM
MMWXKNWMWxcc,. .;cc;. :XMk,''..'lKWMMWx. oWMNx:. .lONNKxl.     .xMM0' :NMMMMMM0:,oKO' ,KMMMMk. lWWNkoc,. .;::;. ,KMMMM
MMXc.lXMMWWWNd..xWM0' :NM0;  ;kXWMMMWNl  oWMK:.;xXWWMWXx:. ,c' .dMM0' :NMMMMMMKc. ...;kWMMMMk. lWWc .:;. .clc:. ,KMMMM
MWWx. lXMWMMMK, lWM0' :NMMNx'.,ldxxoc'   oWMWNKNWWWN0o' .:kNWo .dMM0' :NMMMMMMMNO;  ,OWWWMMMk. lWM' lXx. lNMMX: ,KMMMM
MMMWk'.,xKNX0c..kWM0' :NMMMMNkl;,'',:l:  oWMMMMMWMWO' 'l0WMWWo .xMM0' :NMMMMMMMMMNd. .oXWMMMk. lWMd.....;0WMMX: ,KMMMM
MMMMWKo'...'..:kWMM0' :NMMMMMMMWWNNWWWx. oWMMMMMMMMWOxXWMWMWWo .xMM0' :NMMMMMMMMMMMKl. ,kNMMk. lWMWKkxx0NMWMMX: ,KMMMM
MMMMMMMN0kxk0XNWMMWKl;xNMMMMMMMMMMMMWM0c;kWMMMMMMMMMMMWWMMMMWk;:OMMXl,dNMMMMMMMMMMMMWO; .dNM0c;kWWMMMMMMMMMMMNd,oXMMMM
MMMMMMMMWWMWMMMMMMMMMWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWWMMWWMMWMMMMMMMMMMMMMMNkxKWMMWWMMMMMMMMMMMMMMWMMWMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"""


#				 Author: Udit "luctivud" Gupta @ https://www.linkedin.com/in/udit-gupta-1b7863135/					 #


import math;   		from collections import *
import sys;   		from functools import reduce
import time;   		from itertools import groupby

# sys.setrecursionlimit(10**6)

def input()         : return sys.stdin.readline()
def get_ints()      : return map(int, input().strip().split())
def get_list()      : return list(get_ints())
def get_string()    : return list(input().strip().split())
def printxsp(*args) : return print(*args, end="")
def printsp(*args)  : return print(*args, end=" ")


DIRECTIONS = [(+0, +1), (+0, -1), (+1, +0), (+1, -1)] 
NEIGHBOURS = [(-1, -1), (-1, +0), (-1, +1), (+0, -1),\
              (+1, +1), (+1, +0), (+1, -1), (+0, +1)]



# MAIN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# for _test_ in range(int(input())): 

x, k, d = get_ints()
el_facto = min(k, (x + d-1) // d)
moves = []
if k & 1:
	if el_facto & 1:
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
	else:
		el_facto -= 1
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
else:
	if not (el_facto & 1):
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
	else:
		el_facto -= 1
		moves.append(el_facto)
		el_facto -= 2
		if (el_facto > 0 and el_facto <= k) :
			moves.append(el_facto)
		if el_facto + 4 <= k:
			moves.append(el_facto + 4)
ans = float('inf')
x = abs(x)
# print(moves)
for i in moves:
	ans = min(ans, abs(x - i * d))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import itertools

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

res = 0
saiteki_kaisu = min((x // d), k)
saigoni_hituyo = (k - saiteki_kaisu) % 2
if x > 0:
    res = x - (saiteki_kaisu * d)
elif x <= 0:
    res = x + (saiteki_kaisu * d)
if res > 0:
    res -= saigoni_hituyo * d
else:
    res += saigoni_hituyo * d

print(abs(res))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#Problem C

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

T_min = X//D
ans = 0

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


if (T_min-K)%2 ==0:
    ans = abs(X) - (T_min*D)
else:
    ans1 = abs(abs(X) - (T_min*D) + D)
    ans2 = abs(abs(X) - (T_min*D) - D)
    ans = min(ans1,ans2)
    
print(ans)

Submission status: Wrong Answer

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

from math import floor, ceil
def do(x, k, d):
    t1, t2 = floor(x/d), ceil(x/d)
    ogk = k
    ogx = x

    x -= t1*d
    k -= t1
    ans1 = 10**16
    if k%2 == 0:
        ans1 = abs(x)
    else:
        ans1 = min(abs(x + d), abs(x - d))

    x, k = ogx, ogk
    x -= t2*d
    k -= t2
    ans2 = 10**16
    if k%2 == 0:
        ans2 = abs(x)
    else:
        ans2 = min(abs(x + d), abs(x - d))
    
    print(min(ans1, ans2))


if x > 0:
    if x - k*d < 0:
        do(x, k, d)
    else:
        print(x - k * d)

elif x < 0:
    if x + k*d > 0:
        do(abs(x), k, d)
    else:
        print(x + k*d)

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

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(a)-c*rem
        print(ans)
    elif(b>=rem+1 and b%2!=rem%2):    
        ans=abs(abs(a)-c*(rem+1))
        print(ans)
    else:
        print(abs(a)-c*b)

Submission status: Wrong Answer

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

n = x//d

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

if n%2==0:
  if k%2==0:
    ans = x - n*d
  else:
    ans = (n+1)*d - x
else:
  if k%2==1:
    ans = x - n*d
  else:
    ans = (n+1)*d - x
print(ans)

Submission status: Wrong Answer

Solution code:
#  a = (kd - x)/2d


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


pos = (k - (x//d))//2

if pos < 0:
    pos = 0
ans = x + pos * d - (k-pos) * d
fa = abs(ans) 
pos += 1
ans = x + pos * d - (k-pos) * d
fa = min(fa, abs(ans))
print(fa)