LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search Functions
Binary Numbers
Binary NumbersNumbers in codeBitwise Operations
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of Eratosthenes
Data Structures
StringsStackQueue
Dynamic Programming
About DPDP problems

Maximum Segment Sum

EasyProblem #19
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

The best segment can include negative elements

An array a of n integers is given. A segment of the array is a group of consecutive elements with at least one element. Your task is to find the largest possible sum of a segment of the given array.

Input

The first line contains a single integer t - the number of testcases.

  • The first line of each testcase contains an integer n - the number of elements.
  • The next line contains n integers a0​,a1​,…,an−1​ - the elements of the array.

Output

For every testcase print a single line with the largest sum of a segment.

Example

Input
2
6
2 -3 4 -1 3 -2
3
-5 -2 -8
Output
6
-2

In the first testcase the best segment is [4,−1,3] with sum 6. In the second testcase every element is negative, so the best segment is the single element [−2].

Constraints

1≤t≤1000
1≤n≤2⋅105
−109≤ai​≤109
n1​+n2​+…+nt​≤2⋅105


This problem was adapted, with permission, from Maksimalni zbir segmenta, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit