POJ1160
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath> #include<vector> #define maxn 305 #define rint register int #define ll long long #define int unsigned long long #define inf 0x77777777777777f #define pb push_back #define mod (int)1e9 + 7 using namespace std; inline int read() { char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } inline void write(int x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); return ; } int v, p, a[maxn], dis[maxn][maxn], f[maxn][maxn]; //dis[i][j]表示i到j建一个邮局最小距离,f[i][j]表示1到i建j个最小距离和 signed main() { v = read(), p = read(); for (rint i = 1; i <= v; ++i) a[i] = read(); for (rint i = 1; i <= v; ++i) for (rint j = i; j <= v + 1; ++j) dis[i][j] = dis[i][j - 1] + a[j] - a[(i + j) / 2]; memset(f, 0x3f, sizeof(f)); for (rint i = 1; i <= v; ++i) f[i][1] = dis[1][i]; for (rint j = 1; j <= p; ++j) for (rint i = j; i <= v; ++i) for (rint k = j - 1; k <= i - 1; ++k) f[i][j] = min(f[i][j], f[k][j - 1] + dis[k + 1][i]); write(f[v][p]); return 0; }