Avid Autoplay
You are designing a new music app for your startup called Bungus, and one of your killer features is an experimental autoplay for playlists, designed to make song transitions as gentle as possible by only transitioning between similar songs.
Specifically, you have songs, listed from song
to song
, that you want to use to create a playlist, and you can include any song any amount of times (including zero). However, the playlist must begin with the first song (song
) and end with the final song (song
). Each song has an energy value
, and when you transition between two songs a dissatisfaction of
is incurred.
Note that customers remember playlists based on their most extreme memories, so the overall dissatisfaction incurred is the maximum single transition dissatisfaction score in the playlist.
Moreover, from previous studies, you have decided that listeners can only tolerate transitions between songs sharing either:
- the same artist;
- the same genre;
- or the same year.
Find the minimum possible overall dissatisfaction score you can incur while arranging the playlist.
Input
The first line contains a single integer
, the amount of songs you have available to create your playlist.
The next lines each contain details of a single song, from song
through to song
.
Each line consists of
,
,
and
, where
is the energy score
and
are strings of upper or lowercase characters between
and
characters long representing the artist and genre, and
is the release year.
Output
Output a single integer — the minimum possible overall dissatisfaction score that can be incurred when transitioning through a created playlist. The dissatisfaction score is the maximum absolute difference between the energy values of any two consecutive songs in the created playlist, and songs can only be arranged consecutively if they share a property. Each song can be used as many times as required (or zero, besides songs and
).
If it is impossible to arrange the playlist while maintaining the above properties, output -1.
Examples
Input 1
4
10 TaylorSwift Pop 2000
25 Eminem HipHop 2001
30 BrunoMars Pop 2001
40 SnoopDogg HipHop 1999
Output 1
20
In this case, an optimal playlist that you can create is 1 3 2 4. works as they are both Pop,
works as they are both released in
, and
works as they are both HipHop. The maximum dissatisfaction score is from
. There is no better way to create a playlist starting at
and ending at
.
Input 2
3
5 JustinBieber RandB 2016
100 LadyGaga Jazz 2016
6 LedZeppelin Rock 1971
Output 2
-1
Song can transition into song
as they are both released in
, but it is impossible for either to transition into song
so the playlist cannot be constructed.
Input 3
5
10 A G1 2000
17 A G2 2001
30 B G1 2002
18 C G2 2002
19 D G3 2002
Output 3
7
There are multiple valid arrangements. One is with max dissatisfaction
. Another is
with max dissatisfaction
. The only songs that can follow
are
(same artist) and
(same genre), so any playlist must incur at least
on its first transition. Therefore, the second path is optimal.
Comments