105562024-04-05 13:11:33111Az IKPC legerősebb csapatacpp17Wrong answer 0/100112ms10656 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(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
1Accepted3ms1696 KiB
2Accepted3ms1928 KiB
subtask20/9
3Accepted3ms2276 KiB
4Wrong answer3ms2208 KiB
5Wrong answer3ms2436 KiB
6Accepted3ms2576 KiB
7Accepted3ms2668 KiB
8Wrong answer3ms2892 KiB
9Wrong answer3ms2968 KiB
10Accepted3ms3108 KiB
11Wrong answer2ms3116 KiB
12Wrong answer3ms3120 KiB
subtask30/7
13Wrong answer3ms3172 KiB
14Accepted4ms3356 KiB
15Wrong answer6ms3540 KiB
16Wrong answer7ms3684 KiB
17Wrong answer7ms3524 KiB
18Accepted7ms3516 KiB
19Accepted7ms3828 KiB
20Accepted7ms3780 KiB
21Wrong answer8ms3936 KiB
subtask40/11
22Wrong answer93ms9272 KiB
23Wrong answer92ms9256 KiB
24Accepted93ms9364 KiB
25Wrong answer93ms9472 KiB
26Wrong answer101ms9720 KiB
27Accepted105ms9616 KiB
subtask50/22
28Accepted7ms4428 KiB
29Wrong answer7ms4208 KiB
30Wrong answer7ms4412 KiB
31Wrong answer7ms4376 KiB
32Accepted7ms4428 KiB
33Wrong answer8ms4616 KiB
34Wrong answer6ms4520 KiB
subtask60/51
35Wrong answer103ms10008 KiB
36Accepted101ms10116 KiB
37Wrong answer104ms10328 KiB
38Accepted108ms10236 KiB
39Accepted107ms10236 KiB
40Wrong answer112ms10104 KiB
41Wrong answer85ms10344 KiB
42Wrong answer96ms10340 KiB
43Accepted104ms10424 KiB
44Wrong answer97ms10536 KiB
45Wrong answer97ms10656 KiB