Luogu1880 [NOI1995]石子合并 (区间DP)
一个1A主席树的男人,沦落到褪水DP举步维艰
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
//#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 507;
int f_max[N][N], f_min[N][N], a[N], sum[N];
int main(){
FileOpen();
int n;
io >> n;
R(i,1,n){
io >> a[i];
a[i + n] = a[i];
}
R(i,1, n << 1) sum[i] = sum[i - 1] + a[i];
R(len, 2, n){
R(l, 1, n << 1){
int r = l + len - 1;
if(r > (n << 1)) continue;
f_min[l][r] = 0x3f3f3f3f;
R(k, l, r - 1){
f_min[l][r] = Min(f_min[l][r], f_min[l][k] + f_min[k+1][r] + sum[r] - sum[l-1]);
f_max[l][r] = Max(f_max[l][r], f_max[l][k] + f_max[k+1][r] + sum[r] - sum[l-1]);
}
}
}
int maxx = 0, minn = 0x7fffffff;
R(i,1,n){
maxx = Max(maxx, f_max[i][i + n - 1]);
minn = Min(minn, f_min[i][i + n - 1]);
}
printf("%d\n%d", minn, maxx);
return 0;
}