105672024-04-05 14:30:59111Az IKPC legerősebb csapatacpp17Wrong answer 9/100123ms10796 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 solve=[&](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;
	};
	int l=0,h=1e18;
	while(l<h){
		int m=(l+h)/2;
		auto[x,c]=solve(m);
		if(c<=K){
			h=m;
		}
		else{
			l=m+1;
		}
	}
	auto[x,c]=solve(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+K*h<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2052 KiB
subtask29/9
3Accepted3ms2264 KiB
4Accepted3ms2476 KiB
5Accepted3ms2708 KiB
6Accepted3ms2900 KiB
7Accepted3ms3100 KiB
8Accepted3ms3316 KiB
9Accepted3ms3548 KiB
10Accepted3ms3616 KiB
11Accepted3ms3476 KiB
12Accepted3ms3472 KiB
subtask30/7
13Wrong answer3ms3528 KiB
14Accepted4ms3836 KiB
15Wrong answer7ms4296 KiB
16Wrong answer7ms4504 KiB
17Wrong answer7ms4420 KiB
18Accepted7ms4748 KiB
19Accepted7ms4876 KiB
20Accepted8ms4952 KiB
21Wrong answer8ms5112 KiB
subtask40/11
22Wrong answer100ms10224 KiB
23Wrong answer98ms10236 KiB
24Accepted101ms10220 KiB
25Wrong answer101ms10324 KiB
26Wrong answer108ms10280 KiB
27Accepted108ms10276 KiB
subtask50/22
28Accepted7ms4868 KiB
29Wrong answer7ms4916 KiB
30Wrong answer7ms4916 KiB
31Accepted7ms5076 KiB
32Accepted7ms5120 KiB
33Accepted8ms5040 KiB
34Accepted7ms4976 KiB
subtask60/51
35Wrong answer112ms10364 KiB
36Accepted115ms10472 KiB
37Accepted116ms10580 KiB
38Accepted118ms10576 KiB
39Accepted118ms10688 KiB
40Wrong answer123ms10560 KiB
41Accepted93ms10560 KiB
42Wrong answer104ms10668 KiB
43Accepted108ms10560 KiB
44Wrong answer104ms10556 KiB
45Wrong answer104ms10796 KiB