Slope Hack Portable

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

[ dp[i] = \min_j < i (dp[j] + |a_i - a_j| \cdot C + \textconstant) ] slope hack

| Operation | Effect on function | Slope trick implementation | |-----------|-------------------|----------------------------| | Add ( |x - k| ) | Slope increases by 1 for (x > k), decreases by 1 for (x < k). | Insert (k) into both heaps, then balance. | | Add constant (c) | Shift function up/down | Just add to answer offset. | | Shift entire function right by (t) | All breakpoints increase by (t) | Add (t) to a global offset. | | Get minimum value | (f(\textmedian)) | Store answer offset. | #include &lt;bits/stdc++