【P2858 USACO06FEB】奶牛零食Treats for the Cows(区间DP)

版权声明:虽然我很菜,不过转载请标明出处。 https://blog.csdn.net/Patrickpwq/article/details/86581751

萌新冒泡

这是一道更水的区间dp

#include<bits/stdc++.h>
const int N=2005;
using namespace std;
int n,a[N],f[N][N];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	for(int i=1;i<=n;i++)	f[i][i]=n*a[i];
	for(int len=2;len<=n;len++)
		for(int st=1;st<=n-len+1;st++)
		{
			int ed=st+len-1;
			f[st][ed]=max(f[st][ed-1]+a[ed]*(n-len+1),f[st+1][ed]+a[st]*(n-len+1));
		}
	cout<<f[1][n]<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Patrickpwq/article/details/86581751