#K36002. Minimize Maintenance Cost
Minimize Maintenance Cost
Minimize Maintenance Cost
You are given n maintenance points arranged in a line. Each point is associated with a maintenance cost. Your task is to determine the minimum cost according to the following constraint: you are not allowed to remove both endpoints simultaneously, i.e. at least one of the endpoints must remain.
The input consists of an integer n and a list of n integers representing the maintenance costs of the corresponding points. Based on a specific strategy, compute the minimum maintenance cost ensuring that at least one endpoint (first or last element) is kept.
The solution uses a strategy that, for n = 2, directly returns the minimum cost (since both points are endpoints), and for n > 2, it computes a candidate cost based on the relative values of the two endpoints and then returns the overall minimum among this candidate and the endpoints. In mathematical terms, if we let \( a_1, a_2, \dots, a_n \) be the costs, then the answer is computed as follows:
\[ \text{answer} = \min\Big(\min(C),\, a_1,\, a_n\Big) \]
where \(C\) is either \(\{a_1, a_2, \dots, a_{n-1}\}\) or \(\{a_2, a_3, \dots, a_n\}\) depending on whether \(a_n \ge a_1\) or not.
inputFormat
The first line of input contains a single integer n (the number of maintenance points).
The second line contains n space-separated integers where the i-th integer represents the maintenance cost of the i-th point.
outputFormat
Output a single integer, the minimum maintenance cost following the rule that at least one of the endpoints must remain.
## sample5
3 1 4 1 5
1