105622024-04-05 13:55:34111Az IKPC legerősebb csapatacpp17Wrong answer 0/100112ms10828 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{-y,1};
			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>a.first){
				a.first=b.first;
				a.second=b.second;
			}
			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{-y,1};
			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>=a.first){
				a.first=b.first;
				a.second=b.second;
			}
			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);
	cout<<x+min(c,K)*h<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted3ms2024 KiB
subtask20/9
3Accepted3ms2252 KiB
4Wrong answer3ms2440 KiB
5Wrong answer3ms2672 KiB
6Accepted2ms2740 KiB
7Accepted3ms3140 KiB
8Wrong answer3ms3096 KiB
9Wrong answer3ms3464 KiB
10Accepted3ms3480 KiB
11Wrong answer3ms3576 KiB
12Wrong answer3ms3688 KiB
subtask30/7
13Accepted4ms4076 KiB
14Accepted4ms4284 KiB
15Wrong answer6ms4488 KiB
16Accepted6ms4488 KiB
17Accepted6ms4376 KiB
18Accepted7ms4624 KiB
19Accepted7ms4636 KiB
20Accepted7ms4804 KiB
21Accepted8ms4876 KiB
subtask40/11
22Accepted92ms9864 KiB
23Accepted90ms10088 KiB
24Accepted90ms10164 KiB
25Accepted90ms10160 KiB
26Wrong answer100ms10312 KiB
27Accepted101ms10288 KiB
subtask50/22
28Accepted7ms4864 KiB
29Accepted7ms4892 KiB
30Accepted7ms4892 KiB
31Wrong answer7ms4848 KiB
32Accepted7ms4844 KiB
33Wrong answer8ms4844 KiB
34Wrong answer6ms4840 KiB
subtask60/51
35Accepted103ms10196 KiB
36Accepted103ms10200 KiB
37Wrong answer105ms10280 KiB
38Accepted108ms10520 KiB
39Accepted108ms10492 KiB
40Accepted112ms10488 KiB
41Wrong answer82ms10492 KiB
42Accepted93ms10596 KiB
43Accepted101ms10700 KiB
44Accepted93ms10700 KiB
45Accepted93ms10828 KiB