1004 - Largest Weighted Subsequence

Time Limit: 2s Memory Limit: 256MB

Submissions: 662 Solved: 191
Description

Find the largest weight subsequence of a given sequence of numbers.

  • Given a sequence s=<a1, a2, ..., an>
  • A subsequence is s(i,j)=<ai, ai+1, ..., aj>
  • Weight w(s(i,j))=ai + ai+1 + ... + aj
  • Problem: find the subsequence having largest weight

Example

  • Given sequence: -2, 11, -4, 13, -5, 2
  • The largest weight subsequence is 11, -4, 13 having weight 20
Input

The first line contains a positive integers N (1 N 106).

The second line contains N integers.

Output

You should output on a single line an unique integer that is the maximum value of the sum of the found subsequence.

Sample Input
6
-2, 11, -4, 13, -5, 2
Sample Output
20
Hint
Source