石子归并(区间dp 模板)
#include<iostream> #include<cstdio> #include <cctype> #include<algorithm> #include<cstring> #include<cmath> #include<string> #include<cmath> #include<set> #include<vector> #include<stack> #include<queue> #include<map> using namespace std; #define ll long long #define mem(a,x) memset(a,x,sizeof(a)) #define se second #define fi first const ll mod=998244353; const int INF= 0x3f3f3f3f; const int N=2e5+5; int n; int a[N],sum[N]; int dp[105][105]; int main() { cin>>n; mem(dp,INF); for(int i=1;i<=n;i++) { cin>>a[i]; sum[i]=sum[i-1]+a[i]; dp[i][i]=0; } for(int len=1;len<=n;len++) { for(int i=1;i+len-1<=n;i++) { int ed=i+len-1; for(int j=i;j<ed;j++) { dp[i][ed]=min(dp[i][ed], dp[i][j]+dp[j+1][ed] + sum[ed]-sum[i-1]); } } } cout<<dp[1][n]<<endl; }