105462024-04-04 22:12:49111Az IKPC legerősebb csapatacpp17Wrong answer 18/10032ms11248 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,K;
	cin>>N>>K;
	vector<int>a(N+1),b(N+1);
	for(int i=1;i<=N;i++){
		cin>>a[i];
	}
	for(int i=1;i<=N;i++){
		cin>>b[i];
	}
	vector<vector<int>>g(N+1);
	vector<int>s;
	for(int i=N;i>=1;i--){
		while(!s.empty()&&a[s.back()]<=a[i]){
			s.pop_back();
		}
		g[s.empty()?0:s.back()].push_back(i);
		s.push_back(i);
	}
	auto dfs=[&](auto self,int i)->vector<int>{
		vector<int>s;
		for(int j:g[i]){
			auto z=self(self,j);
			if(s.size()<z.size()){
				swap(s,z);
			}
			s.insert(s.end(),z.begin(),z.end());
		}
		sort(s.begin(),s.end());
		if(s.empty()){
			s.push_back(b[i]);
		}
		else{
			s.back()+=b[i];
		}
		return s;
	};
	auto ans=dfs(dfs,0);
	cout<<accumulate(max(ans.begin(),ans.end()-K),ans.end(),0ll)<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1832 KiB
2Accepted3ms2052 KiB
subtask20/9
3Wrong answer3ms2284 KiB
4Wrong answer3ms2644 KiB
5Wrong answer3ms2388 KiB
6Wrong answer3ms2572 KiB
7Wrong answer3ms2788 KiB
8Wrong answer3ms3000 KiB
9Wrong answer3ms3208 KiB
10Wrong answer3ms3288 KiB
11Wrong answer3ms3524 KiB
12Wrong answer3ms3596 KiB
subtask37/7
13Accepted3ms3664 KiB
14Accepted3ms3984 KiB
15Accepted4ms4216 KiB
16Accepted4ms4344 KiB
17Accepted4ms4424 KiB
18Accepted4ms4312 KiB
19Accepted4ms4320 KiB
20Accepted4ms4372 KiB
21Accepted4ms4412 KiB
subtask411/11
22Accepted27ms10584 KiB
23Accepted28ms10532 KiB
24Accepted29ms10528 KiB
25Accepted30ms10812 KiB
26Accepted30ms10792 KiB
27Accepted32ms10716 KiB
subtask50/22
28Wrong answer4ms4604 KiB
29Wrong answer4ms4724 KiB
30Wrong answer4ms4964 KiB
31Wrong answer4ms4944 KiB
32Wrong answer4ms5048 KiB
33Wrong answer4ms5040 KiB
34Wrong answer4ms5128 KiB
subtask60/51
35Wrong answer28ms11248 KiB
36Wrong answer28ms11132 KiB
37Wrong answer29ms11020 KiB
38Wrong answer30ms11032 KiB
39Wrong answer30ms11028 KiB
40Wrong answer32ms11028 KiB
41Wrong answer32ms11032 KiB
42Accepted32ms11024 KiB
43Accepted32ms11028 KiB
44Wrong answer32ms11024 KiB
45Wrong answer32ms11020 KiB