0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 20:08:47 +00:00

D - ±1 Operation 2

https://atcoder.jp/contests/abc255/submissions/32405572
This commit is contained in:
Baoshuo Ren 2022-06-11 21:13:43 +08:00
parent 47ecb8ab73
commit 46e16f5913
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

42
AtCoder/ABC255/D/D.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2e5 + 5;
int n, q, x, a[N];
long long s[N], s2[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
std::sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
}
while (q--) {
cin >> x;
if (x <= a[1] || x >= a[n]) {
cout << std::abs(s[n] - 1ll * x * n) << endl;
} else {
int p = std::lower_bound(a + 1, a + 1 + n, x) - a - 1;
cout << (1ll * x * p - s[p]) + (s[n] - s[p] - 1ll * x * (n - p)) << endl;
}
}
return 0;
}