Need help in Codeforces Carrot Cakes



I am having trouble in coming up with a solution for Carrot Cakes. The editorials are provided but I can’t understand how do we arrive at such a formula…


let’s say we don’t build second one.

Then it’s just simple to calculate TimeWithoutBuild using formula :

TimeWithoutBuild = ceil(n/k) t*

Now let’s build the second and calculate the TimeWithBuild:

as it take d unit time to build, we first add d to TimeWithBuild now we might have already baked some cakes while building the second one. So we need to subtract that cakes from n, so our remaining cakes will be *n-(d/t)k let’s say this is n_rem Now our speed of baking is doubled

TimeWithBuild = d + ceil(n_rem/(2*k)) * t

Now you will be good to code it yourself but nevertheless here is my solution…
Good Luck :stuck_out_tongue:

[details=Click to view]

using namespace std;
#define int long long
signed main(){
    int n,t,k,d;
    cin >> n >> t >> k >> d;
    int T1 =  t*( (n/k) + (n%k!=0));
    int T2 =  d + t*(( n-(d/t)*k )/(2*k) +(( n-(d/t)*k )%(2*k)!=0));
    if(T2 < T1)puts("YES");
    else puts("NO");
    return 0;


Thanks for replying… Can explain the last part i.e n-(d/t)*k in detail ? I don’t get it…