import java.util.*;


public class Solution {
     static int level = 0;
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 两次交易所能获得的最大收益
     * @param prices int整型一维数组 股票每一天的价格
     * @return int整型
     */
    public int maxProfit (int[] prices) { // 只能看看而已,以前一直以为就几个状态,现在4个状态推到。
        // write code here
        if (prices.length == 0 || prices.length == 1) {
            return 0;
        }
        int buy1 = -prices[0];
        int sel1 = 0;
        int buy2 = -prices[0];
        int sel2 = 0;
        for (int i = 1; i < prices.length; i++) {
            buy1 = Math.max(buy1, -prices[i]);
            sel1 = Math.max(sel1, buy1 + prices[i]);
            buy2 = Math.max(buy2, sel1 - prices[i]);
            sel2 = Math.max(sel2, buy2 + prices[i]);
        }
        return sel2;
    }
}