Gold Camp Forcefield
Time limit1sMemory limit256 MB
Choose a contiguous group of camps whose total energy covers the distance between its ends to maximize total gold.
- Level
Medium7 of 10
- Topics
- Segment tree, Prefix sum, Sorting
- Solved
- No attempts yet
Problem
Mansur plays a new computer strategy game. The main job in a game like this is mining resources. In this game only one resource matters for development, gold, and there is one supporting resource, energy.
The game has mining camps. Each camp supplies a fixed amount of gold and a fixed amount of energy, and all camps sit on one straight line. Camp is at coordinate and supplies gold and energy.
To protect camps, Mansur builds a forcefield. A forcefield is a closed segment on that line, and it needs an amount of energy equal to its length. A camp is protected when it lies inside the segment or on one of its endpoints, and only protected camps supply energy to the forcefield. The segment may have length .
Mansur builds one forcefield. The total energy of the protected camps must be at least the energy the forcefield needs, and the total gold of the protected camps must be as large as possible.
Write a program that finds the largest total gold Mansur can obtain from the protected camps.
Input
The first line contains one integer , the number of camps. Each of the next lines contains three space separated integers , , : the coordinate of the camp, the gold it supplies, and the energy it supplies.
All are distinct and are given in increasing order.
Output
Print the largest total gold Mansur can obtain on one line.