[NOI1995] 石子合并
作者:
多米尼克領主的致意
,
2024-04-23 14:37:53
,
所有人可见
,
阅读 2
#include<bits/stdc++.h>
using namespace std;
const int N = 310;
int w[N][N];
int a[N];
int f[N][N];
int mx[N][N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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++)
{
a[i] += a[i - 1];
}
memset(f, 0x3f, sizeof f);
memset(mx, -0x3f, sizeof mx);
for(int i = 1;i <= 2 * n;i++)
{
mx[i][i] = 0;
f[i][i] = 0;
}
for(int len = 2;len <= n;len++)
{
for(int i = 1;i + len <= 2 * n;i++)
{
int j = i + len - 1;
for(int k = i;k < j;k++)
{
f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j] + a[j] - a[i - 1]);
mx[i][j] = max(mx[i][j], mx[i][k] + mx[k + 1][j] + a[j] - a[i - 1]);
}
}
}
int mn = 0x3f3f3f3f;
int mxx = 0;
for(int i = 1;i <= n;i++)
{
// cout << f[i][i + n - 1] << endl;
mn = min(mn, f[i][i + n - 1]);
mxx = max(mxx, mx[i][i + n - 1]);
}
cout << mn << endl;
cout << mxx;
return 0;
}