#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
vector<ll> dif;
vector<ll> pf;
ll compute(int l, int r, ll x){
int it = lower_bound(dif.begin(), dif.end(), -x) - dif.begin();
int i1 = min(it-1,r);
ll s1 = 0;
if(l <= i1){
s1 += pf[i1];
if(l>0)
s1 -= pf[l-1];
s1 += (i1-l+1) * 1ll * x;
}
ll s2 = 0;
it = max(it, l);
if(it <= r){
s2 += pf[r];
if(it>0)
s2 -= pf[it-1];
s2 += (r-it+1) * 1ll * x;
}
return s2-s1;
}
int main(){
fastIO;
int n, k;
cin >> n >> k;
k = n-k;
if(k == 0){
cout << 0 << "\n";
return 0;
}
ll a;
for(int i = 0 ; i < n; i ++ ){
cin >> a;
dif.push_back(a);
}
for(int i = 0 ; i < n; i ++ ){
cin >> a;
dif[i] -= a;
}
sort(dif.begin(), dif.end());
for(int i = 0 ; i < dif.size(); i ++ ){
pf.push_back(dif[i]);
if(i){
pf[i] += pf[i - 1];
}
}
vector<ll> gg;
for(int i = 0 ; i < dif.size(); i ++ ){
gg.push_back(-dif[i]);
}
sort(gg.begin(), gg.end());
int r = n - 1;
int l = n - k;
ll cur;
ll soln = (ll)1e18;
for(int i = 0 ; i < n; i ++ ){
cur = gg[i];
while(l > 0 && compute(l,r,cur) >= compute(l-1,r-1,cur)){
l--;
r--;
}
soln = min(soln, compute(l, r, cur));
}
cout << soln << "\n";
return 0;
}
Compilation message
simfonija.cpp: In function 'int main()':
simfonija.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 0 ; i < dif.size(); i ++ ){
| ~~^~~~~~~~~~~~
simfonija.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 0 ; i < dif.size(); i ++ ){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
3436 KB |
Output is correct |
2 |
Correct |
34 ms |
3456 KB |
Output is correct |
3 |
Correct |
32 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
3436 KB |
Output is correct |
2 |
Correct |
34 ms |
3436 KB |
Output is correct |
3 |
Correct |
34 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
3436 KB |
Output is correct |
2 |
Correct |
34 ms |
3436 KB |
Output is correct |
3 |
Correct |
31 ms |
3496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
3436 KB |
Output is correct |
2 |
Correct |
38 ms |
3436 KB |
Output is correct |
3 |
Correct |
40 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
3468 KB |
Output is correct |
2 |
Correct |
52 ms |
4844 KB |
Output is correct |
3 |
Correct |
50 ms |
4844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
3564 KB |
Output is correct |
2 |
Correct |
39 ms |
3464 KB |
Output is correct |
3 |
Correct |
45 ms |
4844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
3436 KB |
Output is correct |
2 |
Correct |
39 ms |
4844 KB |
Output is correct |
3 |
Correct |
51 ms |
5132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
3436 KB |
Output is correct |
2 |
Correct |
53 ms |
4844 KB |
Output is correct |
3 |
Correct |
44 ms |
4844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
3436 KB |
Output is correct |
2 |
Correct |
37 ms |
4940 KB |
Output is correct |
3 |
Correct |
47 ms |
4844 KB |
Output is correct |