This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define ld long double
#define fr(i,n) for(i=0;i<n;i++)
#define fa(i,v) for(auto &i:v)
#define yesno cout<<"YES"<<endl; else cout<<"NO"<<endl;
#define vvl vector<vector<ll> >
#define vl vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll> >
#define vvpll vector<vector<pair<ll,ll> > >
#define mp make_pair
#define pb push_back
#define rs resize
#define cl(v) v.clear()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define inf (ll)1e18
#define f1 first
#define f2 second
#define mod (ll)(1e9+7)
int main(){
fastio
ll n,m,i,j;
cin>>n>>m;
ll a[n], b[m];
fr(i,n) cin>>a[i];
fr(i,m) cin>>b[i];
vector<pair<ll,multiset<ll> > > dp(1<<m);
dp[0]=mp(-1,multiset<ll>());
fr(i,(1<<m)) if(i){
multiset<ll> s;
ll mx=-inf;
fr(j,m) if((1<<j)&i){
if(dp[(1<<j)^i].f1>mx){
mx=dp[(1<<j)^i].f1;
s=dp[(1<<j)^i].f2;
s.insert(b[j]);
}
if(b[j]==a[dp[(1<<j)^i].f1+1]){
if(dp[(1<<j)^i].f1+1>mx) mx=dp[(1<<j)^i].f1+1, s=dp[(1<<j)^i].f2;
}
else{
fa(k,dp[(1<<j)^i].f2) if(b[j]+k==a[dp[(1<<j)^i].f1+1]){
if(dp[(1<<j)^i].f1+1>mx){
mx=dp[(1<<j)^i].f1+1;
s=dp[(1<<j)^i].f2;
s.erase(s.find(k));
}
}
}
}
// fa(k,s) cout<<k<<' ';
// cout<<i<<' '<<mx<<endl;
dp[i].f2=s;
dp[i].f1=mx;
if(mx>=n-1){
cout<<"YES";
return 0;
}
}
cout<<"NO";
}
# | 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... |