#P3628. [APIO2010] 特别行动队

    ID: 1806 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2010单调队列APIO斜率优化前缀和队列

[APIO2010] 特别行动队

Description

You command a unit consisting of nn reserve soldiers, numbered from 11 to nn. You need to split them into several commando teams to be deployed to the battlefield. For teamwork reasons, the indices of the members within the same commando team must be consecutive, i.e., a sequence of the form (i,i+1,,i+k)(i, i + 1, \cdots,i + k). Every soldier must belong to exactly one commando team.

The initial combat power of soldier ii is xix_i. The initial combat power XX of a commando team is the sum of the initial combat power of its members, i.e., X=xi+xi+1++xi+kX = x_i + x_{i+1} + \cdots + x_{i+k}.

From long-term observation, you have summarized that for a commando team with initial combat power XX, its adjusted combat power is X=aX2+bX+cX' = aX^2 + bX + c, where a,b,ca, b, c are known coefficients (a<0a < 0). As the commander, you must form the teams so that the sum of adjusted combat power over all commando teams is maximized. Find this maximum sum.

Input Format

The first line contains an integer nn, the number of soldiers.

The second line contains three space-separated integers, a,b,ca, b, c, which are the coefficients of the adjusted combat power formula.

The third line contains nn space-separated integers, where the ii-th integer is the initial combat power xix_i of soldier ii.

Output Format

Output a single integer on one line, the maximum possible sum of adjusted combat power over all commando teams.

4 
-1 10 -20 
2 2 3 4 
9

Hint

Explanation of Sample Input/Output 1

You have 44 soldiers, x1=2, x2=2, x3=3, x4=4x_1 = 2,~x_2 = 2,~x_3 = 3,~x_4 = 4. The parameters in the adjusted combat power formula are a=1, b=10, c=20a = -1,~b = 10,~c = -20.

The optimal plan is to form 33 commando teams: the first team contains soldiers 11 and 22, the second team contains soldier 33, and the third team contains soldier 44. The initial combat powers of the teams are 4, 3, 44,~3,~4, and the adjusted combat powers are 42+10×420=4-4^2 + 10 \times 4 - 20 = 4, 32+10×320=1-3^2 + 10 \times 3 - 20 = 1, 42+10×420=4-4^2 + 10 \times 4 - 20 = 4. The sum of adjusted combat powers is 4+1+4=94 + 1 + 4 = 9, and no other plan yields a larger sum.

Constraints

For 20%20\% of the testdata, n103n \leq 10^3.

For 50%50\% of the testdata, n104n \leq 10^4.

For 100%100\% of the testdata, 1n1061 \leq n \leq 10^6, 5a1-5 \leq a \leq -1, 107b107-10^7 \leq b \leq 10^7, 107c107-10^7 \leq c \leq 10^7, 1xi1001 \leq x_i \leq 100.

Translated by ChatGPT 5