记录编号 43788 评测结果 AAAAAAAAAA
题目名称 [NOIP 2006]能量项链 最终得分 100
用户昵称 GravatarTruth.Cirno 是否通过 通过
代码语言 C++ 运行时间 0.011 s
提交时间 2012-10-13 22:30:24 内存使用 3.32 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
using namespace std;

int a[210],f[210][210];

int maxint(int a,int b)
{
    if(a>b)
        return(a);
    return(b);
}

int main(void)
{
    freopen("energy.in","r",stdin);
    freopen("energy.out","w",stdout);
    int i,j,k,n,n2,l,r,temp=0;
    cin>>n;
    for (i=1,n2=n+1;i<=n;i++,n2++)
	{
        cin>>a[i];
		a[n2]=a[i];
	}
	n--;
    n2-=3;
    for (i=1;i<=n2;i++)
        f[i][1]=a[i]*a[i+1]*a[i+2];
    for (i=1;i<n2;i++)
    {
        f[i][2]=f[i+1][1]+a[i]*a[i+1]*a[i+3];//f[][] i+1 to i+1
        f[i][2]=maxint(f[i][2],f[i][1]+a[i]*a[i+2]*a[i+3]);//f[][] i to i
    }
    for (j=3;j<=n;j++)
        for (i=1;i+j-1<=n2;i++)
        {
            f[i][j]=f[i+1][j-1]+a[i]*a[i+1]*a[i+j+1];//f[][] i+1 to i+j-1
            f[i][j]=maxint(f[i][j],f[i][j-1]+a[i]*a[i+j]*a[i+j+1]);//f[][] i to i+j-2
            l=i+1;
            r=i+j-2;
            for (k=l;k<=r;k++)
                f[i][j]=maxint(f[i][j],f[i][k-i]+f[k+1][i+j-k-1]+a[i]*a[k+1]*a[i+j+1]);//f[][] i+1 to k-1 && k+1 to r
        }
	for (i=1;i<=n+1;i++)
		if (temp<f[i][n])
			temp=f[i][n];
		cout<<temp<<endl;
    return(0);
}