105542024-04-05 13:03:21111Az IKPC legerősebb csapatacpp17Wrong answer 0/100114ms10312 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
1Accepted3ms1824 KiB
2Accepted3ms2060 KiB
subtask20/9
3Accepted3ms2268 KiB
4Wrong answer3ms2496 KiB
5Wrong answer3ms2656 KiB
6Accepted3ms2864 KiB
7Accepted3ms2952 KiB
8Wrong answer3ms2952 KiB
9Wrong answer3ms2956 KiB
10Accepted3ms2964 KiB
11Wrong answer3ms3108 KiB
12Wrong answer3ms3092 KiB
subtask30/7
13Wrong answer3ms3224 KiB
14Accepted4ms3584 KiB
15Accepted6ms3732 KiB
16Wrong answer6ms3704 KiB
17Wrong answer7ms4004 KiB
18Accepted6ms4000 KiB
19Accepted7ms4220 KiB
20Accepted7ms4420 KiB
21Wrong answer8ms4572 KiB
subtask40/11
22Wrong answer92ms9776 KiB
23Wrong answer90ms9860 KiB
24Accepted93ms9968 KiB
25Wrong answer93ms10196 KiB
26Accepted98ms10280 KiB
27Accepted104ms10172 KiB
subtask50/22
28Wrong answer7ms4888 KiB
29Wrong answer7ms4860 KiB
30Wrong answer7ms4864 KiB
31Wrong answer7ms4864 KiB
32Wrong answer7ms4864 KiB
33Wrong answer8ms4872 KiB
34Wrong answer6ms4904 KiB
subtask60/51
35Wrong answer103ms10212 KiB
36Wrong answer101ms10312 KiB
37Wrong answer104ms10288 KiB
38Wrong answer105ms10176 KiB
39Wrong answer107ms10196 KiB
40Wrong answer114ms10200 KiB
41Wrong answer85ms10284 KiB
42Wrong answer97ms10308 KiB
43Accepted104ms10176 KiB
44Wrong answer97ms10180 KiB
45Wrong answer97ms10292 KiB