#abc303g. G - Bags Game

G - Bags Game

Score : 550550 points

问题描述

设有 NN 个袋子排成一行。第 ii 个袋子里装有 xix_i 日元(日本货币)。

拥有足够资金的高桥和青木轮流执行以下动作:

  • 从以下三个动作中选择一个并执行:
    • 选择最左边或最右边的袋子并拿走它。
    • 向 Snuke 支付 AA 日元。然后,重复以下动作 min(B,n)\min(B,n) 次(其中 nn 是剩余袋子的数量):选择最左边或最右边的袋子并拿走它。
    • 向 Snuke 支付 CC 日元。然后,重复以下动作 min(D,n)\min(D,n) 次(其中 nn 是剩余袋子的数量):选择最左边或最右边的袋子并拿走它。

当所有袋子都被拿走时,定义高桥的收益为“高桥拿走的袋子中的总金额 - 高桥支付给 Snuke 的总金额”;该金额记作 XX 日元。类似地,我们定义青木的收益,并用 YY 日元表示这个金额。

若高桥和青木分别采取最优策略以最大化和最小化 XYX-Y,求 XYX-Y 的值。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

NN bags are arranged in a row. The ii-th bag contains xix_i 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 AA yen to Snuke. Then, repeat the following action min(B,n)\min(B,n) times (where nn is the number of the remaining bags): choose the leftmost or rightmost bag and take it.
    • pay CC yen to Snuke. Then, repeat the following action min(D,n)\min(D,n) times (where nn 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 XX yen. We similarly define Aoki's benefit, denoting the amount by YY yen.

Find XYX-Y if Takahashi and Aoki make optimal moves to respectively maximize and minimize XYX-Y.

Constraints

  • 1N30001 \leq N \leq 3000
  • 1xi1091 \leq x_i \leq 10^9
  • 1A,C1091 \leq A,C \leq 10^9
  • 1B,DN1 \leq B,D \leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN AA BB CC DD

x1x_1 x2x_2 \ldots xNx_N

Output

Print the answer.

Sample Input 1

5 10 2 1000000000 1
1 100 1 1 1

Sample Output 1

90

If Takahashi and Aoki make optimal moves, it ends up being X=92X=92 and Y=2Y=2.

Sample Input 2

10 45 3 55 4
5 10 15 20 25 30 35 40 45 50

Sample Output 2

85

Sample Input 3

15 796265 10 165794055 1
18804175 185937909 1934689 18341 68370722 1653 1 2514380 31381214 905 754483 11 5877098 232 31600

Sample Output 3

302361955

update @ 2024/3/10 08:32:30