This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 ++ ){
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |