Slope Hack [new] May 2026

Slope trick code (C++):

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

What is the Slope Hack? The slope hack (also known as the "convex trick" or "slope trick" in competitive programming) is a technique for optimizing DP (Dynamic Programming) transitions of the form: Slope trick code (C++): [ dp[i] = \min_j

#include <bits/stdc++.h> using namespace std; int main() int n; cin >> n; using namespace std