环型石子合并
来源 : https://www.acwing.com/problem/content/1070/
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 610;
int s[N], a[N];
int f[N][N];
int g[N][N];
int main() {
IO;
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i + n] = a[i];
}
for (int i = 1; i <= 2 * n; ++i) s[i] = s[i - 1] + a[i];
memset(f, 0x3f, sizeof f);
memset(g, -0x3f, sizeof g);
for (int len = 1; len <= n; ++len)
for (int l = 1; l + len - 1 <= 2 * n; ++l) {
int r = l + len - 1;
if (l == r) f[l][r] = g[l][r] = 0;
for (int k = l; k < r; ++k) {
f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
g[l][r] = max(g[l][r], g[l][k] + g[k + 1][r] + s[r] - s[l - 1]);
}
}
int minv = inf, maxv = -inf;
for (int i = 1; i <= n; ++i) {
minv = min(minv, f[i][i + n - 1]);
maxv = max(maxv, g[i][i + n - 1]);
}
cout << minv << endl;
cout << maxv << endl;
return 0;
}