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:
import sys,math
try:sys.stdin,sys.stdout=open('input.txt','r'),open('out.txt','w')
except:pass
from sys import stdin,stdout;mod=int(1e9 + 7);from statistics import mode
from collections import *;from math import ceil,floor,inf,factorial,gcd,log2,sqrt,log
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()
# print('{:.3f}'.format(1),round(1.123456789,4))
# sys.setrecursionlimit(500000)
def lcm(a,b): return (a*b)//gcd(a,b)
def setbits(n):return bin(n).count('1')
def resetbits(n):return bin(n).count('0')
def modinv(n,p):return pow(n,p-2,p)
def ncr(n,r):
    num,den=1,1;r=min(n,n-r)
    for i in range(r):num*=(n-i);den*=(i+1)
    return num//den
def ncr_p(n, r, p):
    num,den=1,1;r=min(r,n-r)
    for i in range(r):num = (num * (n - i)) % p ;den = (den * (i + 1)) % p
    return (num * modinv(den,p)) % p
def isPrime(num) :
    if num<=1:return False
    if num==2 or n==3:return True
    if (num % 2 == 0 or num % 3 == 0) :return False
    m = int(num**0.5)+1
    for i in range(5,m,6):
        if (num % i == 0 or num % (i + 2) == 0) :return False
    return True
def bin_search(arr, low, high, val):
    while low <= high:
        mid = low + (high - low) // 2;
        if arr[mid] == val:return mid
        elif arr[mid] < val:low = mid + 1
        else:high = mid - 1
    return -1
def sumofdigit(num):
    count=0;
    while num : count+=num % 10;num //= 10;
    return count;
def inputmatrix():
    r,c=iia();mat=[0]*r;
    for i in range(r):mat[i]=iia();
    return r,c,mat;
def prefix_sum(n,arr):
    for i in range(1,n):arr[i]+=arr[i-1]
    return arr;
def binomial(n, k):
    if 0 <= k <= n:
        ntok = 1;ktok = 1
        for t in range(1, min(k, n - k) + 1):ntok *= n;ktok *= t;n -= 1
        return ntok // ktok
    else:return 0
def divisors(n):
    res = [];
    for i in range(1,ceil(sqrt(n))+1):
        if n%i == 0:
            if i==n//i:res.append(i)
            else:res.append(i);res.append(n//i)
    return res;
# code start from here-
for i in range(10):
    x,k,d=iia()

    xx=abs(x)

    if k*d<=xx:
        ans1=abs(xx-k*d )
        print(ans1)
    else:
        ans1,ans2=0,0
        step=xx//d
        rs=k-step

        f=abs(xx-step*d)
        s=abs(xx-step*d-d)
        if rs%2==0:
            a=f
        else:
            a=s
        print(a)

Submission status: Runtime Error

Solution code:
X,K,D = [int(i) for i in input().split()]
res = X
K %= X
for i in range(K):
    if abs(res - D) > abs(res + D):
        res = abs(res + D)
    else:
        res = abs(res - D)

print(res)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

maximum = max(c[1:])
if maximum < 0:
    print(maximum)
    exit()

used = [False] * (n+1)

ans = 0

for i in range(1, n+1):
    if used[i]:continue
    loop = [i]
    now = i
    while p[now] != i:
        now = p[now]
        loop.append(now)

    L = len(loop)
    S = sum(c[v] for v in loop)
    for a_ind in range(L):
        a = loop[a_ind]
        used[a] = True
        dist_ab = 0
        cost_ab = -c[a]
        for b_ind in range(a_ind, a_ind+L):
            b = loop[b_ind % L]
            cost_ab += c[b]
            max_cost = cost_ab + max(0, S) * ((k - dist_ab) // L)
            ans = max(ans, max_cost)
            dist_ab += 1
            if dist_ab > k:
                break

print(ans)

Submission status: Runtime Error

Solution code:
x,k,d=map(int,input().split())
if x<0:
    print(4/0)
x=abs(x)
s=0
e=k 
ans=x
if(k%2!=0):
    ans=abs(x-d)
while(s<=e):
    mid=(s+e)//2
    if abs(x-mid*d)<=ans:
        ans=min(ans,abs(x-mid*d))
        s=mid+1
    else:
        e=mid-1
print(ans)

Submission status: Runtime Error

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

xi = abs(x)
if abs(x) > d*k:
  m = abs(min(x+d*k, x-d*k))
else:
  for _ in range(k):
    xi -= d
    if xi < 0:
      m = abs(min(xi,xi+d,xi-d))
      break
print(m)

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    global x,k,d
    if d==0:
        print(abs(x))
        return
    if x > 0:
        i = x//d
        if i<k:
            x -= d*i
        else:
            print(x-d*k)
            return
    elif x<0:
        i = -x//d
        if i<k:
            x += d*i
        else:
            print(abs(x+d*k))
            return
    else:
        if k%2==1:
            print(abs(d))
        else:
            print(0)
            return
    
    k -= i
    if k%2==1:
        if abs(x-d)<abs(x+d):
            print(abs(x-d))
        else:
            print(abs(x+d))
    else:
        print(abs(x))
    return

if __name__=='__main__':
    x,k,d = IL()
    solve()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
X, K, D = map(int, input().split())
def recur(x, k, d, memo):
  if k == 0:
    return [abs(x)]
  if memo and memo[k]:
    return memo[k]
  memo[k] = recur(x - d, k - 1, d, memo) + recur(x + d, k - 1, d, memo)
  return memo[k]
print(min(recur(X, K, D, {})))

Submission status: Runtime Error

Solution code:
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    global x,k,d
    if x > 0:
        i = x//d
        if i<k:
            x -= d*i
        else:
            print(x-d*k)
            return
    elif x<0:
        i = -x//d
        if i<k:
            x += d*i
        else:
            print(abs(x+d*k))
            return
    else:
        if k%2==1:
            print(abs(d))
        else:
            print(0)
            return
    
    k -= i
    if k%2==1:
        if abs(x-d)<abs(x+d):
            print(abs(x-d))
        else:
            print(abs(x+d))
    else:
        print(abs(x))
    return

if __name__=='__main__':
    x,k,d = IL()
    solve()

Submission status: Runtime Error

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

xi = abs(x)
if abs(x) > d*k:
  m = abs(min(x+d*k, x-d*k))
else:
  for i in range(k):
    xi -= d*i
    if xi < 0:
      m = abs(min(x+d*i,x-d*i,x+d*(i-1),x-d*(i-1)))
      break
print(m)

Submission status: Runtime Error

Solution code:
X,K,D = list(map(int,input_[0].split()))

N = abs(X)//D

if N >= K:
    ans = abs(X) - K*D
elif (K-N)%2 == 0:
    ans = abs(X) - N*D
else:
    ans = D - (abs(X) - N*D)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    x, k, d = [int(i) for i in input().strip().split()]
    div = x // d
    if x <= d:
        even = x
        odd = abs(x - d)
        if k % 2 == 0:
            ans = even
        else:
            ans = odd
    else:
        if k <= div:
            ans = x - (d * k)
        else:
            k = k - div
            even = x % d
            odd = abs(d - even)
            if even == odd:
                ans = even
            elif k % 2 == 1 and odd < even:
                ans = odd
            elif k % 2 == 0 and even < odd:
                ans = even
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

pm = x // abs(x)
left = k - (abs(x) // d)

if abs(x) // d > k:
    print(abs(x) - k*d)
else:
    ans_abs = abs(x) % d
    if left % 2 == 0:
        print(ans_abs)
    else:
        print(d - ans_abs)

Submission status: Runtime Error

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

x, k, d = map(int, input().split())
dp = [None]*(1000000)
dp[0] = x

for i in range(1, k+1):
  x0, x1 = x+d, x-d
  if abs(x0) < abs(x1):
    x = x0
    dp[i] = x0
  else:
    x = x1
    dp[i] = x1
  if x == dp[i-2]:
    if i%2 != k%2:
      x = dp[i-1]
    break

print(abs(x))

Submission status: Runtime Error

Solution code:
from sys import stdin,stdout
for _ in range(1):#int(stdin.readline())):
    # n=int(stdin.readline())
    x,k,d=list(map(int,stdin.readline().split()))
    if x==k==d:
        print(0 if k&1 else x)
        continue
    des=x%d
    op=x//d
    a=[des,des-d]
    if k<=op:#check for eq
        print(x-k*d)
    else:
        print(abs(a[k%op]))

Submission status: Runtime Error

Solution code:
import numpy as np

x,k,d = np.array(list(map(int,input().split())))
#偶数回前後に移動したら0
#偶数回一方向に移動したら2D

for i in range(k):
  x = np.abs(x) - d

  if x < 0:
    break



if (k-i+1) % 2 == 0:
  #そのまま
  ans = np.abs(x)
else:
  ans = np.min(np.abs(x-d),np.abs(x+d))

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a = abs(x//d)
b = x%d

if k <= a:
    print(abs(abs(x)-k*d))

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

Submission status: Runtime Error

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

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define MOD ll(1000000007)

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	ll d, i, n, j, t, m, x, y, z, k;

	ll ans=0;

	cin>>x>>k>>d;
	x=abs(x);

	ll mul=k*d;
	if(x>=mul)
		ans=x-mul;
	else{
		m=x%d;
		ll quo = x/d;
		if((k-quo)&1)
			ans=abs(d-m);
		else
			ans=abs(m);
	}
	
	cout<<ans;
	cout<<"\n";



	return 0;
}

Submission status: Runtime Error

Solution code:
x,k,d = list(map(int,input().split()))
c = x//d
if c >= k:
    print(x-k*d)
else:
    x = x%(c*d)
    k-=c
    if k&1:
        print(abs(x-d))
    else:
        if x < abs(x-d):
            print(x)
        else:
            print(abs(x-d))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
  
if abs(x) > k * d:
  r = x - int(abs(x)/x) * k * d
else:
  n = int(abs(x) / d)
  r1 = x - int(abs(x)/x) * n * d
  if r1 == 0:
    r = 0
  else:
    r2 = r1 - int(abs(r1)/r1) * d
    r = min(abs(r1), abs(r2))

print(r)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math
X, K, D = map(int, input().split())
if abs(X // D) >= K:
    ans = X - (D * K) * (X // abs(X))
    ans = abs(ans)
else:
    K -= X//D
    ans = X - (D * (X//D)) * (X // abs(X))
    if K % 2 == 0 and abs(ans) < D:
        ans = abs(ans)
    else:
        ans = min(abs(ans+D), abs(ans-D))
    # print((D * X//D) * (X // abs(X)))
print(ans)
# print(X // D, math.ceil(X / D))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <fstream>
#include <tuple>

using namespace std;

#define fori(t) for(int i = 0; i < t; ++i)
#define forj(n) for(int j = 0; j < n; ++j)
#define fork(n) for(int k = 0; k < n; ++k)
#define forai(a) for(auto& i : a)
#define foraj(a) for(auto& j : a)
#define forak(a) for(auto& k : a)
#define forp(s, n, d) for(int p = s; p < n; p += d)
#define fast cin.tie(0); cout.tie(0); std::ios_base::sync_with_stdio(0);
#define pb(a) push_back(a);
#define pob(a) pop_back(a);
#define sorti(a) sort(a.begin(), a.end());
#define sortm(a, n) sort(a, a + n);
#define reversi(a) reverse(a.begin(), a.end());
#define fi first
#define se second
#define yes cout << "Yes\n";
#define no cout << "No\n";
#define ans(a) cout << a << '\n';
#define res(a, n) a.resize(n);
#define read(a, n) a.resize(n); fori(n) cin >> a[i];
#define int long long
//#define double long double

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<long long, long long> llll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<int, pair<int, int>>> viii;
typedef pair<int, pair<int, int>> iii;
typedef vector<pair<long long, long long>> vllll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<bool> vb;
typedef pair<char, char> cc;
typedef pair<pair<char, char>, char> ccc;
typedef vector<char> vc;
typedef pair<int, char> ic;
typedef pair<ii, char> iic;
typedef vector<bool> vb;
typedef set<int> si;
typedef set<ii> sii;
typedef unsigned long long ull;
typedef pair<ull, ull> ullll;
typedef pair<double, double> dd;
typedef vector<dd> vdd;
typedef vector<pair<ull, ull>> vullll;
typedef pair<bool, bool> bb;
typedef vector<bb> vbb;
typedef vector<vector<ii>> vvii;
typedef vector<vector<iii>> vviii;

const int INF = 2147483647;
const ll LONGINF = 9223372036854775807;
const ll MOD = 1e9 + 7;

void solve()
{
    ll x, k, d;
    cin >> x >> k >> d;
    ll times = min(k, llabs(x) / d);
    if (x > 0) x -= d * times;
    else x += d * times;
    k -= times;
    if (k % 2 == 0) ans(llabs(x))
    else ans(llabs(x > 0 ? x - d : x + d))
}

signed main()
{
    fast
    bool mult = false;
    if (mult)
    {
        int t;
        cin >> t;
        while (t--) solve();
    }
    else solve();
}

Submission status: Runtime Error

Solution code:
def main():
    X, K, D = list(map(int, input().split()))
    right = X % D
    left = right - D
    if X in [left, right]:
        if K % 2 == 0:
            print(abs(X))
        else:
            if X == left:
                print(right)
            else:
                print(abs(left))
        return
    if X > right:
        to_right = (X - right) // D
        if to_right >= K:
            print(X - K * D)
        else: # to_right < K
            if (K - to_right) % 2 == 0:
                print(right)
            else:
                print(abs(left))
    else:  # X < left
        to_left = (left - X) // D
        if to_left >= K:
            print(abs(X + K * D))
        else: # to_left < K
            if (K - to_right) % 2 == 0:
                print(abs(left))
            else:
                print(right)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc175/abc175_c
import sys
import math

def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))


def main():
    X, K, D = i2nn()
    absX = abs(X)
    sign = X // absX
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    a, c = divmod(absX, D)
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return


main()

Submission status: Runtime Error

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

for i in range(K):
    if X > 0:
        X -= D
    else:
        X += D
    if X >= 0:
        if X in s: break
        s.add(X)
    else:
        if -X in S: break
        s.add(-X)

print(min(s))

Submission status: Runtime Error

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


def f(C):
    NX = float("inf")
    if C > K:
        return NX

    if K % 2 == 0:
        if C % 2 == 0:
            NX = abs(abs(X) - C * D)
        else:
            NX = abs(abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
    else:
        if C % 2 == 0:
            NX = abs(NX, abs(X) - (C + 1) * D)
            if C > 0:
                NX = min(NX, abs(abs(X) - (C - 1) * D))
        else:
            NX = abs(abs(X) - C * D)

    return NX


C = min(abs(X) // D, K)

print(min(f(C), f(C + 1)))

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:
    if K%2 == 1:
        X -= D
        k -= 1
    
    x1 = X%(2*D)
    print(min(x1, (2*D-x1)))

Submission status: Runtime Error

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

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


print(ans)

Submission status: Runtime Error

Solution code:
X=input()
K=input()
D=input()

min=X+K*D
for k in range(K):
  current=X-k*D+(K-k)*D
  if min>current:
    min=current
print(min)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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


print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    x,k,d = LI()
    s = abs(x) // x
    c = abs(x) // d

    ans = 0
    if c >= k:
        ans = x - d * k * s
    else:
        ans = x - d * c * s
        if (k-c) % 2 == 1:
            ans = ans - d * s
    print(abs(ans))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

if K*D<=X:
    print(abs(X)-K*D)
    exit()
move_num_before = abs(X)//D
move_num_after = move_num_before+1
if (K - move_num_after)%2 == 0:
    print(abs(abs(X)-move_num_after*D))
    exit()
print(min(abs(X)-move_num_before*D))

Submission status: Runtime Error

Solution code:
X, K, D = [int(n) for n in input()]

#X = 10
#K = 1
#D = 2


startN = (D * K - X) // (2 * D)

N1 = startN + 1
N2 = startN
N3 = startN - 1

if N1 < 0:
    N1 = 0
if N2 < 0:
    N2 = 0
if N3 < 0:
    N3 = 0

def getScore(n):
    score = X + n * D - D * (K - n)
    if score < 0:
        score = score * -1
    return score

score1 = getScore(N1)
score2 = getScore(N2)
score3 = getScore(N3)

minScore = score1
if minScore > score2:
    minScore = score2
if minScore > score3:
    minScore = score3

print(minScore)

Submission status: Runtime Error

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

a=x//d

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265358979323846264338327950L
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl '\n'
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1)
    {
        cerr << name << " = " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args)
    {
        const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " = " << arg1 << " || ";
        __f(comma+1, args...);
    }
#else
#define debug(...)
#endif
template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
    os << "{ ";
    for(auto& y : x) os << y << " ; ";
    return os << "}";
}
template <class X, class Y>
ostream & operator << (ostream & os, pair <X, Y> const& p) {
    return os << "[ " << p.first << ", " << p.second << "]" ;
}
ll power(ll x, ll y, ll p)
{ 
    ll res = 1;  
    x = x % p;
    while (y > 0) { 
        if (y & 1) res = (res * x) % p; 
        y = y >> 1;
        x = (x * x) % p;   
    } 
    return res; 
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	ll x, k, d;
 	cin >> x >> k >> d;
 	if (x >= 0) {
	 	ll temp = min(x / d, k);
	 	x -= temp * d;
	 	k -= temp;
	 	if (k % 2 == 1) {
	 		cout << abs(x - d) << endl;
	 	}
	 	else {
	 		cout << abs(x) << endl;
	 	}
 	}
 	else {
 		ll temp = min(abs(x) / d, k);
	 	x += temp * d;
	 	k -= temp;
	 	if (k % 2 == 1) {
	 		cout << abs(x + d) << endl;
	 	}
	 	else {
	 		cout << abs(x) << endl;
	 	}	
 	}
	return 0;
}

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
if x >= 0:
    if x - k * d >= 0:
        print(x - k * d)
    else:
        # 最初に負になってしまうところとその直前を巡回する
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ok + ng) // 2
            if x - mid * d <= 0:
                # いきすぎ
                ok = mid
            else:
                ng = mid
        # ok回目で負にたどり着く？
        rest = k - ok
        if rest % 2 == 0:
            # いまいるところに戻ってこれる
            print(abs(x - ok * d))
        else:
            # 一回正方向にいどうしたもの
            print(x - (ok - 1) * d)
else:
    if x + k * d <= 0:
        print(abs(x + k * d))
    else:
        # 最初に正になるところ
        ok, ng = ok, 0
        while (ok - ng) > 1:
            mid = (ng + ok) // 2
            if x + mid * k >= 0:
                ok = mid
            else:
                ng = mid
        rest = k - ok
        if rest % 2 == 0:
            print(x + ok * d)
        else:
            print(abs(x + (ok - 1) * d))

Submission status: Runtime Error

Solution code:
x, k, d = map(int, input().split())
if x >= 0:
    if x - k * d >= 0:
        print(x - k * d)
    else:
        # 最初に負になってしまうところとその直前を巡回する
        ok, ng = k, 0
        while (ok - ng) > 1:
            mid = (ok + ng) // 2
            if x - mid * d <= 0:
                # いきすぎ
                ok = mid
            else:
                ng = mid
        # ok回目で負にたどり着く？
        rest = k - ok
        if rest % 2 == 0:
            # いまいるところに戻ってこれる
            print(abs(x - ok * d))
        else:
            # 一回正方向にいどうしたもの
            print(x - (ok - 1) * d)
else:
    if x + k * d <= 0:
        print(abs(x + k * d))
    else:
        # 最初に正になるところ
        ok, ng = ok, 0
        while (ok - ng) > 1:
            if x + mid * k >= 0:
                ok = mid
            else:
                ng = mid
        rest = k - ok
        if rest % 2 == 0:
            print(x + ok * d)
        else:
            print(abs(x + (ok - 1) * d))

Submission status: Runtime Error

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

if X >= K * D:
    print(X - K * D)
elif X <= K * (- D):
    print(abs(X + K * D))
else:
    if X >= 0:
        pos = X % D
        neg = X % D - D
        if K % 2 == (X // D) % 2:
            print(pos)
        else:
            print(abs(neg))
    if X <= 0:
        pos = D - (abs(X) % D)
        neg = - (abs(X) % D)
        if K % 2 == (X // D) % 2:
            print(abs(neg)
        else:
            print(pos))

Submission status: Runtime Error

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

Submission status: Runtime Error

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