#include #include #include #include #include using namespace std; #define ll long long # updated ll minCostProduct(const vector> &seg) { int n = seg.size(); // Sort by starting endpoint (L) vector> sortedL = seg; sort(sortedL.begin(), sortedL.end(), [](const vector &a, const vector &b) { if (a[0] != b[0]) return a[0] < b[0]; return a[1] < b[1]; }); // Sort by ending endpoint (R) vector> sortedR = seg; sort(sortedR.begin(), sortedR.end(), [](const vector &a, const vector &b) { if (a[1] != b[1]) return a[1] < b[1]; return a[0] < b[0]; }); ll minCost = LLONG_MAX; // Minimum cost of valid non-overlapping segments ll minProd = LLONG_MAX; // Minimum product of costs for valid pairs int r = 0; // Iterate over sortedL (by left endpoint) for (const auto &cur : sortedL) { // Check for valid non-overlapping segments (sortedR's R < cur.L) while (r < n && sortedR[r][1] < cur[0]) { minCost = min(minCost, sortedR[r][2]); // sortedR[r][2] is the cost ++r; } // Calculate the product of costs if a valid segment is found if (minCost != LLONG_MAX) { ll prod = minCost * cur[2]; minProd = min(minProd, prod); } } // Return the result return (minProd != LLONG_MAX) ? minProd : -1; // Return -1 if no valid pair is found }