提交时间:2024-07-17 10:40:32
运行 ID: 156360
#include<iostream> using namespace std; long long n, ls[410], f1[810][810], f2[810][810], S[410], ans1, ans2; const long long inf = 1e16+1e15; int main(){ ans1 = inf; ans2 = -inf; cin >> n; for(int i = 1; i <= n; i++){ cin >> ls[i]; S[i] = ls[i] + S[i-1]; } for(int i = n+1; i <= n*2; i++){ ls[i] = ls[i-n]; S[i] = ls[i] + S[i-1]; } for(int len = 2; len <= n; len++){ for(int i = 1; i <= n*2; i++){ int j = i + len - 1; f1[i][j] = inf; for(int k = i; k < j; k++){ f1[i][j] = min(S[j] - S[i-1] + f1[i][k] + f1[k+1][j], f1[i][j]); f2[i][j] = max(S[j] - S[i-1] + f2[i][k] + f2[k+1][j], f2[i][j]); } } } for(int st = 1; st <= n; st++){ int ed = st + n - 1; ans1 = min(ans1, f1[st][ed]); ans2 = max(ans2, f2[st][ed]); } cout << ans1; return 0; }