LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
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 FindSqrt Decomposition
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 DPDP problemsTree DPBitmask 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

Robber Problem

EasyProblem #4
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Watch out for the size of the answer.

A robber is planning to rob houses along a street. Each house has a certain amount of money stashed inside. The only thing stopping him from robbing all of them is that the houses have connected security systems: if two adjacent houses are robbed on the same night, the alarm will automatically go off.

Write a program that determines the largest amount of money the robber can steal without triggering the alarm. Formally, find the largest sum of a subsequence of the given sequence that does not contain two consecutive elements.

Input

First line of input will be a single number n, the number of houses.
In the next line will be n numbers a1​,a2​,…,an​, the amount of money in each house.

Output

A single number - the largest amount of money that can be stolen.

Example

Input
5
2 7 9 3 1
Output
12

The robber robs the houses with 2, 9 and 1 (2+9+1=12). No two of them are adjacent.

Constraints

1≤n≤2⋅105
0≤ai​≤109

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit