cswork / OAOpen practice workspace

CiscoOA #19

Find Largest Sum Contiguous Subarray

Estimated MediumJudge-ready

Topics (AI-labelled)DP Basics

Choose a non-empty contiguous segment of an integer array and output the maximum sum of its elements. Even for an all-negative array, the empty segment may not be chosen.

Input

First n is given, followed by n integers. The original gave no numeric bounds; this site publicly adds 1≤n≤200000 and −10^9≤a[i]≤10^9.

Output

Output a single integer, the answer.

Example 1

Input:

6
2 -8 3 -2 4 -10

Output:

5

Example 2

Input:

3
-7 -2 -8

Output:

-2

Example 3

Input:

1
0

Output:

0

Example notes

The outputs of the three public examples are, in order: 5; -2; 0. Verified by independent enumeration or direct simulation.

Editorial

Loading solution…