Advertisement
Galebickosikasa

Untitled

Feb 27th, 2021 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FASTER() ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  4. #define ff first
  5. #define ss second
  6. #define pb push_back
  7. #define all(a) a.begin(), a.end()
  8. #define dbg(x) cerr<<#x<<" : "<<x<<endl
  9.  
  10. typedef long long ll;
  11.  
  12. #define int ll
  13.  
  14. using namespace std;
  15.  
  16. inline int get (int l, int r, vector<int>& pref) {
  17. if (l == 0) return pref[r];
  18. return pref[r] - pref[l - 1];
  19. }
  20.  
  21. signed main() {
  22. FASTER();
  23. int n;
  24. cin >> n;
  25. vector <int> v(n);
  26. vector <int> pref(n);
  27. for(auto &i : v) {
  28. cin >> i;
  29. }
  30. sort(all(v));
  31. pref[0] = v[0];
  32. for(int i = 1; i < n; i++) {
  33. pref[i] = pref[i - 1] + v[i];
  34. }
  35. // for (auto& x: v) dbg (x);
  36. int sum = 0;
  37. for (auto& x: v) sum += abs (x);
  38. int k, x = 0;
  39. cin >> k;
  40. for(int i = 0; i < k; i++) {
  41. int temp;
  42. cin >> temp;
  43. x += temp;
  44. int help = -x;
  45. dbg (x);
  46. ll ans = 0;
  47. if(x > 0) {
  48. help++;
  49. auto r = (int)(upper_bound(all(v), 0) - v.begin() - 1);
  50. auto l = (int)(lower_bound(all(v), help) - v.begin());
  51. // dbg (l);
  52. // dbg (r);
  53. int len = r - l + 1;
  54. if (l > 0) ans += abs (get (0, l - 1, pref)) - x * l;
  55. if (r != n - 1) ans += get (r + 1, n - 1, pref) + x * (n - r - 1);
  56. if (len > 0) {
  57. ans += x * len + get (l, r, pref);
  58. }
  59. } else if(x < 0) {
  60. help--;
  61. auto r = (int)(upper_bound(all(v), help) - v.begin() - 1);
  62. auto l = (int)(lower_bound(all(v), 0) - v.begin());
  63. // dbg (l);
  64. // dbg (r);
  65. int len = r - l + 1;
  66. if (l > 0) ans += abs (get (0, l - 1, pref)) - x * l;
  67. if (r != n - 1) ans += get (r + 1, n - 1, pref) + x * (n - r - 1);
  68. dbg (ans);
  69. if (len > 0) {
  70. ans += -x * len - get (l, r, pref);
  71. }
  72. } else {
  73. ans = sum;
  74. }
  75. cout << ans << "\n";
  76. }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement