Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <vector>
- #include <algorithm>
- #include <limits>
- #define ll long long
- using namespace std;
- /*
- 4
- 3 5
- 3 5
- 3 5
- 3 5
- */
- bool compare(pair<int, pair<int, int>> p1, pair<int, pair<int, int>> p2) {
- return (p1.second.first < p2.second.first || p1.second.first == p2.second.first && p1.second.second > p2.second.second ? true : false);
- }
- char main() {
- int n;
- cin >> n;
- vector<pair<int, pair<int, ll>>> vec(n + 1);
- vector<pair<ll, ll>> shtraf;
- vector<ll> ans(200000 + 1, 0);
- set<int> isFree;
- for (int i = 1; i <= 200000; i++) isFree.insert(i);
- for (int i = 1; i <= n; i++) {
- vec[i].first = i;
- cin >> vec[i].second.first >> vec[i].second.second;
- }
- sort(++vec.begin(), vec.end(), compare);
- for (int x = 1; x <= n; x++) {
- int ii = vec[x].first, di = vec[x].second.first, wi = vec[x].second.second;
- auto it = isFree.lower_bound(di);
- if (it == isFree.end()) {
- it--;
- ans[ii] = *it;
- isFree.erase(it);
- continue;
- }
- if (*it == di) {
- ans[ii] = di;
- isFree.erase(it);
- continue;
- }
- if (it == isFree.begin()) {
- shtraf.push_back(make_pair(ii, wi));
- continue;
- }
- it--;
- ans[ii] = *it;
- isFree.erase(it);
- }
- int j = 0;
- ll bil = 0;
- auto it = isFree.begin();
- for (int i = 1; i <= n; i++) {
- if (ans[i] == 0) {
- ans[i] = *it;
- bil += shtraf[j].second;
- j++;
- it++;
- }
- if (it == isFree.end()) break;
- }
- cout << bil << endl;
- for (int x = 1; x <= n; x++) {
- if (ans[x] != 0) {
- cout << ans[x] << ' ';
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment