Comments

The sign of c does not matters. The only thing that matters is the sign of $$$(cx + d)$$$ because what we want to know is $$$\sum |c_ix + d_i|$$$.

We know that $$$|c_ix + d_i| = c_ix + d_i$$$ when $$$c_ix + d_i \geq 0$$$ are hold, and $$$|c_ix + d_i| = -c_ix -d_i $$$ when $$$c_ix + d_i \lt 0$$$ are hold.

If $$$c_i$$$ is positive, the inequality $$$c_ix + d_i \geq 0$$$ holds when $$$x \geq -\frac{d_i}{c_i}$$$, but if $$$c_i$$$ is negative, it will holds when $$$x \leq -\frac{d_i}{c_i}$$$

To avoid this, we found that $$$|c_ix + d_i| == |-c_ix - d_i|$$$, so we can change $$$|c_ix + d_i|$$$ into $$$|-c_ix - d_i|$$$ if $$$c_i$$$ is negative.

After doing this, we can sort all $$$c_i, d_i$$$ in the order of $$$\frac{d_i}{c_i}$$$. After doing this, we can used binary search to find a position that for all $$$c_i, d_i$$$ before it $$$c_ix + d_i \geq 0$$$ and for all $$$c_i, d_i$$$ after it $$$c_ix + d_i \lt 0$$$

You can change c into positive because $$$ |cx + d| $$$ = $$$ |-cx - d| $$$. Although he didn't write it in the tutorial, you can see it in his code

JeevanJyot I think this might be a mistake in your tutorial. I see that you change $$$ |cx + d| $$$ into $$$ |-cx - d| $$$ if c is negative in your code, you should write it in your tutorial.

I've got a little confusion according to tutorial of problem E.

$$$ |cx + d| $$$ = $$$ cx + d $$$ exist when $$$ cx + d \geq 0 $$$

which is $$$ cx \geq -d $$$.

When $$$ c \gt 0 $$$, the range of x will be $$$ x \geq -\frac{d}{c} $$$

When $$$ c \lt 0 $$$, the range of x will be $$$ x \leq -\frac{d}{c} $$$

But in the tutorial, it says $$$ |cx + d| = cx + d $$$ $$$ x \geq -\frac{d}{c}$$$

Could anyone explain it to me?

I think there might be some mistakes according to tutorial of problem F. In the tutorial it said: "However, the tricky case is to deal with cells with 0 unmarked neighbors". But it is obvious that cells with 0 unmarked neighbors will get the value of 0 in the end. So what is really tricky to deal with is those cells with 4 unmarked neighbors. I think the writer might confuses the definitions of marked and unmarked cell.