105532024-04-05 13:02:01111Az IKPC legerősebb csapatacpp17Wrong answer 0/100114ms10276 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{},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;
	};
	int l=0,h=1e16;
	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);
	cout<<x+K*h<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2056 KiB
subtask20/9
3Accepted3ms2240 KiB
4Wrong answer3ms2480 KiB
5Wrong answer3ms2548 KiB
6Accepted3ms2660 KiB
7Accepted3ms2868 KiB
8Wrong answer3ms3092 KiB
9Wrong answer3ms3180 KiB
10Accepted3ms3288 KiB
11Wrong answer2ms3380 KiB
12Wrong answer2ms3376 KiB
subtask30/7
13Wrong answer3ms3432 KiB
14Accepted4ms3656 KiB
15Accepted6ms3988 KiB
16Wrong answer6ms3896 KiB
17Wrong answer6ms3944 KiB
18Accepted6ms3948 KiB
19Accepted7ms3988 KiB
20Accepted7ms4164 KiB
21Wrong answer8ms4404 KiB
subtask40/11
22Wrong answer93ms9548 KiB
23Wrong answer90ms9540 KiB
24Accepted92ms9524 KiB
25Wrong answer93ms9520 KiB
26Accepted97ms9520 KiB
27Accepted105ms9760 KiB
subtask50/22
28Wrong answer7ms4424 KiB
29Wrong answer7ms4424 KiB
30Wrong answer7ms4432 KiB
31Wrong answer7ms4688 KiB
32Wrong answer7ms4644 KiB
33Wrong answer8ms4644 KiB
34Wrong answer6ms4644 KiB
subtask60/51
35Wrong answer101ms9956 KiB
36Wrong answer101ms10084 KiB
37Wrong answer104ms10164 KiB
38Wrong answer104ms10168 KiB
39Wrong answer107ms10168 KiB
40Wrong answer114ms10168 KiB
41Wrong answer83ms10272 KiB
42Wrong answer97ms10272 KiB
43Accepted105ms10164 KiB
44Wrong answer97ms10168 KiB
45Wrong answer97ms10276 KiB