---
# System prepended metadata

title: 1911. Maximum Alternating Subsequence Sum
tags: [Leetcode, Dynamic Programming, Medium]

---

# 1911. Maximum Alternating Subsequence Sum
###### tags: `Leetcode` `Medium` `Dynamic Programming`
Link: https://leetcode.com/problems/maximum-alternating-subsequence-sum/description/
## 思路
基本型I dp
```even```和```odd```表示```nums[0:i]```的有偶数/奇数个数的subsequence的maximum alternating subsequence sum
## Code
```java=
class Solution {
    public long maxAlternatingSum(int[] nums) {
        long even = 0;
        long odd = nums[0];
        for(int i=1; i<nums.length; i++){
            int num = nums[i];
            even = Math.max(even, Math.max(0, odd-num));
            odd = Math.max(odd, Math.max(num, even+num));
        }
        return Math.max(even, odd);
    }
}
```