import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] prices = new int[n]; for(int i = 0; i < n; i++){ prices[i] = in.nextInt(); } if(n == 1){ System.out.println(0); return; } // ?????????? 不理解 // 先计算相邻两天的价格差,通过累加 “正向差值” 来获取最大利润。 // 本质上是找出所有 “上涨区间” 的总和(下跌区间则不参与计算)。 int rs = prices[1] - prices[0]; // 存储最终的最大利润 int now = prices[1] - prices[0];// 记录当前连续区间的利润总和 int pre = prices[1] - prices[0];// 记录上一轮连续区间的利润总和 for(int i = 1; i < n - 1; i++){ if(pre <= 0){ now = prices[i + 1] - prices[i]; }else{ now = pre + prices[i + 1] - prices[i]; } pre = now; rs = Math.max(rs,now); } System.out.println(rs > 0 ? rs : 0); } }