luogu#P8288. 「DAOI R1」Fireworks
「DAOI R1」Fireworks
Background
Bow your head, and the whole city shines with lights.
Look back, and meteors fly backward across the sky.
Problem Description
People used to set off fireworks, and each firework has its own beauty value.
wants to set off fireworks outdoors, but there are some relationships between fireworks:
-
Relationship 1: For a firework , there is a corresponding firework . If firework and firework are set off together, the beauty value of firework will decrease by .
-
Relationship 2: Some fireworks belong to a series and must be set off at the same time. One of them is the main firework. Each firework belongs to at most one series.
In particular, suppose there is a series (with main firework ). The firework corresponding to in Relationship 1 is a firework in series . Meanwhile, the other fireworks in series form Relationship 1 with fireworks not in series or . Then for such a Relationship 1, it will not reduce the beauty value.
has fireworks at home. He wants to choose some of them to set off, so that the sum of the beauty values of these fireworks is maximized.
Input Format
The first line contains two integers , which denote the number of fireworks and the number of Relationship 2 groups.
The next lines each contain three integers , which are the beauty value of this firework, the corresponding firework in Relationship 1, and the amount of beauty value reduced by Relationship 1.
The last lines: each line first reads two numbers , followed by numbers, meaning these fireworks form a series, and the firework with index is the main firework.
Output Format
Output one line with one integer, representing the total sum of beauty values.
3 0
2 2 1
2 3 1
2 1 1
3
4 1
3 2 1
3 1 3
3 4 2
3 3 2
1 2 1 3
7
Hint
Sample Explanation
Sample 1 Explanation
Set off fireworks together. The maximum beauty value is .
Sample 2 Explanation
Set off fireworks together.
Since are in the same series and is the main firework, Relationship 1 of firework does not take effect.
So the total beauty value is .
Constraints
This problem uses bundled testdata.
| Subtask | Score | |
|---|---|---|
| No special restrictions |
For of the data, it holds that , , , .
Translated by ChatGPT 5