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 sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    X, K, D = MI()

    z = X // D
    if z > K:
        print(X - K * D)
        return
    K -= z
    X = X - z * D
    # print(K)
    X2 = abs(X - D)
    # print(X, X2)
    if K % 2 == 0:
        print(X)
    else:
        print(X2)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

kd = k * d

if(x + kd < 0):
    print(0 - x - kd)
elif(x - kd > 0):
    print(x - kd)
else:
    if(x < 0):
        x *= -1
    xd = x % (d*2)
    xd2 = d*2 - xd
    print(min(xd,xd2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

x, k, d = rm()
t = abs(x) // d
if t > k:
    if x > 0:
        print(abs(x - k*d))
    else:
        print(abs(x + k*d))
    exit()
if x > 0:
    if (k - d) & 1:
        print(abs(x - t*d))
    else:
        print(abs(x - t*d - d))
else:
    if (k - d) & 1:
        print(abs(x + t*d))
    else:
        print(abs(x + t*d + 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 = x%d
        z1 = d-x%d
        if ((abs(x)-z)//d)%2 == k%2:
            print(z)
        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:
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')



def solve():
    
    # for _ in range(ii()):

        


        
    x,k,d= mi()
    x = abs(x)
    c = x//d
    if c>=d:
        print(x-k*d)
        exit(0)
    k -= c
    c1 = x%d
    c2 = c1-d
    if k&1:
        print(abs(c2))
    else:
        print(abs(c1))
    # print(c1,c2,k) 



















        
if __name__ =="__main__":

    if path.exists('input.txt'):
        sys.stdin=open('input.txt', 'r')
        sys.stdout=open('output.txt','w')
    else:
        input=sys.stdin.readline
    solve()

Submission status: Wrong Answer

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

Xmin = X - D*K
Xmax = X + D*K

if Xmin * Xmax <= 0:
    ans = min(X%D, D - X%D)
elif Xmin > 0:
    ans = Xmin
else:
    ans = -Xmax

print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    #l,r,d=map(int,input().split())
    x,k,d = map(int, input().split())
    tempkai=x//d
    ans=INFI
    if tempkai>=k:
        ans=abs(x)-d*k
    else:
        if (k-tempkai)%2==0:
            ans=abs(x)-d*tempkai
        else:
            temp=abs(x)-d*tempkai
            temp2=abs(temp-d)
            temp3=abs(temp+d)
            ans=min(temp3,temp2)
    print(ans)

if __name__ == "__main__":

    main()

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


def main():
    X, K, D = map(int, input().split())
    r = X
    for i in range(D):
        if r >= 0:
            r -= K
        else:
            break
    print(abs(r))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

m = X % D
l = X // D


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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

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

	koko = abs(X-D)
	if koko == 0:
		if K % 2 == 0:
			print(abs(D))
			sys.exit()
		else:
			print(0)
			sys.exit()


	ima = abs(X-0)

	if ima == 0:
		if K % 2 == 0:
			print(0)
			sys.exit()
		else:
			print(abs(D))
			sys.exit()
	else:
		hituyou = ima // D
		saidai = ima % D

		if hituyou < K:
			print(saidai)
			sys.exit()
		else:
			ido = K * D
			ans = ima - ido
			print(ans)
			sys.exit()


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

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 = X % D - 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(neg)
        else:
            print(abs(pos))

Submission status: Wrong Answer

Solution code:
import sys


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


def main():
    x, k, d = _ia()
    xm = x % d
    n = (x-xm)//d

    if k >= n:
        if n % 2 == k % 2:
            return xm
        else:
            return -xm + d
    else:
        return x - k * d


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
step = abs(X) // D
if step >= K:
    ans = abs(X) - D*K
else:
    cur = X%D
    rest = K - step
    # print(cur)
    # print(rest)
    if rest%2 != 0:
        ans = min(abs(cur+D), abs(cur-D))
        # ans = min(abs(cur-D), abs(cur+D))
    else:
        ans = cur

print(ans)

Submission status: Wrong Answer

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

x = max(x, -x)

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

Submission status: Wrong Answer

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

r = x%d
l = (x%d-d)

lc = (x-l)//d
rc = (x-r)//d

ans = x
if(k>=lc):
    if((k-lc)%2): ans = min(ans, min(abs(l+d), abs(l-d)))
    else: ans = min(ans, abs(l))
if(k>=rc):
    if((k-rc)%2): ans = min(ans, min(abs(r+d), abs(r-d)))
    else: ans = min(ans, abs(r))
if(k<lc and k<rc):
    ans = min(ans, x - d * k)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
x,k,d  =ma()
if abs(x) > k*d:
    ans = x-k*d
else:
    if x<0:
        x*=-1
    t1 = x%d
    t2 = abs(x%d-d)
    pal = (x-t1)//d
    if pal%2==k%2:
        ans = t1
    else:
        ans=t2
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
#from queue import *
#import random
#sys.setrecursionlimit(int(1e6))
input = sys.stdin.readline
 
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input()))
def inara():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############

x,k,d=invr()

x=abs(x)
near=(x-1)//d
near+=1

if near>=k:
	print(x-k*d)
else:
	a=k-(near-1)
	b=k-near
	c=k-(near+1)
	
	dis1=x-k*(near-1)
	dis2=x-k*(near)
	dis3=x-k*(near+1)
	
	ans=1000000000000000000
	
	if a>=0:
		if a%2:
			ans=min(ans,abs(dis1-d))
		else:
			ans=min(ans,abs(dis1))
	
	if b>=0:
		if b%2:
			ans=min(ans,abs(dis1+d))
		else:
			ans=min(ans,abs(dis2))
	
	if c>=0:
		if c%2:
			ans=min(ans,abs(dis3+d))
		else:
			nas=min(ans,abs(dis3))
	
	print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if abs(k * d) >= abs(x):
    ans1 = abs(x) % abs(d)
    ans2 = abs(ans1 - d)
    print(min(ans1, ans2))
else:
    print(abs(x) - abs(k * d))

Submission status: Wrong Answer

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

tmp1 = abs(X//D)
tmp2 = abs(X%D)

if tmp2 == 0 and K % 2 == 0:
  print(X)
elif tmp1 <= K:
  print(tmp2)
else:
  print(abs(X)-K*D)

Submission status: Wrong Answer

Solution code:
import math

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

nk = math.floor(abs(x) / d)
if k < nk:
    if x >= 0:
        x -= d * k
    else:
        x += d * k
    print(x)
    exit(0)
    
if x >= 0:
    x -= d * nk
else:
    x += d * nk
k -= nk

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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:
    if(x%d == 0): print(0)
    else: print(math.gcd(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()

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

	if K < 0:
		if X < 0:
			print(X+D*K)
		else:
			print(X-D*K)
		exit()

	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 -d*k+x < 0:
    t = d*k-x
    p = t//(2*d)
    l = []
    #print(p)
    for i in range(-1, 2):
        s = p+i
        if p+i >= 0:
            #print(p+i, abs(2*d*s-d*k+x))
            l.append(abs(2*d*s-d*k+x))
    #print(l)
    ans = min(l)

else:
    ans = -d*k+x
print(ans)

Submission status: Wrong Answer

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

X = abs(X)

low = X // abs(D)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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:
import sys
input = sys.stdin.readline

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

a = abs(x) // d
b = abs(x) // d + 1

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
x,k,d=map(int,input().split())
val = x//d
if val>0:
    if val>k:
        x-=(k*d)
    
    if val<=k:
        x-=(val*d)
    print(x)
    sys.exit()
else:
    print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = abs(x) // d
if k % 2 == cnt % 2:
    ans = min(abs(x - d * min(k,cnt)), abs(x + d * min(k,cnt)))
elif k % 2 != cnt % 2:
    ans = min(abs(x - d * min(k,(cnt + 1))), abs(x - d * min(k,(cnt + 1))))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

t = X // D

if K <= t :
    print(X-D*K)
else :
    X -= D*t
    K -= t
    if K%2 == 0 :
        print(X)
    else :
        print(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
    X = abs(X)
    while r-l>1:
        half = (r+l)//2
        if half*D>X:
            r = half
        else:
            l = half
    now = X-l*D
    if (K-l)%2==0:
        print(now)
    else:
        print(D-now)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
if X // D > K:
    result = X - D*K
else:
    n = X//D
    rem_jump = K - n
    p1 = X % D
    p2 = D - p1

    if rem_jump % 2 == 0:
        result = p1
    else:
        result = p2

print(result)

Submission status: Wrong Answer

Solution code:
x, k, d = [int(x) for x in input().split()]
x = abs(x)
if x - k*d <= 0:
    t = x//d
    x -= (d*t)
    k -= t 


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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 = abs(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:

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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(min(abs(x - k*d), x))
        return

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


main()

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:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
    # combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

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

def main():
    x,k,d = readInt()
    count = x // d
    mn = 0
    if count>k:
        mn = x - (d * k)
    else:
        mn = x % d
    ans = INF
    if (k-count)%2==0:
        ans = mn
    else:
        ans = abs(mn-d)
    print(ans)
    return

if __name__=='__main__':
    main()