1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <stdio.h> #include <string.h> #include <iostream> using namespace std;
const int N = 220;
int ans, dp[N][N], n, a[N], s[N];
int main() { scanf("%d", &n); for(int i=1; i<=n; ++i) { scanf("%d", a+i); s[i] = s[i-1] + a[i]; } for(int i=1; i<=n; ++i) { s[i+n] = s[i+n-1] + a[i]; } memset(dp, 0x3f, sizeof dp); for(int i=1; i<=2*n; ++i) dp[i][i] = 0; for(int l=2; l<=n; ++l) { for(int i=1, j=l; j<=2*n; ++i, ++j) { for(int k=i; k<j; ++k) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + s[j] - s[i-1]); } } } ans = 0x3f3f3f3f; for(int i=1; i<=n; ++i) { ans = min(ans, dp[i][i+n-1]); } printf("%d\n", ans); memset(dp, 0, sizeof dp); for(int i=1; i<=2*n; ++i) dp[i][i] = 0; for(int l=2; l<=n; ++l) { for(int i=1, j=l; j<=2*n; ++i, ++j) { for(int k=i; k<j; ++k) { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j] + s[j] - s[i-1]); } } } ans = 0; for(int i=1; i<=n; ++i) { ans = max(ans, dp[i][i+n-1]); } printf("%d\n", ans); return 0; }
|