Gravatar
终焉折枝
积分:1860
提交:230 / 406

Pro4396  Ave Mujica

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 5 * 1e5 + 5;
ll a[N], b[N], ans, n, k;
struct node{
	ll v;
	int cnt;
	bool operator>(const node& o)const{
		if(v != o.v) return v > o.v;
		return cnt < o.cnt;
	}
};

int check(ll mid){
    priority_queue<node, vector<node>, greater<node>> A;
    priority_queue<node, vector<node>, greater<node>> B;
    int cnt = 0;
    ans = 0;
    for(int i = 1;i <= n;i ++){
        A.push({a[i] + mid, 1});
        node op1 = {A.top().v + b[i], A.top().cnt};
        node op2 = B.empty() ? node{(ll)2e18, 0} : node{B.top().v + b[i], B.top().cnt};
        bool flag = (op1.v < op2.v) || (op1.v == op2.v && op1.cnt > op2.cnt);
        node chose = flag ? op1 : op2;
        if(chose.v <= 0){
            ans += chose.v;
            cnt += chose.cnt;
            if(flag) A.pop();
            else B.pop();
            B.push({-b[i], 0});
        }
    }
    return cnt;
}

int main(){

	cin >> n >> k;

	for(int i = 1;i <= n;i ++) {
		cin >> a[i];
	}

	for(int i = 1;i <= n;i ++){
		cin >> b[i];
	}

	ll l = -1e15, r = 1e15, C = r;
	while(l <= r){
		ll mid = (l + r) >> 1;
		if(check(mid) >= k){
			C = mid;
			l = mid + 1;
		}
		else{
			r = mid - 1;
		}
	}
	check(C);
	cout << ans - k * C << '\n';
	return 0;
}

2026-05-04 16:44:00    
我有话要说
暂无人分享评论!