#include<cstdio> #include<cmath> #include<iostream> using namespace std; int a[505][505],c[505][505],f[505][505]; int main() { int m,n; scanf("%d%d",&m,&n); for(int i = 1; i < m; ++i) scanf("%d",&a[i][i+1]); for(int i = 1; i <= m; ++i) { for(int j = i+1; j <= m; ++j) { a[i][j] = a[j-1][j] + a[i][j-1]; a[j][i] = a[i][j]; } } int mid; for(int i = 1; i <= m; ++i) { for(int j = i+1; j <= m; ++j) { mid=(i+j)/2; c[i][j] = 0; for(int k = i; k <= j; ++k) c[i][j] += a[k][mid]; } } for(int i = 1; i <= m; ++i) f[i][1] = c[1][i]; int s = 0x7fffffff; for(int i = 1; i <= m; ++i) { for(int j = 2; j <= n; ++j) { f[i][j] = s; for(int k = j-1; k <= i; ++k) f[i][j] = min(f[i][j],f[k][j-1]+c[k+1][i]); } } printf("%d",f[m][n]); return 0; }
标签:include,int,小学,scanf,山区,1197,505 From: https://www.cnblogs.com/zheng1945/p/17395458.html