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 calc(int id, ll x){
return abs(dif[id]+x);
}
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];
}
}
int l = 0, r = k-1;
ll cur;
ll s1, s2;
ll soln = (ll)1e18;
for(int i = 0 ; i < n; i ++ ){
cur = -dif[i];
while(l < i && r + 1 < n){
if(r < i || calc(l,cur) + calc(r,cur) >= calc(l+1,cur) + calc(r+1,cur)){
l++;
r++;
}
else{
break;
}
}
s1 = pf[i];
if(l > 0) s1 -= pf[l-1];
s1 += (i-l+1) * 1ll * cur;
s2 = pf[r]-pf[i];
s2 += (r-i) * 1ll * cur;
soln = min(soln,s2-s1);
}
cout << soln << "\n";
return 0;
}
Compilation message (stderr)
simfonija.cpp: In function 'int main()':
simfonija.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | 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... |