DuongNhi99

VODIVIDE

Nov 28th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 5005;
  5.  
  6. struct pack {
  7.     int v, s, in;
  8. };
  9.  
  10. bool cmp_a(pack &a, pack &b) {
  11.     return a.v < b.v;
  12. }
  13.  
  14. int n;
  15. pack ab[N];
  16. long long res;
  17. bool check[N] = {false};
  18.  
  19. void solve() {
  20.     priority_queue<pair<int, int> > pq;
  21.  
  22.     res = 0;
  23.     for(int i = 1; i <= n; i++) {
  24.         pq.push({ab[i].s, i});
  25.  
  26.         if(i%2 == 1) {
  27.             int gt = pq.top().first;
  28.             int cs = pq.top().second;
  29.             pq.pop();
  30.  
  31.             res += gt;
  32.             check[cs] = true;
  33.         }
  34.     }
  35.  
  36.     cout << res << '\n';
  37.  
  38.     stack<int> index;
  39.     for(int i = 1; i <= n; i++) {
  40.         if(check[i]) index.push(ab[i].in);
  41.         else {
  42.             cout << ab[i].in << ' ' << index.top() << '\n';
  43.             index.pop();
  44.         }
  45.     }
  46. }
  47.  
  48. int main() {
  49.     freopen("in.txt", "r", stdin);
  50.     //freopen("VODIVIDE .inp", "r", stdin);
  51.     //freopen("VODIVIDE .out", "w", stdout);
  52.     ios_base::sync_with_stdio(false);
  53.     cin.tie(NULL); cout.tie(NULL);
  54.  
  55.     cin >> n;
  56.     for(int i = 1; i <= n; i++)
  57.         cin >> ab[i].v;
  58.     for(int i = 1; i <= n; i++) {
  59.         cin >> ab[i].s;
  60.         ab[i].in = i;
  61.     }
  62.  
  63.     sort(ab + 1, ab + n + 1, cmp_a);
  64.  
  65.     solve();
  66.  
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment