#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> #include<bitset> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; #define ls (rt<<1) #define rs (rt<<1|1) #define mid (l+r>>1) #define pb(x) push_back(x) #define cls(x, val) memset(x, val, sizeof(x)) #define fi first #define se second #define mp(x, y) make_pair(x, y) #define lowbit(x) (x&(-x)) #define inc(i, l, r) for(int i=l; i<=r; i++) #define dec(i, r, l) for(int i=r; i>=l; i--) const int inf = 0x3f3f3f3f; const int maxn = 1000+10; const double pi = acos(-1.0); const double eps = 1e-7; const int mod = 1e9+7; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n, m; int num[maxn]; int dp[maxn][maxn]; int pre[maxn]; void solve(){ cls(dp, 0x3f); inc(i, 1, 2*n) dp[i][i] = 0; dec(i, 2*n, 1){ inc(j, i+1, min(2*n, n+i-1)){ inc(k, i, j-1){ dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+pre[j]-pre[i-1]); } } } int ans = 0x3f3f3f3f; for(int i=1; i<=n; i++){ ans = min(ans, dp[i][i+n-1]); } printf("%d ", ans); cls(dp, -1); inc(i, 1, 2*n) dp[i][i] = 0; dec(i, 2*n, 1){ inc(j, i+1, min(2*n, n+i-1)){ inc(k, i, j-1){ dp[i][j] = max(dp[i][j], dp[i][k]+dp[k+1][j]+pre[j]-pre[i-1]); } } } ans = -1; for(int i=1; i<=n; i++){ ans = max(ans, dp[i][i+n-1]); } printf("%d\n", ans); } int main() { while(~scanf("%d", &n)){ cls(pre, 0); inc(i, 1, n) num[i] = read(), pre[i] = pre[i-1]+num[i]; inc(i, n+1, 2*n) num[i] = num[i-n], pre[i] = pre[i-1]+num[i]; solve(); } return 0; }
|