同动态规划
#include <iostream>
using namespace::std;
using std::cout;
using std::cin;
int n;
int prices[5100],dp[5100][4];
int zdlr(int n, int prices[])
{
dp[0][0] = -prices[0];
for(int i=1;i<n;i++)
{
dp[i][0] = max(dp[i-1][0],max(dp[i-1][3]-prices[i],dp[i-1][2]-prices[i]));
dp[i][1] = dp[i-1][0] + prices[i];
dp[i][2] = dp[i-1][1];
dp[i][3] = max(dp[i-1][3],dp[i-1][2]);
}
return max(dp[n-1][3],max(dp[n-1][1],dp[n-1][2]));
}
int main()
{
cin >> n;
for(int i=0; i<n; i++)
{
cin >> prices[i];
}
std::cout << zdlr(n, prices);
return 0;
}