Advertisement
AdelKhalilov

Untitled

Jan 29th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. typedef long long ll;
  7. multiset <int> b;
  8. multiset <int> a;
  9.  
  10.  
  11. int main()
  12. {
  13. int n, k;
  14. ll sum = 0;
  15. int m;
  16. ios::sync_with_stdio(0);
  17. cin.tie(0);
  18. cin >> n;
  19. vector <int> c(n);
  20. cin >> k;
  21. char q;
  22. for (int i = 0; i < n; i++)
  23. {
  24. int r;
  25. cin >> r ;
  26. c[i] = r;
  27. int w = c[i];
  28. b.insert(w);
  29. }
  30. cin >> m;
  31.  
  32. for (int i = 0 ; i < m ;i ++) {
  33. cin >> q;
  34. if ('+' == q)
  35. {
  36. int x;
  37. cin >> x;
  38. x--;
  39. int h = c[x] ;
  40. if (0 >= a.size() || *a.rbegin() < h )
  41. {
  42. int u = c[x] + 1;
  43. u--;
  44. b.insert(u);
  45. }
  46. else
  47. {
  48. int u = c[x] + 1;
  49. u--;
  50. a.insert(u);
  51. sum += u;
  52. }
  53. } else {
  54. if (q == '?') {
  55. while (k < a.size())
  56. {
  57. sum -= *a.rbegin();
  58. b.insert(*a.rbegin());
  59. a.erase(--a.end());
  60. }
  61. while (k > a.size() )
  62. {
  63. sum += *b.begin();
  64. a.insert(*b.begin());
  65. b.erase(b.begin());
  66. }
  67. cout << sum << '\n' ;
  68. } else {
  69. int x;
  70. cin >> x;
  71. x--;
  72. if (a.end() != a.find(c[x]) )
  73. {
  74. sum -= c[x];
  75. a.erase(a.find(c[x]));
  76.  
  77. }
  78. else
  79. {
  80. b.erase(b.find(c[x]));
  81. }
  82. }
  83. }
  84. }
  85. return 0;
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement