105722024-04-05 14:38:00111Az IKPC legerősebb csapatacpp17Wrong answer 9/100127ms10632 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{},c{};
			for(int j:g[i]){
				auto [p,q]=self(self,j);
				a.first+=p.first;
				a.second+=p.second;
				if(q>b){
					b=q;
					c=p;
				}
			}
			b.first+=a.first-c.first+w[i];
			b.second+=a.second-c.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]<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
1Accepted3ms1768 KiB
2Accepted3ms1928 KiB
subtask29/9
3Accepted3ms2160 KiB
4Accepted3ms2380 KiB
5Accepted3ms2588 KiB
6Accepted3ms2792 KiB
7Accepted3ms2880 KiB
8Accepted3ms3084 KiB
9Accepted3ms3320 KiB
10Accepted3ms3528 KiB
11Accepted3ms3596 KiB
12Accepted3ms3624 KiB
subtask30/7
13Accepted3ms3772 KiB
14Accepted4ms4268 KiB
15Wrong answer7ms4332 KiB
16Accepted7ms4432 KiB
17Accepted7ms4376 KiB
18Accepted7ms4380 KiB
19Accepted7ms4512 KiB
20Accepted8ms4488 KiB
21Accepted9ms4644 KiB
subtask40/11
22Accepted101ms9744 KiB
23Accepted101ms9740 KiB
24Accepted104ms9744 KiB
25Accepted104ms9740 KiB
26Wrong answer112ms9744 KiB
27Accepted118ms9868 KiB
subtask50/22
28Wrong answer7ms4676 KiB
29Wrong answer7ms4676 KiB
30Wrong answer8ms4820 KiB
31Accepted8ms4744 KiB
32Wrong answer8ms4996 KiB
33Accepted8ms5040 KiB
34Accepted7ms4968 KiB
subtask60/51
35Wrong answer114ms10292 KiB
36Wrong answer116ms10376 KiB
37Accepted118ms10528 KiB
38Wrong answer119ms10632 KiB
39Wrong answer120ms10508 KiB
40Wrong answer127ms10288 KiB
41Accepted97ms10284 KiB
42Accepted108ms10288 KiB
43Accepted116ms10176 KiB
44Wrong answer108ms10304 KiB
45Wrong answer108ms10412 KiB