-
Notifications
You must be signed in to change notification settings - Fork 13
/
make_pair.cpp
executable file
·41 lines (39 loc) · 941 Bytes
/
make_pair.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
int main()
{
int tests;
cin>>tests;
while(tests--) {
vector<pair<int,int>> books;
vector<int> smallest;
int prev=-1;
long long curr=0;
long long rem=0;
long long total=0;
int num;
cin>>num;
for(int i=0;i<num;i++) {
int auth,happ;
cin>>auth>>happ;
books.push_back(make_pair(auth,happ));
}
sort(books.begin(),books.end()); //Groups books by same author together. Within the same groups, books are sorted from smallest happiness to largest happiness.
for(const auto& book : books) {
if(book.first==prev) {
rem+=book.second;
} else {
smallest.push_back(book.second);
prev=book.first;
}
}
//pick smallest book from each author
sort(smallest.begin(),smallest.end()); //sort these books from smallest to largest.
for(const auto& small:smallest) {
total+=(++curr)*small;
}
total+=rem*curr;
cout<<total<<'\n';
}
return 0;
}