Problem:


Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

Solution code:
import math;

# input 現在座標ｘ, 移動回数k, 移動距離d
x, k, d = map(int, input().split())


a = math.ceil(x / d);

if a >= k:
    for i in range(k):
        if x > 0:
            x = x - d;
        elif x <= 0:
            x = x + d;

elif a < k:
    
    if x > 0:
        for i in range(a):
            x = x - d;

        if (k-a) % 2 != 0:
            x = x + d;

    elif x <= 0:    
        for i in range(a):
            x = x + d;

        if (k-a) % 2 != 0:
            x = x - d;
            

# xが負の数の場合正の数に編集する
if x < 0:
    x = x * (-1);

print(x);

Submission status: Wrong Answer

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

ans = 0
count = X // D

if X >= 0:
    if K <= count:
        ans = X - D * K
    else:
        ans = X - D * count
        if ((K - count) % 2 == 1):
            ans = ans - D
else:
    if K <= (-1) * count:
        ans = X + D * K
    else:
        ans = X + D * count
        if ((K - count) % 2 == 1):
            ans = ans + D

if ans < 0:
    ans = ans * (-1)

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
'''
for i in range(k):
  if abs(x-d)<abs(x+d):
    x-=d
  elif abs(x+d)<abs(x-d):
    x+=d
  else:
    x+=d
print(abs(x))
'''

c=0
while c<k:
  while abs(x-d)<abs(x+d):
    x-=d
    c+=1
  while abs(x+d)<abs(x-d):
    x+=d
    c+=1
print(abs(x))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m, x = map(int, input().split())
p = n //x 
p = min(p, m)
n -= x * p
m -= p
if m % 2 == 0:
    print(n)
else:
    a = abs(n-x)
    b = abs(n+x)
    print(min(a,b))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C_fix
# CreatedDate:  2020-08-15 21:52:53 +0900
# LastModified: 2020-08-15 22:04:27 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    x, k, d = map(int, input().split())
    tmp = 1e31
    s_index = 0
    flag = 1
    if x > 0:
        for s in range(k+1):
            if tmp > abs(x-s*d):
                tmp = abs(x-s*d)
                s_index = s
            else:
                flag = 0
                break
    elif x < 0:
        for s in range(k+1):
            if tmp < abs(x+s*d):
                tmp = abs(x+s*d)
                s_index = s
            else:
                flag = 0
                break
    else:
        s_index = 0
    if flag == 1:
        print(tmp)
    else:
        if (k-s_index)%2 == 0:
            print(tmp)
        elif x > 0:
            print(abs(tmp-d))
        else:
            print(tmp+d)
            




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from decimal import *
x, k, d = map(Decimal, input().split())

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

Submission status: Wrong Answer

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

times_in = abs(X)//D
times_out = (abs(X)//D) + 1

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

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

print(ans)

Submission status: Wrong Answer

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

q = X // D
mod = X % D

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


print(ans)

Submission status: Wrong Answer

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

x,k,d = I()
a = x%d
b = x //d
if x == d:
    if k % 2 ==0:
        print(x)
        sys.exit()
    else:
        print(0)
        sys.exit()
        
if b <= k:
    print(a)
else:
    print(abs(x-k*d))

Submission status: Wrong Answer

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


for i in range(k):

    if x + d >= 0:
        break
    elif x - d <= 0:
        break

    elif abs(x + d) < abs(x - d):
        x = x + d
        a += 1
    else:
        x = x - d
        a += 1

if k == a:
    print(abs(x))

elif (k - a) % 2 == 0:
    print(abs(x))

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

Submission status: Wrong Answer

Solution code:
X, K ,D = input().split()
X = int(X)
K = int(K)
D = int(D)
if (K == 1):
  if (X >= 0):
    print(abs(X -D))
  if (int(X) < 0):
      print(abs(X + D))
  exit()
if (X >= 0):
  while ((X > 0) or (K == 0)):
         X = X - D
         K -= 1
  print(abs(abs(X) - (K % 2) *D))
elif (X < 0):
  while ((X < 0) or (K == 0)):
         X = X + D
         K -= 1
  print(abs(abs(X) - (K % 2) *D))

Submission status: Wrong Answer

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

abs_num = abs(X)
counter = 0

while (counter < K):
  p = abs(X)
  if (X > 0):
    X = X - D
  else:
    X = X + D
  if (p < abs(X)):
    break
  abs_num = abs(X)
  counter += 1

print(abs_num)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
anl = []
for i in range(k+1):
	anl.append(abs(d*k + x - 2*d*(i+1)))
print(min(anl))

Submission status: Wrong Answer

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

X = abs(X)
a = X/D
ans = 0

if a>=K:
    ans = X-(D*K)
else:
    if K%2==0:
        if a%2==0: ans = X-(D*a)
        else: ans = X-(D*a)-D
    else:
        if a%2==0: ans = X-(D*a)-D
        else: ans = X-(D*a)

ans = int(abs(ans))
print(ans)

Submission status: Wrong Answer

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

tmp = [X]
min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)

    a = min([abs(X-D), abs(X+D)])
    if X in tmp:
        min_score = valid(a, min_score, K-i)
        break
    tmp.append(X)
print(min_score)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
while True:
    if x-d<=0:
        print(min(x,(abs(x-d))))
        break
    else:
        if k:
            x-=d
            k-=1
        else:
            print(x)
            break

Submission status: Wrong Answer

Solution code:
x,k,d=[int(i) for i in input().split()]

x=abs(x)

t=x

if k%2==1:
    t-=d
    k-=1

min=abs(t)

if t-k*d>0:
    print(t-k*d)
    exit(0)

if t-k*d<0:
    s=(x/(d*2))*2
    k-=s
    t-=d*s

while(-x<t and k>0):
    t-=d*2
    k-=2
    if min < abs(t):
        break
    else:
        min=abs(t)

print(min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = X
done = False
while K:
    K -= 1
    pre = x
    if x >= 0:
        x -= D
    else:
        x += D
    if pre * x <=0:
        if K % 2 == 0:
            ans = abs(x)
        else:
            ans = abs(pre)
        done = True
        break

if not done:
    ans = x

print(ans)

Submission status: Wrong Answer

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

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


for p in range(k+1):
  ima = abs(x+(d*p-d*(k-p)))
  if p == 0:
    mae = ima
    continue
  if mae < ima:
    print(mae)
    exit()
  mae = ima

Submission status: Wrong Answer

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

q, mod = divmod(abs(X), abs(D))

ans = 0
if q <= K:
    if mod == 0:
        if q % 2 == 1:
            ans = D
        else:
            ans = mod
    else:
        if q % 2 == 0:
            ans = mod
        else:
            ans = D - mod
else:
    div = q - K
    ans = div * D + mod

print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
result = 0
amari = K%2
amari2 = 0

for i in range(1, K+1):
    if X <= (i*D):
        result = i*D 
        amari2 = i%2
        break

if amari2 == amari:
    result = result - X
else:
    if result > X:
        result = result - (X + D)
    else:
        result = (result + D) - X

print(abs(result))

Submission status: Wrong Answer

Solution code:
def zet(num):
    if num<0:
        return -num
    else:
        return num

x,k,d = map(int, input().split())
x = zet(x)
amari = x%d
syou = x//d
if syou > k:
    syou = k

sa = k-syou
genzai = zet(x) - syou * d
if sa == 0:
    print(genzai)
    exit()
    
if sa % 2 == 0:
    if amari*2 < d:
        print(genzai)
else:
    print(zet(genzai - d))

Submission status: Wrong Answer

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


def solve(X: int, K: int, D: int):
    X=abs(X)

    ans=min(X, X - K * D)

    if K%2 == 1:
        X += D
        ans = min(ans, X)
    Kh=(K+1)//2

    N = X // (D*2)
    ans = min(ans, abs(X - D * 2 * (N - 1)))
    ans = min(ans, abs(X - D * 2 * N))
    if N < Kh:
        ans = min(ans, abs(X - D * 2 * (N+1)))

    print(ans)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(X, K, D)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
ans=0
KK=K-X//D
XX=X%D

if KK%2==0:
    ans=abs(XX)
else:
    if abs(XX+D)>=abs(XX-D):
        ans=abs(XX-D)
    else:
        ans=abs(XX+D)
        
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if( x == 0 ):
    if( k % 2 == 0 ):
        print("{}".format(0))
    else:
        print("{}".format(abs(d)))

if( x > 0 ) :
    if( x - k * d >= 0 ) :
        ret = x - k * d
        print("{}".format(ret))
    rem = x % d
    cost = x // d
    rem2 = abs(rem - d)
    remc = k - cost
    if( remc % 2 == 0 ) : 
        ret = rem
        print("{}".format(ret))
    else:
        ret = rem2
        print("{}".format(ret))
elif( x < 0 ) :
    if( x + k * d <= 0 ) :
        ret = abs( x + k * d )
        print("{}".format(ret))
    rem = (-x) % d
    cost = (-x) // d
    rem *= -1
    rem2 = rem + d
    remc = k - cost
    if( remc % 2 == 0 ):
        ret = abs(rem)
        print("{}".format(ret))
    else :
        ret = rem2
        print("{}".format(ret))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().rstrip().split())
before = X + 1
for i in range(K):
  if X == before:
    if (K-i)%2 == 1:
      if X<0:
        X += D
      else:
        X -= D
    else:
      X = before
    break
  if X<0:
    X += D
  else:
    X -= D
  before = X
print(abs(X))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

abs_x = abs(X)
num = int(abs_x / D)


def calc_min():
    if K < num:
        # マイナス方面or+方面に向かうだけで回数の上限
        if X < 0:
            return X + D * K
        return X - D * K

    # これより先は距離Dを行ったり来たりするだけ
    base_pos = X - D * num if X > 0 else X + D * num

    if num % 2 == 0:
        return base_pos
    else:
        return base_pos - D if base_pos > 0 else base_pos + D


print(abs(calc_min()))

Submission status: Wrong Answer

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


if K <= X//D:
    print(abs(X-D*K))
    
else:
    if X%D == 0:
        print(abs(D))
    else:
        Y = X%D
        a = abs(D)+Y
        b = abs(Y-D)
        print(min(Y,a,b))

Submission status: Wrong Answer

Solution code:
from collections import deque
import collections

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

X,K,D = MI()
flag = 1
if(X<0):
  flag = -1

n = int(X/D)
if(n < K):
  X = X-n*D*flag
  K = K - n
else:
  X = X -K*D*flag
  print(abs(X))
  exit()

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

Submission status: Wrong Answer

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

x=abs(x)
i=x//d

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

# 正方向へn回動くとすると
# abs(x + 2dn - kd)を最小とするようなnを求める

n = d//2
min = x + 2*d*n - d*k
for _ in range(10):
    if min < 0:
        n += 1
    else:
        n -= 1
    if n < 0 or n > d:
        break
    v = y = x + 2*d*n - d*k
    if abs(v) < abs(min):
        min = v
    else:
        break
print(abs(min))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if k%2 == 0 and x==d:
  print(abs(x))
  exit()
for i in range(k+1):
  if abs(x) > abs(x - (d*i) + d*(k-i)):
    #print(x)
    x = x - (d*i) + d*(k-i)
print(abs(x))

Submission status: Wrong Answer

Solution code:

def solve():
    a = X//D

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



if __name__ == "__main__":
    X,K,D = list(map(int, input().split()))
    solve()

Submission status: Wrong Answer

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

low = 0
high = K

while low <= high:
    mid = (low + high) //2
    i = mid
    j = K-mid
    ans_pre = (X + D*i - D*j)
    if ans_pre > 0:
        high = mid-1
    else:
        low = mid+1
        

if low > K :
    low = high
    
ans_l = min(abs((X + D*low - D*(K-low))), abs(X + D*(K-low)-D*low))
    
print(ans_l)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math
X, K, D = list(map(int, input().split(" ")))

if X==K==D:
    print(X)
    exit()
x = X
vmin = abs(X)
cnt = 0
# for k in range(K):
#     if X>0:
#         x -= D
#     else:
#         x += D
#     if vmin>abs(x):
#         vmin = abs(x)
#         cnt += 1
#     else:
#         break
cnt = min(math.floor(X / D), K)
if X>0:
    print(abs(X - (cnt * D)))
else:
    print(abs(X + (cnt * D)))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
t = x // d
a = []
a.append(t - 3)
a.append(t - 2)
a.append(t - 1)
a.append(t)
a.append(t + 1)
a.append(t + 2)
a.append(t + 3)
a.append(k)
a.append(k - 1)
a.append(-k)
a.append(-k + 1)

res = abs(x)
for i in a:
    if i % 2 == k % 2 and abs(i) <= k:
        res = min(res, abs(x - i * d))
print(res)

Submission status: Wrong Answer

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

q = X // D
mod = X % D

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


print(ans)

Submission status: Wrong Answer

Solution code:
a,b,c = map(int,input().split())
if (a==c==b):
   print(a)
else:
   f = []
   dis = 0
   for i in range(b):
      a = a - c
      f.append(abs(a))
   print(min(f))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
a=divmod(abs(x),d)  
if a[0]<k:
  if a[1]==0 and a[0]%2==1:
    print(d)
  elif a[1]==0 and a[0]%2==0:
    print(0)
  elif a[1]!=0 and a[0]%2!=0:
    if a[1]<=abs(d-a[1]):
      print(a[1])
    else:
      print(abs(d-a[1]))
  else:
    print(a[1])
else:
  print(abs(x)-d*k)

Submission status: Wrong Answer

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

ans = 10**15+1
for i in range(K):
    if X >= 0:
        X -= D
    else:
        X += D
    if X <= abs(ans):
        ans = X
    if abs(ans) >= K:
        break

print(abs(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
[X,K,D]=list(map(int, input().split()))
abx=round(abs(X))
n=abx//D
if n>K:
    if X>0:
        ans=X-K*D
    else:
        ans=X+K*D
else:
    x2=abx-n*D
    x1=(n+1)*D-abx
    if x2>=x1:
        if (K-n-1)%2==0:
            ans=x1
        else:
            ans=x2
    else:
        if (K-n)%2==0:
            ans=x2
        else:
            ans=x1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

tmp = [X]
min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    a = min([abs(X-D), abs(X+D)])
    if X in tmp:
        min_score = valid(a, min_score, K-i)
        break
    tmp.append(min_score)
print(min_score)

Submission status: Wrong Answer

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

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

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

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

	imaima = abs(X-(K*D))
	if imaima == 0:
		print(0)
		sys.exit()

	if X > 0:
		pass
	else:
		X = abs(2*X)

	if X - K*D > 0:
		ans = X - K*D
		print(ans)
		sys.exit()
	else:
		tmp = X // D
		nokori = K - tmp
		if nokori % 2 == 0:
			ans = X % D
			print(ans)
			sys.exit()
		else:
			o000 = X % D
			assd = abs(D - abs(D-o000))
			print(assd)
			sys.exit()





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

Submission status: Wrong Answer

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

b = x - k * d
b_ = x + k * d


if x >= 0:
    a = -(-x // d)
    if a >= k:
        if b >= 0:
            print(b)
        else:
            print(-b)
    elif a < k:
        b = x - a * d
        c = k - a
        if c % 2 == 0:
            if abs(b) > abs(-b + d):
                print(-b + 2*d)
            else:
                print(-b)
        else:
            print(b + d)
if x < 0:
    a = x // d
    if -a >= k:
        if b_ >= 0:
            print(b_)
        else:
            print(-b_)
    elif -a < k:
        b = x - a * d
        c = k + a
        if c % 2 == 0:
            if abs(b) > abs(b - 2*d):
                print(b - d)
            else:
                print(b)
        else:
            print(b - d)

Submission status: Wrong Answer

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

if X<0:
	L=X+i*D
	while L<=0:
		i+=1
		L=X+i*D
	if abs(L)>abs(X+(i+1)*D):
		i+=1
		L=abs(X+i*D)
else:
	L=X-i*D
	while L>=0:
		i+=1
		L=X-i*D
	if abs(L)>abs(X-(i-1)*D):
		i-=1
		L=abs(X-i*D)

kaisuu=K-i
if kaisuu>=0:
	if kaisuu%2==0:
		print(abs(L))

	else:
		a=abs(L+D)
		b=abs(L-D)
		if a>=b:
			print(a)
		else:
			print(b)
            
else:
	if X>=0:
		print(abs(X-D*K))
	else:
		print(abs(X+D*K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
  from sys import stdin, exit
  x,k,d = map(int, stdin.readline().split())
  m = [abs(x)]
  for a in range(k):
    x = x - d if x > 0 else x + d
    if abs(x) in m:
      print(min(m) if (k - a) // 2 == 0 else min(m) + d)
      exit(0)
    else:
      m.append(abs(x))
  print(abs(x))
main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m, x = map(int, input().split())
p = n //x 
p = min(p, m)
n -= x * p
m -= p
if m % 2 == 0:
    print(n)
else:
    print(abs(n-x))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if x <= 0:
    flag = False
else:
    flag = True
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        if flag:
            x = x - (a * d)
        else:
            x = x + (a * d)
    else:
        x = x - (a * d)
        if flag:
            x = x - abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

Solution code:
def cal(x,f, k ,d):
    return x+(((2*f)-k)*d)
[x, d, k]=list(map(int, input().split()))
low=1
high=k
mini=1000000000000000000
curr=abs(cal(x, k , k, d))
while low<=high:
    mid=low+(high-low)//2
    new=cal(x, mid, k ,d)
    if abs(new)>abs(curr):
        low=mid+1
    else:
        high=mid-1
    curr=abs(new)
    mini=min(abs(new), mini)
print(mini)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
mx = x + d*k
mn = x - d*k

if (x <= 0 and mx > 0) or (x >= 0 and mn < 0):
  if k%2 == 0:
    if x < 0:
      b = min(abs(abs(x)%(2*d)),abs(x%(2*d)-2*d))
    else:
      b = min(abs(x%(2*d)),abs(x%(2*d)-2*d))
    b = min(abs(b),abs(mx),abs(mn))
  else:
    if x > 0:
      b = min(abs(abs(x-d)%(2*d)),abs(abs(x-d)%(2*d)-2*d))
    else:
      b = min(abs(abs(x+d)%(2*d)),abs(abs(x+d)%(2*d)-2*d))
  ans = min(abs(b),abs(mx),abs(mn))
else:
  ans = min(abs(mx),abs(mn))
print (ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math
X, K, D = list(map(int, input().split(" ")))

if X==K==D:
    print(X)
    exit()
x = X
vmin = abs(X)
cnt = 0
# for k in range(K):
#     if X>0:
#         x -= D
#     else:
#         x += D
#     if vmin>abs(x):
#         vmin = abs(x)
#         cnt += 1
#     else:
#         break
cnt = min(math.floor(X / D), K)
print(cnt)
if X>0:
    print(abs(X - (cnt * D)))
else:
    print(abs(X + (cnt * D)))

Submission status: Wrong Answer

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
import math
x, k, d = map(int, input().split())

num = k*d

x = abs(x)

if x >= num:
    print(x - num)
else:
    if x > d:
        j = int(x / d)
    else:
        if x*2 > d:
            j = 1
        else:
            j = 0

    if j != 0:
        b = x % (d*j)
    else:
        b = x
    min = min(b, d - b)
    max = max(b, d - b)
    if min == d - b:
        j += 1

    if (k - j) % 2 == 0:
        print(min)
    else:
        print(max)

Submission status: Wrong Answer

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