105682024-04-05 14:32:35111Az IKPC legerősebb csapatacpp17Wrong answer 82/100126ms10648 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

#define pii pair<int,int>

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,K;
	cin>>N>>K;
	vector<int>v(N+1),w(N+1);
	for(int i=1;i<=N;i++){
		cin>>v[i];
	}
	for(int i=1;i<=N;i++){
		cin>>w[i];
	}
	vector<vector<int>>g(N+1);
	vector<int>s;
	for(int i=N;i>=1;i--){
		while(!s.empty()&&v[s.back()]<=v[i]){
			s.pop_back();
		}
		g[s.empty()?0:s.back()].push_back(i);
		s.push_back(i);
	}
	auto solveMin=[&](int y)->pii{
		auto dfs=[&](auto self,int i)->pair<pii,pii>{
			pii a{},b{};
			for(int j:g[i]){
				auto [p,q]=self(self,j);
				a.first+=p.first;
				a.second+=p.second;
				if(q.first-p.first>b.first){
					b.first=q.first-p.first;
					b.second=q.second-p.second;
				}
			}
			b.first+=a.first+w[i];
			b.second+=a.second;
			if(w[i]>y){
				a.first+=w[i]-y;
				a.second++;
			}
			if(b.first-y>a.first){
				a.first=b.first-y;
				a.second=b.second+1;
			}
			return{a,b};
		};
		return dfs(dfs,0).first;
	};
	auto solveMax=[&](int y)->pii{
		auto dfs=[&](auto self,int i)->pair<pii,pii>{
			pii a{},b{};
			for(int j:g[i]){
				auto [p,q]=self(self,j);
				a.first+=p.first;
				a.second+=p.second;
				if(q.first-p.first>b.first||q.first-p.first==b.first&&q.second-p.second>b.second){
					b.first=q.first-p.first;
					b.second=q.second-p.second;
				}
			}
			b.first+=a.first+w[i];
			b.second+=a.second;
			if(w[i]>=y){
				a.first+=w[i]-y;
				a.second++;
			}
			if(b.first-y>=a.first){
				a.first=b.first-y;
				a.second=b.second+1;
			}
			return{a,b};
		};
		return dfs(dfs,0).first;
	};
	int l=0,h=1e18;
	while(l<h){
		int m=(l+h)/2;
		auto[x,c]=solveMin(m);
		if(c<=K){
			h=m;
		}
		else{
			l=m+1;
		}
	}
	auto[x,c]=solveMax(h);
	sort(w.begin()+1,w.end());
	if(c<K&&w[N-K+1]<0){
		int ans=0;
		for(int i=N;i>N-K;i--){
			ans+=w[i];
		}
		cout<<ans<<'\n';
		return 0;
	}
	cout<<x+min(c,K)*h<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2100 KiB
2Accepted3ms2240 KiB
subtask29/9
3Accepted3ms2388 KiB
4Accepted3ms2592 KiB
5Accepted3ms2812 KiB
6Accepted3ms2848 KiB
7Accepted3ms2932 KiB
8Accepted3ms2896 KiB
9Accepted3ms3128 KiB
10Accepted3ms3120 KiB
11Accepted3ms3216 KiB
12Accepted3ms3348 KiB
subtask30/7
13Accepted4ms3728 KiB
14Accepted4ms3764 KiB
15Wrong answer7ms4220 KiB
16Accepted7ms4148 KiB
17Accepted7ms4404 KiB
18Accepted7ms4360 KiB
19Accepted7ms4616 KiB
20Accepted8ms4616 KiB
21Accepted9ms4804 KiB
subtask40/11
22Accepted101ms9996 KiB
23Accepted101ms10000 KiB
24Accepted104ms10000 KiB
25Accepted104ms10000 KiB
26Wrong answer112ms10000 KiB
27Accepted116ms10004 KiB
subtask522/22
28Accepted7ms4804 KiB
29Accepted8ms4848 KiB
30Accepted8ms4692 KiB
31Accepted8ms4736 KiB
32Accepted8ms4876 KiB
33Accepted8ms4920 KiB
34Accepted7ms4904 KiB
subtask651/51
35Accepted114ms10216 KiB
36Accepted115ms10172 KiB
37Accepted116ms10168 KiB
38Accepted119ms10280 KiB
39Accepted119ms10168 KiB
40Accepted126ms10272 KiB
41Accepted96ms10276 KiB
42Accepted107ms10428 KiB
43Accepted116ms10516 KiB
44Accepted108ms10624 KiB
45Accepted108ms10648 KiB