Panda and Shopping
Practice
3.6 (114 votes)
Approved
Data structures
Dynamic programming
Fenwick tree
Hiring
Medium
Ready
Segment trees
Problem
10% Success 1233 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Panda loves to shop! And well, anyone who loves to shop gets a huge amount of happiness even after a tiring session of window shopping.

There are N shops, all aligned linearly having index \(1,2,3 ... N\). Buying any positive number of items from a shop \(S_i\) gives a happiness equal to \(H_i\) . Unfortunately due to some unavoidable circumstances, every shop owner has introduced a value \(L_i\). You are credited \(H_i\) happiness from the \(ith\) shop if and only if this is the first shop you are buying something from or you buy atleast one item from this shop and the last shop you shopped from was \(S_j\) such that \(L_j \le L_i\) and \(j < i\). Find the maximum sum of happiness that can be obtained following the rules given above!

Input Format:

The first line contains N , the number of shops in the area. Each of the next N lines contains two space separated integers, \(H_i\) and \(L_i\) , which denotes the values for the \(ith\) shop (defined in the problem statement).

Output Format:

Output consists of one line, which contains the maximum sum of happiness that can be obtained!

Constraints :

\( 1 \le N \le 10^5\)
\(1 \le abs(H_i) \le 10^5\)
\(1 \le L_i \le 10^{18}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
Advanced Data StructuresData StructuresLongest Increasing SubsequenceMediumSegment Trees
Points:30
1 votes
Tags:
AlgorithmsApprovedData StructuresMediumOpenSimulationTrees
Points:30
41 votes
Tags:
ApprovedGraphsGreedy AlgorithmsMediumOpenSegment TreesTrees