Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Nguyen Huu Hoang Minh
- #include <bits/stdc++.h>
- #define sz(x) int(x.size())
- #define all(x) x.begin(),x.end()
- #define reset(x) memset(x, 0,sizeof(x))
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define N 100005
- #define remain(x) if (x > MOD) x -= MOD
- #define ii pair<int, int>
- #define iiii pair< ii , ii >
- #define viiii vector< iiii >
- #define vi vector<int>
- #define vii vector< ii >
- #define bit(x, i) (((x) >> (i)) & 1)
- #define Task "test"
- #define int long long
- using namespace std;
- typedef long double ld;
- const int inf = 1e10;
- const int minf = -1e10;
- int n;
- struct da{
- int val, id;
- };
- struct db{
- int val, pri, id;
- };
- da a[N];
- db b[N];
- bool cmp1(da a, da b){
- return a.val < b.val;
- }
- bool cmp2(db a, db b){
- return a.val < b.val;
- }
- void readfile()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);cout.tie(0);
- if (fopen(Task".inp","r"))
- {
- freopen(Task".inp","r",stdin);
- //freopen(Task".out","w",stdout);
- }
- cin >> n;
- for(int i=1; i<=n; i++){
- cin >> a[i].val;
- a[i].id = i;
- }
- for(int i=1; i<=n; i++){
- cin >> b[i].val >> b[i].pri;
- b[i].id = i;
- }
- sort(a+1,a+1+n,cmp1);
- sort(b+1,b+1+n,cmp2);
- }
- int res[N];
- void proc()
- {
- priority_queue<ii> q;
- memset(res, -1, sizeof res);
- int cur = n;
- int ans = 0;
- for(int i=n; i>=1; i--){
- while (cur>=1 && b[cur].val >= a[i].val){
- q.push(ii(b[cur].pri,b[cur].id));
- cur--;
- }
- if (q.size()){
- ii tmp = q.top(); q.pop();
- res[a[i].id] = tmp.se;
- ans+=tmp.fi;
- }
- }
- vector<int> pos;
- vector<bool> ap(n+1,false);
- for(int i=1; i<=n; i++) if (res[i]==-1) pos.pb(i);
- for(int i=1; i<=n; i++) if (res[i]!=-1) ap[res[i]]=true;
- int Cur = 0;
- for(int i=1; i<=n; i++){
- if (ap[i]==false){
- res[pos[Cur]] = i;
- Cur++;
- }
- }
- cout << ans << '\n';
- for(int i=1; i<=n; i++) cout << res[i] << ' ';
- }
- signed main()
- {
- readfile();
- proc();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement