105652024-04-05 14:22:10111Az IKPC legerősebb csapatacpp17Wrong answer 9/100115ms9832 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(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=1e16;
	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);
	if(c<K){
		sort(w.begin()+1,w.end());
		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
1Accepted3ms1824 KiB
2Accepted3ms2020 KiB
subtask29/9
3Accepted3ms2232 KiB
4Accepted3ms2460 KiB
5Accepted3ms2660 KiB
6Accepted3ms2632 KiB
7Accepted3ms2756 KiB
8Accepted2ms2840 KiB
9Accepted3ms2836 KiB
10Accepted2ms2840 KiB
11Accepted2ms2936 KiB
12Accepted3ms2936 KiB
subtask30/7
13Wrong answer3ms3248 KiB
14Accepted4ms3288 KiB
15Wrong answer6ms3620 KiB
16Wrong answer6ms3576 KiB
17Wrong answer6ms3620 KiB
18Accepted6ms3616 KiB
19Accepted6ms3572 KiB
20Accepted7ms3920 KiB
21Wrong answer8ms3984 KiB
subtask40/11
22Wrong answer93ms9196 KiB
23Wrong answer90ms9216 KiB
24Accepted87ms9092 KiB
25Wrong answer93ms9200 KiB
26Wrong answer101ms9200 KiB
27Accepted96ms9092 KiB
subtask50/22
28Accepted6ms3784 KiB
29Wrong answer7ms3784 KiB
30Wrong answer7ms3784 KiB
31Accepted7ms3788 KiB
32Accepted7ms3932 KiB
33Accepted8ms3896 KiB
34Accepted6ms3784 KiB
subtask60/51
35Wrong answer104ms9116 KiB
36Accepted101ms9216 KiB
37Accepted107ms9208 KiB
38Accepted104ms9096 KiB
39Accepted105ms9224 KiB
40Wrong answer115ms9328 KiB
41Accepted86ms9432 KiB
42Wrong answer96ms9452 KiB
43Accepted96ms9664 KiB
44Wrong answer96ms9752 KiB
45Wrong answer96ms9832 KiB