1.3 Saee’s Path to Prosperity
Centuries ago, the desert kingdom of Navrata was famous for its vast trade routes. Along one such route, there are n oases, each at a fixed position from the start of the route. Every oasis has a certain amount of resources stored, represented as an integer value.
Saee, a merchant caravan leader, wants to choose two oases as her main trade hubs. The profit she can earn from transporting goods between these two hubs depends on:
The distance between the two oases (in route units).
The smaller of the two resource values at those oases (since the weaker hub limits the maximum goods that can be moved).
The profit from selecting two hubs is calculated as:
distance_between_oases × minimum_resource_value_of_the_two
Your task is to determine the maximum profit Saee can earn by choosing any two oases as hubs.
Constraints
Input Format
First line: integer n — number of oases.
Second line: n space-separated integers, where the i-th number ni is the resource value at the i-th oasis.
Output Format
A single integer — the maximum profit possible.
Example 1
Input:
9
1 8 6 2 5 4 8 3 7
Output:
49
Explanation:
If the caravan chooses the oasis at position 1 (resources = 8) and the oasis at position 8 (resources = 7):
Distance = 8 - 1 = 7
Minimum resources between them = 7
Profit = 7 × 7 = 49
Log In to solve the Question