D - Doin‘ Time

D - Doin' Time

 Gym - 103107D 

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+1​mod1000003 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值