10566 | 2024-04-05 14:27:18 | 111 | Az IKPC legerősebb csapata | cpp17 | Wrong answer 82/100 | 115ms | 9968 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);
sort(w.begin()+1,w.end());
if(c<K&&w[N-K+1]<0){
int ans=0;
for(int i=N;i>N-K;i--){
ans+=w[i];
}
cout<<ans<<'\n';
return 0;
}
cout<<x+min(c,K)*h<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1832 KiB | ||||
2 | Accepted | 3ms | 2020 KiB | ||||
subtask2 | 9/9 | ||||||
3 | Accepted | 3ms | 2228 KiB | ||||
4 | Accepted | 3ms | 2464 KiB | ||||
5 | Accepted | 3ms | 2676 KiB | ||||
6 | Accepted | 3ms | 2884 KiB | ||||
7 | Accepted | 2ms | 2952 KiB | ||||
8 | Accepted | 3ms | 3180 KiB | ||||
9 | Accepted | 3ms | 3292 KiB | ||||
10 | Accepted | 3ms | 3516 KiB | ||||
11 | Accepted | 3ms | 3604 KiB | ||||
12 | Accepted | 3ms | 3732 KiB | ||||
subtask3 | 0/7 | ||||||
13 | Accepted | 3ms | 3892 KiB | ||||
14 | Accepted | 4ms | 4164 KiB | ||||
15 | Wrong answer | 6ms | 4356 KiB | ||||
16 | Accepted | 6ms | 4320 KiB | ||||
17 | Accepted | 6ms | 4324 KiB | ||||
18 | Accepted | 6ms | 4372 KiB | ||||
19 | Accepted | 7ms | 4512 KiB | ||||
20 | Accepted | 7ms | 4672 KiB | ||||
21 | Accepted | 8ms | 4524 KiB | ||||
subtask4 | 0/11 | ||||||
22 | Accepted | 92ms | 9628 KiB | ||||
23 | Accepted | 90ms | 9632 KiB | ||||
24 | Accepted | 92ms | 9760 KiB | ||||
25 | Accepted | 93ms | 9864 KiB | ||||
26 | Wrong answer | 101ms | 9780 KiB | ||||
27 | Accepted | 100ms | 9740 KiB | ||||
subtask5 | 22/22 | ||||||
28 | Accepted | 7ms | 4428 KiB | ||||
29 | Accepted | 7ms | 4484 KiB | ||||
30 | Accepted | 7ms | 4476 KiB | ||||
31 | Accepted | 7ms | 4328 KiB | ||||
32 | Accepted | 7ms | 4324 KiB | ||||
33 | Accepted | 8ms | 4320 KiB | ||||
34 | Accepted | 6ms | 4368 KiB | ||||
subtask6 | 51/51 | ||||||
35 | Accepted | 104ms | 9628 KiB | ||||
36 | Accepted | 104ms | 9628 KiB | ||||
37 | Accepted | 107ms | 9628 KiB | ||||
38 | Accepted | 109ms | 9764 KiB | ||||
39 | Accepted | 109ms | 9968 KiB | ||||
40 | Accepted | 115ms | 9868 KiB | ||||
41 | Accepted | 85ms | 9864 KiB | ||||
42 | Accepted | 94ms | 9864 KiB | ||||
43 | Accepted | 100ms | 9948 KiB | ||||
44 | Accepted | 94ms | 9844 KiB | ||||
45 | Accepted | 94ms | 9844 KiB |