红色线段代表交换两个没有交的区间之后能获得的最大收益,并且交换操作能获得收益当且仅当两个线段没有交集。
//编译器级优化
// #pragma GCC optimize("O2")
// #include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <time.h>
#include <cstring>
#include <cmath>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <cstdio>
#include <cassert>
#include <bitset>
// #include <bits/extc++.h>
//外部包含文件
// #include "D:/program_learning/vs_code/workplace/ACMtemplate/data_structure/SparseTable.hpp"
// #include "D:/program_learning/vs_code/workplace/ACMtemplate/computational_geometry/Convex.hpp"
//类型别名
using namespace std;
// using B_ll = __int128_t;
// using B_ull = __uint128_t;
using f32 = float;
using f64 = double;
using ll = long long;
//排序用
#define htl(v, n) (v),(v)+(n), greater<decltype((v)[1])>()
#define lth(v, n) (v),(v)+(n), less<decltype((v)[1])>()
//宏函数
#define Mt(args...) make_tuple(args)
#define Mp(a, b) make_pair((a), (b))
#define Gt(a, b) std::get<(b)>((a))
#define Add(a, b) accumulate((a), (b), 0)
#define Abs(a) (((a) > 0) ? (a) : -(a))
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define Ceil(a, b) (((a) + (b) - 1) / (b))
#define Floor(a, b) ((a) / (b))
#define Pow2(x) ((x) * (x))
#define Pow3(x) ((x) * (x) * (x))
//浮点数关系运算
#define isZero(x) ((x)>0?(x):-(x)<eps)
#define equal(x, y) (abs((x)-(y))<eps)
#define lthan(x, y) ((y)-(x)>eps)
#define bthan(x, y) ((x)-(y)>eps)
//简化循环
#define FOR(i, a, b) for(ll i = (ll)(a); i <= (ll)(b); i++)
#define rFOR(i, a, b) for(ll i = (ll)(a); i >= (ll)(b); i--)
#define gFOR(i, a) for(ll i = h[(a)] ; ~i ; i = ne[i])
#define bFOR(i, a, b) for(ll i = (a); i <= (b); i <<= 1)
#define jFOR(i, a, b, step) for(ll i = (a); i <= (b); i += (step))
#define iFOR(it, begin, end) for(auto it = (begin); it!=(end);it++)
#define aFOR(a, s) for(auto (a) : (s))
//debug用
#define _var(x) cout << #x << " = " << (x) << endl
#define _arr1(a) for(ll i = 0 ; i < (a).size() ; i ++) cout << (a)[i] << ((i == (a).size() - 1) ? endl : space)
#define _arr2(a) for(auto s : (a) )for(ll j = 0 ; j < s.size() ; j ++) cout << s[j] << ((j == s.size() - 1) ? endl : space)
#define checktime() cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n" ; system("pause")
//加快io
#define endl "\n"
#define __faster_io__ std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);
//操作输出
#define space " "
#define setd(n) fixed << setprecision(n)
//初始化设置
#define case(t,i) ll t;cin >> t;for(ll i=1;i<=t;i++)
#define _set(b, c) memset((b), (c), sizeof(b))
//模板函数
template<typename T>
int read(T& result) { // fast read
char ch;int w = 1;
result = 0;
ch = getchar();
while(ch != '-' && !(ch >= '0' && ch <= '9'))
ch = getchar();
if(ch == '-')
w = -1;
while(isdigit(ch)){
result = result * 10 + (ch - '0');
ch = getchar();
}
result *= w;
return 1;
}
template<typename T>
void write (T x) { // fast output
string tmp;string tail = (x < 0) ? "-" : "";
while(x){
tmp += x % 10 + '0';
x /= 10;
}
tmp += tail;
reverse(tmp.begin(), tmp.end());
cout << tmp;
}
template<typename T>
T qpow(T x, T n, const T& mod){
ll ans = 1;
while(n){
if(n & 1)
ans = ans % mod * x % mod;
x = x % mod * x % mod;
n >>= 1;
}
return ans;
}
template<typename T>
inline T lowbit(T& x){
return x&-x;
}
template<typename T>
T m_plus(T a, T b, const T& mod){
return (a % mod + b % mod) % mod;
}
template<typename T>
T m_minus(T a, T b, const T& mod){
return ((a % mod - b % mod) % mod + mod) % mod;
}
template<typename T>
T m_mult(T a, T b, const T& mod){
return a % mod * b % mod;
}
template<typename T>
T Mod(T x, T y)
{
return (x % y + y) % y;
}
//常数
constexpr f32 fINF32 = 1e30;
constexpr f64 fINF64 = 1e300;
constexpr int INF32 = 0x3f3f3f3f;
constexpr ll INF64 = 0x3f3f3f3f3f3f3f3f;
constexpr f32 _fINF32 = -1e30;
constexpr f64 _fINF64 = -1e300;
constexpr int _INF32 = 0xcfcfcfcf;
constexpr ll _INF64 = 0xcfcfcfcfcfcfcfcf;
constexpr f64 eps = 1e-6;
const f64 pi = acos(-1);
constexpr ll mod = 9 + 7;
constexpr ll Bit = 1 << 20;
constexpr ll N = 5e5 + 5;
/*定义全局变量*/
ll a[N], b[N];
ll mn[N], mx[N];
/*辅助函数*/
/*核心函数*/
void solve(){
ll n, k, res = 0;
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n) cin >> b[i];
FOR(i, 1, n){
res += Abs(a[i] - b[i]); // 计算初始时候的绝对值之差
mn[i] = Min(a[i], b[i]); // 记录每对数中较小的那个
mx[i] = Max(a[i], b[i]); // 记录每对数中较大的那个
}
sort(htl(mn + 1, n)); // 将每对数较大的数从大到小排序
sort(lth(mx + 1, n)); // 将每对数较小的数从小到大排序(原因见后面)
// 当只有两对数的时候需要特殊判断因为操作有可能将答案强制变劣
if(n == 2){
if(k % 2 == 0) // 经过偶数次对换对答案无影响,直接输出res即可
cout << res << endl;
else // 经过奇数次对换相当于一次对换
cout << (Abs(a[1] - b[2]) + Abs(a[2] - b[1])) << endl;
}else{
FOR(i, 1, n){
if(i > k) break;
if(mn[i] > mx[i])
res += 2 * (mn[i] - mx[i]); // 交换两对数之后获得的收益(后面配合图片才想明白这一步)
}
cout << res << endl;
}
}
/*程序入口*/
int main()
{
#ifdef LOCAL
freopen("D:/program_learning/vs_code/workplace/ACMtemplate/DataIn.txt", "r", stdin);
freopen("D:/program_learning/vs_code/workplace/ACMtemplate/DataOut.txt", "w", stdout);
#endif
__faster_io__
// case(t, i){
solve();
// }
#ifdef LOCAL
cout.flush();
freopen("CON", "r", stdin);
freopen("CON", "w", stdout);
#endif
checktime();
return 0;
}