#include <bits/stdc++.h>
#define in(x) freopen(x,"r",stdin)
#define out(x) freopen(x,"w",stdout)
#define F first
#define S second
#define pb push_back
#define M ll(998244353)
#define inf ll(2e9+7)
#define N ll(200500)
#define sz(x) x.size()
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
#define el '\n'
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> os;
ll a[N], b[N], i, n, k, ans = 1e18, kl, sm, sum, pr[N], l, r;
vector <ll> ve;
int main()
{
// in("input.txt");
// out("output.txt");
ios_base::sync_with_stdio(false);
iostream::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= n; i++) cin >> b[i];
for (i = 1; i <= n; i++) ve.pb(a[i] - b[i]);
sort(ve.begin(), ve.end());
pr[0] = ve[0];
for (i = 1; i < n; i++) pr[i] = pr[i - 1] + ve[i];
l = 0;
r = n - k - 1;
while (r <= n - 1)
{
int md = (l + r) >> 1;
ll left = ve[md] * (md - l + 1) - (pr[md] - ((l - 1 >= 0) ? pr[l - 1] : 0));
ll right = (pr[r] - pr[md]) - ve[md] * (r - md);
ans = min(ans, left + right);
l++;
r++;
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
3568 KB |
Output is correct |
2 |
Correct |
37 ms |
3696 KB |
Output is correct |
3 |
Correct |
35 ms |
3568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
3696 KB |
Output is correct |
2 |
Correct |
37 ms |
3568 KB |
Output is correct |
3 |
Correct |
36 ms |
3568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
3568 KB |
Output is correct |
2 |
Correct |
34 ms |
3704 KB |
Output is correct |
3 |
Correct |
36 ms |
3568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
3576 KB |
Output is correct |
2 |
Correct |
33 ms |
3568 KB |
Output is correct |
3 |
Correct |
35 ms |
3704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
3696 KB |
Output is correct |
2 |
Correct |
41 ms |
3696 KB |
Output is correct |
3 |
Correct |
41 ms |
3568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
3696 KB |
Output is correct |
2 |
Correct |
36 ms |
3576 KB |
Output is correct |
3 |
Correct |
37 ms |
3576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
3568 KB |
Output is correct |
2 |
Correct |
35 ms |
3568 KB |
Output is correct |
3 |
Correct |
39 ms |
3656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
3580 KB |
Output is correct |
2 |
Correct |
40 ms |
3696 KB |
Output is correct |
3 |
Correct |
39 ms |
3696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
3568 KB |
Output is correct |
2 |
Correct |
35 ms |
3576 KB |
Output is correct |
3 |
Correct |
39 ms |
3568 KB |
Output is correct |