提交时间:2024-06-01 13:47:44
运行 ID: 150008
# include <bits/stdc++.h> using namespace std ; int dp [3] [550] , b [550] ; int main ( ) { int n , k ; cin >> n >> k ; for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = i + 1 ; j < 501 ; j ++ ) { dp [i] [j] = 1 << 25 ; } } for ( int i = 1 , x ; i <= n ; i ++ ) { cin >> x ; b [i] = b [i - 1] + x ; } for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = i + 1 ; j <= n - ( k - i ) ; j ++ ) { for ( int k1 = i - 1 ; k1 < j ; k1 ++ ) { dp [i] [j] = min ( dp [i - 1] [k1] + ( b [j] - b [k1] ) * ( j - k1 - ( b [j] - b [k1] ) ) , dp [i] [j] ) ; } } } cout << dp [k] [n] << endl ; return 0 ; }