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.

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

//
// ABC175C
// URL : https://atcoder.jp/contests/abc175/tasks/abc175_c
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace AtCoder.Template.ABC175C
{
    class Solver
    {
        public static readonly long MOD_CONST = 1000000007;//(long)Math.Pow(10, 9) + 7;

        public static void Main(string[] args)
        {
            //出力高速化
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            var solver = new Solver();
            solver.Solve();
            Console.Out.Flush();
        }
        public void Solve()
        {
            var input = Console.ReadLine().Split(' ').Select(_inputStr => long.Parse(_inputStr)).ToArray();
            long X = input[0];//最初の位置 7
            long K = input[1];//移動回数 4
            long D = input[2];//1度の移動距離 3
            long ans = 0;
            {
                //Console.WriteLine( 7 % 3);// 1
                //Console.WriteLine(-7 % 3);//-1
                //Console.WriteLine( 7 / 3);// 2
                //Console.WriteLine(-7 / 3);//-2


                //XからDだけ移動、Kの制限なしで原点近くに行くとしたら何回かかる?
                long ifK = Math.Abs(X / D);
                if(ifK < K)
                {
                    //まだ回数は余る。原点付近をうろつく
                    //残り回数
                    long remainK = K - ifK;//>= 1
                    //今の位置
                    long pos = X % D;
                    //一度だけ移動した次の移動位置
                    long nextPos = (pos > 0) ? pos - D : pos + D;

                    //どちらかが目的地候補。行けるのは残り回数の偶奇で決定
                    if (remainK % 2 == 0)
                    {
                        ans = Math.Abs(pos);
                    }
                    else
                    {
                        ans = Math.Abs(nextPos);
                    }


                }
                else
                {
                    //原点付近移動途中で終わるパターン
                    //K回制限内でできる限り原点に近づく
                    long _X = Math.Abs(X);//こうなるとXの+/-は関係ない
                    ans = _X - K * D;
                }
            }
            Console.WriteLine(ans);
        }

        //---------------------------------------------------------------------------------
        #region MyLib
        /// <summary>
        /// string -> List<char>に分解する。1文字がListの1要素
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static List<char> separeteToList(string a)
        {
            var l = new List<char>(a.Length);
            foreach (char x in a)
            {
                l.Add(x);
            }
            return l;
        }
        /// <summary>
        /// List<char> -> stringに合成する
        /// </summary>
        /// <param name="l"></param>
        /// <returns></returns>
        public static string combineCharList(List<char> l)
        {
            var ans = new StringBuilder();
            foreach (char x in l)
            {
                ans.Append(x);
            }
            return ans.ToString();
        }

        /// <summary>
        /// Aの文字をなるべくCorrectに合うように並びかえる。当然Correctにしかない文字もあるので、その場合はAの適当な文字で置き換える
        /// 例)
        /// A       = abcde, eppstein
        /// Corrent = acfbe, pipeline
        /// ans     = acdbe, pspetine
        /// </summary>
        /// <param name="A"></param>
        /// <param name="Correct"></param>
        public static List<char> FillChar(List<char> A, List<char> Correct)
        {
            char Empty = ' ';
            var ARemain = new List<char>(A);
            var ans = new List<char>(A.Count);
            for (int i = 0; i < Correct.Count; ++i)
            {
                //Aの文字をなるべくCorrectに合うように、ansの文字を決める
                int foundPos = ARemain.FindIndex(x => x == Correct[i]);
                if (foundPos != -1)//文字がCorrectの中で見つかったら
                {
                    char found = ARemain[foundPos];
                    ans.Add(found);
                    ARemain.Remove(found);//使った文字は除外
                }
                else//Aにない文字もある。後で使わなかったAの適当な文字で埋める
                {
                    ans.Add(Empty);
                }
            }
            //Aにない文字がCorrectにあったら、Aの残りの文字で適当に埋める
            for (int i = 0; i < A.Count; ++i)
            {
                if (ans[i] == Empty)
                {
                    ans[i] = ARemain.First();
                    ARemain.RemoveAt(0);
                }
            }
            return ans;
        }


        /// <summary>
        /// リストから、重複したものを除外したリストを新規に作成する
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="src"></param>
        /// <param name="dst"></param>
        public static void getDistinctList<T>(List<T> src, out List<T> dst)
        {
            var iter = src.Distinct();
            dst = new List<T>();
            foreach (var item in iter)
            {
                dst.Add(item);
            }
        }

        /// <summary>
        /// グラフ系
        /// </summary>
        public class Node
        {
            public Node(long val){
                this.Value = val;
                this.Children = new List<Node>();
            }

            public Node(long val, List<Node> children)
            {
                this.Value = val;
                this.Children = children;
            }

            public long Value { get; set; }
            public List<Node> Children { get; set; }
        }

        /// <summary>
        /// Clamping
        /// </summary>
        /// <typeparam name="T">比較型</typeparam>
        /// <param name="x">clampしたい値</param>
        /// <param name="min">最小値</param>
        /// <param name="max">最大値</param>
        /// <returns>clamp済みの値</returns>
        public static T Clamp<T>(T x, T min, T max) where T : IComparable<T>
        {
            if (x.CompareTo(min) < 0) { return min; }
            else if (x.CompareTo(max) > 0) { return max; }
            else return x;
            //return Math.Max(Math.Min(x, max), min);
        }

        /// <summary>
        /// 指定された底log_baseでの, 対数を計算
        /// </summary>
        /// <param name="x"></param>
        /// <param name="log_base">指定の底</param>
        /// <returns></returns>
        public static double LogBase(double x, double log_base)
        {
            //log_b(N) = log_a(N) / log_a(B)より
            return Math.Log(x) / Math.Log(log_base);
        }

        /// <summary>
        /// 初項a, 公差d, 項数nの数列の和
        /// </summary>
        /// <param name="n">求めたいところ</param>
        /// <returns>初項からxまで足した値</returns>
        static long SumSeries(long a, long d, long n)
        {
            return (2 * a + (n - 1) * d) * n / 2;
        }
        /// <summary>
        /// 累積和の配列を計算する
        /// Sum[ a[0], a[i] )がs[i+1]に入っている
        /// s[0]  : 0                //一切和を取らない場合
        /// s[1]  : ls[0]            //a[0]   + s[0] = a[0]
        /// s[2]  : ls[1] + s[1]     //a[1]   + a[0]
        /// ...
        /// s[N]  : ls[N-1] + s[N-1] //a[N-1] + ... + a[0]
        /// s[N+1]: ls[N] + s[N]     //a[N]   + ... + a[0]
        /// 
        /// 例: [1, 7)の和(a[1] + ... + a[6])
        /// s[1] = a[0] //[0, 1)
        /// s[7] = a[0] + a[1] + ... + a[6] //[0, 7)
        /// よってs[7] - s[1]);
        /// </summary>
        /// <param name="ls">計算したい配列</param>
        /// <returns>累積和配列</returns>
        static List<long> AggregateSumList(List<long> a)
        {
            var s = new List<long>(a.Count + 1);
            if (a.Count == 0)
            {
                return s;
            }
            s.Add(0);//s[0] = 0
            for (int i = 0; i < a.Count; ++i)
            {
                s.Add(a[i] + s[i]);
            }
            return s;
        }


        /// <summary>
        /// 与えられた数が素数かどうかを計算する
        /// </summary>
        /// <param name="num">対象数</param>
        /// <returns>true:素数, false:素数ではない(合成数)</returns>
        public static bool isPrime(int num)
        {
            if (num < 2) return false;
            else if (num == 2) return true;
            else if (num % 2 == 0) return false;

            double sqrtNum = Math.Sqrt(num);
            for (int i = 3; i <= sqrtNum; i += 2)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 素因数分解
        /// </summary>
        /// <param name="n"></param>
        /// <returns>keyが素数、valueがその素数の登場回数</returns>
        public static Dictionary<long, long> PrimeFactorization(long n)
        {
            var result = new Dictionary<long, long>();
            //初めの素数2から割っていく
            for (long p = 2; p * p <= n; p++)
            {
                //割り切れないなら、素因数に含まれてない
                if (n % p != 0) continue;
                long count = 0;

                // 割り続ける
                while (n % p == 0)
                {
                    count++;
                    n /= p;
                }

                result[p] = count;
            }

            // 素数が残っている場合
            if (n != 1) result[n] = 1;
            return result;
        }


        /// <summary>
        /// 順列mPnを計算する
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static long Permutation(long m, long n)
        {
            long ret = 1;
            for (var i = 0; i < n; ++i)
            {
                ret *= m - i;
            }
            return ret;
        }
        /// <summary>
        /// 組み合わせmCnを計算する
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static long Combination(long m, long n)
        {
            if (m == n || n == 0)
            {
                return 1;
            }
            long ans = 1;
            for (var i = 1; i <= n; ++i)
            {
                ans = ans * (m - i + 1) / i;
            }
            return ans;
        }

        static long Gcd(long m, long n)
        {
            if (n == 0) return m;
            return Gcd(n, m % n);
        }

        static long Lcm(long m, long n)
        {
            var gcd = Gcd(m, n);
            //return (m * n) / gcd;
            return m / gcd * n;//桁あふれ対策
        }
        #endregion MyLib
    }
}