Spotify Blend
The data analysts at Spotify have calculated the top songs for two people, person
and person
. For each song, they also computed a preference score, representing how much that person likes the song.
Given this data, you, an engineer at Spotify, are tasked with creating a 'blended' playlist with exactly songs, as follows:
Each person must contribute exactly songs to the blend.
is guaranteed to be even.
For any song :
is
's preference score for song
is
's preference score for song
If a song does not appear in one person's top- list, that person's preference score for the song is considered to be
.
A song can be contributed by person
only if
and by person
only if
. Thus, if preference scores are equal, the song may be contributed by either person.
The overall score of song if it were in the blended playlist is
. The score of the playlist is the sum of these scores of all selected songs. You are tasked with finding a blended playlist that maximises the total score so you can satisfy both people, as well as your manager.
Input
The first line contains two integers, and
. We additionally have that
, and
must be even.
The next lines contain the top-
list of person
. Each of the next
lines contain a string
and an integer
, representing the name of the song and the preference score of that song. The song name consists only of upper and lowercase English characters, and digits
-
. The song names are unique within each persons top-
list. These lines are given in decreasing order of
.
The next lines are given in the same format, but represent the top-
list of person
.
Output
Output any valid blend with exactly songs (in any order) such that each song appears at most once, exactly
songs are contributed by person
, exactly
songs are contributed by person
, and the total playlist score is maximized. If it is impossible to construct a valid blend, output
-1.
Specifically, you should output lines, where each line is of the form
song_i contributor. song_i is the song name, and contributor is who contributes the song (it should be a single character A or B).
Example
Input 1
2 2
despacito 5
gangnamstyle 3
baby 4
despacito 1
Output 1
despacito A
baby B
The blended playlist needs song from
and
song from
. The best choice is
despacito contributed by (score
) and
baby contributed by B (score ), for a total of
.
Input 2
4 3
good4u 5
airplanes 4
ghostbusters 1
good4u 5
peaches 4
disturbia 1
Output 2
good4u A
airplanes A
peaches B
disturbia B
We need 2 songs from each person. Here good4u has equal scores, so it can be contributed by either or
. The shown blend has total score
. Other optimal blends are possible, including swapping who contributes
good4u, or simply reordering the output lines, as long as the same multiset of songs and contributors is valid.
Input 3
6 4
aa 9
bb 8
cc 7
dd 1
bb 10
cc 7
ee 6
ff 2
Output 3
aa A
cc A
dd A
bb B
ee B
ff B
We need 3 songs from each person. cc is the only song that can go either way; to reach 3 songs from A, it must be contributed by A. The total score is (each term is
). Other valid outputs can reorder the lines but must keep the same contributor assignment for
cc to stay feasible.
Input 4
4 4
Hello 5
RIPMyGranny 4
Cupid 4
Phonk 2
RIPMyGranny 3
Hello 2
Cupid 1
Goodbye 1
Output 4
-1
It is impossible for each person to contribute songs, as three songs are shared between both people and person
has a higher preference score for all three of them, so person
is only able to contribute a single song —
Goodbye.
Comments