In a bustling medieval town, Arwa stumbled upon a lively carnival, its tents and banners fluttering in the breeze. Amidst the crowd’s chatter and laughter, she spotted a peculiar stall adorned with colorful balloons and mysterious prizes. Curiosity piqued, she approached the stall, where a wizened carnival master awaited. “Welcome, fair maiden, to the Pop! Shop” he proclaimed, gesturing towards a row of balloons, each bearing its own enigmatic value, .
“In this grand spectacle, ye shall wield special darts, each endowed with magical properties.“, the carnival master explained, “With these darts, ye can perform daring feats to pop the balloons and earn points.” He continued, “Be wary maiden, you can leave a balloon untouched, but each balloon can be popped only once. And the more points ye amass, the grander the reward!”. With a twinkle in her eye, Arwa listened intently as the carnival master detailed the ways she could wield her dart:
The Single Shot: Arwa could aim her dart at a single balloon, popping it to earn its value ;
The Double Strike: By targetting two adjacent balloons simultaneously - the -th and -th balloons - Arwa’s magical dart, when thrown, would unleash double darts popping the two balloons, earning the product of their values ;
The Spectacular Sweep: Should Arwa choose, she could aim her magical dart in a way that hits two different ballons and , earning the value . But she needs to keep two things in mind when using this move:
Sweep on balloons and can be performed only if all balloons between them, from to , had been popped earlier.
In case she decides to perform multiple sweeps, their ranges cannot be disjoint. Given Sweep 2 (involving balloons and ) is performed after Sweep 1 (involving balloons and ), Arwa needs to maintain and . This should be followed for all subsequent cases of sweeps.
Eager to embark on this daring adventure, Arwa faced the challenge: given the number of balloons and their associated values, she sought to devise a strategy to maximize her score. With determination in her heart, Arwa pondered her options, ready to seize the opportunity and claim the greatest prize the carnival had to offer.
The first line of the input contains a single integer denoting the number of balloons.
The following line contains space-separated integers, indicating the value associated with each balloon.
Print a single integer denoting the maximum score.
Input | Output |
---|---|
3 4 5 3 | 23 |
Arwa can use the Double Strike to pop the first two balloons getting points. Then she can use the Single Shot to pop the third balloon to get , totalling points. |
Input | Output |
---|---|
7 9 5 15 20 13 17 30 | 932 |
Arwa can perform a Single Shot to get and use the Double Strike to get . Then she can perform the Spectacular Sweep to get and finally use the Double Strike to get . Note that, if she were allowed to perform disjoint sweeps, she could get , but the carnival master will not allow it. |