105642024-04-05 14:19:34111Az IKPC legerősebb csapatacpp17Runtime error 0/100109ms10744 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)exit(1);
	cout<<x+min(c,K)*h<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2056 KiB
subtask20/9
3Accepted3ms2400 KiB
4Runtime error3ms2388 KiB
5Runtime error3ms2260 KiB
6Accepted3ms2392 KiB
7Accepted3ms2600 KiB
8Runtime error3ms2692 KiB
9Runtime error3ms2752 KiB
10Accepted3ms2992 KiB
11Runtime error3ms2968 KiB
12Runtime error3ms2960 KiB
subtask30/7
13Runtime error3ms3280 KiB
14Accepted4ms3300 KiB
15Runtime error6ms3772 KiB
16Runtime error6ms3812 KiB
17Runtime error6ms4112 KiB
18Accepted6ms4068 KiB
19Accepted6ms4072 KiB
20Accepted7ms4028 KiB
21Runtime error8ms4172 KiB
subtask40/11
22Runtime error90ms9628 KiB
23Runtime error87ms9716 KiB
24Accepted87ms9668 KiB
25Runtime error89ms9884 KiB
26Runtime error96ms9996 KiB
27Accepted96ms9868 KiB
subtask50/22
28Accepted7ms4436 KiB
29Runtime error7ms4436 KiB
30Runtime error7ms4744 KiB
31Runtime error7ms4636 KiB
32Accepted7ms4948 KiB
33Runtime error8ms4936 KiB
34Runtime error6ms4980 KiB
subtask60/51
35Runtime error101ms10532 KiB
36Accepted101ms10244 KiB
37Runtime error104ms10396 KiB
38Accepted105ms10268 KiB
39Accepted104ms10396 KiB
40Runtime error109ms10636 KiB
41Runtime error79ms10724 KiB
42Runtime error90ms10612 KiB
43Accepted96ms10476 KiB
44Runtime error90ms10608 KiB
45Runtime error90ms10744 KiB