Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

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

Sample Input 1

6 2 4

Sample Output 1

2

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

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

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

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

Sample Input 2

7 4 3

Sample Output 2

1

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

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

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

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

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

times = abs(X) // D

if times < K:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
    K = K - times
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
else:
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
print(current_position)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
#0またがない
if X > K*D:
    ans = X - K*D
#0またぐ
else:
    while X > 0 :
        X = X - D
        K -= 1
    if K % 2 == 0:
        ans = min(abs(X),X+D)
    if K % 2 != 0:
        ans = X+D

print(ans)

Submission status: Wrong Answer

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

if K > 100000 and K % 2 == 0:
    K = 10000
if K > 100000 and K % 2 == 1:
    K = 10001

for i in range(K):
    if place == 0 and (K-i)%2==0:
        break

    if place >= 0 and (place - D) >= 0:
        place -= D

    elif place >= 0 and (place - D) <= 0:
        place -= D
  
    else: 
        place += D 
      
if place < 0:
    place = abs(place)
print(place)

Submission status: Wrong Answer

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

isReturn = False
i = 0
while i < k:
    if(x >= 0):
        x -= d
    else:
        x += d
        isReturn = True
    if isReturn:
        if((k-i) % 2 == 0):
            # print("a", k)
            i = k-2
        else:
            i = k-1
            # print("b", k)

    i += 1

print(abs(x))

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int, input().split())
K = int(K/D)+1
for _ in range(K):
    if abs(X+D) > abs(X-D):
        X -= D
    else:
        X += D

print(abs(X))

Submission status: Wrong Answer

Solution code:
import math;

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


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

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

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

if X > 0:
    k = X - K * D
    if k >= 0:
        print(k)
    else:
        kouho1 = X % D
        kouho2 = abs(kouho1 - D)
        sho = X // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)
if X < 0:
    k = X + K * D
    if k <= 0:
        print(k)
    else:
        kouho1 = (-X) % D
        kouho2 = abs(kouho1 - D)
        sho = (-X) // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ans=X

a=X//D

"""
for i in range(a):
    ans=min(abs(ans-D),abs(ans+D))
    
"""

if (K>a>0 or -K<a<0) and (K-a)%2==0:
    ans=min(abs(X-(a)*D),abs(X+(a)*D))
    
elif (K>a>0 or -K<a<0) and (K-a)%2==1:
    ans=min(abs(X-(a+1)*D),abs(X+(a+1)*D))
elif a>=K:
    ans=abs(X-K*D)
else:
    ans=abs(X+K*D)
    


print(ans)

Submission status: Wrong Answer

Solution code:
import numpy

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

now = X
temp = None
temp2 = None
count = 0
for i in range(0, K):
    if min(abs(now + D), abs(now - D)) == abs(now + D):
        if count > 2:
            temp2 = temp
        temp = now
        now += D
    else:
        if count > 2:
            temp2 = temp
        temp = now
        now -= D
    count += 1
    if temp2 == now:
        break
    

print(abs(now))

Submission status: Wrong Answer

Solution code:
now2,kaisu,power = map(int,input().split())
now=abs(now2)
if abs(now)-kaisu*power>=0:
    print(abs(now)-kaisu*power)
else:
    n=now/power
    sein=round(n)
    if (kaisu+sein)%2==0:
        print(abs(now-power*sein))
    if (kaisu+sein)%2!=0:
        Q=(min(now-power*(sein-1),now-power*(sein+1)))
        print(abs(Q))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if x>=0:
    while x > 0:
        x = x-d
        # print('x',x)
        k-=1
        if k == 0:
            # print('a',x)
            break
        if x==0:
            if k%2!=0: 
                x=d
                d=0
                # print('aa',d,x)
            else: 
                x=0
                d=0
            break
    # print(abs(x),abs(x+d))
    print(min(abs(x),abs(x+d)))
else:
    while x < 0:
        x = x+d
        # print('x',x)
        k-=1
        if k == 0:
            # print('a',x)
            break
        if x==0:
            if k%2!=0:
                x=d
                d=0
                # print('aa',d,x)
            else: 
                x=0
                d=0
            break
    print(min(abs(x),abs(x-d)))

Submission status: Wrong Answer

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

if x > k*d:
    # 届かない場合は極力近づく
    print(x-(k*d))
elif x < k*d and x > (k-1)*d:
    # ぎりぎり０をまたげるパターン
    x1 = x - k*d
    x2 = x - (k-2)*d
    # またぐか、2つ前か、の2択
    if abs(x1) > abs(x2):
        print(abs(x2))
    else:
        print(abs(x1))
else:
    # それ以降
    # 目的地を求める
    step1 = int(x/d) # 目的地までのステップ数
    a1 = x - step1*d # 目的地
#    print("step1:{} a1:{}".format(step1,a1))
    # 0を突き抜けたほうが目的地のケース
    if abs(a1) > abs(a1-d):
        a1 = a1-d
        step1 +=1
#        print("step1:{} a1:{}".format(step1,a1))
    if (k-step1)%2 == 0:
        # 残ステップが偶数ならぴったり
        a2 = a1
    else:
        # 残ステップが奇数なら1個余分に移動
        if a1 > 0:
            a2 = a1-d
        else:
            a2 = a1+d
    print(abs(a2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
s = X//D
a = X%D
z = K-s
if z % 2 == 0:
    print(a)
else:
    if D / 2 < a:
        print(D - a)
    else:
        print(a)

Submission status: Wrong Answer

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

num = X - K*D
if 0<num:
  print(num)
  sys.exit(0)
for _ in range(K):
  num += D
  if 0<num:
    break
print(min(-(num-D), num))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
a=divmod(abs(x),d)  
if x==0:
  if k%2==0:
    print(0)
  else:
    print(d)
else:
  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:
      print(d-a[1])
      
    else:
      print(a[1])
  else:
    print(abs(x)-d*k)

Submission status: Wrong Answer

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

    y = x

    for i in range(k // 2):
        if abs(x-d) < abs(x+d):
            x = x - d
        else:
            x = x + d

        if abs(x-d) < abs(x+d):
            x = x - d
        else:
            x = x + d
        if y == x:
            break

        y = x

        if abs(x - 4 * d) > 0:
            x = x - 4 * d
            i -= 4

    if k % 2 == 1:
        if abs(x-d) < abs(x+d):
            x = x - d
        else:
            x = x + d


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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
Xa, K, D = map(int, input().split())
X = abs(Xa)
Xs = X // D
Xm = X % D
if K < Xs:
#     print("A")
    print(abs(X-K*D))
elif Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif Xm / 2 > K:
#     print("B")
#     print(abs(D-Xm))
    if ((K-Xs) % 2 == 0):
        print(abs(D-Xm))
    else:
        print(abs(D-K))
else:
#     print(abs(D-K))
    if ((K-Xs) % 2 == 0):
        print(abs(D-K))
    else:
        print(abs(D-Xm))

Submission status: Wrong Answer

Solution code:
def abso(n):
    if n < 0:
        n *= -1
    return n


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

    if X//D>K:
        ans = X-K*D
    else:
        ans = X- X//D*K
        if (K - X//D)%2==1:
            if abso(X- X//D*D+D) > abso(X- X//D*D-D):
                ans = X- X//D*D-D
            else:
                ans = X- X//D*D+D


    return abso(ans)

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

Submission status: Wrong Answer

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

if k%2==0:
  ans=0 #偶数
else:
  ans=1 #奇数

semin=x//d #最小に行くための個数
if semin%2==0:
  minb=0 #最小に行くための個数が偶数
else:
  minb=1 #最小に行くための個数が奇数

if semin>k:
  print(abs(x-k*d))
  exit()
else:
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-k*semin))
    else:
      print(abs((x-(d*semin))-d))
  else:#ｋが奇数の時
    if minb==0:
      print(abs((x-(d*semin))-d))
    else:
      print(abs(x-k*semin))

Submission status: Wrong Answer

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

cent = x % d
num = x // d

if d / 2 > cent:
    cent = d - cent
    num += 1

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

rem = k - num

if rem % 2 == 0:
    print(cent)
    exit(0)
else:
    print(d - cent)

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

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())
if  (X - K*D) < D:
  print((X - K*D)%D)
else:
  print(X-K*D)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if K > 50000 and K % 2 == 0:
    K = 10000
if K > 50000 and K % 2 == 1:
    K = 10001

for i in range(K):
    if place == 0 and (K-i)%2==0:
        break

    if place >= 0 and (place - D) >= 0:
        place -= D

    elif place >= 0 and (place - D) <= 0:
        place -= D
  
    else: 
        place += D 
      
if place < 0:
    place = abs(place)
print(place)

Submission status: Wrong Answer

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

res = 0
for i in range(K):
    n = res if res != 0 else X
    
    res_m = n - D
    res_p = n + D

    res_m *= -1 if res_m < 0 else 1

    res = res_m if res_m < res_p else res_p

print(res)

Submission status: Wrong Answer

Solution code:
X,  K,  D = list(map(int, input().split()))
L = X + D * K
 
if   X>= 0 and (K == 1 or D == 1):
      print(abs(X - D*K))
      exit()

elif X < 0 and (K == 1 or D == 1):
      print(abs(X + D*K))
      exit()

elif L >= 0:
   if L / (2 * D) - int(L / (2 * D)) > 0.5:
      a = L // (2 * D) + 1
   else:
      a = L // (2 * D)
 
elif L < 0:
   if L / (2 * D) - int(L / (2 * D)) < -0.5:
      a = int(L / (2 * D)) - 1
   else:
      a = int(L / (2 * D)) 
print(abs(X-2*D*a+D*K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

max_ = x
pre_max_ = 0
for i in range(k): 
    pre_max_ = max_
    if max_ > 0:
        max_ -= d
    else:
        max_ += d

    if abs(max_) >= abs(pre_max_):
        break

if min(abs(max_), abs(pre_max_)) <= 1:
    print(max(abs(max_), abs(pre_max_)))
else:
    print(min(abs(max_), abs(pre_max_)))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
def resolve_best_i(X, K, D):
    if abs(X - int(X / D) * D) < abs(X - (int(X / D)+1)*D):
        return abs(X - int(X / D) * D), int(X /D)
    else:
        return abs(X - (int(X / D)+1)*D), int(X /D) + 1
def make_most_min(X, K, D):
    mokuhyou = X
    best = 0
    if X >= 0:
        mokuhyou, best = resolve_best_i(X, K, D)
        if best > K:
            best_posi = abs(X - best * D)
            return best_posi
        if best % 2 == K % 2:
            return abs(X - best * D)
        else:
            return min(abs(X - (best+1) * (D)), abs(X - (best-1) * (D)))
if X >= 0:
    a = make_most_min(abs(X), K, abs(D))
else:
    a = make_most_min(-X, K, abs(D))
print(a)

Submission status: Wrong Answer

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

from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

ans = None
if x > d:
    q, r = divmod(x, d)
    if k <= q:
        ans = x - k * d
    else:
        y = x - (k - q) * d
        if (k - q) % 2 == 0:
            ans = y
        else:
            ans = min(abs(y + d), abs(y - d))
else:
    if k % 2 == 0:
        ans = x
    else:
        ans = x - d

print(ans)

Submission status: Wrong Answer

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

times = abs(X) // D

if times < K:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D*times
        elif current_position < 0:
            current_position = current_position + D*times
    K = K - times
    if K % 2 == 1:
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
else:
    if times > 0 :
        if current_position >= 0:
            current_position = current_position - D
        elif current_position < 0:
            current_position = current_position + D
print(current_position)

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
a = X / D
A = math.floor(a)
if A <= K:
  if A % 2 == 0 and K % 2 == 0:
    if A + 2 <= K:
  	  print(min(abs(X - D * A), abs(X - D * (A + 2))))
    else:
      print(abs(X - D * A))
  if A % 2 == 1 and K % 2 == 0:
    if A + 1 <= K:
      print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 0 and K % 2 == 1:
    if A + 1 <= K:
      print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 1 and K % 2 == 1:
    if A + 2 <= K:
  	  print(min(abs(X - D * A), abs(X - D * (A + 2))))
    else:
      print(abs(X - D * A))
else:
  print(abs(X - D * K))

Submission status: Wrong Answer

Solution code:
X,K,D = [int(a) for a in input().split()]
X0=X
u=max(abs(X),D)
l=-u
now=0
for i in range(K):
    if i==0:
        if X>0 and X-K*D>0:
            print(X-K*D)
            break
        if X<0 and X+K*D<0:
            print(X+K*D)
            break
        if X>0:
            i=X//D
            X=X-i*D
            i-=1
        if X<0:
            i=abs(X)//D
            X=X+i*D
            i-=1
    if X>0:X-=D
    elif X<=0:X+=D
    if X>0 and X<X0:
        u=X
        now=1
    if X<0 and X>-X0:
        l=X
        now=-1
    if X==abs(X0):
        u=X
        if i!=K-1:l=0
        now=1
    if X==-abs(X0):
        l=X
        if i!=K-1:u=0
        now=-1
    #print(X,u,l,i,now)
    if X==0 and i==K-1:
        print("0")
        break
    if (u>0 and u<X0 and l<0 and l>-X0) or (u==0 or l==0):
        if (K-1-i)%2==0:
            if now==1:
                print(u)
            else:
                print(abs(l))
        else:
            if now==1:
                print(abs(l))
            else:
                print(u)
        break
    if i==K-1:
        print(abs(X))
        break

Submission status: Wrong Answer

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

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

if K % 2 == 0 and x == D:
    print(abs(ans-D))
else:
    print(abs(ans))

Submission status: Wrong Answer

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

KOE=K%2

A=abs(X)//D
M=abs(X)%D

# print(A)

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

else :
    if A%2==0:
        print(min(M,abs(M-D)))
    else :
        print(min(abs(M+D),abs(M-D)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


print(abs(x))

Submission status: Wrong Answer

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

res = 0
if x <= (k*d):
    m1 = x % k
    c1 = x // k
    c2 = abs(k - c1)
    if c2%2 == 0:
        res = m1
    else:
        res = abs(m1-k)
else:
    res = abs(x - (k*d))
print(res)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int, input().split())
min_list=[]
for k in range(K+1):
  current=abs(X+(K-2*k)*D)
  print(current)
  min_list.append(current)
print(min(min_list))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
i=0
count=0
if X<0:
	if X+K*D<=0:
		print(abs(X+K*D))
		count+=1

	else:
		s=int(X/D)
		if abs(X+s*D)>=abs(X+(s+1)*D):
			s+=1
			L=abs(X+s*D)
		else:
			L=abs(X+s*D)
else:
	if X-K*D>=0:
		print(abs(X-K*D))
		count+=1

	else:
		s=int(X/D)
		if abs(X-s*D)>=abs(X-(s+1)*D):
			s+=1
			L=abs(X-s*D)
		else:
			L=abs(X-s*D)
            
if count==0:
	kaisuu=K-s
	if kaisuu%2==0:
		print(abs(L))

	else:
		a=abs(L+D)
		b=abs(L-D)
		if a<=b:
			print(a)
		else:
			print(b)

Submission status: Wrong Answer

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

for num in range(K):
    plus = num * D
    minus = -(K - num) * D

    x = X + plus + minus
    X_list.append(x)


for i in range(len(X_list)):
    if X_list[i] < 0:
        X_list[i] = -X_list[i]

X_list.sort()
print(X_list[0])

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))
    exit()
if x < 0:
    x = -x
a = x // d
if a % 2 == k % 2:
    ans = min(x % d, d - x % d)
else:
    ans = max(x % d, d - x % d)
print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
flag = True
if abs(X)-K*D>=X:
  ans = abs(abs(X)-K*D)
  flag = False
else:
  ans = X

res = abs(X)%D
r = abs(X)//D
if flag:
  if r%2 == K%2:
    ans = min(ans, res)
  else:
    ans = min(ans, abs(res-D))
  
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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( X - ( Y - 1 ) * D ), abs( X - ( Y + 1 ) * D ),
                abs( X + ( Y - 1 ) * D ), abs( X + ( Y + 1 ) * D )) )

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
temp=a//c
if(temp>b):
    print(a-(b*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))

Submission status: Wrong Answer

Solution code:
X,K,D = list(map(int,input().split()))
count = K
ans = abs(X)
x = X
if X>0:
    while x>0 and count>0:
        x -= D
        count -= 1
        if abs(x)<ans and count%2==0:
            ans = abs(x)
elif X<0:
    while x<0 and count>0:
        x += D
        count -= 1
        if abs(x)<ans and count%2==0:
            ans = abs(x)

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

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
flag = True
if abs(X)-K*D>X:
  ans = abs(abs(X)-K*D)
  flag = False
else:
  ans = X

res = abs(X)%D
r = abs(X)//D
if flag:
  if r%2 == K%2:
    ans = min(ans, res)
  else:
    ans = min(ans, abs(res-D))
  
print(ans)

Submission status: Wrong Answer

Solution code:

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

ans=X

a=abs(X//D)

"""
for i in range(a):
    ans=min(abs(ans-D),abs(ans+D))
    
"""

if (K>a>0 or -K<a<0) and (K-a)%2==0:
    ans=min(abs(X-(a)*D),abs(X+(a)*D))
    
elif (K>a>0 or -K<a<0) and (K-a)%2==1:
    ans=min(abs(X-(a+1)*D),abs(X+(a+1)*D))
elif a>=K:
    ans=abs(X-K*D)
else:
    ans=abs(X+K*D)
    


print(ans)

Submission status: Wrong Answer

Solution code:


def solve():
	arr=[int(i)for i in input().split()]
	n=arr[0];k=arr[1];d=arr[2]
	diff=n//d
	
	if(diff>k):
		print(n-(d*k))
		return
	else:
		if((k-diff)%2==0):
			print(n-(diff*d))
			
		else:
			print(abs((n-(diff*d))-d))





	


	   
	    
	
	
#test=int(input())
test=1
while test:

	solve()
	test-=1

Submission status: Wrong Answer

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

init = 10**15
count =0
while True:
    if count == k:
        break
    count += 1
    next1 = abs(x)+d
    next2 = abs(x)-d
    next = min(abs(next1),abs(next2))
    if abs(x) <= abs(next):
        break
    else:
        x=next

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
x,k,d = map(int, stdin.readline().rstrip().split())
ret = x
if k%2 == 0:
    if abs(x)<d :
        print(abs(x))
    else:
        if (x // d) <= k:
            if (x // d) % 2 == 0:
                print(int(abs(x - ((x // d) * d))))  #
            else:
                print(int(abs(min((x - ((x // d) - 1) * d), (x - ((x // d) + 1) * d)))))

        else:
            print(int(abs(x-(d*k))))
else:
    if abs(x)<d :
        print(abs(x))
    else:
        if (x // d) <= k:
            if (x // k) % 2 == 0:
                print(int(abs(min((x - ((x // k) - 1) * d), (x - ((x // k) + 1) * d)))))
            else:
                print(int(abs(x- ((x // k) * d))))  #


        else:
            print(int(abs(x-(d*k))))

Submission status: Wrong Answer

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

l = []

for i in range(k):
  a = x - d*i
  l.append(abs(a))
  if a<0:
    break

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

Submission status: Wrong Answer

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

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

    a=abs(X//D)
    b=abs(X//D)+1
    kyori_a=X-a*D
    kyori_b=X-b*D

    if(K-a==0):
        print('b')
        print(abs(kyori_a))
        exit()
    if((K-a)%2==0):
        print('a')
        print(abs(kyori_a))
        exit()
    else:
        print(abs(kyori_b))
        exit()

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

Submission status: Wrong Answer

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

if k%2==0:
  ans=0 #偶数
else:
  ans=1 #奇数

semin=x//d #最小に行くための個数
if semin%2==0:
  minb=0 #最小に行くための個数が偶数
else:
  minb=1 #最小に行くための個数が奇数
humin=semin+1

if semin>=k:
  print(abs(x-k*d))
  exit()
else:
  if ans==0:#ｋが偶数の時
    if minb==0:
      print(abs(x-k*semin))
    else:
      print(abs((x-(d*semin))-d))
  else:#ｋが奇数の時
    if minb==0:
      print(abs((x-(d*semin))-d))
    else:
      print(abs(x-k*semin))

Submission status: Wrong Answer

Solution code:

inp = list(map(int,raw_input().split()))
x = inp[0]
k = inp[1]
d = inp[2]
x = abs(x)
if(x >= k*d):
    print(x-k*d)
else:
    res=x
    tmp = int(x/d)
    for y in range(-100,100):
        if((y+tmp)<=k and y+tmp >=0):
            rem = k-y-tmp
            if(rem%2 == 0):
                res=min(res,abs(x-(y+tmp)*d))
    print(res)