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: Accepted

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

X = abs(X)
n = X//D

if K < n:
    ans = X - K*D
else:
    X = X - n*D
    K = (K-n)%2
    X -= K*D
    ans = abs(X)

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
X,K,D = map(int,input().split())
x = abs(X)
if x > K*D:
    print(x-K*D)
else:
    n = x // D
    p = K - n
    if p % 2 == 0:
        print(x-D*n)
    else:
        print(abs(x-D*(n+1)))

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    x, k, d = map(int, readline().split())
    cur = abs(x)
    rem = k
    cnt = min(cur // d, k)
    cur -= cnt * d
    rem -= cnt
    if rem % 2 == 0:
        ans = cur
    else:
        ans = abs(cur - d)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

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

if x < 0: x = -x

q,r = x//d,x%d

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
def MI():
    return map(int,input().split())
    
def LI():
    return list(MI())


x,k,d=MI()

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

if x<0 and x+k*d<0:
    ans=x+k*d
    ans=abs(ans)
    print(ans)
    exit()
if x>0 and x-k*d>0:
    ans=x-k*d
    print(ans)
    exit()
    
ans=x%(2*d)
if k%2==1:
    ans=min(abs(ans-d),abs(ans+d))
    print(ans)
    exit()
else:
    ans=min(abs(ans-2*d),abs(ans),abs(ans+2*d))
    print(ans)
    exit()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
k = X // D
rt = X - k * D
lt = abs(rt - D)
if k > K:
    print(X - K * D)

elif k % 2 == K % 2:
    print(rt)

else:
    print(lt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split(' '))
x = abs(x)
if k * d <= x:
    print(x - k * d)
    exit(0)
p = x // d
assert p < k

x -= p * d
k -= p
x = abs(x)
ans = [x, abs(x - d)]
print(ans[k % 2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

Submission status: Accepted

Solution code:
def solve(x, k, d):
    x = abs(x)
    n, m = divmod(x, d)

    if n >= k:
        return x - k * d

    if n % 2 == k % 2:
        return m

    return abs(m - d)


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

print(solve(x, k, d))

Submission status: Accepted

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

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

p_min_cnt = x//d
rem = k - p_min_cnt
p_min = x%d

if rem%2 == 0:
    print(p_min)
else:
    m_min = abs(p_min-d)
    print(m_min)

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

need = X // D
t = min(need, K)

X -= D * t
K -= t

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
mi = min(abs(X - K * D), abs(X + K * D))
for a in (X - K * D) % (2 * D), (X - K * D) % (2 * D) - 2 * D:
    b = (X - a) // D
    if abs(b) <= K:
        mi = min(mi, abs(a))
print(mi)

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

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

if abs(X) >= D*K:
    print(abs(X)-D*K)
else:
    q, r = divmod(X, D)
    if q & 1 == K & 1:
        print(r)
    else:
        print(D-r)

Submission status: Accepted

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

if X<0:
    X *= -1
n = X//D
m = min(K,n)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

a = abs(x)//d

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)


def solve(X: int, K: int, D: int):
    X = abs(X)
    mx = K * D
    tmp = X % D
    #print(mx, tmp, X - tmp)
    if mx < X - tmp:
        ret = X - mx
    elif (X - tmp) // D == K:
        ret = tmp
    else:
        d = X - tmp
        n = d // D
        if n % 2 == K % 2:
            ret = tmp
        else:
            ret = abs(tmp - D)
    print(ret)
    return

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: Accepted

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


lap, rem = divmod(x, d)
if lap >= k:
    print(x - k * d)
    exit()

nokori = k - lap
rem0, rem1 = rem, d - rem
if nokori % 2 == 0:
    print(rem0)
else:
    print(rem1)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
X, K, D = map(int, input().split())
if abs(X) >= K * D:
  print(abs(X) - K * D)
  exit(0)
a = abs(X) // D
b = -(-abs(X) // D)
res = float("inf")
if (K - a) % 2:
  res = min(res, abs(X) % D + D, abs(abs(X) % D - D))
else: res = min(res, abs(X) % D)
print(res)

Submission status: Accepted

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

min_x = x - k * d
max_x = x + k * d
if min_x >= 0:
    print(min_x)
elif max_x <= 0:
    print(abs(max_x))
else:
    a1 = max_x % (d * 2)
    a2 = abs(min_x) % (d * 2)
    print(min(a1, a2))

Submission status: Accepted

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

minimum = x - d * k
maximum = x + d * k

if minimum >= 0:
    print(minimum)
elif maximum <= 0:
    print(-maximum)
else:
    c1 = maximum % (2*d)
    c2 = c1 - 2*d
    print(min(abs(c1), abs(c2)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    x,k,d = map(int,ipt().split())
    if abs(x) > k*d:
        print(abs(abs(x)-k*d))
    else:
        t = abs(x)//d
        dd = abs(x)%d
        if (t-k)%2:
            print(d-dd)
        else:
            print(dd)


    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

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

tmp = min(k, abs(x) / d)
k -= tmp

if k % 2 == 1:
    tmp += 1

if x > 0:
    x -= tmp * d
else:
    x += tmp * d

print abs(x)

Submission status: Accepted

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

X=abs(X)

if D*K<=X:
    print(X-D*K)
else:
    A=X//D
    if (K-A)%2:
        print((A+1)*D-X)
    else:
        print(X-A*D)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

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

Submission status: Accepted

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

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    X,K,D=MI()
    t=abs(X)//D
    if t>=K:
        ans=abs(X)-D*K
    else:
        a=abs(abs(X)-D*t)
        rem=K-t
        if rem%2==0:
            ans=a
        else:
            ans=a=abs(abs(X)-D*(t+1))
        
        
    print(ans)
    
    

main()

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
X=abs(X)
if X>K*D:
  print(X-K*D)
  exit()
M=X//D
X-=(M*D)
K-=M
if K&1:
  print(D-X)
else:
  print(X)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    x,k,d = LI()
    if x < 0:
        x = -x
    m = x//d
    if k < m:
        print(x-k*d)
        return
    x -= d*m
    k -= m
    k &= 1
    x -= d*k
    print(abs(x))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

nex = abs(X) - K * D

if nex > 0:
    print (nex)

else:
    num = abs(X) // D

    a = abs(X) % D
    b = a - D

    if K % 2 == num % 2:
        print (a)
    else:
        print (abs(b))

Submission status: Runtime Error

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

if k*d <= x:
  answer = x - k*d
else:
  while i * d <= x and i <= d:
    minx = abs(i*d-x)
    if (k-i) % 2 == 0:
      answer = abs(minx)
    if (k-i) % 2 == 1:
      answer = min(abs(minx-d),abs(minx+d))
    i += 1

print(answer)

Submission status: Runtime Error

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

if k*d <= x:
  answer = x - k*d
else:
  while i * d <= x and i <= d:
    minx = min(abs(i*d), (i * d - x))
    if (k-i) % 2 == 0:
      answer = abs(minx)
    if (k-i) % 2 == 1:
      answer = abs(min(abs(minx-d), abs(minx+d)))
    i += 1

print(answer)

Submission status: Runtime Error

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

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



print(answer)

Submission status: Runtime Error

Solution code:
x,k,d = map(int, input().split())
lists = []
i = 0
 

if k*d <= x:
  answer = x - k*d
else:
  while i * d <= x and i <= d:
    answer = min(abs(i*d-x),i*d+x)
    i += 1


print(answer)

Submission status: Runtime Error

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

def moving(x,k,d):
  if k == 0:
    return x
  
  x = min(abs(x-d), abs(x+d))
  return moving(x,k - 1, d)

answer = moving(x,k,d)
print(answer)

Submission status: Runtime Error

Solution code:
x,k,d = list(map(int,input().split()))
dis = x
cnt = 0
while cnt<k:
    if abs(dis)<d:
        recnt = k -cnt
        if recnt%2==0:
        else:
            dis = d-abs(dis)
        break
    elif dis>=0:
        dis = dis - d
    else:
        dis = dis + d
    cnt += 1
print(abs(dis))

Submission status: Runtime Error

Solution code:
import sys

x = abs(int(input()))
k = int(input())
d = int(input())

move_cnt = int(x / d)
if (move_cnt <= k):
    k -= move_cnt
    amari = x - move_cnt * d

else:
    amari = x - k * d
    k = 0

if (k > 0):
    if (k % 2 == 1):
        if (amari > 0):
            amari -= d
        else:
            amari += d

print(abs(amari))

Submission status: Runtime Error

Solution code:
import sys

x = abs(int(input()))
k = int(input())
d = int(input())

move_cnt = float(x / d)
if (move_cnt <= k):
    k -= move_cnt
    amari = x - move_cnt * d

else:
    k = 0
    amari = x - move_cnt * d

if (k > 0):
    if (k % 2 == 1):
        if (amari > 0):
            amari -= d
        else:
            amari += d

print(int(abs(amari)))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
x,k,d = input().split()
ans = 0
if abs(x) > abs(k*d):
    ans =  abs(x) - abs(k*d)
else:
    kaisuu = abs(x)/d 
    k_2 = k - kaisuu
    if k_2/2 == 2:
        ans = abs(x) - d*kaisuu 
    else:
        ans = (abs(x) - d*kaisuu) - d
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
mport math
x,k,d=map(int,input().split())
def f(x,k,d,i):
    return abs(x+(k-2*i)*d)
a=0
b=k
m=int((a+b)/2)
if k==1:
    print(min(f(x,k,d,0),f(x,k,d,1)))
else:
    while f(x,k,d,m)>f(x,k,d,m-1) or f(x,k,d,m)>f(x,k,d,m+1):
        if f(x,k,d,m)<f(x,k,d,m+1):
            m=math.floor((a+m)/2)
            b=m
        else:
            m=math.ceil((m+b)/2)
            a=m
    print(f(x,k,d,m))

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if x > k*d:
    print(x-k*d)
else:
    p = x//d
    if p == 0 or p==1:
          for i in range(1,k+1):
            p2 = x-i*d
            c = k-i
            if p2 <= 0:
                if c%2 != 0:
                    print(abs(d+p2))
                    break
                elif c%2 == 0:
                    print(abs(p2))
                    break
    c = k-p
    elif c%2 == 0:
        print(abs(x-p*d))
    else:
        print(abs(d-abs(x-p*d)))

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if x > k*d:
    print(x-k*d)
else:
    p = x//d
    if p == 1:
          for i in range(1,k+1):
            p2 = x-i*d
            c = k-i
            if p2 <= 0:
                if c%2 != 0:
                    print(abs(d+p2))
                    break
                elif c%2 == 0:
                    print(abs(p2))
                    break
    elif k%p == 0:
        print(abs(x-p*d))
    else:
        print(abs(d*(p+1)-x))

Submission status: Runtime Error

Solution code:
for i in range(5,0,-1):

Submission status: Runtime Error

Solution code:
l = list(map(int,input().split()))
a = []
for i in range(l[2]):
  abs(l[1] + i*l[3] -l[3]*(l[2]-i))
  a.append(abs)
print(a.min())

Submission status: Runtime Error

Solution code:
l = list(map(int,input().split()))
a()
for i in range(l[2]):
  abs(l[1] + i*l[3] -l[3]*(l[2]-i))
  a.append(abs)
print(a.min())

Submission status: Runtime Error

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

l = [-d,d]

ans = 10**15
for v in itertools.product(l, repeat=k):
  ans = min(ans, abs(x+sum(v)))

print(ans)

Submission status: Runtime Error

Solution code:
1000000000000000 1000000000000000 1000000000000000

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = -float('inf')
for s in range(N):
    ### Step1
    S = []  # 累積和S. ただし, 初項は0ではなく1回目の移動後の得点とする.
    # 1回目の移動
    i = P[s] - 1
    S.append(C[i])
    # 2回目以降の移動. スタート地点に戻ってくるまで繰り返し.
    while i != s:
        i = P[i] - 1
        S.append(S[-1] + C[i])

    ### Step2: KとSの長に応じて場合分けして, 得点の最大値を求める.
    # 1周未満しか移動できない場合:
    if K <= len(S):
        score = max(S[:K])
    # 1周以上回ることができるが, ループを1周したときに得点が減る場合:
    elif S[-1] <= 0:
        score = max(S)
    # 1週以上回ることができ, かつループを1週するごとに得点が増える場合:
    else:
        # ループを (K // len(S) - 1)回 廻る場合:
        score1 = S[-1] * (K // len(S) - 1)
        score1 += max(S)
        # ループを (K // len(S))回 廻る場合:
        score2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            score2 += max(0, max(S[:r]))
        # score1 と score2 の大きい方がこの場合の得点の最大値
        score = max(score1, score2)

    ans = max(ans, score)

print(ans)

Submission status: Runtime Error

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
if(X >= K * D):
  print(X - K * D)
  return
else:
  q = X // D
  r = X // D
  if((K - q) % 2 == 0):
    print(r)
    return
  else:
    print(abs(r - D))
    return

Submission status: Runtime Error

Solution code:
from math import abs
X, K, D = list(map(int, input().split()))
X = abs(X)
if(X >= K * D):
  print(X - K * D)
  return
else:
  q = X // D
  r = X // D
  if((K - q) % 2 == 0):
    print(r)
    return
  else:
    print(abs(r - D))
    return

Submission status: Runtime Error

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

y = abs(X)//D

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

else  :
    a = (X-y*D)
    b = (X-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)


#print(y+1)
#print(1000000000000000)

Submission status: Runtime Error

Solution code:
c = x // d
# 必要な移動回数
k_r = k - c
# 移動した後の残りの移動回数
ans_2 = x - d * c
#残り移動回数が偶数のときのｘからの距離

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

x_l = x - d * c
#ｘまでの残りの距離

if k_r % 2 == 0:
    print(ans_2)
    exit()
else :
    x_l -= d
    x_l =abs(x_l)
    print(x_l)

Submission status: Runtime Error

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

# 初期値は「最小値の最大値」
if K % 2 == 0:  # 偶数回の移動ならabs(X)より、奇数回の移動ならabs(X)+abs(D)よりは小さくできる
    ans = abs(X)
elif X > 0:
    ans = min(abs(X + D), abs(X - D))


for i in range(K+1):
    goal_hyp_1 = X + i * D - (K - i) * D
    goal_hyp_2 = X - i * D + (K - i) * D
    if abs(goal_hyp_1) < ans:
        ans = abs(goal_hyp_1)
    elif abs(goal_hyp_2) < ans:
        ans = abs(goal_hyp_2)
    else:
        break  # 一端増加に転じたらもう減りることはない

print(ans)

Submission status: Runtime Error

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

ans = abs(X) + abs(D)  # 偶数回の移動ならabs(X)より、奇数回の移動ならabs(X)+abs(D)よりは小さくできる

for i in range(th, int(K/2)):
    goal_hyp = X + i * D - (K - i) * D
    if abs(goal_hyp) < ans:
        ans = abs(goal_hyp)
    else:
        break  # 一端増加に転じたらもう減りることはない
        
print(ans)

Submission status: Runtime Error

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

x=abs(x)

a=x//d

if a>=k:
    ans=x-k*d

elif (k-a)%2==0:
    ans=x-d*m

else:
    ans=x-d*m-d

print(abs(ans)

Submission status: Runtime Error

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

x=abs(x)

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

if k>=x:

    
else:
    print(x%d)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
y = abs(x)
count = y // d
amari = y % d

if k >= d:
    if (k - d) % 2 == 1:
        amari -= d
else:
    amari = y - k * d

print(abs(amari))

Submission status: Runtime Error

Solution code:
y = abs(x)
count = y // d
amari = y % d

if k >= d:
    if k - d % 2 == 1:
        amari -= d
else:
    amari = y - k * d

print(abs(amari))

Submission status: Runtime Error

Solution code:
import numpy as np
import matplotlib.pyplot as plt

inpt = input()
inpt_list = inpt.split(" ")
X, K, D = [int(x) for x in inpt_list]

X_abs = np.abs(X)
num = X_abs // D
rem = K - num
if rem < 0:
    out = X_abs - D*K
else:
    if rem%2==0:
        out = X_abs - num*D
    else:
        out = np.abs(X_abs - num*D - D)
print(out)

Submission status: Runtime Error

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

p_min = X % D
n_min = abs(p_min - D)

if p_min <= n_min:
  ans_min = p_min
  min_idx = X_div_D
else:
  ans = min = n_min
  min_idx = X_div_D + 1
  
if K <= min_idx:
  print(X - K * D)
else:
  if (K - min_idx) % 2 == 0:
    print(ans_min)
  else:
    print(abs(ans_min - D))

Submission status: Runtime Error

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

p_min = X % D
n_min = abs(p_min - D)

if p_min <= n_min:
  ans_min = p_min
  min_idx = X_div_D
  sign_min = 1
else:
  ans = min = n_min
  min_idx = X_div_D + 1
  sign_min = -1
  
if K < min_idx:
  print(X - K * D)
else:
  if (K - min_idx) % 2 == 0:
    print(ans_min)
  else:
    print(abs(ans_min - sign_min * D))

Submission status: Runtime Error

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

p_min = X % D
n_min = abs(p_min - D)

if p_min <= n_min:
  ans_min = p_min
  min_idx = X_div_D
  sign_min = 1
else:
  ans = min = n_min
  min_idx = X_div_D + 1
  sign_min = -1
  
if K < min_idx:
  print(X - K * D)
else:
  if (K - min_idx) % 2 == 0:
    print(ans_min)
  else:
    print(abs(ans_min - sign_min * D))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

lst = [-n*d]*n
for i in range(0,n):
	lst[i] = lst[i] + i*d
ans = 100000000000000
for y in lst:
	ans = min(ans,abs(x+y))

print(ans)

Submission status: Runtime Error

Solution code:
import math
x,k,d=map(int,input().split())
x=abs(x)
if x>k*d:
    print(abs(x-k*d))
else:
    j=math.ceil(x/d)
    print(min([abs(x-j*d),abs(x-(j-1)*d)])

Submission status: Runtime Error

Solution code:
k = int(input())
x = 7 % k
for i in range(1, k+1):
    if x == 0:
        print(i)
        exit()
    x = (x*10+7)%k
print(-1)

Submission status: Runtime Error

Solution code:
k = int(input())
x = 7 % k
for i in range(1, k+1):
    if x == 0:
        print(i)
    x = (x*10+7)%k
print(-1)

Submission status: Runtime Error

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

for i range(K):
  if X > 0:
    X -= D
  else:
    X += D
    
print(X)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#移動の最小値(高速化問題)
x,k,d = map(int,input().split())
x = abs(x)
if (x//d) >=k:
    ans = x - d*k
y = x-(x//d)*d
elif k- (x//d)%2 != 0:
    ans = y
else:
    ans = y-d
print(abs(ans))