Flappy Bird
Time limit4sMemory limit1024 MB
Find the maximum total weight of axis-parallel segments Anna can touch on a rightward flight that changes height once inside the box 0 to W by 0 to H.
- Level
Hard8 of 10
- Topics
- Segment tree, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem

Have you heard of Flappy Bird, the game that was briefly popular in 2014? A bird flies forward and keeps rising and falling so that it does not leave the screen or hit a pipe, going as far as it can.
The bird in Flappy Bird is actually Anna, a pet bird raised by Gyojun.
In a two-dimensional world, Gyojun and Anna are playing a game similar to Flappy Bird. As Anna flies around the sky and passes through line segments, she earns points equal to the sum of the weights of those segments.
Here is a precise description of the game.
On a two-dimensional plane, there are line segments, each parallel to the -axis or the -axis. The weight of segment is , and its two endpoints are and . ()
Anna starts flying in the direction from any point on the line , and stops when she reaches the line . For safety reasons, however, Anna cannot fly below or above . In other words, Anna can only fly within the region , .
Anna flies parallel to the -axis, so she cannot change her coordinate while flying straight. With Gyojun's help, however, she can change her coordinate exactly once during the flight by rising or falling parallel to the -axis. Her coordinate does not change during the rise or fall. Note that she cannot both rise and fall.
The score Anna earns is the sum of the weights of all segments that share at least one point with her flight path.
For example, suppose , , and segments are placed as shown below.

The solid blue lines are the segments, and the dotted line is Anna's path. The number in each circle is the segment number, and the signed number is its weight.
If Anna starts at , rises from to , and then finishes at , she passes segments 0, 1, 2, 4, and 6, so she earns points.
But if Anna starts at , falls from to , and then finishes at , she passes segments 0, 2, and 5, so she earns point.
Given the information about the segments, write a program that finds the maximum score Anna can earn.
Constraints
- All numbers in the input are integers.
- For every , or .
- Every segment has a positive length. That is, for every , .