当前位置:网站首页>DP energy Necklace

DP energy Necklace

2022-04-23 14:13:00 Stingy old Sao

Energy necklace

#include <bits/stdc++.h>

#pragma GCC optimize(2)
using namespace std;
#define int long long
typedef long long LL;
typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int inf = 1e18;
const int mod = 998244353;
//const int mod = 1e9 + 7;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }

const int maxn = 1e4 + 10;
const int N = 6e6 + 100;

int dp[maxn][maxn];
int a[maxn];
int v[maxn];

void solve() {
    int n;
    cin>>n;
    for (int i = 1; i <=n; ++i) {
        cin>>a[i];
        a[n+i]=a[i];
    }
    for(int len=3;len<=n+1;len++){
        for(int l=1;l<=2*n-len+1;l++){
            int r=l+len-1;
            for(int k=l+1;k<r;k++){
                dp[l][r]=max(dp[l][r],dp[l][k]+dp[k][r]+a[l]*a[k]*a[r]);
            }
        }
    }
    int res=0;
    for(int i=1;i<=n;i++){
        res=max(res,dp[i][i+n]);
    }
    cout<<res;
}

signed main() {
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();//cout<<"\n";
    }
    return 0;
}

//12341
//14321
//

版权声明
本文为[Stingy old Sao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407093980.html