10570 | 2024-04-05 14:35:14 | 111 | Az IKPC legerősebb csapata | cpp17 | Wrong answer 18/100 | 127ms | 10356 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{},c{};
for(int j:g[i]){
auto [p,q]=self(self,j);
a.first+=p.first;
a.second+=p.second;
if(q.first>b.first){
b=q;
c=p;
}
}
b.first+=a.first-c.first+w[i];
b.second+=a.second-c.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 l=0,h=1e18;
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 | 1888 KiB | ||||
2 | Accepted | 3ms | 2120 KiB | ||||
subtask2 | 0/9 | ||||||
3 | Accepted | 3ms | 2352 KiB | ||||
4 | Accepted | 3ms | 2544 KiB | ||||
5 | Accepted | 3ms | 2780 KiB | ||||
6 | Wrong answer | 2ms | 2864 KiB | ||||
7 | Wrong answer | 2ms | 2860 KiB | ||||
8 | Accepted | 2ms | 2840 KiB | ||||
9 | Accepted | 3ms | 2928 KiB | ||||
10 | Accepted | 2ms | 3020 KiB | ||||
11 | Accepted | 3ms | 3244 KiB | ||||
12 | Accepted | 2ms | 3248 KiB | ||||
subtask3 | 7/7 | ||||||
13 | Accepted | 4ms | 3656 KiB | ||||
14 | Accepted | 4ms | 3652 KiB | ||||
15 | Accepted | 7ms | 4120 KiB | ||||
16 | Accepted | 7ms | 4072 KiB | ||||
17 | Accepted | 7ms | 4052 KiB | ||||
18 | Accepted | 7ms | 4104 KiB | ||||
19 | Accepted | 7ms | 4100 KiB | ||||
20 | Accepted | 8ms | 4140 KiB | ||||
21 | Accepted | 9ms | 4252 KiB | ||||
subtask4 | 11/11 | ||||||
22 | Accepted | 101ms | 9372 KiB | ||||
23 | Accepted | 101ms | 9596 KiB | ||||
24 | Accepted | 104ms | 9612 KiB | ||||
25 | Accepted | 104ms | 9752 KiB | ||||
26 | Accepted | 112ms | 9796 KiB | ||||
27 | Accepted | 118ms | 9900 KiB | ||||
subtask5 | 0/22 | ||||||
28 | Accepted | 7ms | 4748 KiB | ||||
29 | Accepted | 8ms | 4660 KiB | ||||
30 | Accepted | 8ms | 4712 KiB | ||||
31 | Accepted | 8ms | 4644 KiB | ||||
32 | Wrong answer | 8ms | 4640 KiB | ||||
33 | Accepted | 8ms | 4620 KiB | ||||
34 | Accepted | 7ms | 4652 KiB | ||||
subtask6 | 0/51 | ||||||
35 | Accepted | 112ms | 10080 KiB | ||||
36 | Wrong answer | 114ms | 10180 KiB | ||||
37 | Accepted | 118ms | 10224 KiB | ||||
38 | Wrong answer | 119ms | 10228 KiB | ||||
39 | Wrong answer | 122ms | 10104 KiB | ||||
40 | Accepted | 127ms | 10012 KiB | ||||
41 | Accepted | 96ms | 10008 KiB | ||||
42 | Accepted | 108ms | 10012 KiB | ||||
43 | Accepted | 119ms | 10248 KiB | ||||
44 | Accepted | 109ms | 10356 KiB | ||||
45 | Accepted | 111ms | 10332 KiB |