105612024-04-05 13:43:14111Az IKPC legerősebb csapatacpp17Wrong answer 0/1001.082s6616 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(b.first-y>=a.first){
				a.first=b.first-y;
				a.second=b.second+1;
			}
			return{a,b};
		};
		return dfs(dfs,0).first;
	};
	int ans=0;
	for(int i=0;i<200000;i++){
		auto[x,c]=solveMin(i);
		auto[x2,c2]=solveMax(i);
		if(x!=x2)exit(1);
		if(c<=K)ans=max(ans,x+min(K,c2)*i);
	}
	cout<<ans<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted20ms1760 KiB
2Accepted30ms1864 KiB
subtask20/9
3Accepted37ms1948 KiB
4Wrong answer56ms2172 KiB
5Wrong answer54ms2512 KiB
6Accepted54ms2600 KiB
7Accepted54ms2732 KiB
8Wrong answer54ms2808 KiB
9Wrong answer54ms2876 KiB
10Accepted68ms3004 KiB
11Wrong answer59ms3108 KiB
12Wrong answer54ms3108 KiB
subtask30/7
13Time limit exceeded1.024s3452 KiB
14Time limit exceeded1.041s3868 KiB
15Time limit exceeded1.049s3008 KiB
16Time limit exceeded1.049s3944 KiB
17Time limit exceeded1.057s3052 KiB
18Time limit exceeded1.049s4208 KiB
19Time limit exceeded1.052s4416 KiB
20Time limit exceeded1.057s3380 KiB
21Time limit exceeded1.057s3536 KiB
subtask40/11
22Time limit exceeded1.057s6196 KiB
23Time limit exceeded1.074s6216 KiB
24Time limit exceeded1.074s6196 KiB
25Time limit exceeded1.065s6284 KiB
26Time limit exceeded1.074s6244 KiB
27Time limit exceeded1.057s6380 KiB
subtask50/22
28Time limit exceeded1.049s3372 KiB
29Time limit exceeded1.072s4380 KiB
30Time limit exceeded1.069s3380 KiB
31Time limit exceeded1.049s3496 KiB
32Time limit exceeded1.036s3492 KiB
33Time limit exceeded1.077s3520 KiB
34Time limit exceeded1.052s4476 KiB
subtask60/51
35Time limit exceeded1.057s6328 KiB
36Time limit exceeded1.069s6240 KiB
37Time limit exceeded1.065s6376 KiB
38Time limit exceeded1.074s6300 KiB
39Time limit exceeded1.054s6392 KiB
40Time limit exceeded1.062s6408 KiB
41Time limit exceeded1.077s6596 KiB
42Time limit exceeded1.065s6616 KiB
43Time limit exceeded1.054s6572 KiB
44Time limit exceeded1.065s6576 KiB
45Time limit exceeded1.082s6516 KiB