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: Runtime Error

Solution code:

li = list(map(int ,input().split()))

x = abs(li[0])
k = li[1]
d = li[2]

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

if x < k * d:
    x = x % d
    print(x)
    exit()


for _ in range(k):
    sum_result = x + d
    minus_result = x - d
    k_count = k_count - 1
    if abs(sum_result) < abs(minus_result):
        x = sum_result
    elif abs(sum_result) > abs(minus_result):
        x = minus_result
        
    if abs(x) < d and k_count % 2 == 0:
        print(abs(x))
        exit()
    if abs(x) < d and k_count % 2 == 1:
        if abs(x) -d <  abs(x) + d
            print(abs(x) - d)
        else:
            print(abs(x) + d)
        
print(abs(x))

Submission status: Runtime Error

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:
      if x<0:
        print(d-a[1])
      else:
        print(abs(a[1]-d))
    else:
    print(a[1])
  else:
    print(abs(x)-d*k)

Submission status: Runtime Error

Solution code:
def step(x, k):
    for mm in m:
        if mm == x:
            return x

    if k == K:
        return x

    a = step(x-D, k+1)
    b = step(x+D, k+1)
    m.append(min(abs(a),abs(b),abs(x)))
    return min(abs(a),abs(b),abs(x))

    

X,K,D = map(int, input().split())
m = []
print(step(X,0))

Submission status: Runtime Error

Solution code:
from sys import stdin


def f(x, k, d):
    if k == 0:
        return x

    if x == 0:
        if k % 2 == 0:
            return 0
        elif k % 2 == 1:
            return d
    elif x > 0:
        tmp = abs(x) / d
        if d * tmp > d * k:
            return x - d * k
        else:
            return f((x-d*tmp), (k-tmp), d)
    elif x < 0:
        tmp = abs(x) / d
        if d * tmp > d * k:
            return x + d * k
        else:
            return f((x+d*tmp), (k-tmp), d)


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(abs(f(X, K, D)))

Submission status: Runtime Error

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

leafs = []


def build_tree(cur_step, num_steps, step_size, prev):
    down, up = cur_step - step_size, cur_step + step_size
    num_steps -= 1
    if num_steps == 0:
        leafs.append(min(abs(down), abs(up)))
        return
    if prev == "plus":
        build_tree(down, num_steps, step_size, "minus")
    elif prev == "minus":
        build_tree(up, num_steps, step_size, "plus")
    else:
        build_tree(down, num_steps, step_size, "minus")
        build_tree(up, num_steps, step_size, "plus")


build_tree(x, k, d, None)
print(min(leafs))

Submission status: Runtime Error

Solution code:
if K <= 0:
    list_ = [abs((X - K * D) + (2* i * D)) for i in list(range(0,K+1))]
    result = min(list_)
    print(result)
    

else:
    
    it = int(K /2)
    tmp_result = abs((X - K * D) + (2* (it +1) * D))
    
    flag =True
    
    while flag:
        
        if it == 0:
            result = abs((X - K * D) + (2* 0 * D))
            flag = False
            
        elif it == (K+1):
            result = abs((X - K * D) + (2* (K+1) * D))
            flag = False
        
        
        
        result = abs((X - K * D) + (2* it * D)) 
        result_m = abs((X - K * D) + (2* (it -1) * D))
        result_p = abs((X - K * D) + (2* (it +1) * D))
        
        if result <= result_m and result <= result_p:
            flag =False
            
        if result > result_m:
            if it == 4:
                it = 3
            elif it == 3:
                it = 2
            elif it == 2:
                it =1
            elif it ==1:
                it = 0
            else:
                it = int(it/2)
                
        elif result > result_p:
            if (K+1- it) == 4:
                it = K-2
            elif (K+1- it) == 3:
                it = K-1
            elif (K+1- it) == 2:
                it =K
            elif (K+1- it) ==1:
                it = K+1
            else:
                it = it +(int((K+1- it)/2))
    print(result)

Submission status: Runtime Error

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:
      if x<0:
        print(d-a[1])
      else:
        print(abs(a[1]-d))
    else:
    print(a[1])
  else:
    print(abs(x)-d*k)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;

#define int long long int
#define ll long long
#define quickie ios_base::sync_with_stdio(false); cin.tie(NULL);
#define rep(i, a, b) for(int i=a; i<b; i++)
#define rep1(i, a, b) for(int i=a; i<=b; i++)
#define repp(i, a, b) for(int i=b-1; i>=a; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define db double
#define mi map<int, int>
#define vi vector<int>
#define qi queue<int>
#define MI(x) power(x, mod-2)
#define test int t; cin >> t;
#define pii pair<int, int>
#define mod 1000000007
#define pi 3.141592653589
#define fact(n) rep(i, 1, n+1)ft.pb((ft[i-1]*i)%mod) ;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename T>
// using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
// template<typename T>
// using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
//(*****FOR USING ORDERED SET CHANGE INT*******)

int power(int x, int y) ;
int gcd(int a, int b) ;


signed main() {
    quickie
    int n, k, d; 
    cin >> n >> k >> d ;
    n = abs(n) ;
    int tt = n/d ;
    tt = min(k, tt) ;
    k -= tt ;
    n -= tt*d ;
    if(k%2 == 0) {
        cout << n << "\n" ;
    }
    else {
        cout << abs(n-d) << "\n" ;
    }
}

int power(int x, int y) {
    int res = 1; x %= mod;
    while (y > 0) {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x) % mod;
    }
    return res%mod;
}

int gcd(int a,int b){
    if(a==0) return b;
    return gcd(b%a,a);
}

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
[X,K,D]=[int(_) for _ in input().split()]
X/=D
X=abs(X)
k1 = K//2
k2 = K%2
#print(k1,k2)
for _ in [0]*k1:
  X = min([abs(X-2), abs(X), abs(X+2)])
  #print(X)
if k2 == 1:
  X = X-1 if abs(X-1) < abs(X+1) else X+1
  #print(X)
print(abs(int(X*D)))

Submission status: Runtime Error

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

if K == 1:
    print(min(abs(X-D),abs(X+D)))
elif K == 2:
    print(min(abs(X+D+D),abs(X-D-D),abs(X)))
elif K == 3:
    print(min(abs(X+D+D+D),abs(X+D),abs(X-D),abs(X-D-D-D)))
else:
    for i in range(3):
        if abs(X - D) >= abs(X+D):
            List.append(abs(X+D))
            X = X+D
        else:
            List.append(abs(X-D))
            X = X-D

    for i in range(K-3):
        if List[i] == List[i+2]:
            if K%2 == i%2:
                print(List[i+1])
            else:
                print(List[i])
            break
        else:
            if abs(X - D) >= abs(X+D):
                List.append(abs(X+D))
                X = X+D
            else:
                List.append(abs(X-D))
                X = X-D
        print(List[K-1])

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
k = input()
t = time.process_time()
lst = k.split()
for i in range(len(lst)):
    lst[i] = int(lst[i])

print(lst)


w = lst[2]
q = 0
t = lst[0]
r = 0
y = 0
while q != lst[1]:
    if abs(t-w) <= abs(t+w):
        t = t-w
    else:
        t = t+w
    q += 1


print(abs(t))

Submission status: Runtime Error

Solution code:
import itertools
X,K,D = map(int,input().split())
lst = list(map(list, itertools.product([0, 1], repeat=K)))
ans = 10**15
for i in range(2**K):
    x = 0
    n = (lst[i].count(0))*-D
    p = (lst[i].count(1))*D
    x = abs(X + n + p)
    if x < ans:
        ans = x
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import deque


def is_odd(n):
    return n % 2 != 0


def is_even(n):
    return n % 2 == 0


def solve():
    X, K, D = map(int, input().split())
    X = abs(X)

    q = deque([None, None, None])

    if X == 0:
        if is_even(K):
            print(0)
        else:
            print(D)

    q.append(X)
    q.popleft()

    for i in range(K):
        remain = K - i - 1
        X = X - D

        q.append(X)
        q.popleft()

        if abs(q[-1]) > abs(q[-2]):
            if is_odd(remain):
                print(abs(q[-2]))
                break

            if abs(q[-1]) > abs(q[-3]):
                print(abs(q[-3]))
                break

            print(abs(q[-1]))
            break
    else:
        print(abs(q[-1]))


solve()

Submission status: Runtime Error

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:
    if abs(mod) <= abs(amod):
        if (K-q)%2 == 1:
            ans = D - mod
        else:
            ans = mod



print(ans)

Submission status: Runtime Error

Solution code:
X, K, D  = map(int, input().split())
shou = X//D
amari = X%D 

if D * K <= X:
    print(X - (D*K))
    print(error)
else:
    nokori = K - shou
    if nokori % 2 == 0:
        print(amari)
    else:
        print(abs(amari - D))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input()) 
k = int(input())
d = int(input())
n = abs(n)
tt = int(n/d) 
if tt > k:
    p = k*d 
    print(n-p) 
else:
    k = k - tt 
    n = n - tt*d 
    if k%2 == 0:
        print(n)
    else: 
        print(abs(n-d))

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
c=0
for i in range(k):
  while x>0 and i<k:
    x-=d
    i+=1
  while x<0 and i<l:
    x+=d
    i+=1
print(abs(x))

Submission status: Runtime Error

Solution code:
# input here
_INPUT = """\
7 4 3
"""

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

    A = (0,1)
    min = float('inf')
    for i in itertools.product(A, repeat=K):
        location = X
        for j in range(len(i)):
            if i[j] == 0:
                location = location - D
            if i[j] == 1:
                location = location + D
        
        if abs(min) > abs(location):
            min = location
            # print(min)
    
    print(abs(min))
    


if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    # sys.stdin = io.StringIO(_INPUT)
    main()

Submission status: Runtime Error

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 l[-1] = l[-2]:
  print(l[-1])
elif l[-1] < l[-2]:
  if (k-(x-l[-1])/d)%2 = 0:
    print(l[-1])
  else:
    print(l[-2])

Submission status: Runtime Error

Solution code:

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

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

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

Submission status: Runtime Error

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 K<=a:
        print(a-K)
    elif a<=K:
        pront(K-a)

Submission status: Runtime Error

Solution code:
X, K, D  = map(int, input().split())
shou = X//D
amari = X%D 

if D * K <= X:
    print(X - (D*K))
else:
    nokori = K - shou
    if nokori % 2 == 0:
        print(amari)
    else:
        print(abs(amari - D))
        print(error)

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
c=0
for i in range(k):
  while x>0 and i<k:
    x-=d
    i+=1
  while x<0i and i<k:
    x+=d
    i+=1
print(abs(x))

Submission status: Runtime Error

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

if d / x >= 2:
  if k % 2 == 0:
    min_x = x
  else:
    min_x = abs(x - d)

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

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
c=0
for i in range(k):
  while x>0 and i<k:
    x-=d
    i+=1
  while x<0i and i<l:
    x+=d
    i+=1
print(abs(x))

Submission status: Runtime Error

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

x, k, d = map(int, input().split())
p_lst = list(range(0, k*d + 1, d))
n_lst = list(map(lambda x: -x, p_lst[1:]))
potential_pos = n_lst + p_lst
potential = [x + y for y in potential_pos]
potential.sort()
print(min(map(lambda x: max(x, -x), potential[0::2])))

Submission status: Runtime Error

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

if K == 1:
    print(min(abs(X-D),abs(X+D)))
elif K == 2:
    print(min(abs(X+D+D),abs(X-D-D),abs(X)))
else:
    for i in range(3):
        if abs(X - D) >= abs(X+D):
            List.append(abs(X+D))
            X = X+D
        else:
            List.append(abs(X-D))
            X = X-D

    for i in range(K-3):
        if List[i] == List[i+2]:
            break
            if K%2 == i%2:
                print(List[i])
            else:
                print(List[i+1])
        else:
            if abs(X - D) >= abs(X+D):
                List.append(abs(X+D))
                X = X+D
            else:
                List.append(abs(X-D))
                X = X-D
        print(List[K-1])

Submission status: Runtime Error

Solution code:
import sys

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

p = X
_p = 0
k = 0

if abs(p) > K*D:
    print(abs(p)-K*D)
    sys.exit()

if p < 0:
    for i range(K):
        _p = p
        p += D
        k += 1
        if p > 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
elif p > 0:
    for i in range(K):
        _p = p
        p -= D
        k += 1
        if p < 0:
            break
    if (K-k) % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Runtime Error

Solution code:
import itertools

X,K,D = map(int,input().split())
L = [D,-D]
i = 0
for comb in itertools.combinations_with_replacement(L, K):
  x = X
  for d in comb:
    x = x + d
    if(i == 0):
      x_min = x
    if(abs(x) < abs(x_min)):
      x_min = x
  i = +1
print(abs(x_min))

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
if x>=0:
    dummy
    br=False
    for i in range(int(k/2)):
        print(i,dummy)
        if dummy>=0 and dummy-2*d<0:
            br=True
            print(min(dummy,2*d-dummy))
            break
        dummy=dummy-2*d
    if br==False:
        print(dummy-k*d)
else:
    dummy=x
    br=False
    for i in range(int(k/2)):
        if dummy<=0 and dummy+2*d>0:
            br=True
            print(min(-dummy,dummy+2*d))
            break
        dummy=dummy+2*d
    if br==False:
        print(dummy+k*d)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int main()
{
  ll x, k, d, ans;
  
  cin >> x >> k >> d;
  
  ll count = x / d;
  ll b = x % d;
  ll sub_k = k - count;
  
  if (k > count) {
  	if (sub_k % 2 == 0) {
      ans = x % d;
    } else {
      ans = abs(d) - abs(x % d);
    }
  } else {
  	ans = abs(x) - (k * d);
  }
  
  cout << abs(ans) << endl;
}

Submission status: Runtime Error

Solution code:
import sys
import math
X,K,D = map(int, input().split()) 
X1=abs(X)
syou=math.floor(X1/D)
amari=math.floor(K/syou)
ans=0
if amari==0:
    ans=X1-D*K
else:
    ya=math.floor(K/amari)
    ans=X1-ya*D
    if ya%2==0:
        print(ans)
        sys.exit()
    if abs(ans+D)>abs(ans-D):
        ans=ans-D
    else:
        ans=ans+D
print(abs(ans))

Submission status: Runtime Error

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

leafs = []


def build_tree(cur_step, num_steps, step_size):
    left, right = cur_step - step_size, cur_step + step_size
    num_steps -= 1
    if num_steps == 0:
        leafs.append(min(abs(left), abs(right)))
        return
    build_tree(left, num_steps, step_size)
    build_tree(right, num_steps, step_size)


build_tree(x, k, d)
print(min(leafs))

Submission status: Runtime Error

Solution code:
X,K,D = [int(input()) for i in range(3)]
print(2)

Submission status: Runtime Error

Solution code:
import itertools

X,K,D = map(int,input().split())
L = [D,-D]

for comb in itertools.combinations_with_replacement(L, K):
  x = X
  for d in comb:
    x = x + d
    if(i == 0):
      x_min = x
    if(abs(x) < abs(x_min)):
      x_min = x

print(abs(x_min))

Submission status: Runtime Error

Solution code:
import sys
import math
X,K,D = map(int, input().split()) 
X1=abs(X)
syou=math.floor(X/D)
amari=math.floor(K/syou)
ans=0
if amari==0:
    ans=X-D*K
else:
    ya=math.floor(K/amari)
    ans=X-ya*D
    if ya%2==0:
        print(ans)
        sys.exit()
    if abs(ans+D)>abs(ans-D):
        ans=ans-D
    else:
        ans=ans+D
print(abs(ans))

Submission status: Runtime Error

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

X = abs(X)

if X == D :
  print (X)
elif X < D :
  pirnt( D- X)
elif X > K * D:
  print (X - K * D)
elif X // D < K:
  print (X % D)
else:
  print(X)

Submission status: Runtime Error

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

min_pos = x % d
min_neg = min_pos - d

num_to_min_pos = x // d
if k <= num_to_min_pos:
    print(x - d * k)
    
else:
    if (k - num_to_min_pos) % 2 == 0:
        print(min_pos)
    else:
        print(abs(min_neg)

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
tmp2 = []
tmp2.append(x)
tmp = x
ans = 0
frag_break = 0
frag = int(x / d)
if k < frag and frag % 2 == k % 2:
    if x < 0:
        ans = x + k * d
        frag_break = 1
    else:
        ans = x - k * d
        frag_break = 1
else:
    if x < 0:
        tmp = x + frag * d
        tmp2.append(tmp)
    else:
        tmp = x - frag * d
        tmp2.append(tmp)
    if tmp < 0:
        tmp += d
        tmp2.append(tmp)
    else:
        tmp -= d
        tmp2.append(tmp)
for i in range(3, k + 1):
    if tmp < 0:
        tmp += d
        tmp2.append(tmp)
    else:
        tmp -= d
        tmp2.append(tmp)
    if tmp2[i - 2] == tmp2[i]:
        if i % 2 == k % 2:
            ans = tmp2[i]
        else:
            ans = tmp2[i - 1]
        frag_break = 1
        break
if frag_break == 0:
    ans = tmp
print(abs(int(ans)))

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
xa=abs(x)
pm=x//xa
kz=xa//d
xz=xa-kz*d
if xz*2>d:
  xzz=d-xz
  kz+=1
else:
  xzz=xz
rest=k-kz
if rest%2==0:
  print(xzz)
else:
  print(d-xzz)

Submission status: Runtime Error

Solution code:
def find_children(cur, step):
    return cur - step, cur + step


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

leafs = []


def build_tree(cur_step, num_steps, step_size):
    left, right = find_children(cur_step, step_size)
    num_steps -= 1
    if num_steps == 0:
        leafs.append((left, right))
        return
    build_tree(left, num_steps, step_size)
    build_tree(right, num_steps, step_size)


build_tree(x, k, d)
min_leaf = min([min(abs(e[0]), abs(e[1])) for e in leafs])
print(min_leaf)

Submission status: Runtime Error

Solution code:
x,k,d = [int(x) for x in input().split()]
t = abs(int(x/d))
if x < 0:
    d = -d
if t >= k:
    print(x-d*k)
elif t == 0:
    if k % 2 == 0:
        print(abs(x))
    else:
        print(min(abs(x-d),abs(x+d)))
elif t < k:
    if abs(x-d*t) < abs(x-d*t-d):
        if (t-k)%2==0:
            print(abs(x-d*t))
        else:
            print(min(abs(x-d*t+d),abs(x-d*t-d)))
    elif abs(x-d*t) = abs(x-d*t-d):
        print(abs(x-d*t))
    else:
        if (t-k-1)%2==0:
            print(abs(x-d*t-d))
        else:
            print(min(abs(x-d*t),abs(x-d*t-2*d)))

Submission status: Runtime Error

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

X = abs(X)

if X == D :
  print (X)
elif X < D :
  pirnt( X - D)
elif X > K * D:
  print (X - K * D)
elif X // D < K:
  print (X % D)
else:
  print(X)

Submission status: Runtime Error

Solution code:
x,k,d = [int(x) for x in input().split()]
t = abs(int(x/d))
if x < 0:
    d = -d
if t >= k:
    print(x-d*k)
elif t == 0:
    if k % 2 == 0:
        print(abs(x))
    else:
        print(min(abs(x-d),abs(x+d)))
elif t < k:
    if abs(x-d*t) < abs(x-d*t-d):
        if (t-k)%2==0:
            print(abs(x-d*t))
        else:
            print(min(abs(x-d*t+d),abs(x-d*t-d)))
    elif  abs(x-d*t) = abs(x-d*t-d):
        print(abs(x-d*t))
    else:
        if (t-k-1)%2==0:
            print(abs(x-d*t-d))
        else:
            print(min(abs(x-d*t),abs(x-d*t-2*d)))

Submission status: Runtime Error

Solution code:
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    l = 0
    r = k+1
    tmp_res = 0
    while(l+1!=r):
        mid = (l+r)//2
        tmp_res = copy.deepcopy(res)
        if((k-mid)%2==1):
         	res = min(res, abs(abs(x)-(mid+1)*d))
            res = min(res, abs(abs(x)-(mid-1)*d))
            mid+=1
        if((k-mid)%2==0):
            res = min(res, abs(abs(x)-mid*d))
            res = abs(res)
        if(abs(abs(x)-mid*d) > x):
            r = mid
        else:
            l = mid
        if tmp_res == res:
            break

    print(res)




if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    l = 0
    r = k+1
    tmp_res = 0
    while(l+1!=r):
        mid = (l+r)//2
        tmp_res = copy.deepcopy(res)
        if((k-mid)%2==1):
         	res = min(res, abs(abs(x)-(mid+1)*d))
            res = min(res, abs(abs(x)-(mid-1)*d))
            mid+=1
        if((k-mid)%2==0):
            res = min(res, abs(abs(x)-mid*d))
            res = abs(res)
        if(abs(abs(x)-mid*d) > x):
            r = mid
        else:
            l = mid
        if tmp_res == res:
            break

    print(res)




if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

    for i in range(k + 1):
        if abs(x - k * i) < d:
            check = k - i
            anslist = [abs(x - k * i), abs(d - x + k * i)]
            continue
    if check % 2 == 0:
        print(anslist[0])
    else:
        print(anslist[1])

Submission status: Runtime Error

Solution code:
x,k,d = map(int,input().split())
cnt = x//d
mod = x%d
min = 0

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

if mod > abs(mod-d):
    min = abs(mod-d)
    cnt += 1
else:
    min = mod

if k%2 == 1:
    if cnt%2 == 1:
        print(min)
    else:
        print(min(abs(min-d),min+d))
else:
    if cnt%2 == 0:
        print(min)
    else:
        print(min(abs(min-d),min+d))

Submission status: Runtime Error

Solution code:
from sys import stdin
from functools import lru_cache


@lru_cache(maxsize=100)
def f(x, k, d):
    if k == 0:
        return x

    if x == 0:
        if k % 2 == 0:
            return 0
        if k % 2 == 1:
            return d
    if x > 0:
        return f(x-d, k-1, d)
    if x < 0:
        return f(x+d, k-1, d)


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(abs(f(X, K, D)))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

def move(x,k,d):
    global kouho

    if len(kouho) != 0:
        if x > 0 and min(kouho) < (x-k*d)*(x-k*d):
            return 0
        elif x < 0 and min(kouho) < (x+k*d)*(x+k*d):
            return 0
    
    if k == 0:
        kouho.append(x*x)
        return 0
    elif k >= 1:
        move(x+d,k-1,d)

        move(x-d,k-1,d)
    



kouho = []
dt = input()
l = dt.split()
dt1 = [0]*3
for i in range(3):
    dt1[i] = int(l[i])

move(dt1[0],dt1[1],dt1[2])
print(round(math.sqrt(min(kouho))))

Submission status: Runtime Error

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()
	flg = True
	if X < 0:
		flg = False

	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 - K*D > 0:
		ans = X - K*D
		print(ans)
		sys.exit()
	else:
		tmp = X // D
		nokori = K - tmp
		if nokori % 2 == 0:
			if flg = True:
				ans = X % D
				print(ans)
				sys.exit()
			else:
				o000 = X % D
				assd = abs(D - abs(D-o000))
				print(assd)
				sys.exit()
		else:
			if flg = True:
				o000 = X % D
				assd = abs(D - abs(D-o000))
				print(assd)
				sys.exit()
			else:
				ans = X % D
				print(ans)
				sys.exit()


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

Submission status: Runtime Error

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

X = abs(X)

if X == D :
  print (X)
elif X <D :
  pirnt(X-D)
elif X > K * D:
  print (X - K * D)
elif X // D < K:
  print (X % D)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

ans_list = []

if X+K*D <= 0:
    ans = abs(X+K*D)
else:
    a = (X+K*D)//(2*D)
    for i in range(-3,4):
        if a+i <= K and a+i >= 0:
            ans_list.append(abs(X+(K-(a+i))*D - (a+i)*D))
    ans = min(ans_list)
print(ans)

Submission status: Runtime Error

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

if k * d <= x:
    print(x - k * d)
    os.exit()

r = x % d
cnt = (x - r) / d
k -= cnt
if k % 2 == 0:
    print(r)
else:
    print(math.abs(r - d))

Submission status: Runtime Error

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

if x>= 0:
  if x > d*k:
    m = x - d*k
  else:
    for i in range(k):
      x -= d
      if x <=0:
        if (i+k) % 2 != 0:
          m = (-1)*x
        else:
          m = x+d
        break

else:
  if x > (-1)*d*k:
    m = x + d*k
  else:
    for i in range(k):
      x += d
      if x >=0:
        if (i+k) % 2 != 0:
          m = x
        else:
          m = (-1)*(x-d)
        break
print(m)

Submission status: Runtime Error

Solution code:
import itertools

X,K,D = map(int,input().split())
L = [D,-D]
comb = list(itertools.permutations(L, K))
#print(comb)


for i in range(len(comb)):
  x = X
  for d in comb[i]:
    x = x + d
    if(i == 0):
      x_min = x
    if(abs(x) < abs(x_min)):
      x_min = x

print(abs(x_min))