10564 | 2024-04-05 14:19:34 | 111 | Az IKPC legerősebb csapata | cpp17 | Runtime error 0/100 | 109ms | 10744 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(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 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);
if(c<K)exit(1);
cout<<x+min(c,K)*h<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 3ms | 2056 KiB | ||||
subtask2 | 0/9 | ||||||
3 | Accepted | 3ms | 2400 KiB | ||||
4 | Runtime error | 3ms | 2388 KiB | ||||
5 | Runtime error | 3ms | 2260 KiB | ||||
6 | Accepted | 3ms | 2392 KiB | ||||
7 | Accepted | 3ms | 2600 KiB | ||||
8 | Runtime error | 3ms | 2692 KiB | ||||
9 | Runtime error | 3ms | 2752 KiB | ||||
10 | Accepted | 3ms | 2992 KiB | ||||
11 | Runtime error | 3ms | 2968 KiB | ||||
12 | Runtime error | 3ms | 2960 KiB | ||||
subtask3 | 0/7 | ||||||
13 | Runtime error | 3ms | 3280 KiB | ||||
14 | Accepted | 4ms | 3300 KiB | ||||
15 | Runtime error | 6ms | 3772 KiB | ||||
16 | Runtime error | 6ms | 3812 KiB | ||||
17 | Runtime error | 6ms | 4112 KiB | ||||
18 | Accepted | 6ms | 4068 KiB | ||||
19 | Accepted | 6ms | 4072 KiB | ||||
20 | Accepted | 7ms | 4028 KiB | ||||
21 | Runtime error | 8ms | 4172 KiB | ||||
subtask4 | 0/11 | ||||||
22 | Runtime error | 90ms | 9628 KiB | ||||
23 | Runtime error | 87ms | 9716 KiB | ||||
24 | Accepted | 87ms | 9668 KiB | ||||
25 | Runtime error | 89ms | 9884 KiB | ||||
26 | Runtime error | 96ms | 9996 KiB | ||||
27 | Accepted | 96ms | 9868 KiB | ||||
subtask5 | 0/22 | ||||||
28 | Accepted | 7ms | 4436 KiB | ||||
29 | Runtime error | 7ms | 4436 KiB | ||||
30 | Runtime error | 7ms | 4744 KiB | ||||
31 | Runtime error | 7ms | 4636 KiB | ||||
32 | Accepted | 7ms | 4948 KiB | ||||
33 | Runtime error | 8ms | 4936 KiB | ||||
34 | Runtime error | 6ms | 4980 KiB | ||||
subtask6 | 0/51 | ||||||
35 | Runtime error | 101ms | 10532 KiB | ||||
36 | Accepted | 101ms | 10244 KiB | ||||
37 | Runtime error | 104ms | 10396 KiB | ||||
38 | Accepted | 105ms | 10268 KiB | ||||
39 | Accepted | 104ms | 10396 KiB | ||||
40 | Runtime error | 109ms | 10636 KiB | ||||
41 | Runtime error | 79ms | 10724 KiB | ||||
42 | Runtime error | 90ms | 10612 KiB | ||||
43 | Accepted | 96ms | 10476 KiB | ||||
44 | Runtime error | 90ms | 10608 KiB | ||||
45 | Runtime error | 90ms | 10744 KiB |