#P8461. 「REOI-p1」回忆
「REOI-p1」回忆
Background

Problem setter: LinkyChristian.
Story writer: Xiao Nuomi (xiaonuomi).
Problem Description
"That said, we did not roam around and let the fairies be born one by one.
We only carved spell marks onto the huge soul body used as material, so that they could naturally be born with a physique and personality close to humans."
A golden fairy is a "person" who is cursed with early death from the moment of birth. If not for being a monster that cannot resist it, she should never have been born in this vast mortal world, but rather as a weapon.
Whenever memories of a previous life surge up in a fairy, it is the day the countdown begins for a budding flower to wither. A fairy's memory is made up of a ray. For convenience, we may treat this ray as a number line. In her memory, there are several intervals within which memories are very easily eroded by memories of a previous life. However, since a creature's memory itself cannot be clearly observed, we can only know start intervals and end intervals . In this memory, various previous-life memories will gradually emerge and erode earlier memories. When all these easily-eroded memory intervals are completely eroded, the golden fairy will "disappear". These previous-life memories can be regarded as non-overlapping subsegments on the number line. The start point and end point of each subsegment lie respectively inside one start interval and one end interval. Also, due to the nature of memory, the start and end points of different previous-life memories naturally lie in different start intervals and different end intervals.
Memory is like a rushing stream: the water babbles and flows downstream, stirring stones and silt, rising and sinking on and under the surface. Upstream, there may be steep rocks and boulders, heavy in their own right, so the water is clear and calm; the closer it gets to the sea, stones are gradually replaced by silt. Even a light breeze making small waves can stir up a muddy mess, tangling the memories into confusion. Each erosion of memory is like a surging wave: perhaps it stirs the silt, or perhaps it only nudges a boulder slightly. In numeric terms, matching the -th start interval brings a contribution of , and matching the -th end interval brings a contribution of . When these contributions reach the maximum value, the so-called "completely eroded" will happen. Now the fairies want to know what this maximum value is.
In particular, if the testdata has no solution, output .
Simplified statement:
Given start intervals and end intervals . Choose non-overlapping subsegments on the number line such that the start point and end point of each subsegment lie in a start interval and an end interval, respectively. The start points of different subsegments must lie in different start intervals, and the end points of different subsegments must lie in different end intervals. Matching the -th start interval yields contribution , and matching the -th end interval yields contribution . The total contribution equals the sum of lengths of the chosen subsegments plus the contributions brought by matched intervals. Find the maximum possible total contribution.
Note: The length of a subsegment is defined as . Two subsegments "overlap" means there exists an interval of length that is contained in both subsegments.
If there is no solution, output .
Input Format
The first line contains three integers .
The second line contains integers representing .
The third line contains integers representing .
The fourth line contains numbers representing .
The fifth line contains numbers representing .
Output Format
Output one integer, the maximum total contribution.
2 2 2
1 3 7 8
4 5 9 10
0 0
0 0
7
2 3 3
1 2 3 5 100 200
5 7 9 10 400 500
1000 1000 0
1000 1000 0
4009
2 2 2
1 2 4 5
7 7 3 10
2 1
3 2
14
2 2 2
1 2 4 5
6 7 8 9
12 33
23 1
-1
Hint
For the first sample, match the start interval with the end interval , choose subsegment with length . Then match the start interval with the end interval , choose subsegment with length . The total chosen length is . This satisfies that there are matches between start and end intervals and the chosen subsegments do not overlap, and the total contribution is maximized.
For the second sample, match with , and match with , choosing subsegments and .
For the third sample, match with , and match with , choosing subsegments and .
subtask1: For of the testdata, .
subtask2: For of the testdata, , and all values in the problem are .
Translated by ChatGPT 5