0w1

CFR Educational 15 D. Road to Post Office ( Ad hoc )

Problem - D - Codeforces
The key is to realize that there are only 3 possible optimal strategies.
1. Drive the car once, and walk for the rest.
2. Keep going with the car no matter what.
3. Drive until the remained distance is not worth to repair, so that last part should be finished with walking.

void solve(){
    ll D, K, A, B, T; cin >> D >> K >> A >> B >> T;
    if( K > D ) K = D;
    ll ans = K * A + ( D - K ) * B; // car once
    ans = min( ans, ( D - 1 ) / K * T + D * A ); // all car
    ans = min( ans, ( D / K - 1 ) * T + D / K * K * A + ( D % K ) * B ); // all car but last walk
    cout << ans << "\n";
}