#P4971. Calculating Sin Value
Calculating Sin Value
Calculating Sin Value
A person's final sin value \(E\) is determined by the bad and good deeds performed during their lifetime together with the manner of death. All bad deeds have an associated sin value. Sometimes several bad deeds influence each other by being grouped into a set: the sin value of a set is defined as the maximum sin value of all deeds in that set. After all the deeds, the overall sin value \(E\) is the sum of the sin values of all sets.
There are four types of actions performed during life:
- BAD: Commit a bad deed with a given sin value. This deed forms a new set on its own.
- GOOD: Commit a good deed which clears (sets to 0) the sin value of the current maximum deed in a specified set.
- REPENT: Repent for a deed by reducing the sin value of the current maximum deed in a specified set by a given amount \(d\) (but not below 0).
- MERGE: Realize and merge two bad deed sets. The resulting set's sin value is the maximum of the sin values from both sets.
After a lifetime of deeds, the manner of death further affects the final sin value. There are three possibilities:
- NATURAL: No additional effect.
- ACCIDENT: The set with the maximum sin value is exempted (i.e. its value is removed from the sum).
- SUICIDE: The set with the maximum sin value has its sin value doubled.
Given a sequence of operations and a death mode, compute the final sin value \(E\) according to the rules above.
inputFormat
The input begins with an integer \(n\) representing the number of operations. Following this are \(n\) lines, each describing an operation. The operations can be one of the following forms:
BAD v
– Commit a bad deed with sin value \(v\) (an integer). A new set is created containing this deed.GOOD i
– In the active set with ID \(i\), clear the sin value of the deed currently contributing the maximum value (set that value to 0).REPENT i d
– In the active set with ID \(i\), reduce the sin value of the deed with the maximum sin value by \(d\) (minimum value is 0).MERGE i j
– Merge the active sets with IDs \(i\) and \(j\). After merging, the merged set takes the ID \(i\) and the sin values from set \(j\) are merged into it; set \(j\) is then removed from the active sets. The new set's effective sin value is the maximum among all its deeds.
After the \(n\) operations, there is one more line containing the death mode: one of NATURAL
, ACCIDENT
, or SUICIDE
.
Note: The sets are identified by positive integers in the order they are created. All operations reference valid active set IDs.
outputFormat
Output a single integer, the final sin value \(E\) after applying all operations and the death effect.
sample
5
BAD 10
BAD 20
MERGE 1 2
REPENT 1 5
GOOD 1
NATURAL
10