import java.io.*;
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static int N = 100010, INF = 0x3f3f3f3f;
static int[][][] dp = new int[N][3][2];
public static void main(String[] args) throws Exception{
int n = Integer.valueOf(read.readLine());
int[] a = new int[n + 1];
String[] ss = read.readLine().split(" ");
for(int i = 1; i <= n; i++){
a[i] = Integer.valueOf(ss[i - 1]);
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= 2; j++){
dp[i][j][0] = -INF;
dp[i][j][1] = -INF;
}
}
for(int i = 0; i <= n; i++) dp[i][0][0] = 0; //注意初始化, 这里容易出错
for(int i = 1; i <= n; i++){
for(int k = 1; k <= 2; k++){
dp[i][k][0] = Math.max(dp[i - 1][k][0], dp[i - 1][k][1] + a[i]);
dp[i][k][1] = Math.max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - a[i]);
}
}
int max = 0;
for(int k = 0; k <= 2; k++){
max = Math.max(max, dp[n][k][0]);
}
System.out.println(max);
}
}
这里时表示买股票的时候算作一次交易吗?
是的,买就算开始一次新的交易
dp[i][k][1]=Math.max(dp[i-1][k][1],dp[i-1][k-1][0]-a[i]);//要么什么都不做,要么买股票,买股票的时候算交易,需要转移状态