萌新 求助
查看原帖
萌新 求助
235657
hwx12233楼主2020/5/31 11:20
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>

using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff

inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x<y?x:y;}

const int N=1e5+10;
const int M=210;


int n,k,dp[N][M],a[N],pre[N], sb[N][M];
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		pre[i]=pre[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
	for(int j=0;j<i;j++)
	for(int z=1;z<=k+1;z++){
		int tmp=dp[j][z-1]+(pre[n]-pre[i])*(pre[i]-pre[j]);
		if(dp[i][z]<=tmp)
		dp[i][z]=tmp,sb[i][z] = j;
		}
	}
	
	cout<<dp[n][k+1]<<endl;
	for(int i=n,j=k+1;j>=2;i=sb[i][j],j--) cout << sb[i][j] << " ";
}
2020/5/31 11:20
加载中...