atcoder#KEYENCE2021E. Greedy Ant
Greedy Ant
Score : points
Problem Statement
There are candies on a number line. The -th candy from the left is at the position and has the tastiness of . Here, it is guaranteed that the tastinesses of all candies are distinct.
Snuke and Ant have decided to play a game of alternately taking one candy. At the beginning of the game, Ant chooses one of the positions and stands there.
Snuke goes first. In his turn, he can choose any one candy and get it.
In Ant's turn, she chooses the candy with the greater tastiness of the following two: the nearest candy to the left of her and the nearest candy to the right of her. If there are candies in only one direction, she chooses the nearest one.
The game ends when no candies are left. For each of the positions , find the maximum possible sum of the tastinesses of candies Snuke takes when Ant chooses that position.
Constraints
- All values in input are integers.
- are distinct.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the maximum possible sum of the tastinesses of candies Snuke takes when Ant initially stands at the position .
7
4 3 1 2 1000 2000 3000
6004
6004
6004
6001
5007
4007
4007
4007
- We will explain one optimal strategy for Snuke when Ant initially stands at the position .- Snuke chooses the candy of tastiness .
- The candies to the left and right of Ant have the tastinesses of and , respectively. She chooses the one with the greater tastiness, that is, the one with tastiness .
- Snuke chooses the candy of tastiness .
- The candies to the left and right of Ant have the tastinesses of and , respectively. She chooses the one with the greater tastiness, that is, the one with tastiness .
- Snuke chooses the candy of tastiness .
- There is no candy to the left of Ant, and the candy to the right of Ant has the tastiness of . She chooses this candy with tastiness .
- Snuke chooses the candy of tastiness .
- The sum of the tastinesses of candies Snuke has taken is . There is no way to take candies that results in a greater total tastiness.
- Snuke chooses the candy of tastiness .
- The candies to the left and right of Ant have the tastinesses of and , respectively. She chooses the one with the greater tastiness, that is, the one with tastiness .
- Snuke chooses the candy of tastiness .
- The candies to the left and right of Ant have the tastinesses of and , respectively. She chooses the one with the greater tastiness, that is, the one with tastiness .
- Snuke chooses the candy of tastiness .
- There is no candy to the left of Ant, and the candy to the right of Ant has the tastiness of . She chooses this candy with tastiness .
- Snuke chooses the candy of tastiness .
- The sum of the tastinesses of candies Snuke has taken is . There is no way to take candies that results in a greater total tastiness.
40
45651 92206 55173 24815 34809 73343 60978 57984 6919 89624 19693 30037 87070 6713 65976 37597 51929 93304 70911 7343 65414 38977 47998 52123 53590 35714 59319 50872 53850 40991 85668 8808 32846 70831 3416 42173 89538 73410 21502 69631
1416699
1416699
1416699
1416699
1413888
1410894
1410894
1410894
1413888
1413888
1413888
1413888
1413888
1413888
1419943
1419943
1419943
1400961
1400961
1400961
1419943
1419943
1419943
1419749
1419749
1419749
1419749
1419749
1419749
1419749
1419749
1419749
1419943
1419943
1419943
1419943
1398462
1398462
1398462
1402241
1402241