I am new to Algorithms and Competitive Programming. I am learning about Dynamic programming and I have a problem as below:
Given an array with n numbers. Define a sub-array is
a[i, j] = {a[i], a[i + 1], ..., a[j]}, in other words, elements must be contiguous.The problem is the find the maximum weight of a sub-array such that that weight is an even number.
The input is
2 <= n <= 1000000; -100 <= a[i] <= 100Sample test:
5
-2 1 -4 4 9
Output: 10
For this problem, I can do brute force but with a large value of n, I can not do it with the time limit is 1 second. Therefore, I want to change it to Dynamic programming.
I have an idea but I do not know if it works. I think I can divide this problem into two sub-problems. For each element/number, I consider if it is odd/even and then find the largest sum with its corresponding property (odd + odd or even + even to get a even sum). However, that is just what I think and I really need your help.