#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
#define TRACE(x) cerr << #x << " = " << x << endl
typedef long long ll;
typedef pair<int, int> point;
const int mod = 1e9 + 7, inf = 1e9, MAXN = 2e5 + 5;
int add(int x, int y) {x += y; if(x >= mod) return x - mod; return x;}
int sub(int x, int y) {x -= y; if(x < 0) return x + mod; return x;}
int mul(int x, int y) {return (ll) x * y % mod;}
int n, k;
int a[MAXN], b[MAXN];
vector <int> v;
ll sol;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
REP(i, n) cin >> a[i];
REP(i, n) cin >> b[i];
REP(i, n) { v.pb(a[i] - b[i]); sol += abs(a[i] - b[i]); }
sort(v.begin(), v.end());
ll lftCnt = 0, lftSum = 0, rghtCnt = n - k, rghtSum = 0, pnt1 = 0, pnt2 = n - k;
REP(i, n - k)
rghtSum += abs(v[i] - v[0]);
REP(i, n){
while(pnt1 < k && pnt1 < i && abs(v[pnt1] - v[i]) > abs(v[pnt2] - v[i] ) ){
lftSum -= abs(v[pnt1] - v[i - 1]); lftCnt --;
rghtSum += abs(v[pnt2] - v[i - 1]); rghtCnt ++;
pnt1 ++; pnt2 ++;
}
lftSum += lftCnt * (i > 0 ? v[i] - v[i - 1] : 0);
rghtSum -= rghtCnt * (i > 0 ? v[i] - v[i - 1] : 0);
lftCnt ++; rghtCnt --;
sol = min(sol, lftSum + rghtSum);
}
cout << sol;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
1844 KB |
Output is correct |
2 |
Correct |
35 ms |
1784 KB |
Output is correct |
3 |
Correct |
42 ms |
1756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
1756 KB |
Output is correct |
2 |
Correct |
28 ms |
1764 KB |
Output is correct |
3 |
Correct |
35 ms |
1860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
1720 KB |
Output is correct |
2 |
Correct |
37 ms |
1784 KB |
Output is correct |
3 |
Correct |
35 ms |
1756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
1784 KB |
Output is correct |
2 |
Correct |
32 ms |
1808 KB |
Output is correct |
3 |
Correct |
34 ms |
1756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
1756 KB |
Output is correct |
2 |
Correct |
36 ms |
1784 KB |
Output is correct |
3 |
Correct |
32 ms |
1724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
1784 KB |
Output is correct |
2 |
Correct |
29 ms |
1784 KB |
Output is correct |
3 |
Correct |
34 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
1912 KB |
Output is correct |
2 |
Correct |
34 ms |
1780 KB |
Output is correct |
3 |
Correct |
36 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
1784 KB |
Output is correct |
2 |
Correct |
38 ms |
1820 KB |
Output is correct |
3 |
Correct |
37 ms |
1792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
1892 KB |
Output is correct |
2 |
Correct |
35 ms |
1900 KB |
Output is correct |
3 |
Correct |
49 ms |
1784 KB |
Output is correct |