Magednetic Raysonance
You are a technician for MRIMRI (Maged & Ray's Installation for Magnetic Resonance Imaging), tasked with travelling to a hospital and fixing their faulty MRI machine.
The MRI machine can be calibrated to one of stable resonance frequencies
. It can only ever be calibrated to one of these frequencies, or the machine will explode. The machine in the hospital is currently operating at frequency
, but you want to recalibrate it to frequency
in order to make it operational for hospital use.
and
are among the stable frequencies.
You may manually recalibrate the MRI machine. If the device is currently at stable frequency , you may calibrate it to any other stable frequency
with required cost
.
In addition to this, you have calibration modules that can be used any number of times to make certain calibrations at different costs. Each module is one of two kinds:
Type 1 - Compressor. A module is described by . If the device's current frequency
satisfies
, you may recalibrate it to
at cost
. It is guaranteed that
is among the stable frequencies.
Type 2 - Expander. A module is described by . If the device's current frequency is exactly
, you may recalibrate it to any
at cost
. It is guaranteed that
is among the stable frequencies, and you can only pick targets among the stable frequencies.
Find the minimum total cost to successfully recalibrate the MRI machine to frequency .
Input
The first line contains two integers (
), the number of stable frequencies and the number of available calibration modules.
The next line contains space-separated integers
(
), the value of each of the stable frequencies. It is guaranteed that each stable frequency is distinct, and it is guaranteed
.
The next line contains two integers (
), the initial frequency of the MRI machine and the desired end frequency.
The next lines each detail one of the calibration modules. Each line contains
space-separated integers. The first is
for compressor-type modules or
for expander-type modules. Thus, the lines are one of the two formats:
0 L R Y C(,
,
)
1 X L R C(,
,
)
Output
Output the minimum total cost to recalibrate the MRI machine from starting frequency to desired frequency
using any sequence of the operations above.
Example
Input 1
5 1
10 20 30 50 80
10 80
0 15 35 80 7
Output 1
17
The machine starts at frequency . The only module is a compressor that can be used from any stable frequency in
, sending the machine directly to
for cost
.
As a baseline, you could manually recalibrate directly to for a cost of
. However, you could use the compressor to achieve this at a cheaper cost. The stable frequencies within the range are
and
. You can manually recalibrate to
at cost
, and then use the compressor to recalibrate directly to
, which is the target frequency, at cost
. Thus the total minimum cost is
.
Input 2
8 3
3 8 14 21 34 55 56 67
3 56
1 3 10 30 4
0 13 15 55 2
0 20 40 55 20
Output 2
7
The first module is an expander, taking frequency to any stable frequency in
for cost
. The possible destinations are
.
The second module is a compressor, taking any stable frequency in to
for cost
. This module can thus only be used from stable frequency
.
The third module is a compressor, taking any stable frequency in to
for cost
. This module can thus only be used from stable frequencies
and
.
The optimal sequence is as follows: starting from , use the expander to recalibrate the machine to
at cost
. Then, use the second compressor to recalibrate the machine to
at cost
. Then manually recalibrate the machine from
to
at cost
, reaching the target frequency for total cost
.
Input 3
6 2
10 20 30 50 80 82
50 80
0 15 25 82 2
1 50 18 22 4
Output 3
8
The optimal sequence is taking the second module (expander) from to
at cost
, then taking the first module (compressor) from
to
at cost
. Then, manually recalibrate from
to
at cost
.
Comments