文件目录

#include <bits/stdc++.h>
using namespace std;
int dis[550][550];   // 村庄间距离
int scdis[550][500]; // 村庄间小学距离和
int dp[550][550];
int main()
{
    int m, n;
    cin >> m >> n;
    for (int i = 1; i < m; i++)
    {
        cin >> dis[i][i + 1];
        dis[i + 1][i] = dis[i][i + 1];
    }
    for (int i = 1; i < m; i++)
    {
        for (int j = i + 1; j <= m; j++)
        {
            dis[i][j] = dis[j][i] = dis[i][j - 1] + dis[j - 1][j];
        }
    }

    for (int i = 1; i < m; i++)
    {
        for (int j = i + 1; j <= m; j++)
        {
            int mid = (i + j) / 2;
            for (int k = i; k <= j; k++)
            {
                scdis[i][j] += dis[k][mid];
            }
        }
    }
    memset(dp, 0x3f, sizeof(dp));
    for (int i = 1; i <= m; i++)
    {
        dp[i][1] = scdis[1][i]; // 从1到i之间建一个小学
    }

    for (int i = 1; i <= m; i++)
    {
        for (int j = 2; j <= n; j++)
        {
            for (int k = j - 1; k <= i; k++)
            {
                dp[i][j] = min(dp[i][j], dp[k][j - 1] + scdis[k + 1][i]);
            }
        }
    }
    cout << dp[m][n];
    return 0;
}