luogu 1063

环形dp模板
开个二倍数组然后做区间dp

#include 
#include 
#include 
#define MAXN 210

using namespace std;

int n, a[MAXN], f[MAXN][MAXN], ans;

int read() {
    int f = 1, k = 0;
    char c = getchar();
    while(c < '0' || c> '9') {
        if(c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        k = k * 10 + c - '0';
        c = getchar();
    }
    return f * k;
}

int main() {
    n = read();
    for(int i = 1; i <= n; i ++) {
        a[i] = read();
        a[i + n] = a[i];
    }
    for(int i = 2; i < 2 * n; i ++) {
        for(int j = i - 1; j > 0 && i - j < n; j --) {
            for(int k = j; k < i; k ++) {
                f[j][i] = max(f[j][i], f[j][k] + f[k + 1][i] + a[j] * a[k + 1] * a[i + 1]);
            }
            ans = max(ans, f[j][i]);
        }
    }
    printf("%d", ans);
    return 0;
}

你可能感兴趣的:(luogu,dp)