LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDE
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 FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion Find
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

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