Imagine you're a master gardener tasked with creating the most beautiful garden possible! ๐ธ
You have a garden of n flowers arranged in a line, where each flower has an integer beauty value. Your goal is to create a valid garden by potentially removing some flowers, then calculate the maximum possible total beauty.
What makes a garden valid?
- It must have at least 2 flowers
- The first and last flowers must have the same beauty value
You're given an array flowers where flowers[i] represents the beauty of the i-th flower. You can remove any flowers you want (or none at all) to maximize the beauty sum of the remaining valid garden.
Example: If you have flowers [1, 2, 3, 1, 5], you could keep the subsequence [1, 2, 3, 1] (beauty sum = 7) or [1, 5, 1] by rearranging... wait, no rearranging allowed! You must maintain the original order.
Input & Output
Constraints
- 2 โค flowers.length โค 105
- -104 โค flowers[i] โค 104
- The garden must have at least 2 flowers
- First and last flowers must have the same beauty value