← 返回 amazon 的题目列表Maximum Alternating Subsequence Sum
类型:online_judge
Given an integer array nums of length n, you may choose a subsequence (not necessarily contiguous, but order preserved).
Define the subsequence's alternating sum as:
x0 - x1 + x2 - x3 + ... (0-indexed: add even positions, subtract odd positions).
Return the maximum alternating sum you can obtain.
Input (stdin)
Line 1: integer n. Line 2: n integers representing nums.
Output (stdout)
Print one integer: the maximum alternating sum.
Constraints
1 <= n <= 2*10^5
1 <= nums[i] <= 1e9
Example
Input
4
4 2 5 3
Output
7