105602024-04-05 13:32:10111Az IKPC legerősebb csapatacpp17Wrong answer 0/100114ms10704 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 solveMin=[&](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;
	};
	auto solveMax=[&](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||q.first-p.first==b.first&&q.second-p.second>b.second){
					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]=solveMin(m);
		if(c<=K){
			h=m;
		}
		else{
			l=m+1;
		}
	}
	auto[x,c]=solveMax(h);
	cout<<x+min(c,K)*h<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1696 KiB
2Accepted3ms1928 KiB
subtask20/9
3Accepted3ms2020 KiB
4Wrong answer3ms2140 KiB
5Wrong answer3ms2360 KiB
6Accepted3ms2568 KiB
7Accepted3ms2588 KiB
8Wrong answer2ms2580 KiB
9Wrong answer3ms2708 KiB
10Accepted3ms3068 KiB
11Wrong answer2ms2944 KiB
12Wrong answer3ms3176 KiB
subtask30/7
13Accepted3ms3304 KiB
14Accepted4ms3488 KiB
15Wrong answer6ms3588 KiB
16Accepted6ms3768 KiB
17Accepted7ms4068 KiB
18Accepted7ms3976 KiB
19Accepted7ms4288 KiB
20Accepted7ms4452 KiB
21Accepted8ms4292 KiB
subtask40/11
22Accepted93ms9624 KiB
23Accepted90ms9640 KiB
24Accepted93ms9872 KiB
25Accepted92ms9932 KiB
26Wrong answer101ms9932 KiB
27Accepted105ms10136 KiB
subtask50/22
28Accepted7ms4836 KiB
29Accepted7ms4836 KiB
30Accepted7ms4832 KiB
31Wrong answer7ms4816 KiB
32Accepted7ms5108 KiB
33Wrong answer8ms5152 KiB
34Wrong answer6ms5084 KiB
subtask60/51
35Accepted103ms10348 KiB
36Accepted103ms10468 KiB
37Wrong answer104ms10348 KiB
38Accepted108ms10368 KiB
39Accepted108ms10472 KiB
40Accepted114ms10456 KiB
41Wrong answer83ms10460 KiB
42Accepted96ms10704 KiB
43Accepted105ms10584 KiB
44Accepted97ms10672 KiB
45Accepted96ms10672 KiB