Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- constexpr int N = 5e5 + 5;
- int n, q;
- ll a[N], b[N];
- void Read()
- {
- cin >> n >> q;
- for (int i = 1; i <= n; ++i)
- cin >> a[i];
- }
- ll Get(ll v)
- {
- int id = lower_bound(a + 1, a + n + 1, v) - a;
- return v * (id - 1) - b[id - 1] + (b[n] - b[id - 1]) - v * (n - id + 1);
- }
- void Solve()
- {
- ll sum(0);
- sort(a + 1, a + n + 1);
- for (int i = 1; i <= n; ++i)
- b[i] = b[i - 1] + a[i];
- b[n + 1] = b[n];
- cout << Get(sum) << "\n";
- for (int i = 1; i <= q; ++i)
- {
- char c;
- ll v;
- cin >> c >> v;
- if (c == 'A')
- sum -= v;
- else
- sum += v;
- cout << Get(sum) << "\n";
- }
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement