atcoder#ABC303G. [ABC303G] Bags Game
[ABC303G] Bags Game
Score : points
Problem Statement
bags are arranged in a row. The -th bag contains yen (the currency in Japan).
Takahashi and Aoki, who have sufficient money, take alternating turns to perform the following action:
- choose one of the following three actions and perform it.- choose the leftmost or rightmost bag and take it.
- pay yen to Snuke. Then, repeat the following action times (where is the number of the remaining bags): choose the leftmost or rightmost bag and take it.
- pay yen to Snuke. Then, repeat the following action times (where is the number of the remaining bags): choose the leftmost or rightmost bag and take it.
- choose the leftmost or rightmost bag and take it.
- pay yen to Snuke. Then, repeat the following action times (where is the number of the remaining bags): choose the leftmost or rightmost bag and take it.
- pay yen to Snuke. Then, repeat the following action times (where is the number of the remaining bags): choose the leftmost or rightmost bag and take it.
When all the bags are taken, Takahashi's benefit is defined by "(total amount of money in the bags that Takahashi took) - (total amount of money that Takahashi paid to snuke)"; let this amount be yen. We similarly define Aoki's benefit, denoting the amount by yen.
Find if Takahashi and Aoki make optimal moves to respectively maximize and minimize .
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
5 10 2 1000000000 1
1 100 1 1 1
90
If Takahashi and Aoki make optimal moves, it ends up being and .
10 45 3 55 4
5 10 15 20 25 30 35 40 45 50
85
15 796265 10 165794055 1
18804175 185937909 1934689 18341 68370722 1653 1 2514380 31381214 905 754483 11 5877098 232 31600
302361955