#include <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = int;
const ll N=1e5+29;
const string br="617283";
ll dp[N],a[N],k[N],n,opt[N],op;
vector<ll>pos[257];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("subsequence.in","r",stdin);
// freopen("subsequence.out","w",stdout);
cin>>n;
for(ll i=1;i<=n;i++){
cin>>a[i];
}for(ll i=1;i<=n;i++){
cin>>k[i];
}
ll ans=0;
if(n<=5000){
for(ll i=1;i<=n;i++){
dp[i]=1;
for(ll j=i-1;j>=0;j--){
if(__builtin_popcountll(a[i]&a[j])==k[i]){
dp[i]=max(dp[i],dp[j]+1);
if(dp[i]==dp[j]+1)opt[i]=j;
}
}
ans=max(ans,dp[i]);
if(ans==dp[i])op=i;
}
}else{
for(ll i=1;i<=n;i++){
pos[a[i]].push_back(i);
dp[i]=1;
for(ll j=1;j<=256;j++){
if(__builtin_popcountll(j&a[i])==k[i]){
for(ll ii:pos[j]){
// if(i==3)cout<<(2&3)<<' ';
if(dp[ii]+1>dp[i]){
dp[i]=dp[ii]+1;
opt[i]=ii;
}
}
}
}
ans=max(ans,dp[i]);
if(ans==dp[i])op=i;
}
}
cout<<ans<<'\n';
vector<ll>v;
ll i=op;
while(i){
v.push_back(i);
i=opt[i];
}
reverse(v.begin(),v.end());
for(ll i:v){
cout<<i<<' ';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |