#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=100;
int bj[N],sum[N],dis[N][N],f[N][N][N];
struct lamp{int pos,p;}a[N];
int main(){
int n=read(),m=read();
for(int i=1;i<=n;++i){
a[i].pos=read();a[i].p=read();
sum[i]=sum[i-1]+a[i].p;
}
for(int i=1;i<n;++i)
for(int j=i+1;j<=n;++j)
dis[i][j]=dis[j][i]=a[j].pos-a[i].pos;
memset(f,0x3f,sizeof(f));f[m][m][m]=0;
for(int len=2;len<=n;++len){
for(int i=1;i+len-1<=n;++i){
int j=i+len-1;
f[i][j][i]=min(f[i+1][j][i+1]+dis[i][i+1]*(sum[i]+sum[n]-sum[j]),f[i+1][j][j]+dis[i][j]*(sum[i]+sum[n]-sum[j]));
f[i][j][j]=min(f[i][j-1][i]+dis[i][j]*(sum[i-1]+sum[n]-sum[j-1]),f[i][j-1][j-1]+dis[j-1][j]*(sum[i-1]+sum[n]-sum[j-1]));
}
}
printf("%d\n",min(f[1][n][1],f[1][n][n]));
return 0;
}
原文:https://www.cnblogs.com/PPXppx/p/11632499.html