Description
Little H has recently been fascinated by a split-sequence game. In this game, small H takes a long
A non-negative integer sequence of degrees N is divided into k+l non-empty subsequence sequences. In order to get the K+l subsequence,
The small H will repeat the following steps seven times:
1. Small h first selects a sequence with a length of more than 1 (at the beginning of the small h only a length of n
Sequence one by one is the whole sequence obtained at the beginning);
2. Select a location and divide the sequence into contiguous two non-empty new
Sequence.
After each of these steps, small h will get a certain score. This is a score of two new sequences
The product of the elements in the column. Small h wants to choose an optimal partitioning scheme, so that after K-wheel (TH),
The total score for small h is the largest.
Input
The first line of the input file contains two integers n and Guineas (K+1≤n).
The second line contains n non-negative integers a1,n2 .... , an (0≤ai≤10^4), which represents the beginning of a small h
to the sequence.
Output
A row contains an integer that is the maximum score a small h can get.
Sample Input7 3
4 1 3 4 0 2 3
Sample Output108
HINT
"Sample description"
In the example, small H can get 108 points with the following 3-wheel operation:
1. -Start small H has a sequence (4,1,3,4,0,2,3). Small H selects the position after the 1th number
Divide the sequence into two parts and get the 4x (1+3+4+0+2+3) =52 points.
2. At the beginning of this round, small H has two sequences: (4), (1,3,4,0,2,3). Small h selection in 3rd number
The position after the word divides the second sequence into two parts and gets (1+3) x (4+0+2+
3) =36 points.
3. At the beginning of this round, small H has three sequences: (4), (1,3), (4,0,2,3). Small h selection in 5th
The position after the number divides the third sequence into two parts and gets (4+0) x (2+3) =
20 points.
After the three-wheeled operation, the small H will get four sub-sequences: (4), (1,3), (4,0), (2,3) and a total of 52+36+20=108 points.
"Data size and scoring"
: Data meets 2≤n≤100000,1≤k≤min (N-1,200).
This preface prefix and S, current state f[i], then the state transition equation is F[i]=max (f[j]+s[j]* (S[i]-s[j])), if pushed to one step, the problem is almost solved.
Using the technique of slope optimization and then adding a scrolling array, it is possible to have a very suspended AC (slope-optimized DP is a semi-positive solution) ~ ~ ~
1 A's Oh ~, the long 26 good egg ache ~
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=100010;6 Long Longdp[maxn][2];7 Long LongS[MAXN];8 intQ[maxn],front,back;9 intMain () {Ten intn,k; Onescanf"%d%d", &n,&k); k++; A for(intI=1; i<=n;i++){ -scanf"%lld",&s[i]); -s[i]+=s[i-1]; the } - intNow,pre; - for(intk=2; k<=k;k++){ -now= (k +1)%2; +pre=k%2; -front=back=1; +q[back++]=0; A for(intI=1; i<=n;i++){ at while(front<back-1&&dp[q[front+1]][pre]-dp[q[front]][pre]+ (s[q[front+1]]-s[q[front]]) *s[i]-s[q[front+1]]*s[q[front+1]]+s[q[front]]*s[q[front]]>=0) -front++; -dp[i][now]=dp[q[front]][pre]+s[q[front]]* (s[i]-S[q[front]]); - while(front<back-1&& (dp[i][pre]-dp[q[back-1]][pre]-s[i]*s[i]+s[q[back-1]]*s[q[back-1]]) * (s[q[back-2]]-s[q[back-1]]) <= (dp[q[back-1]][pre]-dp[q[back-2]][pre]-s[q[back-1]]*s[q[back-1]]+s[q[back-2]]*s[q[back-2]]) * (s[q[back-1]]-S[i])) -back--; -q[back++]=i; in } - } toprintf"%lld\n", Dp[n][now]); +}
Dynamic planning (Slope optimization): Bzoj 3675 [Apio2014] Sequence Segmentation