Bob和Alice(3)

求的是最大值!!!
Bob和Alice在玩一个序列游戏
有一个n-1个数的序列
B 1 B 1 ​ B 2 B 2 ​ . . . . . . . B n − 1 B n − 1 B_1 B 1 ​ B_2B 2 ​ ....... B_{n-1}B n−1 B1B1B2B2.......Bn1Bn1

Bob又定义了一个新的序列
A 1 A 1 ​ A 2 A 2 ​ . . . . . . . A n A n A_1A 1 ​ A_2A 2 ​ .......A_nA n A1A1A2A2.......AnAn

对每一个 i [ 1 < = i < = n − 1 ] B i > = m a x ( A i , A i + 1 ) B i ​ > = m a x ( A i , A i + 1 ) i [1 <= i <= n - 1] B_i>=max(A_i,A_{i+1})B i ​ >=max(A i ,A i+1) i[1<=i<=n1]Bi>=max(Ai,Ai+1)Bi>=max(Ai,Ai+1)
好奇的Alice想知道A序列在这样的约束之下
A序列的总和的最大值是多少
即求 m a x ∑ i = 1 n a n max\sum_{i=1}^na_n maxi=1nan

输入格式
n
B 1 B 1 ​ B 2 B 2 ​ . . . . . . . B n − 1 B n − 1 ​ B_1B 1 ​ B_2B 2 ​ .......B_{n-1}B n−1 ​ B1B1B2B2.......Bn1Bn1

2 <= n <= 100
0 <= B i B i ​ < = 105 B_iB i ​ <= 105 BiBi<=105
输出格式
A序列的总和的最大值是多少

输入样例
6
0 153 10 10 23

输出样例
53

#include
#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector<int>b(n), a(n+1);
	for (int i = 1; i < n; i++)cin >> b[i];
	a[1] = a[2] = b[1];
	for (int i = 2; i <= n - 1; i++)
	{
		if (a[i] > b[i])
			a[i + 1] = a[i] = b[i];
		else 	a[i + 1] = b[i];
	}
	long sum = 0;
	for (int i = 1; i <= n; i++) sum += a[i];
	cout << sum;
}

你可能感兴趣的:(acwing,动态规划,算法,c++,循环)