提交时间:2024-05-27 13:20:45
运行 ID: 149444
#include<bits/stdc++.h> using namespace std; int a[514],b[514],dp[514][514]; int main(){ int n; cin>>n; memset(dp,0x3f3f3f3f,sizeof(dp)); for (int i = 1;i <= n;i++){ cin>>a[i]; b[i] = b[i - 1] + a[i]; dp[i][i] = 0; } for (int len = 2;len <= n;len++){ for (int i = 1,j = i + len - 1;j <= n;i++,j++){ for (int k = i;k < j;k++){ dp[i][j] = min(dp[i][j],dp[i][k] + dp[k + 1][j] + b[j] - b[i - 1]); } } } cout<<dp[1][n]<<endl; return 0; }