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:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

def to10(A):
    ret=0
    for a in range(len(A)-1,-1,-1):
        ret+=X[a]*2**(N-1-a)
    return ret
######################################################################################################
X,K,D=inputintlist()
X=abs(X)
if X-K*D>=0:
    print(X-K*D)
else:
    K-=X//D
    X=X-(X//D)*D
    if K%2==0:
        print(X)
    else:
        print(abs(X-D))

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    x, k, d = i_map()

    x = abs(x)

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':

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

    #マイナス座標はプラスに変換する
    x = abs(x)

    #総移動回数でどこまで行くか？
    tmp1 = x - k * d

    ans = 0
    if 0 <= tmp1:
        #残りがないのでその地点が最終値
        ans = tmp1
    else:
        #残りを正確に計算
        tmp = x // d
        #残り距離
        x -= tmp * d
        #残り回数
        k -= tmp

        if k%2 == 1:
            ans = abs(x-d)
        else:
            ans = x
    print(ans)

Submission status: Accepted

Solution code:
# ABC175
# C
# 座標XからK回正または負にD移動
# できない
x, k, d = map(int, input().split())
c = abs(x) // d
if c > k:
    print(abs(x) - d * k)
else:
    x = abs(x) - d * c
    c = k - c
    if c % 2 == 0:
        print(abs(x))
    else:
        print(abs(abs(x)-d))

Submission status: Accepted

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

if abs(x//d) > k:
    print(abs(x)-k*d)
else:
    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())
if abs(x) / d > k:
    print(abs(x) - d * k)
else:
    c = (-(-abs(x) // d))
    if (k - c) % 2 == 0:
        print(abs(abs(x) - d * c))
    else:
        print(abs(abs(abs(x) - d * c) - d))

Submission status: Accepted

Solution code:

def p(X):
    print(X)
N = list(map(int, input().split()))

zahyou = abs(N[0])
sums = N[1]
move = N[2]
shou = zahyou // move
a =[0,0]

if shou >= sums:
    p(zahyou-move*sums)
else:
    zahyou -= move*shou
    if (sums - shou) %2 ==0:
        p(zahyou)
    else:
        p(abs(zahyou-move))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(x, k, d):
    x = abs(x)
    n = x // d
    if n > k:
        return x - k*d
    else:
        best = x - n*d
        if (k - n) % 2 == 0:
            return best
        else:
            return abs(best - 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())
x1=abs(x)%d
x2=abs(abs(x1)-d)
num=abs(x)//d
if((k-num)>=0):
    if((k-num)%2==0):
        print(x1)
    else:
        print(x2)
else:
    print(abs(x)-k*d)

Submission status: Accepted

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

MIN = x-k*d


if 0 <= MIN:
    print(MIN)
else:
    a, R = divmod(x, d)
    L = d-R
    cnt = k-a
    if cnt % 2 == 0:
        print(R)
    else:
        print(L)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
ans=1
minb=1
if k%2==0:ans=0  #偶数

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

if semin>k:print(abs(x-k*d))
else:
  if ans==minb:
    print(abs(x-d*semin))
  else:
    print(abs((x-(d*semin))-d))

Submission status: Accepted

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

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

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

if semin>k:print(abs(x-k*d))
else:
  if ans==minb:
    print(abs(x-d*semin))
  else:
    print(abs((x-(d*semin))-d))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
x=abs(x)
if k%2==0:
  ans=0 #偶数
else:
  ans=1 #奇数

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

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

Submission status: Accepted

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

X=abs(X)
k=min(K,X//D)
X_=X-(k*D)
K_=K-k

if K_%2!=0:
    ans=min(abs(X_-D),abs(X_+D))
else:
    ans=abs(X_)

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

X,K,D = LI()

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if k*d <= x:
    ans = x-k*d
else:
    # 残り移動回数
    modCount = k - x//d
    # 残り距離
    modDis = x%d

    if modCount%2 == 0:
        ans = min(abs(x%d - d*modCount), abs(x%d))
    else:
        ans = min(abs(x%d - d), abs(x%d + d))
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    X, K, D = [int(i) for i in input().split()]
    if abs(X) > K * D:
        print(abs(X) - K * D)
        return
    else:
        if K % 2 == (abs(X) // D) % 2:
            print(abs(abs(X) - D * (abs(X) // D)))
        else:
            print(abs(abs(X) - D * ((abs(X) // D) + 1)))

resolve()

Submission status: Accepted

Solution code:
x,k,d=[int(i) for i in input().split()]

x=abs(x)

a=int(x/d)


if a>k:
    a=k

k-=a

x-=a*d

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

print(x)

Submission status: Accepted

Solution code:
def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    count = x // d
    if count > k:
        ans = x - (k*d)
    else:
        # if k % 2 == count % 2:
        if (k - count) % 2 == 0:
            ans = x - (count*d)
        else:
            ans = x - (count+1) * d
    print(abs(ans))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X, K, D = map(int,input().split())
X = abs(X)
time = X // D
time = min(K, time)
X = X - (D * time)
K = K - time

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

Submission status: Accepted

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


def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    step = x // d
    if step >= k:
        ans = x - d * k
        print(ans)
        return

    k -= step
    x = x - step * d
    k %= 2
    ans = abs(x - d*k)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.readline
import time
from heapq import heappush, heappop
import math
import itertools as it
def inp():
    return int(input())
def inpl():
    return list(map(int, input().split()))
start_time = time.perf_counter()
# ------------------------------

x, k, d = inpl()

t = abs(x) // d

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

k -= t
if k % 2 == 1:
    print(d - abs(x) % d)
else:
    print(abs(x) % d)

# -----------------------------
end_time = time.perf_counter()
print('time:', end_time-start_time, file=sys.stderr)

Submission status: Accepted

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

	ans = 0

	if abs(X) >= K * D: # 0にたどり着かない or ちょうど0にたどり着く
		ans = abs(X) - K * D
	else: # 0を通り過ぎて往復する
		s = abs(X) // D
		if (K - s) % 2 == 0:
			ans = abs(X) - s * D # abs(X) - s * D - 0
		else:
			ans = -abs(X) + (s + 1) * D # 0 - (abs(X) - (s + 1) * D)

	print(ans)

 
if __name__ == "__main__":
  	main()

Submission status: Accepted

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

if X < 0: X = -X

if X >= D*K:
    print(X - D*K)
else:
    moves = X // D
    K -= moves
    X = X - D * moves    

    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)
time = X // D
time = min(K, time)
X = X - (D * time)
K = K - time

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

Submission status: Accepted

Solution code:
from math import *
x, k, d = map(int, input().split())
x = abs(x)
z = ceil(x/d)
if x-k*d >= 0:
    print(x-k*d)
else:
    if (k-z)%2 == 0:
        print(abs(x-z*d))
    else:
        print(x-z*d+d)

Submission status: Accepted

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

    cur = abs(x)
    rem = k

    cnt = min(cur//d, k)
    cur -= d * cnt
    rem -= cnt
    if rem > 0:
        if rem % 2 == 1:
            cur = cur - d

    ans = abs(cur)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int, input().split( ))
if x<0:
    x*=-1

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

k-=c
print(d-x%d if k%2 else x%d)

Submission status: Accepted

Solution code:
def solve():
    X,K,D = map(int, input().split())
    X = abs(X)
    pos = abs(X % D)
    neg = abs(pos - D)
    if X // D >= K:
        print(abs(X - D*K))
        return
    elif (X // D) % 2 == K %2:
        print(pos)
    else:
        print(neg)


solve()

Submission status: Accepted

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

p = x//d
m = k - p
ans = 0
if k<p:
    ans = x - d*k
else:
    ans = x - d*p
    k = k - p
    if (k%2 ==1):
        ans = abs(ans) - d
print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def main():
    X, K, D = map(int, sys.stdin.readline().rstrip().split())

    # 0方向に移動
    q, r = divmod(abs(X), D)

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

    K -= q

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


main()

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
from math import log

x, k, d = (int(x) for x in input().split())
if x < 0:
    x *= -1
logk = log(k, 10)
logd = log(d, 10)
if logk + logd <= 16 and x >= k * d:
    ans = x - k * d
    print(ans)
    exit()

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

Submission status: Accepted

Solution code:
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！

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

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]


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

x = abs(x)

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

tmp = k - x//d

if (tmp % 2 != 0):
    print(abs(x-x//d*d-d))
else:
    print(abs(x-x//d*d))

Submission status: Accepted

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
syo = X // D
#print(syo, amari)
if X <= K * D:
    K -= syo
    X -= D * syo
    if K % 2 == 0:
        print(X)
    else:
        print(abs(X - D))
else:
    print(X - K * D)

Submission status: Accepted

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

if X > D:
    t = min(X // D, K)
    K -= t
    X -= t * D
else:
    t = min(-X // D, K)
    K -= t
    X += t * D

K %= 2

if K == 0:
    print(abs(X))
else:
    if abs(X + D) < abs(X - D):
        print(abs(X + D))
    else:
        print(abs(X - D))

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if abs(x)>=k*d:
    ans=abs(x)-k*d
else:
    c=abs(x)//d#0に近づくまでの移動回数
    tmp1=abs(x)-c*d#c回移動時点での座標
    t=k-c#残り移動回数
    tmp2=tmp1-d
    #print(tmp1,tmp2)
    if t%2==0:
        ans=tmp1
    else:
        ans=abs(tmp2)


print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from bisect import bisect_left
from itertools import product
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def LF(): return list(map(float, input().split()))
def LC(): return [c for c in input().split()]
def LLI(n): return [LI() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]

def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)]

def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret


INF = float("inf")
MOD = 10**9 + 7


def main():
    X, K, D = MAP()

    if 0 <= X and 0 <= X - K*D:
        print(X-K*D)
        return
    if X <= 0 and X + K*D <= 0:
        print(abs(X+K*D))
        return

    if (X // D) % 2 == K % 2:
        print(X % D)
    else:
        print(abs(X % D-D))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

a = X//D

A = abs(X-D*a)
k = K - a

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

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

x,k,d = map(int, input().split())
#print([x,k,d])
if x > 0 and k * d <= x:
    print(x - k * d)
elif x < 0 and (k * d) <= -x:
    print(-x - k * d)
else:
    r = abs(x) % d
    s = abs(x) // d
    if (k - s)  % 2  == 0 : 
        print(r)
    else :
        print(d-r)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

k = min(K, X // D)
X -= k * D
K -= k

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

X = abs(X)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import floor, ceil

num_arr = input().split(" ")
X = int(num_arr[0])
K = int(num_arr[1])
D = int(num_arr[2])

if abs(X) >= K*D:  
    print(abs(X) - K*D)
else:
    steps_needed = floor(abs(X)/D)
    
    if (K - steps_needed)%2 == 0:
        print(abs(abs(X) - steps_needed * D))
    else:
        print(abs(abs(X) - (steps_needed+1)*D))