题目链接:https://www.luogu.org/problemnew/show/P1880
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <sstream> #include <iostream> #include <queue> #include <stack> #include <set> #include <map> #include <algorithm> #include <functional> using namespace std; #define ll long long #define re register #define fi first #define se second #define mp make_pair #define pb push_back #define P pair<int,int> const int N=1e6+10; const int mod=1e9+7; void read(int &a) { a=0; int d=1; char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch-'0'; while(ch=getchar(),ch>='0'&&ch<='9') a=a*10+ch-'0'; a*=d; } void write(int x) { if(x<0) putchar(45),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int f1[205][205],f2[205][205],s[205]; int main() { int n; read(n); for(re int i=1;i<=n;i++) { read(s[i]); s[i+n]=s[i]; } for(re int i=1;i<=2*n;i++) s[i]=s[i-1]+s[i]; int j; for(re int k=1;k<n;k++) { for(re int i=1,j=i+k;j<2*n&&i<2*n;i++,j++) { f2[i][j]=0x3fffffff; for(re int t=i;t<j;t++) f1[i][j]=max(f1[i][j],f1[i][t]+f1[t+1][j]+s[j]-s[i-1]),f2[i][j]=min(f2[i][j],f2[i][t]+f2[t+1][j]+s[j]-s[i-1]); } } int ans1=0x3fffffff,ans2=0; for(re int i=1;i<=n;i++) ans1=min(ans1,f2[i][i+n-1]),ans2=max(ans2,f1[i][i+n-1]); write(ans1); putchar(' '); write(ans2); putchar(' '); return 0; }