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: Wrong Answer

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

#x：現在地
#k：回数
#d:移動距離

idou = x // d

if idou > k:
  idou = k

minus = abs(x - d * idou)
plus = abs(x + d * idou)


if minus < plus:
  x = x - d * idou
else:
  x = x + d * idou

nokori = k - idou

nokori = nokori % 2

minus = abs(x - d * nokori)
plus = abs(x + d * nokori)

if minus < plus:
  x = x - d * nokori
else:
  x = x + d * nokori
  
print(abs(x))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cnt = x//d
mod = x%d
min = 0
if mod >= abs(mod-d):
    min = abs(mod-d)
    cnt += 1
else:
    min = mod

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

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

Submission status: Wrong Answer

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

a = -(-x // d)
b = x - k * d
b_ = x + k * d

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

Submission status: Wrong Answer

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

d = int(abs(X)/D)
if X > 0:
    r = X - d*D
    if abs(r-D) < abs(r):
        r = r-D
else:
    r = X + d*D
    if abs(r+D) < abs(r):
        r = r+D

min_count = int((X-r)/D)

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

Submission status: Wrong Answer

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

first=X//D
if K>first:
    if X>0:
        X=X-first*D
        if K-first%2==0:
            pass
        else:
            if X>0:
                X=X-D
            else:
                X=X+D
    else:
        X=X+first*D
else:
    if X>0:
        X=X-K*D
    else:
        X=X+K*D

print(abs(X))

Submission status: Wrong Answer

Solution code:
[x, k, d] = list(map(int, input().split()))
x = abs(x)
howMany = x // d
remainder = x % d
stepLeft = k - howMany
if k < howMany:
    print((howMany - k) * d)
elif stepLeft % 2 == 0:
    print(remainder)
elif stepLeft % 2 == 1:
    print(d - remainder)

Submission status: Wrong Answer

Solution code:
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
if abs(X)>K*D:
    print(min(abs(X-K*D), abs(X+K*D)))
else:
    N=abs(X)//D
    Min=10**16
    n=0
    for i in range(N-1, N+2):
        M=abs(X-i*D)
        if M<Min:
            Min=M
            n=i
    if (K-n)%2==0:
        print (abs(Min))
    else:
        print(min(abs(Min+D), abs(Min-D)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split(" "))
x = abs(x)
min_val = x
i = 0
while i <= k:
    val = abs(x + (i * d) - ((k-i) * d))
    if val < min_val:
        min_val = val
    i = i + 1
print(min_val)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
if k == 1:
    ans = min(abs(x-d),abs(x+d))
elif k % 2 == 0:
    ans = x
    tmp = [ans]
    if abs(x) == d:
        ans = x
    elif x > 0:
        ans = x
        for i in range(int(k/2)):
            ans = min(abs(ans),abs(ans-d))
            tmp.append(ans)
            if tmp[i-1] == tmp[i]:
                break
    elif x < 0:
        ans = x
        for i in range(int(k/2)):
            ans = min(abs(ans), abs(ans+d))
            tmp.append(ans)
            if tmp[i-1] == tmp[i]:
                break
elif k % 2 == 1:
    ans = min(abs(x-d),abs(x+d))
    tmp = [ans]
    if abs(x) == d:
        ans = 0
    elif ans > 0:
        for i in range(int((k-1)/2)):
            ans = min(abs(ans),abs(ans-d))
            tmp.append(ans)
            if tmp[i-1] == tmp[i]:
                break
    elif ans < 0:
        for i in range(int((k-1)/2)):
            ans = min(abs(ans), abs(ans+d))
            tmp.append(ans)
            if tmp[i-1] == tmp[i]:
                break

print(ans)

Submission status: Wrong Answer

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

if d * k >= abs(x):
  # 0超える1つ手前は？
  near_0 = x // d
  if x < 0:
    near_0 += 1
  if (k - abs(near_0)) %2 == 0:
    print(x - near_0 * d)
  else:
    hoge = x - near_0 * d + d
    huga = x - near_0 * d - d
    if abs(hoge) < abs(huga):
      print(abs(hoge))
    else:
      print(abs(huga))
else:
  if x == 0:
    if k%2 == 0:
      print(0)
    else:
      print(d)
  elif x > 0:
    print(x-(d*k))
  else:
    print(x+(d*k))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    a = [int(i) for i in input().split()]
    x1 = a[0] // a[2]

    if x1 > a[1]:
        print(abs(abs(a[0])-a[1]*a[2]))
    else:
        od = (a[1] -x1)%2
        if od == 0:
            if x1 - a[1] >2:
                print(min(
                    abs(abs(a[0]) - (x1-2) * a[2]),abs(abs(a[0]) - x1 * a[2]),abs(abs(a[0]) - (x1+2) * a[2])
                ))
            else:
                print(min(
                    abs(abs(a[0]) - x1 * a[2]),abs(abs(a[0]) - (x1-2) * a[2])
                ))
        else:
            if x1 - a[1] >3:
                print(min(
                    abs(abs(a[0]) - (x1-1) * a[2]),abs(abs(a[0]) - (x1+1) * a[2]),abs(abs(a[0]) - (x1+3) * a[2])
                ))
            elif x1 - a[1] >1:
                print(min(
                    abs(abs(a[0]) - (x1-1) * a[2]),abs(abs(a[0]) - (x1+1) * a[2])
                ))
            else:
                print(
                    abs(abs(a[0]) - (x1-1) * a[2])
                )

Submission status: Wrong Answer

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

tmp = [X]
min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)
    a = min([abs(X-D), abs(X+D)])
    if X in tmp:
        min_score = valid(a, min_score, K-i)
        break
    if min_score==abs(X):
        min_score = valid(a, min_score, K-i)
        break
    tmp.append(X)
print(min_score)

Submission status: Wrong Answer

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

mod1 = x % d
mod2 = mod1 - d
mod = min(mod1, abs(mod2))
modx = max(mod1, abs(mod2))
x1 = abs(x)

a = (x1-mod)/d

if mod1+k*d <= x1:
    print(x-k*d)
elif (k-a) % 2 == 0:
    print(mod)
else:
    print(modx)

Submission status: Wrong Answer

Solution code:
import sys

line = sys.stdin.readline()

arr = line.rstrip().split(" ")
x = int(arr[0])
k = int(arr[1])
d = int(arr[2])

max = x // d

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

maxv = x % d

if 0 == (k - max) % 2:
    print(maxv)
    sys.exit()


maxnv = d - maxv
print(maxnv)

Submission status: Wrong Answer

Solution code:
# 2020/08/15
# AtCoder Beginner Contest 175 - C

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

# Calc
# d * k < x -> 足りない場合
if d * k < x:
    # 足りない場合：
    ans = x - k * d
else:
    # 飛び越す場合：
    # 0に近い正の値までの回数
    wkc = x // d
    
    if k % 2 == 0:
        # kが偶数の場合：留まれるのは偶数回目
        if wkc % 2 == 0:
            ans = x % d
        else:
            ans = abs(x % d - d)
    else:
        # kが奇数の場合：留まれるのは奇数回目
        if wkc % 2 == 0:
            ans = abs(x % d - d)
        else:
            ans = x % d

# Output
print(ans)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

q, mod = divmod(abs(X), abs(D))

ans = 0
if q < K:
    if mod == 0:
        if q % 2 == 1:
            ans = D + mod
        else:
            ans = mod
    else:
        ans = mod
else:
    div = q - K
    ans = div * D + mod

print(ans)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
mind = 1000000000000000
c = 0
while k > 0:
    k -= 1
    if abs(x - d - 0) <= abs(x + d - 0):
        x -= d
    else:
        x += d
    mind = min(mind, x)
    if x == mind:
        break
if k%2 == 0 or k == 0:
    print(abs(mind - 0))
else:
    mind = min(abs(mind + d - 0), abs(mind - d - 0))
    print(mind)

Submission status: Wrong Answer

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

if K * D >= X:
    cgoal = X % D
    fgoal = abs(abs(cgoal) - D)
    times2cgoal = (X - cgoal) // D
    if times2cgoal % 2 == K % 2:
        print(min(cgoal, fgoal))
    else:
        print(fgoal)
else:
    print(X - K * D)

Submission status: Wrong Answer

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

init = x

for i in range(round(abs(x) // abs(d))):
    tempx = abs(x) + i*abs(d) - (k-i)*abs(d)
    init = min(init, abs(tempx))

print(init)

Submission status: Wrong Answer

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()
mmin=X-K*D
mmax=X+K*D
y=None
if X>=0:
    y=min(X%D,abs(X%D-D))
else:
    y=min(X%D,abs(X%D-D))
if mmin<y and y<mmax:
    if (abs(X-y)//D)%2==K%2:
        ans=abs(y)
    else:
        ans=min(abs(y-D),abs(y+D))
elif mmax<y:
    ans=abs(mmax)
else:
    ans=abs(mmin)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

Appr = X // D
Near = X - (X // D) * D

if (K - Appr) % 2 != 0:
    print(abs(Near - D))
else:
    print(abs(Near))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
for n in range(k):
  if x > 0:
    if x > d:
      x = x - d
    elif x <= d:
      if (k-n) % 2 == 0:
        print(x)
        break
      else:
        print(-(x-d))
        break
  elif x < 0:
    if -x < -d:
      x = x + d
    elif x >= d:
      if (k-n) % 2 == 0:
        print(-x)
        break
      else:
        print(x+d)
        break

Submission status: Wrong Answer

Solution code:

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

capa_iter = abs(x) // abs(d)
if capa_iter < k:
  if (k - capa_iter) % 2 == 0:
    print(abs(x) - capa_iter*d)
  else:
    print(min([abs(x - capa_iter*d-d),abs(x - capa_iter*d+d)]))
  exit()


  
ans = min([abs(x-d),abs(x+d)])

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

flag = 0
a = abs(X)
b = 0

b = abs(X)//D
if b <= K:
    K -= b
else:
    b = K
    K = 0
    
    
if X > 0:
    X -= D*b
    a = abs(X)
elif X < 0:
    X += D*b
    a = abs(X)
    
if K == 0:
    print(X)
else:
    if X == 0:
        if K % 2 == 0:
            print(X)
        else:
            print(D)
    elif X > 0:
        if abs(X-D) < a:
            X -= D
            a = abs(X)
            K -= 1
        if K % 2 == 0:
            print(abs(X))
        else:
            print(min(abs(X-D),abs(X+D)))
    elif X < 0:
        if abs(X+D) < a:
            X += D
            a = abs(X)
            K -= 1
        if K % 2 == 0:
            print(abs(X))
        else:
            print(min(abs(X-D),abs(X+D)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
li=  list(map(int,input().split()))	       
X, K, D=li[0], li[1], li[2]
if abs(X)>K*D:
    print(min(abs(X-K*D), abs(X+K*D)))
else:
    N=abs(X)//D
    Min=10**16
    n=0
    for i in range(N-1, N+2):
        M=abs(X-i*D)
        if M<Min:
            Min=M
            n=i
    if (K-n)%2==0:
        print (abs(Min))
    else:
        print(min(abs(Min+D), abs(Min-D)))
                  
N=X//D
Min=10**16
n=0
for i in range(K+1):
    M=abs(X-i*D)
    if M<Min:
        Min=M
        n=i
if (K-n)%2==0:
        print (abs(Min))
else:
    print(min(abs(Min+D), abs(Min-D)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

mini = math.ceil(X/D)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().strip().split())
a={}
if k!=1:
	for i in range(k):
	
		x=min(abs(x-d),abs(x+d))
		if x in a:
			break

		else:
			a[x]=1

	k=k%i
	k+=1


for i in range(k):
	x=min(abs(x-d),abs(x+d))



print(abs(x))

Submission status: Wrong Answer

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

if X >D*K:
    print(abs(X-D*K))
elif X< -D*K:
    print(abs(X+D*K))
    
if X>=0 and X<D*K:
    N = int(abs(X)/D)
    e = X - N*D
    if (K-N) % 2 == 0:
        print(abs(e))
    else:
        print(abs(e-D))

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

Submission status: Wrong Answer

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

distList = [abs(X)]
minV = abs(X)
for i in range(0, K+1):
    value = abs(X-i*D)
    if minV < value:
        break
    if (K-i) % 2 == 0:
        minV = X-i*D
        distList.append(value)
print(distList[-1])

Submission status: Wrong Answer

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

first=X//D
if K>first:
    if X>0:
        X=X-first*D
        if K-first%2==0:
            pass
        else:
            if X>0:
                X=X-D
            else:
                X=X+D
    else:
        X=X+first*D
else:
    if X>0:
        X=X-K*D
    else:
        X=X+K*D

print(X)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

for i in range(k):
    if x < 0:
        ans = min(abs(x),abs(x + d))
        i2 = i
        if i2 % 2 == k % 2:
            #print("a")
            print(abs(x))
            exit()
        else:
            #print("b")
            print(x + d)
            exit()
    x = x - d

Submission status: Wrong Answer

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

#最大まで移動するとき
if abs(X) >= K * D:
    print(abs(X) - K*D)
    exit()

if D > X:
    if X >= 0:
        nex = X - D
        bef = X

        if K % 2 == 0:
            print(abs(bef))
        else:
            print(abs(nex))
    else:
        nex = X + D
        bef = X

        if K % 2 == 0:
            print(abs(bef))
        else:
            print(abs(nex))
else: #D<=X
    if X >= 0:
        bef = X % D
        nex = bef - D
        
        if (K - X//D) % 2 == 0:
            print(abs(bef))
        else:
            print(abs(nex))
    else:
        bef = abs(X) % D
        nex = bef + D

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

Submission status: Wrong Answer

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

q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    if (K-q)%2 != 0:
        a1 = mod - D
        a2 = mod + D
        ans = min(abs(a1),abs(a2))
    else:
        ans = mod

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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)))

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

capa_iter = abs(x) // abs(d)
if capa_iter < k:
  if (k - capa_iter) % 2 == 0:
    print(x - capa_iter*d)
  else:
    print(min([abs(x - capa_iter*d-d),abs(x - capa_iter*d+d)]))
  exit()


  
ans = min([abs(x-d),abs(x+d)])

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
cnt = x//d
mod = x%d
min = 0
if mod >= abs(mod-d):
    min = abs(mod-d)
    cnt += 1
else:
    min = mod

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

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