Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <utility>
- #include <set>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- bool comp(pair <ll, pair<ll, ll> > a, pair <int, pair<ll, ll> > b) {
- if (a.second.second > b.second.second)
- return true;
- else if (a.second.second < b.second.second)
- return false;
- else {
- if (a.second.first < b.second.first)
- return true;
- return false;
- }
- }
- signed int main() {
- ll n;
- cin >> n;
- vector <pair < ll, pair<ll, ll> > > v(n);
- for (int i = 0; i < n; i++) {
- int a, b;
- cin >> a >> b;
- v[i] = { i, {a, b} };
- }
- sort(v.begin(), v.end(), comp);
- set <ll> s;
- for (int i = 1; i < n + 1; i++)
- s.insert(i);
- vector <ll> curr(n + 1, -1);
- vector <ll> st;
- long long ans = 0;
- for (int i = 0; i < n; i++) {
- set<ll>::iterator t = s.lower_bound(v[i].second.first);
- if (*t == v[i].second.first) {
- //c++;
- s.erase(*t);
- curr[v[i].first] = *t;
- v[i].first = -1;
- continue;
- }
- else if (t == s.begin()) {
- if (*t == v[i].second.first) {
- //c++;
- s.erase(*t);
- curr[v[i].first] = *t;
- v[i].first = -1;
- }
- else {
- ans += v[i].second.second;
- }
- }
- else {
- //c++;
- t--;
- s.erase(*t);
- curr[v[i].first] = *t;
- v[i].first = -1;
- }
- }
- cout << ans << endl;
- for (int i = 0; i < n; i++) {
- if (v[i].first != -1) {
- auto t = s.begin();
- curr[v[i].first] = *t;
- s.erase(*t);
- }
- }
- for (int i = 0; i < n; i++)
- cout << curr[i] << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement