#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool compare (pair<string, int> a, pair<string, int> b){
if(a.second > b.second) return true;
return false;
}
vector<int> solution(vector<string> genres, vector<int> plays) {
vector<int> answer;
map<string, int> m;
map<string, vector<pair<int,int>>> v;
for(int i=0;i<genres.size();i++)
{
m[genres[i]] += plays[i];
if(v[genres[i]].size() == 0)
v[genres[i]].push_back({i,plays[i]});
else if(v[genres[i]].size() == 1){
if(v[genres[i]][0].second > plays[i])
v[genres[i]].push_back({i,plays[i]});
else
v[genres[i]].insert(v[genres[i]].begin(),{i,plays[i]});
}else{
if(v[genres[i]][0].second < plays[i]){
v[genres[i]].insert(v[genres[i]].begin(),{i,plays[i]});
v[genres[i]].erase(v[genres[i]].end()-1);
}else if(v[genres[i]][1].second < plays[i])
v[genres[i]][1] = {i,plays[i]};
}
}
vector<pair<string,int>> tmp(m.begin(), m.end());
sort(tmp.begin(), tmp.end(), compare);
for(auto it=tmp.begin();it != tmp.end();it++)
{
for(int j=0;j < v[it->first].size();j++)
{
answer.push_back(v[it->first][j].first);
}
}
return answer;
}