提交时间:2024-05-25 16:05:17

运行 ID: 149097

#include <bits/stdc++.h> using namespace std; #define dp(i , j) dp[(i) % 2][j] const int N = 510; int dp[2][N], b[N]; 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 << 30; for (int i = 1 , x; i <= n; i++){ scanf("%d", &x); b[i] = b[i - 1] + x; } for (int i = 1; i <= k; i++) for (int j = i; j <= n - (k - i); j++) for (int k = i - 1; k < j; k++) dp(i, j) = min(dp(i, j), dp(i - 1, k) + (b[j] - b[k]) * (j - k - (b[j] - b[k]))); cout << dp(k, n); return 0 ; }