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:
x,k,d = map(int,input().split())
ans = 0
cnt = 0
if abs(x)//d <= k:
  cnt = abs(x)//d
  ans = min(abs(x-cnt*d),abs(x+cnt*d))
  if abs(ans)==min(abs(ans),abs(ans+d),abs(ans-d)):
    ans = min(abs(ans+(k-cnt)%2*d),abs(ans-(k-cnt%2)*d))
    print(ans)
  else:
    ans = min(abs(x-d),abs(x+d))
    cnt += 1
    ans = min(abs(ans+(k-cnt)%2*d),abs(ans-(k-cnt%2)*d))
    print(ans)
if abs(x)//d >= k:
    print(min(abs(x-(x//d)*k)),abs(x+(x//d)*k))

Submission status: Runtime Error

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:
                if times_in+2 <= K:
                    ans = min(abs(X - (times_in * D)),abs(X - ((times_in + 2) * D)))
                else:
                    ans = abs(X - (times_in * 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:
                if times_in+2 <= K
                    ans = min(abs(X - (times_in * D)), abs(X - ((times_in + 2) * D)))
                else:
                    ans = abs(X - (times_in * D))
    else:
        ans = X - (K * D)

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

if X==0:
    if K%2==0:
        ans = 0
    else:
        ans = D

print(ans)

Submission status: Runtime Error

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

if x >= k*d:
    print(x-(k*d))
else:
    if x <= -(k*d):
        print(x+(k*d))
    else:
        if x <= 0:
            lxl = -x
        else:
            lxl = x
        if x <= 0:
            now_x = x + (d * (int(lxl / d) + 1))]
            left_x = -now_x
            right_x = now_x + d
        else:
            now_x = x - (d * (int(lxl / d) + 1))
            left_x = -(now_x - d)
            right_x = now_x
        now_k = k - int(lxl / d) - 1
        if now_x <= 0:
            if now_k % 2 == 0:
                print(left_x)
            else:
                print(right_x)
        else:
            if now_k % 2 == 0:
                print(right_x)
            else:
                print(left_x)

Submission status: Runtime Error

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

m=[]

for i in range(1,k+1):
    n=abs(d*k+x-(2*i*d))
    m.append(n)

printmin(m)

Submission status: Runtime Error

Solution code:
def walking():
    x, k, d = map(int, input().split())
    r = 0
    if x > 0 and x > k * d:
        return x - k * d
    elif x < 0 and x < -k * d:
        return x + k * d
    elif x > 0 and x < k * d:
        r = x//d
        if (k - r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d
    elif x < 0 and x < -1 * k * d:
        r = x//d
        if (k + r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d        
    
loc = walking()
print(abs(loc))

Submission status: Runtime Error

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

a = round(x / d)

b = k % a

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

Submission status: Runtime Error

Solution code:
c_place, N, distance = [ int(v) for v in input().split(" ") ]

max_distance = distance * N
candidate=[]


#print(n, n+1)
#print(candidate)

if c_place > max_distance:
  print(c_place - max_distance)
else:
  n = c_place // distance
  
  if (N - (n+1)) % 2 == 1:
    print(abs(c_place - distance*n))
  elif N % (n+1) == 0:
    print(abs(c_place - distance*(n+1)))
  else:
    print(min(abs(c_place - distance*n), abs(c_place - distance*(n+1))))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def dfs(i, ans):
  global ans_min
  if abs(ans_min) > abs(ans) and ans != 0:
    ans_min = ans
  if i == k:
    return abs(ans_min)
  dfs(i + 1, ans + d)
  dfs(i + 1, ans - d)

dfs(0, x)
print(ans_min)

Submission status: Runtime Error

Solution code:
def walking():
    x, k, d = map(int, input().split())
    r = 0
    if x > 0 and x > k * d:
        return x - k * d
    elif x < 0 and x < -k * d:
        return x + k * d
    elif x > 0 and x < k * d:
        r = x//d
        if (k - r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d
    elif x < 0 and x < -k * d:
        r = x//d
        if (k + r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d        
    
loc = walking()
print(abs(loc))

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:
    if D>0:
        a = int((X+K*D)/(2*D))
        for i in range(-2,3):
            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())
a_list = []
if K <= 1000000:
  for i in range(K+1):
    if X >= 0:
      a_list.append(abs(X - i*D))
    else:
      a_list.append(abs(X + i*D))
  if K % 2 == 0:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
else:
  q = X // K
  for j in range(-100000+q, 100000+q):
    if j > K:
      break
    elif q >= 0:
      a_list.append(abs(X - j*D))
    else:
      a_list.append(abs(X + j*D))
  if K % 2:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
print(ans)

Submission status: Runtime Error

Solution code:
# input here
_INPUT = """\
1000000000000000 1000000000000000 1000000000000000
"""

def main():
    # print(sys.maxsize)
    X, K, D = map(int, input().split())
    if X == 1000000000000000:
        print(1000000000000000)
    else:

        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:
#python
X=0
K=0
D=0
X = input()
K = input()
D = input()
num = X-K*D
if (num>=0):
    print(num)
    exit()

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

if(K%2 == 1):
    print(((X%D)-D))
    exit()

Submission status: Runtime Error

Solution code:
def walking():
    x, k, d = map(int, input().split())
    r = 0
    if x > 0 and x > k * d:
        return x - k * d
    elif x < 0 and x < -k * d:
        return x + k * d
    elif x > 0 and x < k * d:
        r = x//d
        if (k - r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d
    elif x < 0 and x < -k * d:
        r = x//d
        if (k - r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d        
    
loc = walking()
print(abs(loc))

Submission status: Runtime Error

Solution code:
XKD=list(input().split(' '))
X=int(XKD[0])
K=int(XKD[1])
D=int(XKD[2])
AX=abs(X)

if AX>K*D:
    print(AX-K*D)
else:
    K2=AX//D
    if (K-K2)%2==0:
        P=AX-K2*D
    else:
        P=abs(AX-(K2+1)*D)
print(P)

Submission status: Runtime Error

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


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

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

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

    Kh = K // 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: Runtime Error

Solution code:
N=int(input())
A = list(map(int, input().split()))
for i in range(N):
    B[i]==(A[i])*(A[i])

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

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
x_max = 10**15+1 
X_min = 10**15+1
while x > 0 and k > 0:
  x -= d
  k -= 1
  x_min = x
while x < 0 and k > 0:
  x += d
  k -= 1
  x_max = x
if x == 0:
  print(abs(x))
elif k % 2 == 0:
  if abs(x_max) > abs(x_min):
    print(abs(x_min))
  else:
    print(abs(x_max))
else:
  if abs(x_max) > abs(x_min):
    print(abs(x_max))
  else:
    print(abs(x_min))

Submission status: Runtime Error

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

def dfs(i, ans):
  global ans_min
  if abs(ans_min) > abs(ans):
    ans_min = ans
  if i == k:
    return abs(ans_min)
  dfs(i + 1, ans + d)
  dfs(i + 1, ans - d)

dfs(0, x)
print(ans_min)

Submission status: Runtime Error

Solution code:
arr=list(map(int,input().rstrip().split()))
X=arr[0]
K=arr[1]
D=arr[2]

ab=X/D

if ab.is_integer():
    if ab<=K:
        ab=int(ab)
    else:
        ab=K
        
else:
    if ab<=K:
        ab=int(ab)+1
    else:
        ab=K
X=X-ab*D

cc=X
cc1=X
cc11=X

K=K-ab
K1=K
K11=K


if K>1:
    asa=K//2
    cc=cc+(asa)*(D)
    K=K-asa
    cc=cc-K*D
    aw=abs(cc)
else:
    cc=cc+K*D
    aw=abs(cc)

if K1>1:
    
    asa1=K1//2
    cc1=cc1-(asa1)*(D)
    K1=K1-asa1
    cc1=cc1+K1*D
    aw1=abs(cc1)
else:
    cc1=cc1-K1*D
    aw1=abs(cc1)

if K11>1:
    
    asa11=K11//2
    cc11=cc11-(asa11)*(D)
    K11=K11-asa11
    cc11=cc11+K11*D
    aw11=abs(cc11)
else:
    cc11=cc11+K11*D
    aw11=abs(cc11)
    



AAAA=min(aw1,aw)
BBBB=min(aw11,aw111)
print(min(AAAA,aw11))

Submission status: Runtime Error

Solution code:
import numpy as np

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

X = kk[0]
K = kk[1]
D = kk[2]

a = list(range(X-K*D, X+K*D, 2*D))
aa = np.abs(a)

print(min(aa))

Submission status: Runtime Error

Solution code:
X,K,D=list(map(int,input().split()))
memo=[]
def dfs(i,X):
    if i==K:
        memo.append(abs(X))
        return False
    
    if dfs(i+1,X+D):
        return True

    if dfs(i+1,X-D):
        return True

def solve():
    while dfs(0,X)!=False:
        print(min(memo))
        break

if __name__=='__main__':
    solve()

Submission status: Runtime Error

Solution code:
def walking():
    x, k, d = map(int, input().split())
    r = 0
    if x > 0 and x > k * d:
        return x - k * d
    elif x < 0 and x < -k * d:
        return x + k * d
    elif x > 0 and x < k * d:
        r = x//d
        if (k - r)%2 == 0:
            return x - r * d
        else:
            return x - (r + 1 ) * d
    elif x < 0 and x < -k * d:
        r = abs(x//d)
        if (k - r)%2 == 0:
            return x + r *d
        else:
            return x + (r + 1 ) * d        
    
loc = walking()
print(abs(loc))

Submission status: Runtime Error

Solution code:
arr=list(map(int,input().rstrip().split()))
X=arr[0]
K=arr[1]
D=arr[2]

ab=X/D

if ab.is_integer():
    if ab<=K:
        ab=int(ab)
    else:
        ab=K
        
else:
    if ab<=K:
        ab=int(ab)+1
    else:
        ab=K
X=X-ab*D

cc=X
cc1=X
cc11=X
cc111=X
K=K-ab
K1=K
K11=K
K111=K

if K>1:
    asa=K//2
    cc=cc+(asa)*(D)
    K=K-asa
    cc=cc-K*D
    aw=abs(cc)
else:
    cc=cc+K*D
    aw=abs(cc)

if K1>1:
    
    asa1=K1//2
    cc1=cc1-(asa1)*(D)
    K1=K1-asa1
    cc1=cc1+K1*D
    aw1=abs(cc1)
else:
    cc1=cc1-K1*D
    aw1=abs(cc1)

if K11>1:
    
    asa11=K11//2
    cc11=cc11-(asa11)*(D)
    K11=K11-asa11
    cc11=cc11+K11*D
    aw11=abs(cc11)
else:
    cc11=cc11+K11*D
    aw11=abs(cc11)
    



AAAA=min(aw1,aw)
BBBB=min(aw11,aw111)
print(min(AAAA,aw11))

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 = int((X+K*D)/(2*D))
    for i in range(-2,3):
        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:
k = input()
t = time.process_time()
lst = k.split()
for i in range(len(lst)):
    lst[i] = int(lst[i])


i = 2
while i * i <= lst[1] :
    if lst[1] % i:
        i += 1
    else:
        lst[1] //= i



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
if t == 0:
    t=t+w

print(abs(t))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(3000)
def dfs(x,k,d):
  if k==0:
    return abs(x)
  return min(dfs(x-d,k-1,d),dfs(x+d,k-1,d))
def find(x,k,d):
  if x==k and k==d:
    return x
  else:
    return dfs(x,k,d)

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

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(-2,3):
        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:
def step(x, k):
    if k == K:
        return x

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


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

Submission status: Runtime Error

Solution code:
def solve(i, x, m):
	if(i >= n+1): 
		return m
	o1 = solve(i+1, x, x+m)
	o2 = solve(i+1, x, x-m)
	return min(abs(o1), abs(o2))

s = list(map(int, input().split()))
x = s[0]
n = s[1]
m = s[-1]

print(solve(0, x, m))

Submission status: Runtime Error

Solution code:
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    private static StringTokenizer st;

    private static long readLong() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }

    public static void main(String[] args) throws IOException {
        pw.println(solve());
        pw.close();
    }

    private static long solve() throws IOException {
        long OrigX = readLong();
        long K = readLong();
        long D = readLong();

        if (OrigX == 0) return K * (D % 2);

        return solve(Math.abs(OrigX), K, D);
    }

    private static long solve(long X, long K, long D) {
        if (X / D >= K) return (X - K * D);
            // Now X < D * K

        else {
            K -= X/D;
            X -= X / D * D;

            if ( K % 2 == 0) {
                return X;
            } else {
                return D - X;
            }
        }
    }
}

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(30000)
def dfs(x,k,d):
  if k==0:
    return abs(x)
  return min(dfs(x-d,k-1,d),dfs(x+d,k-1,d))
def find(x,k,d):
  if x==k and k==d:
    return x
  else:
    dfs(x,k,d)

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

Submission status: Runtime Error

Solution code:
import itertools

X, K, D = map(int, input().split())
term = [D, -D]
num = itertools.combinations_with_replacement(term, K)
result = []
for v in num:
    pre_result = abs(sum(v) + X)
    result.append(pre_result)
print(min(result))

Submission status: Runtime Error

Solution code:
import sys
import itertools

if __name__ == '__main__':
    # 初期位置、移動回数、一回の移動量
    X, K, D = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split()))
    origX = X
    # oldX = X
    # lasti = 0
    # 7-3=4
    # 4-3=1
    # 1-3=-2

    # for i in range(K):
    #     X -= D
    #     if abs(X) > abs(oldX):
    #         X = oldX
    #         lasti = i
    #         break
    #     oldX = X
    # else:
    #     print(abs(X))
    #     exit()

    # 6 3 2
    # 9 3 4
    lasti = X // D
    X = X - D*lasti

    if lasti >= K:
        print(abs(origX-D*K))
        exit()

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

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
a_list = []
if K <= 1000000:
  for i in range(K+1):
    if X >= 0:
      a_list.append(abs(X - i*D))
    else:
      a_list.append(abs(X + i*d))
  if K % 2 == 0:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
else:
  q = X // K
  for j in range(-100000+q, 100000+q):
    if q >= 0:
      a_list.append(abs(X - j*D))
    else:
      (abs(X - j*D))
  if K % 2:
    check_list = a_list[::2]
  else:
    check_list = a_list[1::2]
  ans = min(check_list)
print(ans)

Submission status: Runtime Error

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

abs_num = abs(X)
counter = 0
d_p = D

while (X > d_p):
  d_p += D
  counter += 1

if (counter <= K):
  point = d_p % X
  print(min(abs(point-D), point))
else:
  d_p = D * counter
  print(X - d_p)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:

x, k, d = (int(x) for x in input().split())

ans = x
ans_is_k = False

if 0 < x :

    # 理想の数字を追求
    if abs(x - (x // d * d)) < abs(x - (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x - (x // d * d))
        s_ans = abs(x - (((x // d) + 1) * d))
        ans_is_k == True
    else :
        # print('2採用')
        i_ans = abs(x - (((x // d) + 1) * d))
        s_ans = abs(x - (x // d * d))

    # print("回数制限なしの理想の答え :  {0}".format(i_ans))
    # print("回数制限なしの次点の答え :  {0}".format(s_ans))

    if k <= (x // d) :
        ans = x - (k * d)
    else : 
        if k % 2 == 0 :

            if x // d == 0 :
                ans = i_ans
            else :
                ans = s_ans
        else :
            if x // d == 0:
                ans = s_ans
            else :
                ans == i_ans
else :
    if abs(x + (x // d * d)) < abs(x + (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x + (x // d * d))
        s_ans = abs(x + (((x // d) + 1) * d))
    else :
        # print('2採用')
        i_ans = abs(x + (((x // d) + 1) * d))
        s_ans = abs(x + (x // d * d))
        ans_is_Plass == True

    if k <= (x // d) :
        ans = x + (k * d)
    else : 
        if k % 2 == 0 :

            if x // d == 0 :
                ans = i_ans
            else :
                ans = s_ans
        else :
            if x // d == 0:
                ans = s_ans
            else :
                ans == i_ans

print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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 abs(current_position) <= D:
    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*times
        elif current_position < 0:
            current_position = current_position + D*times
print(current_position)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n, k, l = map(int, input().split())
for _ in range(k):
  print(abs(n-l))

Submission status: Runtime Error

Solution code:
x, k, d = map(int,input().split(' '))
flg = True
hituyou_kaisuu = x//d
saisyou = x%d


if saisyou > d / 2:
    saisyou -= d
    hituyoukaisuu += 1
    flg = False

amari = k - hituyou_kaisuu

if amari >= 0:
    if amari % 2 == 0:
        print(abs(saisyou))
    else:
        if flg:
            print(abs(saisyou - d))
        else:
            print(abs(saisyou + d))
else:
    print(abs(amari * d))

Submission status: Runtime Error

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

elif X < 0:
   if 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: Runtime Error

Solution code:
x,k,d=list(map(int,input.split()))
syou, amari = divmod(x,d)
if x>0:
    if x-k*d==0:
        print(0)
    elif x-k*d<0:
        if amari==0:
            newk=k-syou
            if newk%2==0:
                print(0)
            else:
                print(d)
        elif amari!=0:
            newk=k-syou+1
            if newk%2==0:
                print(abs(x-d*(syou+1)))
            else:
                print(abs(x-d*syou))
    elif x-k*d>0:
        print(abs(x-k*d))
if x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

Submission status: Runtime Error

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

if  X == 0 and K >=1 and K%2!=0:
    print(D)
    exit()


while K>=100:
    if X-(D*50)>=D:
        x-=D*50
        K-=50
    else:
        break


for i in range(K):
    if X<=D:
        break
    if X-(K*D)>=D:
        X -=(K*D)
        break

   
    else:
        X-=D

_K = K-i
if X<=D and  _K>=2 and _K%2==0:
    print(X)
elif X<=D and _K>=3 and _K%2!=0:
    print(X)
elif X<=D and _K ==1:
    print(abs(X-D))
else:
    print(X)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

m = X % D
l = X // D

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

Submission status: Runtime Error

Solution code:
c_place, N, distance = [ int(v) for v in input().split(" ") ]

max_distance = distance * N
candidate=[]

#for n in range(1, N):
#  candidate.append(abs(c_place - distance*n))

n = c_place // distance
candidate.append(abs(c_place - distance*n))
candidate.append(abs(c_place - distance*(n+1)))

#print(n, n+1)
#print(candidate)

if c_place > max_distance:
  print(c_place - max_distance)
else:
  if N % (n+1) == 0:
    print(candidate[1])
  else:
    print(min(candidate))

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
x_max = 10**15+1 
while x > 0 and k > 0:
  x -= d
  k -= 1
  x_min = x
while x < 0 and k > 0:
  x += d
  k -= 1
  x_max = x
if x == 0:
  print(abs(x))
elif abs(x_max) > abs(x_min):
  print(abs(x_min))
else:
  print(abs(x_max))

Submission status: Runtime Error

Solution code:
list = list(map(int, input().split())) 
hajime=list[0]
kaisuu=list[1]
hohaba=list[2]
if hajime>=0:
  ayumi=hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime-hohaba*kaisuu
  kouho_2=hajime-hohaba*ayumi
  kouho_3=-hajime+hohaba*(ayumi+1)
  if kaisuu=<ayumi:
    print(kouho_1)
  else:
    if nokori%2==0:
      print(kouho_2)
    else:
      print(kouho_3)
      
if hajime<0:  
  ayumi=-hajime//hohaba
  nokori=kaisuu-ayumi
  kouho_1=hajime+hohaba*kaisuu
  kouho_2=hajime+hohaba*ayumi
  kouho_3=-hajime-hohaba*(ayumi+1)
  if kaisuu=<hohaba:
    
    print(kouho_1)  else:
    if nokori%2==0:
      print(kouho_2)
    else:
      print(-kouho_3)

Submission status: Runtime Error

Solution code:
n, k, l = map(int, input().split())
for _ in range(0, k+1):
    print(min(abs(n-k))

Submission status: Runtime Error

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

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

m = X % D
l = X // D

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
x,k,d=map(int,input().split())
m=abs(x)
if x<0:
  for i in range(k):
    x+=d
    if abs(x)<m:
      m=abs(x)
    else:
      n=k-i
      break
  if n%2==0:
    print(m)
  else:
    print(abs(x))
else:
  for i in range(k):
    x-=d
    if abs(x)<m:
      m=abs(x)
    else:
      n=k-i
      break
  if n%2==0:
    print(m)
  else:
    print(abs(x))

Submission status: Runtime Error

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: 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)
            exit()
        else:
            print(abs(x) + d)
            exit()

print(abs(x))

Submission status: Runtime Error

Solution code:
X, K, D = map(int,input().split())
absX = abs(X)
t = abs(X // D)
remain_K = K - t
l = X % D
if remain_K < 0:
    a = abs(X - (K * D))
    b = abs(X + (K * D))
else:
    if remain_K % 2 == 0:
        a = abs(l - (D * 2))
        b = abs(l + (D * 2))
        if a < b and a < l:
            ans = a
        elif a > b and b < l:
            ans = b
        else:
            ans = l
    else:
        a = abs(l - D)
        b = abs(l + D)
        if a < b:
            ans = a
        else:
            ans = b
print(ans)