105732024-04-05 14:40:29111Az IKPC legerősebb csapatacpp17Accepted 100/100123ms11312 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);
	}
	if(*min_element(w.begin(),w.end())>=0){
		auto dfs=[&](auto self,int i)->vector<int>{
			vector<int>s;
			for(int j:g[i]){
				auto z=self(self,j);
				if(s.size()<z.size()){
					swap(s,z);
				}
				s.insert(s.end(),z.begin(),z.end());
			}
			sort(s.begin(),s.end());
			if(s.empty()){
				s.push_back(w[i]);
			}
			else{
				s.back()+=w[i];
			}
			return s;
		};
		auto ans=dfs(dfs,0);
		cout<<accumulate(max(ans.begin(),ans.end()-K),ans.end(),0ll)<<'\n';
		return 0;
	}
	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(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(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
1Accepted3ms1892 KiB
2Accepted3ms2084 KiB
subtask29/9
3Accepted3ms2316 KiB
4Accepted3ms2396 KiB
5Accepted3ms2628 KiB
6Accepted3ms2832 KiB
7Accepted3ms2904 KiB
8Accepted3ms3028 KiB
9Accepted3ms3232 KiB
10Accepted3ms3132 KiB
11Accepted3ms3260 KiB
12Accepted3ms3456 KiB
subtask37/7
13Accepted3ms3612 KiB
14Accepted3ms3936 KiB
15Accepted4ms4256 KiB
16Accepted4ms4260 KiB
17Accepted4ms4260 KiB
18Accepted4ms4384 KiB
19Accepted4ms4472 KiB
20Accepted4ms4528 KiB
21Accepted4ms4568 KiB
subtask411/11
22Accepted27ms10572 KiB
23Accepted28ms10832 KiB
24Accepted29ms10824 KiB
25Accepted30ms10832 KiB
26Accepted30ms10832 KiB
27Accepted32ms10836 KiB
subtask522/22
28Accepted7ms4548 KiB
29Accepted7ms4576 KiB
30Accepted7ms4836 KiB
31Accepted7ms4744 KiB
32Accepted7ms4700 KiB
33Accepted8ms4740 KiB
34Accepted6ms4956 KiB
subtask651/51
35Accepted112ms10296 KiB
36Accepted115ms10296 KiB
37Accepted115ms10336 KiB
38Accepted116ms10212 KiB
39Accepted118ms10212 KiB
40Accepted123ms10324 KiB
41Accepted92ms10344 KiB
42Accepted32ms11312 KiB
43Accepted32ms11204 KiB
44Accepted103ms10208 KiB
45Accepted103ms10320 KiB