Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define FASTER() ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
- #define ff first
- #define ss second
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define dbg(x) cerr<<#x<<" : "<<x<<endl
- typedef long long ll;
- #define int ll
- using namespace std;
- inline int get (int l, int r, vector<int>& pref) {
- if (l == 0) return pref[r];
- return pref[r] - pref[l - 1];
- }
- signed main() {
- FASTER();
- int n;
- cin >> n;
- vector <int> v(n);
- vector <int> pref(n);
- for(auto &i : v) {
- cin >> i;
- }
- sort(all(v));
- pref[0] = v[0];
- for(int i = 1; i < n; i++) {
- pref[i] = pref[i - 1] + v[i];
- }
- // for (auto& x: v) dbg (x);
- int sum = 0;
- for (auto& x: v) sum += abs (x);
- int k, x = 0;
- cin >> k;
- for(int i = 0; i < k; i++) {
- int temp;
- cin >> temp;
- x += temp;
- int help = -x;
- dbg (x);
- ll ans = 0;
- if(x > 0) {
- help++;
- auto r = (int)(upper_bound(all(v), 0) - v.begin() - 1);
- auto l = (int)(lower_bound(all(v), help) - v.begin());
- // dbg (l);
- // dbg (r);
- int len = r - l + 1;
- if (l > 0) ans += abs (get (0, l - 1, pref)) - x * l;
- if (r != n - 1) ans += get (r + 1, n - 1, pref) + x * (n - r - 1);
- if (len > 0) {
- ans += x * len + get (l, r, pref);
- }
- } else if(x < 0) {
- help--;
- auto r = (int)(upper_bound(all(v), help) - v.begin() - 1);
- auto l = (int)(lower_bound(all(v), 0) - v.begin());
- // dbg (l);
- // dbg (r);
- int len = r - l + 1;
- if (l > 0) ans += abs (get (0, l - 1, pref)) - x * l;
- if (r != n - 1) ans += get (r + 1, n - 1, pref) + x * (n - r - 1);
- dbg (ans);
- if (len > 0) {
- ans += -x * len - get (l, r, pref);
- }
- } else {
- ans = sum;
- }
- cout << ans << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement