提交时间:2024-07-30 22:24:41
运行 ID: 159749
# include <bits/stdc++.h> using namespace std ; int a [101] , g [101] [101] , dp [101] [101] ; int main ( ) { int n ; cin >> n ; for ( int i = 1 ; i <= n ; i ++ ) { cin >> a [i] ; } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { g [i] [j] = g [i] [j - 1] + a [j] ; } } for ( int s = 2 ; s <= n ; s ++ ) { for ( int i = 1 , j = s ; j <= n ; i ++ , j ++ ) { dp [i] [j] = 1 << 30 ; for ( int k = i ; k < j ; k ++ ) { dp [i] [j ] = min ( dp [i] [j] , dp [i] [k] + dp [k + 1] [j] + g [i] [j] ) ; } } } cout << dp [1] [n] << endl ; return 0 ; }