Sum Over Zero
InterviewTime limit1sMemory limit1024 MB
Choose pairwise disjoint segments whose element sums are all non-negative, maximizing the total length of the chosen segments.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Prefix sum, Sorting
- Solved
- No attempts yet
Problem
You are given an array of integers. Let be a set of segments that satisfies the following conditions.
- Each element of has the form , where and are integers between and , inclusive, and .
- No two segments in intersect. Two segments and intersect if and only if there exists an integer such that and .
- For each in , .
The length of a segment is . is the sum of the lengths of every segment in , so . If is empty, is .
Find the maximum over all possible .
Input
The first line contains one integer ().
The next line contains integers ().
Output
Print a single integer, the maximum .