105662024-04-05 14:27:18111Az IKPC legerősebb csapatacpp17Wrong answer 82/100115ms9968 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);
	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
1Accepted3ms1832 KiB
2Accepted3ms2020 KiB
subtask29/9
3Accepted3ms2228 KiB
4Accepted3ms2464 KiB
5Accepted3ms2676 KiB
6Accepted3ms2884 KiB
7Accepted2ms2952 KiB
8Accepted3ms3180 KiB
9Accepted3ms3292 KiB
10Accepted3ms3516 KiB
11Accepted3ms3604 KiB
12Accepted3ms3732 KiB
subtask30/7
13Accepted3ms3892 KiB
14Accepted4ms4164 KiB
15Wrong answer6ms4356 KiB
16Accepted6ms4320 KiB
17Accepted6ms4324 KiB
18Accepted6ms4372 KiB
19Accepted7ms4512 KiB
20Accepted7ms4672 KiB
21Accepted8ms4524 KiB
subtask40/11
22Accepted92ms9628 KiB
23Accepted90ms9632 KiB
24Accepted92ms9760 KiB
25Accepted93ms9864 KiB
26Wrong answer101ms9780 KiB
27Accepted100ms9740 KiB
subtask522/22
28Accepted7ms4428 KiB
29Accepted7ms4484 KiB
30Accepted7ms4476 KiB
31Accepted7ms4328 KiB
32Accepted7ms4324 KiB
33Accepted8ms4320 KiB
34Accepted6ms4368 KiB
subtask651/51
35Accepted104ms9628 KiB
36Accepted104ms9628 KiB
37Accepted107ms9628 KiB
38Accepted109ms9764 KiB
39Accepted109ms9968 KiB
40Accepted115ms9868 KiB
41Accepted85ms9864 KiB
42Accepted94ms9864 KiB
43Accepted100ms9948 KiB
44Accepted94ms9844 KiB
45Accepted94ms9844 KiB