D - Doin' Time
One day, Fop_zz is playing a boring game merge numbers though he is busy.
There is a sequence \{a_i\}{ai} of length NN and he should play this game for n-1n−1 turns. In one turn, he chooses an index xx and then he merges a_xax and a_{x+1}ax+1 to a_x \times a_{x+1} \bmod 1\,000\,003ax×ax+1mod1000003 and gets (a_x-a_{x+1})^2(ax−ax+1)2 scores.
So, after one turn, the length of sequence will decrease exactly one, and after the last turn there's only 11 integer in the sequence and he stops.
Now he want to know the maximum total scores he can get. Can you help him?
Input
The first line contains one integer N ~ (1 \leq N \leq 300)N (1≤N≤300) denoting the length of sequence.
The second line contains NN integers a_1, a_2, \cdots, a_n ~ (1 \leq a_i \leq 10^6)a1,a2,⋯,an (1≤ai≤106).
Output
Output one line with only one integer denoting the answer.
Sample 1
3
1 2 3
Output
26
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int a[301];//原数组
int b[301][301];//区间i到j的合并值
int dp[301][301];
signed main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++)
{
int t = 1;
for(int j=i;j<=n;j++)
{
t = t*a[j]%1000003;
b[i][j] = t;
}
}
for(int len=2;len<=n;len++)//合并的区间长度
{
for(int i=1;i+len-1<=n;i++)
{
int l = i,r = i+len-1;
for(int k=l;k<r;k++)
dp[l][r] = max(dp[l][r],dp[l][k]+dp[k+1][r]+(b[l][k]-b[k+1][r])*(b[l][k]-b[k+1][r]));
}
}
cout<<dp[1][n];
return 0;
}