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 os

x,k,d = (int(x) for x in input().split())
check = 0
if x<0:
    check = 1

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

x = abs(x)

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

Submission status: Wrong Answer

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

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


print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import math
#input=stdin.readline
#def print(x):stdout.write(str(x)+'\n')
x,n,d=map(int,input().split())
ans=x
if x>0:
    if x-n*d<0:
        c=x//d
        n=n-c
        ans=x-c*d
        a=abs(ans-d)
        b=ans
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x-n*d)
elif x==0:
    if n%2==0:
        print(x)
    else:
        print(x+d)
else:
    if x+n*d>0:
        c=abs(x//d)
        n=n-c
        ans=x+c*d
        a=abs(ans+d)
        b=ans
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x+n*d)

Submission status: Wrong Answer

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

if x > 0:
    kaisu = x//d
    if x//d <= k:
        k -= kaisu
        x -= kaisu*d
    else:
        x -= k*d
        k = 0
else:
    kaisu = -(x//d) 
    if x//d <= k:
        k -= kaisu
        x += kaisu*d
    else:
        x += k*d
        k = 0

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

Submission status: Wrong Answer

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


def print_ans(X, K, D):
    """Test Case
    >>> print_ans(6, 2, 4)
    2
    >>> print_ans(7, 4, 3)
    1
    >>> print_ans(10, 1, 2)
    8
    >>> print_ans(1000000000000000,1000000000000000,1000000000000000)
    1000000000000000
    """
    if K % 2 == 0 and D >= X:
        print(abs(X))
    else:
        for i in range(K):
            if abs(X-D) > abs(X+D):
                X = X + D
            else:
                X = X - D

        print(abs(X))


if __name__ == '__main__':
    X, K, D = map(int, input().rstrip().split())
    print_ans(X, K, D)

Submission status: Wrong Answer

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

ans = x
if k > 0 and x >= 0:
    if k % 2 != 0:
        ans = x - d
    else:
        ans = x
elif k > 0:
    if k % 2 != 0:
        ans = x - d
    else:
        ans = x

print(abs(ans))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
a = x//d
amari = x%d
if amari > abs(d-amari):
    amari = d-amari
    a += 1
if amari == 0:
    if k>=a:
        if (k-a)%2==0:
            print(amari)
        else:
            print(min(amari+d,abs(d-amari)))
    elif k<a:
        print(abs(x)-(k*d))
elif amari > 0:
    if k>=a:
        if (k-a)%2==0:
            print(amari)
            #print(1)
        elif amari == abs(d-amari):
            print(amari)
        else:
            print(min(amari+d,abs(d-amari)))
            #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())
a = 0
b = k + 1
if d < 0:
    d *= -1
mid = 0
while b - a > 1:
    mid = (a + b) >> 1
    if x - d * mid >= 1:
        a = mid
    else:
        b = mid
print(x - a * d)

Submission status: Wrong Answer

Solution code:
T = input()
T = T.split(" ")
x = int(T[0])
k = int(T[1])
d = int(T[2])
val = (x+k*d)//(2*d)
print(min(abs(-2*d*val+x+k*d), abs(-2*d*(val+1)+x+k*d)))

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:

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

x = abs(x)

decim = x/d

if (decim > k):
    print(abs(x - k*d))
    exit(0)
else:
    if (x % d == 0 and (x // d)% 2 == 0 ):
        # print('b')
        print(0)
    elif (x % d == 0 and (x // d)% 2 == 1 ):
        # print('b2')
        print(d)
    else:
        # print('c')
        lower = x // d
        upper = lower + 2
        print(min(abs(x - lower*d), abs(x - upper*d)))

Submission status: Wrong Answer

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

if K*D<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)

a = x // k

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

x -= a * k
d -= a

ans = min(x, x - d)
print(abs(ans))

Submission status: Wrong Answer

Solution code:
# Date [ 2020-08-15 21:13:42 ]
# Problem [ c.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 6 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    x, k, d = read_ints()
    x = abs(x)
    cnt = x // d
    if cnt < k:
        x -= cnt * d
    else:
        cnt = k
        x -= cnt * d
    if abs(x - d) < x and k - cnt > 0:
        x = abs(x - d)
        cnt += 1        
    
    if (k - cnt) % 2 == 0:
        print(x)
    elif x == 0:
        print(d)
    else:
        print(min(abs(x - d), x)) 

if __name__ ==  '__main__':
    Main()

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 9999999
now = X
i = 0
if now >= 0:
  while True:
    now = now - D
    cnt+=1
    if now >= 0:
      if (K - cnt) % 2 == 0:
        a = now
    else:
      if (K - cnt) % 2 == 0:
        b = now
        break
    if cnt == K:
      break
else:
    while True:
      now = now + D
      cnt+=1
      if now <= 0:
        if (K - cnt) % 2 == 0:
          a = now
      else:
        if (K - cnt) % 2 == 0:
          b = now
          break
      if cnt == K:
        break
print(min(abs(a),abs(b)))

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 = list(map(int,input().split()))

shoki = X // D
if shoki > X:
  print(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:
i = list(map(int, input().split()))
x = i[0]
k = i[1]
d = i[2]
c=x-d*k
if c>=0:
    print(c)
else:
    p=-c//(2*d)
    ans = 10000000000000000000000000000000000000000
    for i in range(-100,100):
        if 0<=p+i and p+i<=k:
            ans=min(ans,abs(c+2*d*(p+i)))
    print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if abs(x)-k*d >= 0: # 0の反対側へ行けない
    ans = abs(x)-k*d
else:
    c = x//d#0を超えないときの移動回数
    ans1 = abs(x) - c*d
    ans2 = abs(x) - (c+1)*d
    if (k-c)%2 == 0:
        ans = abs(ans1)
    else:
        ans = abs(ans2)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

KL = (X+K*D)/(2*D)

if KL < K:
    ans1 = X - math.floor(KL)*D + (K-math.floor(KL))*D
    ans2 = X - math.ceil(KL)*D + (K-math.ceil(KL))*D
    if abs(ans1) < abs(ans2):
        print(abs(ans1))
    else:
        print(abs(ans2))
else:
    print(abs(X-K*D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

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


print(ans)

Submission status: Wrong Answer

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

k = round(X / D)

if k > K:
    if k == 1:
        print(abs(abs(X) - K * D))
    else:
        print(min(abs(abs(X) - K * D), abs(abs(X) - (K - 1) * D)))

else:
    if k % 2 == K % 2:
        print(min(abs(abs(X) - k * D), abs(abs(X) - (k + 2) * D), abs(abs(X) - (k - 2) * D)))

    else:
        print(min(abs(abs(X) - (k + 1) * D), abs(abs(X) - (k - 1) * D), abs(abs(X) - (k + 3) * D), abs(abs(X) - (k - 3) * D)))

Submission status: Wrong Answer

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

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

        if(times%2 == K%2):
            print(abs(min_l))
        else:
            print(abs(min_r))
    else:
        print(abs(X+D*K))

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int, input().split())
X = abs(X)
# ずっとマイナスにいく
if X-K*D >= 0:
    print(X-K*D)
    sys.exit()

ans = X%D
ans2 = abs(ans-D)
print(min(ans, ans2))

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

x=abs(x)

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

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=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())
ans = 0
if X == 0:
    if K % 2 != 0:
        ans = D
elif X > 0:
    a = int(X/D)
    if K > a:
        diff = K - a
        X_temp = X - a * D
        if abs(X_temp - D) < X_temp:
            if (diff - 1) % 2 == 0:
                ans = abs(X_temp - D)
            else:
                ans = X_temp
        else:
            if diff % 2 == 0:
                ans = X_temp
            else:
                ans = abs(X_temp - D)
    else:
        ans = X - K * D
else:
    a = int(X/D)
    if K > a:
        diff = K - a
        X_temp = X + a * D
        if abs(X_temp + D) < abs(X_temp):
            if (diff - 1) % 2 == 0:
                ans = abs(X_temp + D)
            else:
                ans = abs(X_temp)
        else:
            if diff % 2 == 0:
                ans = abs(X_temp)
            else:
                ans = abs(X_temp + D)
    else:
        ans = abs(X + K * D)
print(ans)

Submission status: Wrong Answer

Solution code:
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((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 = list(map(int,input().split()))

shoki = X // D
if shoki >= X:
  print(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:
s = input().split()
x = int(s[0])
k = int(s[1])
d = int(s[2])
ans = 0
x = abs(x)
if x > 0:
    if k*d < x:
        ans = x - (k*d)
    else:
        con = x/d
        rem = k - con
        if rem%2 == 1:
            ans = d - x%d
        else:
            ans = x%d

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

print(ans)

Submission status: Wrong Answer

Solution code:
#c
x, k, d=map(int, input().split())
rest = k - (abs(x) // abs(d))

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

else:
    if x >= 0:
        near = x- d*(abs(x) // abs(d))
    else:
        near = x+ d*(abs(x) // abs(d))

    if rest % 2 == 0:
        print(abs(near))
    else:
        if near >= 0:
            print(abs(near - d))
        else:
            print(abs(near + d))

Submission status: Wrong Answer

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

guki=(K-one)%2

if guki==0:
    print(abs(Xa-Da*one))
else:
    print(abs(Xa-Da*(one+1)))

Submission status: Wrong Answer

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

X = abs(X)
X0 = abs(X)

for i in range(K):
    X -= D
    K -= 1
    if abs(X) < abs(X0):
        X0 = X
    else:
        break

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

Submission status: Wrong Answer

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

if X - K*D > 0:
    ans = X-K*D
elif X + D*K < 0:
    ans = abs(X+D*K)
else:
    ans = float('inf')
    for i in range(-1, 2):
        res = X + D*(K-2*(n+i))
        ans = min(ans, abs(res))
        #print(res)

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

min_abs = 10**15
min_step = X // D
if K <= min_step:
    min_abs = X - D*K
else:
    lowest_plus = X - D*min_step
    highest_minus = abs(lowest_plus - D)
    if (K - min_step) % 2 == 0:
        min_abs = lowest_plus
    else:
        min_abs = highest_minus

print(min_abs)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split()) #x 最初の座標 k 移動回数　D移動距離
dum2 = 10000000
div,mod = divmod(x,d)
if div >= k:
    print(x-(k*d))
else:
    dum1 = mod
    if div + 1 <= k:
        dum2 = abs(mod-d)
    if (k-div)%2 == 1:
        print(dum2)
    else:
        print(dum1)

Submission status: Wrong Answer

Solution code:
X, K, D = [int(i) for i in input().split()]
ans = 0
if K*D < X:
    ans = X - K * D
elif X//D%2 == 0 and K%2 == 0 or X//D%2 * K%2 == 1:
    ans = X%D
else:
    ans = D - X % D

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
X,K,D=map(int, sys.stdin.readline().split())
a,b=X/D,X%D
if a<K:
    K-=a
    if K%2==0:
        print b
    else:
        print abs(b-D)
else:
    print X-D*K

Submission status: Wrong Answer

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

    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import sys
X,K,D=map(int,input().split())
t=abs(X)//D
if t>=K:
  print(abs(X)-D*K)
  sys.stderr.write("1\n")
elif t+1==K:
  print(D*K-abs(X))
  sys.stderr.write("2\n")
else:
  if abs(abs(X)-D*t)<abs(D*(t+1)-abs(X)):
    if (K-t)%2==0:
      print(abs(X)-D*t)
      sys.stderr.write("3\n")
    else:
      print(D*(t+1)-abs(X))
      sys.stderr.write("4\n")
  else:
    if (K-t)%2==0:
      print(D+(t+1)-abs(X))
      sys.stderr.write("5\n")
    else:
      print(abs(X)-D*t)
      sys.stderr.write("6\n")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import math
#input=stdin.readline
#def print(x):stdout.write(str(x)+'\n')
x,n,d=map(int,input().split())
ans=x
if x>0:
    if x-n*d<0:
        c=x//d
        n=n-c
        ans=x-c*d
        a=abs(ans-d)
        b=ans
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x-n*d)
elif x==0:
    if n%2==0:
        print(x)
    else:
        print(x+d)
else:
    if x+n*d>0:
        c=x//d
        n=n-c
        ans=x+c*d
        a=abs(ans+d)
        b=ans
        if n%2==0:
            print(b)
        else:
            print(a)
    else:
        print(x+n*d)

Submission status: Wrong Answer

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

while x>0 and k>0:
    x-=d
    k-=1

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

Submission status: Wrong Answer

Solution code:

if __name__ == '__main__':
    a = [int(i) for i in input().split()]
    minx = a[0]
    now = a[0]
    count=0

    for i in range(a[1]):
        x1 = now + a[2]
        x2 = now - a[2]

        if minx > abs(x1) or minx > abs(x2):
            if abs(x1) > abs(x2):
                now =x2
                minx = abs(x2)
                count += 1
            else:
                now =x1
                minx = abs(x1)
                count += 1
        else:
            break
    if (a[1]-count) %2 ==0:
        print(minx)
    else:
        print(min(abs(minx+a[2]),abs(minx-a[2])))

Submission status: Wrong Answer

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

k = X // D
if abs(X - D * k) <= abs(X - D * (k + 1)):
    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 - 1) * D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[23]:


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


# In[24]:


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


# In[ ]:

Submission status: Wrong Answer

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

# case0: 絶対値を超える範囲まで届かない場合
if x > k*d:
    print (x - k*d)
# case1: 絶対値を超える範囲に届く場合: 0の両隣のどちらかを選択
else:
    wall = (x // d) + 1 # 0を超えるのに必要な移動回数
    if wall % 2 == 0: # Xが偶数時: どちらも選択可能
        print (abs(x - wall*d))
    else: # Xが奇数時: どちらかのみとなるので注意
        print (abs(x - (wall-1)*d))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

mod = x % d
d2 = abs(x)

if mod+k*d >= x:
    if k%2 == 0:
        print(mod)
    else:
        x1 = abs(mod+d)
        x2 = abs(mod-d)
        print(min(x1, x2))
else:
    print(d2-k*d)

Submission status: Wrong Answer

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    x,k,d = map(int, input().split())
    n = x//d
    ans = x
    if n >= k:
        print(x-k*d)
        exit()

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

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

if np.abs(x) >= np.abs(k*d):
    print(np.abs(x)-np.abs(k*d))
    exit()
for i in range(k):
    if x > 0:
        if np.sign(x) != np.sign(x-d):
            print(min(np.abs(x),np.abs(x-d)))
            exit()
        x = x - d
    elif x < 0:
        if np.sign(x) != np.sign(x+d):
            print(min(np.abs(x),np.abs(x+d)))
            exit()
        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:
A = list(map(int,input().split()))
X,K,D = A[0], A[1], A[2]

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

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

Submission status: Wrong Answer

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

if x == 0:
    print(0 if k % 2 == 0 else k)
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:
X, K, D = map(int, input().split())
X = abs(X)

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

K -= X//D

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x > 0:
    d *= -1
    for i in range(k):
        x += d
        if x < 0:
            d *= -1
    print(abs(x))
    exit()

if x < 0:
    for i in range(k):
        x += d
        if x > 0:
            d *= -1
    print(abs(x))
    exit()

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(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 (x//d)<=k:
  x-=(x//d)*d
  k-=x//d+1

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