#K15241. Maximize Crystal Energy with Spells
Maximize Crystal Energy with Spells
Maximize Crystal Energy with Spells
You are given S spells and C crystals. Each crystal has an initial energy level. There are three types of spells:
- Boost Spell: Represented as
(0, i, e)
. This spell increases the energy of the ith crystal by e. In formula, \(E_i := E_i + e\). - Transfer Spell: Represented as
(1, i, j, x)
. If the ith crystal has at least x energy (i.e. \(E_i \ge x\)), then x energy is transferred from the ith crystal to the jth crystal. Formally, if \(E_i \ge x\), then \(E_i := E_i - x\) and \(E_j := E_j + x\). - Chain Spell: Represented as
(2, a, b, c)
. This spell increases the energy of the ath, bth, and cth crystals by 5 each. That is, \(E_a := E_a + 5\), \(E_b := E_b + 5\), and \(E_c := E_c + 5\).
Your task is to apply the given S spells in order on the crystals and output the maximum total energy of all crystals after processing all spells.
inputFormat
The input is read from standard input (stdin) with the following format:
S C E1 E2 ... EC Spell_1 Spell_2 ... Spell_S
Here, the first line contains two integers: S (the number of spells) and C (the number of crystals). The second line contains C integers representing the initial energies of the crystals. The next S lines each represent a spell. The format for each spell is:
- For a Boost Spell:
0 i e
- For a Transfer Spell:
1 i j x
- For a Chain Spell:
2 a b c
Note: All crystal indices are 1-indexed.
outputFormat
Output a single integer to standard output (stdout): the maximum total energy of the crystals after all spells have been applied.
## sample5 4
10 20 30 40
0 1 5
1 3 4 10
2 1 2 3
0 4 15
1 2 4 20
135