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:
def main():
    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:
        K -= abs(X) // D
        if K % 2 == 0:
            print(abs(X)%D)
        else:
            print(D-abs(X)%D)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    X, K, D = map(int, readline().split())
    if X >= 0:
        if X//D > K:
            ans = X - D*K
        else:
            if (K-X//D)%2==0:
                ans = X - X//D*D
            else:
                ans = abs(X-X//D*D -D)
    else:
        if (-X)//D > K:
            ans = abs(X+D*K)
        else:
            if (K-(-X)//D)%2==0:
                ans = X + (-X)//D*D
            else:
                ans = X + (-X)//D*D + D
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if(abs(x) - k*d >= 0):
    print(abs(x) - k*d)
else:
    print(math.gcd(abs(x), d))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

X, K, D = mapint()
if X>=abs(K*D):
    print(abs(abs(X)-K*D))
else:
    l, r = 0, K
    while r-l>1:
        half = (r+l)//2
        if half*D>X:
            r = half
        else:
            l = half
    now = abs(X)-l*D
    if (K-l)%2==0:
        print(now)
    else:
        print(D-now)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    x = X

    visited = {abs(X)}

    for i in range(K):
        if x > 0:
            x -= D
        elif x < 0:
            x += D
        else:
            print(0)
            return

        abs_ = abs(x)

        if abs_ in visited:
            break
        else:
            visited.add(abs_)

    print(min(visited))
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

moves = abs(x//d)

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

Submission status: Wrong Answer

Solution code:
import sys
import  math
import fractions
from collections import defaultdict
import copy
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

X,K,D=nm()
X=abs(X)
if(K*D<=X):
    print(X-K*D)
    sys.exit(0)
else:
    tmp=X%D
    print(min(tmp,D-tmp))

Submission status: Wrong Answer

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

if abs(X) < K * D:
    t1 = X % D
    t2 = D - t1
    print(min(t1, t2))

else:
    t1 = abs(abs(X) - K * D)
    t2 = abs(abs(X) - (K - 1) * D)
    print(min(t1, t2))

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

if X < 0:
  xminus = True
  X = (-1) * X
else:
  xminus = False
  
if abs(X) > K*D:
  ans = X - K*D
else:
  wk = X // D
  K -= wk
  now = X % D
  #print(wk,now)
  if K % 2 == 0:
    ans = now
  else:
    ans = abs(now-D)
    
print((-1) * ans if xminus else ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if x//d>=k:
  print(x-d*k)
  exit()
c=k-x//d
ans=x%d
print([ans,abs(ans-d)][c%2])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    x, k, d = MI()

    min_idou = x // d
    amari = x % d

    if min_idou >= k:
        print(x - k*d)
        return

    k -= min_idou
    if k % 2 == 0:
        print(amari)
    else:
        print(-1*(amari - d))


main()

Submission status: Wrong Answer

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


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

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

Submission status: Wrong Answer

Solution code:
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    k = X // D
    if k >= K:
        return X - D * K
    K -= k
    X -= D * k
    if k & 1:
        return D - X
    return X

print(main())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l = X - K * D
r = X + K * D


if l >= 0:
    print(l)
elif r <= 0:
    print(r)
else:
    X = abs(X)
    k = X // D
    if (K - k) % 2 == 0:
        print(X - X // D * D)
    else:
        print(-(X - (X // D + 1) * D))

Submission status: Wrong Answer

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

moves = x//d

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

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

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

Submission status: Wrong Answer

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

d = abs(X) // D

ans = -1
if K <= d:
    ans = abs(X) - D * K
else:
    if (d - K) % 2 == 1:
        ans = D * (d + 1) - X
    else:
        ans = X - D * d

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


X, K, D = inl()


def solve():
    if X >= K * D:
        return X - K * D
    if X <= -K * D:
        return abs(X + K * D)
    y = X + K * D
    assert y > 0
    r = y // (2 * D)

    ans = 2 * X
    for k in range(r - 10, r + 10):
        if k < 0 or k > K:
            continue
        z = y - k * 2 * D
        ans = min(ans, abs(z))

    return ans


print(solve())

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
#input = stdin.buffer.readline
setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

MOD = 10 ** 9 + 7
INF = 10 ** 18

#N = int(input())
X, K, D = map(int, input().split())
#A = list(map(int, input().split()))
#A = [int(input()) for _ in range(N)]
#ABC = [list(map(int, input().split())) for _ in range(N)]
#S = input()
#S = [input() for _ in range(N)]

n = X // D + 1

if n >= K:
    answer = abs(abs(X) - D * K)
else:
    answer = abs(abs(X) - D * n)
    K -= n
    
    if K % 2:
        answer = abs(answer - D)
        
print(answer)

Submission status: Wrong Answer

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

s = 0

if x - k * d > 0:
    s = x - k * d
elif x + k * d < 0:
    s = x + k * d 
elif x % d == 0:
    e = x // d
    if (k-e) % 2 == 1:
        s = d
    else:
        s = 0
else:
    e = x // d
    r = x % d
    if (k-e) % 2 == 1:
        s = -(r-d)
    else:
        s = r

print(s)

Submission status: Wrong Answer

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

cand = []
if X//D<=K and (X//D-K)%2==0:
    cand.append(X%D)
if X//D+1<=K and (X//D-K)%2==1:
    cand.append(abs(X%D-D) )
    
if cand:
    print(min(cand))
else:
    print(min(X-K*D, X+K*D))

Submission status: Wrong Answer

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

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


# x< k*d
ans1=x%d
ans2=abs(ans1-d)


ans1_moved = x//d

if d%2==ans1_moved%2:
    print(ans1)
else:
    print(ans2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    x, k, d = [int(i) for i in input().strip().split()]
    div = x // d
    if k < div:
        ans = x - (d * k)
    else:
        k = k - div
        even = x % d
        odd = abs(d - even)
        if k % 2 == 1:
            ans = odd
        else:
            ans = even
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# ------------------- fast io --------------------
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")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi


def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)


def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y


for _ in range(int(input()) if not True else 1):
    #n = int(input())
    x, k, d = map(int, input().split())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    if x > 0:
        req = x // d
        if k <= req:
            print(abs(x - k*d))
        else:
            k -= req
            cur = x % d
            print(abs(cur) if k % 2 == 0 else abs(cur - d))
    else:
        req = (-x) // d
        if k <= req:
            print(abs(x + k*d))
        else:
            k -= req
            cur = - (x%d)
            print(abs(cur) if k % 2 == 0 else abs(cur + 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(K * D + X)
elif X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
else:
    X = abs(X)
    d, m = divmod(X, D)
    K -= d
    if K % 2 == 0:
        print(m)
    else:
        print(abs(m - D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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:
		print(0)
	else:
		print(D)
	exit()

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

	if K%2 == 0:
		print(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 X >= K * D:
    print(X - K * D)
elif X <= K * (- D):
    print(abs(X + K * D))
else:
    if X >= 0:
        pos = X % D
        neg = D - X % D
        if K % 2 == (X // D) % 2:
            print(pos)
        else:
            print(abs(neg))
    if X <= 0:
        pos = D - (abs(X) % D)
        neg = - (abs(X) % D)
        if K % 2 == (X // D) % 2:
            print(pos)
        else:
            print(abs(neg))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    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:
        K -= X // D
        if K % 2 == 0:
            print(X%D)
        else:
            print(D-X%D)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

K -= X//D
X %= D
if K % 2 == 1:
    X = D-X
print(X)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

l = X - K * D
r = X + K * D


if l >= 0:
    print(l)
elif r <= 0:
    print(-1)
else:
    X = abs(X)
    k = X // D
    if (K - k) % 2 == 0:
        print(X - X // D * D)
    else:
        print(-(X - (X // D + 1) * D))

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

x,k,d=mp()
x=abs(x)
if k*d<=x:
    print(x-k*d)
    exit()
y=x//d
if y%2==1:
    print(abs(x-d*(y+1)))
else:
    print(abs(x-d*y))

Submission status: Wrong Answer

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

if abs(X) >= K * D:
  if X > 0:
    ans = X - K * D
  else:
    ans = X + K * D
  print(abs(ans))
else:
  count = int(X / D)
  if X > 0:
    one = X - D * count
    two = one - D
    if (K - count) % 2 == 0:
      print(abs(one))
    else:
      print(abs(two))
  else:
    one = X + D * count
    two = one + D
    if (K - count) % 2 == 0:
      print(abs(one))
    else:
      print(abs(two))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

s = 0

if x - k * d > 0:
    s = x - k * d
elif x + k * d < 0:
    s = x + k * d 
elif x % d == 0:
    e = x // d
    if (k-e) % 2 == 1:
        s = d
    else:
        s = 0
else:
    r = x % d
    s = min(r, - (r-d))

print(s)

Submission status: Wrong Answer

Solution code:
# import sys; input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	X, K, D = getlist()
	if X - K * D >= 0:
		print(X - K * D)
	elif X + K * D <= 0:
		print(abs(X + K * D))

	elif X >= 0:
		X = abs(X)
		K -= X // D
		X = X % D

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



if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

moves = x//d

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

Submission status: Wrong Answer

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

k = (D*K-X)//(2*D)
ans = min(abs(X+D*K), abs(X-D*K))
for i in range(k-10, k+10):
  if 0<=k<=K:
    ans = min(ans, abs(X+k*D-(K-k)*D))
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import bisect
import math
import fractions
import sys
import copy


def L(): return sys.stdin.readline().split()


def I(): return int(sys.stdin.readline().rstrip())


def SL(): return list(sys.stdin.readline().rstrip())


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def LS(): return [list(x) for x in sys.stdin.readline().split()]


def IR(n): return [I() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def LIR1(n): return [LI1() for _ in range(n)]


def SR(n): return [SL() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def LR(n): return [L() for _ in range(n)]


def perm(n, r): return math.factorial(n) // math.factorial(r)


def comb(n, r): return math.factorial(
    n) // (math.factorial(r) * math.factorial(n-r))


alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007
INF = float("inf")


def main():
    X, K, D = LI()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

x,k,d = iim()
x = abs(x)
if k*d>x :
    num = x//d
    k -= num
    l = abs(x-(x//d)*d-d)
    r = abs(x-(x//d)*d)
    if num%2 == 1:
        ans = l
    else:
        ans = r
elif k*d==x :
    ans = 0
else:
    ans = x-d*k
print(ans)

Submission status: Wrong Answer

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

for i in range(k):
    if x > 0:
        x = x - d
    elif x < 0:
        x = x + d
    elif x == 0:
        if k % 2 != 0:
            print(0)
            exit()
        else:
            print(d)
            exit()
print(abs(x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
for i in range(k):
  if x**2 >= d**2:
    if x > 0:
      x = x - d
    else:
      x = x + d
  else:
    if x >=0:
      x = x - d
    else:
      x = x + d
print(x)

Submission status: Wrong Answer

Solution code:
import copy
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    x,k,d=INTM()
    if abs(x)-k*d>0:
        ans=abs(x)-k*d
    else:
        a=x%d
        b=a-d
        #c,d=max(a,abs(b)),min(a,abs(b))
        if ((x-a)//d)%2==0:
            ans=abs(a)
        else:
            ans=abs(b)
            
    print(ans)
        
    
    
if __name__ == '__main__':
    do()

Submission status: Wrong Answer

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

X, K, D = LI()


def main():
    x = abs(X)
    a = x // D
    if K <= a:
        ans = x - K * D
        print(ans)
        return
    
    x -= a * D
    k = K - a
    if k % 2 == 0:
        ans = x - a * D
    else:
        ans = abs(x - D)
    print(ans)
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

X = abs(X)
a = min(X//D, K)
X -= D * a
K -= a

if K == 0:
    print(X)
    exit()

if abs(X-D) < X:
    X = abs(X-D)
    a -= 1

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

Submission status: Wrong Answer

Solution code:
x,k,d = list(map(int,input().split()))
mst = x//d
if mst&1 != k&1:
    mst+=1
print(abs(x-(min(mst,k))*d))

Submission status: Wrong Answer

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

X, K, D = LI()


def main():
    x = abs(X)
    a = x // D
    if K <= a:
        ans = x - K * D
        print(ans)
        return
    
    x -= a * D
    k = K - a
    print(a, k)
    if k % 2 == 0:
        ans = x - a * D
    else:
        ans = abs(x - D)
    print(ans)
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
num = X//D
if num >= K:
  ans = X-K*D
  print(ans)
  exit()
a = X%D
b = abs(a-D)

if K%2 == num%2:
  ans = a
else:
  ans = b
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
x,k,d=map(int,input().split())
if k%2==1:
  if x>0:
    x-=d
  else:
    x+=d
k-=1
ans=min(x%(2*d),2*d-x%(2*d))
if abs(x)>k*d:
  print(abs(x)-k*d)
else:
  print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def solve(x, k, d):
    n, m = divmod(abs(x), d)

    if n >= k:
        return x - k * d

    if n % 2 == k % 2:
        return m

    return abs(m - d)


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

print(solve(x, k, d))

Submission status: Wrong Answer

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

k = X // D
rt = X - k * D
lt = abs(rt - D)
if k > K:
    print(X - K * D)

elif k % 2 == K % 2:
    print(rt)

else:
    print(lt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

t = X // D

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

K -= t
X -= t * D

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

Submission status: Wrong Answer

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

K -= X//D
X %= D

X -= D*(K%2)

print(abs(X))