Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
    ll x,k,d;
    cin >> x >> k >> d;
    ll a,b,time_a,time_b;
    if(k%2==0){
        if(x>=0){
            a=x%(2*d);
            b=a-2*d;
            time_a=(x-a)/(d);
            time_b=(x-b)/(d);
            if(abs(a)<=abs(b)){
                if(time_a<=k){
                    cout << abs(a) << endl;
                    return 0;
                }
                else{
                    cout << abs(x-d*k) << endl;
                    return 0;
                }
            }
            else{
                if(time_b<=k){
                    cout << abs(b) << endl;
                    return 0;
                }
                else if(time_a<=k){
                    cout << abs(a) << endl;
                    return 0;
                }
                else{
                    cout << abs(x-d*k) << endl;
                    return 0
                }
            }
        }
        else{
            b=(abs(x)%(2*d))*(-1);
            a=b+2*d;
            time_a=(a-x)/(d);
            time_b=(b-x)/(d);
            if(abs(b)<=abs(a)){
                if(time_b<=k){
                    cout << abs(b) << endl;
                    return 0;
                }
                else{
                    cout << abs(abs(x)-d*k) << endl;
                    return 0;
                }
            }
            else{
                if(time_a<=k){
                    cout << abs(a) << endl;
                    return 0;
                }
                else if(time_b<=k){
                    cout << abs(b) << endl;
                    return 0;
                }
                else{
                    cout << abs(abs(x) - d*k) << endl;
                }
            }
        }
    }
    else{
        if(x>=0){
            a=(x+d)%(2*d);
            b=a-2*d;
            if(x<d){
                time_a=1;
                time_b=1;
            }
            else{
                time_a=1+abs(x-d-a)/(d);
                time_b=time_a+2;
            }
            if(abs(a)<=abs(b)){
                if(time_a<=k){
                    cout << abs(a) << endl;
                    return 0;
                }
                else{
                    cout << abs(abs(x) - d*k) << endl;
                    return 0;
                }
            }
            else{
                if(time_b<=k){
                    cout << abs(b) << endl;
                    return 0;
                }
                else if(time_a<=k){
                    cout << abs(a) << endl;
                    return 0;
                }
                else{
                    cout << abs(abs(x) - d*k) << endl;
                    return 0;
                }
            }
        }
        else{
            b=((abs(x)+d)%(2*d))*(-1);
            a=b+2*d;
            if(x>-d){
                time_a=1;
                time_b=1;
            }
            else{
                time_b=1+(abs(x)-d-abs(b))/d;
                time_a=time_b+2;
            }
            if(abs(b)<=abs(a)){
                if(time_b<=k){
                    cout << abs(b) << endl;
                    return 0;
                }
                else{
                    cout << abs(abs(x)-x*d) << endl;
                    return 0;
                }
            }
            else{
                if(time_a<=k){
                    cout << abs(a) << endl;
                    return 0;
                }
                else if(time_b<=k){
                    cout << abs(b) << endl;
                    return 0;
                }
                else{
                    cout << abs(abs(x)-d*k) << endl;
                    return 0;
                }
            }
        }
    }

}

Compiler output:
a.cc: In function 'int main()':
a.cc:36:29: error: expected ';' before '}' token
   36 |                     return 0
      |                             ^
      |                             ;
   37 |                 }
      |                 ~