105692024-04-05 14:34:14111Az IKPC legerősebb csapatacpp17Wrong answer 27/100131ms10792 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{},c{};
			for(int j:g[i]){
				auto [p,q]=self(self,j);
				a.first+=p.first;
				a.second+=p.second;
				if(q.first>b.first){
					b=q;
					c=p;
				}
			}
			b.first+=a.first-c.first+w[i];
			b.second+=a.second-c.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.first>b.first||q.first==b.first&&q.second>b.second){
					b=q;
					c=p;
				}
			}
			b.first+=a.first-c.first+w[i];
			b.second+=a.second-c.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;
	};
	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
1Accepted3ms1888 KiB
2Accepted3ms2124 KiB
subtask29/9
3Accepted3ms2348 KiB
4Accepted3ms2416 KiB
5Accepted3ms2544 KiB
6Accepted3ms2756 KiB
7Accepted3ms2988 KiB
8Accepted3ms3080 KiB
9Accepted3ms3160 KiB
10Accepted3ms3400 KiB
11Accepted3ms3504 KiB
12Accepted2ms3580 KiB
subtask37/7
13Accepted4ms4040 KiB
14Accepted4ms3976 KiB
15Accepted7ms4076 KiB
16Accepted7ms4332 KiB
17Accepted7ms4380 KiB
18Accepted8ms4664 KiB
19Accepted8ms4684 KiB
20Accepted8ms4572 KiB
21Accepted9ms4560 KiB
subtask411/11
22Accepted104ms9772 KiB
23Accepted104ms9868 KiB
24Accepted108ms10104 KiB
25Accepted108ms9972 KiB
26Accepted114ms10100 KiB
27Accepted119ms10188 KiB
subtask50/22
28Wrong answer8ms4996 KiB
29Wrong answer8ms4888 KiB
30Accepted8ms5020 KiB
31Accepted8ms4956 KiB
32Wrong answer8ms5128 KiB
33Accepted8ms5340 KiB
34Accepted7ms5044 KiB
subtask60/51
35Wrong answer116ms10280 KiB
36Wrong answer116ms10476 KiB
37Accepted120ms10468 KiB
38Wrong answer123ms10592 KiB
39Wrong answer125ms10788 KiB
40Accepted131ms10676 KiB
41Accepted98ms10692 KiB
42Accepted112ms10680 KiB
43Accepted120ms10552 KiB
44Accepted114ms10664 KiB
45Accepted112ms10792 KiB