➤ Problem Link : ADAFRIEN
👉 Hint : Think about using both hashing and sorting
✅ C++ Solution :
#include<bits/stdc++.h> using namespace std; #define ll long long int int main() { string str; ll q,k,e,ans=0; cin>>q>>k; unordered_map<string,ll> m; multiset<ll,greater<ll> > s; for(ll i=1;i<=q;i++) { cin>>str>>e; m[str]+=e; } for(auto it=m.begin();it!=m.end();it++) s.insert((*it).second); ll i=0; for(auto it=s.begin();it!=s.end() && i<k;it++) { ans+=*it; i++; } cout<<ans<<endl; }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!