糖豆人吃糖豆串
Description
在糖豆世界,所有东西都是能吃的,包括糖豆人自己,好了,你知道了这些并没什么用。
有一个糖豆人,它手里有n个糖豆,每个糖豆都有一个甜度,而且它有一个奇怪的味蕾,每吃完一个糖豆后,它的味蕾感就会加1,它吃一个糖豆获得的甜度为 糖豆的甜度*味蕾感,如今它想把所有的糖豆按顺序串起来做成一个糖豆串,每次只能吃左边的第一个或者吃右边的第一个,直到全部吃完,请问它最多能得到多少甜度。
Input
只有两行输入,第一行包含2个正整数 糖豆数量n,初始味蕾感q.
第二行包含n个正整数,代表每个糖果的甜度。(所有数据小于等于1000)
Output
输出一个整数,表示最多能得到多少甜度。
Sample Input 1
4 2
5 7 3 4
Sample Output 1
72
简单dp
dp[i][j] 表示左侧选i个 右侧选j个的最大甜度
易错点
写的贪心 估计是比较左边和右边的 选择一个最小的 但是这是错的 以下案例 贪心错误
6 1
3 9999 1 1 1 4
贪心顺序: 3 4 1 1 1 9999
正确顺序: 4 1 1 1 3 9999
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define LL long long
const LL MAXN = 1005;
LL a[MAXN],dp[MAXN][MAXN];
int main()
{
IOS;
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
{
dp[i][0]=dp[i-1][0]+a[i]*(q+i-1);
dp[0][i]=dp[0][i-1]+a[n-i+1]*(q+i-1);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[i][j]=max(dp[i-1][j]+a[i]*(q+i+j-1),dp[i][j-1]+a[n-j+1]*(q+i+j-1));
}
}
LL ans=0;
for(int i=0;i<=n;i++)
ans=max(dp[i][n-i],ans);
cout<<ans<<endl;
// for(int i=0;i<=n;i++)
// {
// for(int j=0;j<=n;j++)
// cout<<dp[i][j]<<" ";
// cout<<endl;
// }
return 0;
}