#include <bits/stdc++.h>
#define int long long
#define float double
#define pb push_back
#define F first
#define S second
#define T int t; cin >> t; while(t--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
/// Benzema in the best player in the world
const int inf = 8e18;
const int N = 4e6 + 6;
const int M = 1e3 + 3;
const int LOG = 31;
const int mod = 1e9 + 7;
const float pi = atan(1) * 4;
int n, k, a[N], b[N];
int cnt[N];
vector<int> v;
main()
{
IOS
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= n; i++)
cin >> b[i];
int neg = 0, pos = 0, negn = 0, posn = 0, zn = 0;
int sum = 0;
for(int i = 1; i <= n; i++)
{
a[i] -= 2e6;
if (a[i] < b[i])
neg += b[i] - a[i], negn++, cnt[b[i] - a[i]]++;
else if (a[i] > b[i])
pos += a[i] - b[i], posn++;
else
zn++;
sum += abs(a[i] - b[i]);
}
int x = 0, lst = sum;
for(int i = 1; i <= 4e6 + 2; i++)
{
neg -= negn;
negn -= cnt[i];
pos += posn + zn;
zn = cnt[i];
if (neg + pos > lst)
break;
lst = neg + pos;
x = i;
}
int ans = 0;
for(int i = 1; i <= n; i++)
a[i] += x, v.pb(abs(a[i] - b[i])), ans += abs(a[i] - b[i]);
sort(v.begin(), v.end(), greater<int>());
for(int i = 0; i < k; i++)
ans -= v[i];
cout << ans;
}
///use scanf
Compilation message
simfonija.cpp:22:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
22 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
340 KB |
Output is correct |
2 |
Incorrect |
5 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
10876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
10780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
10952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
3028 KB |
Output is correct |
2 |
Incorrect |
30 ms |
3120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
10992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
3172 KB |
Output is correct |
2 |
Incorrect |
35 ms |
3276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
11040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
10164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
3276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |