#include #include #include /** A rather insane (and wrong) solution to the problem from a popular online chatbot. An excerpt from its manifesto about the concept: 3. An Efficient O(n) Solution The bottleneck in the brute-force approach is re-calculating the maximum of the "other" legs every time. We can optimize this by pre-computing the necessary information. The key insight is that for each removed stop s_i, the maximum leg length in the new itinerary will be the greater of two values: The length of the new, merged leg: s_{i+1} - s_{i-1}. The maximum length among all the other, unaffected original legs. Finding the maximum of the unaffected legs for each i can be done quickly if we pre-calculate prefix and suffix maximums on the original list of leg distances This approach involves a few initial passes over the data (each taking $O(n)$ time) and then a final loop that performs $O(1)$ work for each of the $n-2$ possible stops to remove. The total time complexity is $O(n)$, and the space complexity is $O(n)$ for the auxiliary arrays. This is efficient enough for the given constraints. **/ int main() { // Fast I/O std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector s(n); for (int i = 0; i < n; ++i) { std::cin >> s[i]; } // 1. Calculate original leg distances std::vector dists(n - 1); for (int i = 0; i < n - 1; ++i) { dists[i] = s[i + 1] - s[i]; } // 2. Pre-compute prefix maximums std::vector prefix_max(n - 1); prefix_max[0] = dists[0]; for (int i = 1; i < n - 1; ++i) { prefix_max[i] = std::max(prefix_max[i - 1], dists[i]); } // 3. Pre-compute suffix maximums std::vector suffix_max(n - 1); suffix_max[n - 2] = dists[n - 2]; for (int i = n - 3; i >= 0; --i) { suffix_max[i] = std::max(suffix_max[i + 1], dists[i]); } long long min_max_dist = -1; // 4. Iterate through each removable stop s_i (1-indexed i=2 to n-1) // This corresponds to 0-indexed k=1 to n-2. // Removing s[k] merges dists[k-1] and dists[k]. for (int k = 1; k < n - 1; ++k) { // The new, merged leg long long new_leg = s[k + 1] - s[k - 1]; // The maximum of the unaffected legs long long max_other_legs = 0; // Handle edge cases for finding max_other_legs bool has_prefix = (k - 2 >= 0); bool has_suffix = (k + 1 < n - 1); if (has_prefix && has_suffix) { max_other_legs = std::max(prefix_max[k - 2], suffix_max[k + 1]); } else if (has_prefix) { max_other_legs = prefix_max[k - 2]; } else if (has_suffix) { max_other_legs = suffix_max[k + 1]; } // If n=3, there are no "other" legs, max_other_legs remains 0. long long current_max = std::max(new_leg, max_other_legs); if (min_max_dist == -1 || current_max < min_max_dist) { min_max_dist = current_max; } } std::cout << min_max_dist << std::endl; return 0; }