P1880 [NOI1995] 石子合并https://www.luogu.com.cn/problem/P1880
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <cctype>
#include <cstdlib>
#include <deque>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
int sum[MAXN];
int s[MAXN];
int n;
int f1[1000][1000];
int f2[1000][1000];
int main() {
memset(f1, 0x3f, sizeof(f1));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", s + i);
s[i + n] = s[i];
}
for (int i = 1; i <= n + n; i++) {
sum[i] = sum[i - 1] + s[i];
f1[i][i] = 0;
}
for (int l = 1; l < n + n; l++) {
for (int i = 1; i <= n + n - l; i++) {
int j = i + l;
for (int k = i; k < j; k++) {
f1[i][j] = min(f1[i][j], f1[i][k] + f1[k + 1][j] + sum[j] - sum[i - 1]);
f2[i][j] = max(f2[i][j], f2[i][k] + f2[k + 1][j] + sum[j] - sum[i - 1]);
}
}
}
int ans1 = INF;
int ans2 = 0;
for (int i = 1; i <= n; i++) {
ans1 = min(ans1, f1[i][n + i - 1]);
ans2 = max(ans2, f2[i][n + i - 1]);
}
printf("%d\n%d", ans1, ans2);
return 0;
}