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>
#define Shrek_Crush228 ios_base::sync_with_stdio(0), cin.tie(0);
using namespace std;
const int N = 1e5 + 123;
int a[N], b[N], l, r, n, k, m, w, sum, sum1, y, ans1, ans, x;
string s, s1;
int nt[N], taked[N];
bool ok;
vector <int> v[22];
int mp[22][N * 11];
void mask( int i, int sop, int need, int st, int sum = 0, int ss = 0 )
{
if( sum > need ) return;
if( i == m )
{
if( sum != need || mp[st][ss] ) return;
mp[st][ss] = 1;
v[st].push_back( ss );
//cout << ss << '\n';
return;
}
mask( i + 1, sop, need, st, sum, ss + ((( sop >> i ) & 1 ) ? ( 1 << i ) : 0 ));
if( !(( sop >> i ) & 1 )) mask( i + 1, sop, need, st, sum + b[i + 1], ss + ( 1 << i ));
}
void rec( int i, string s )
{
if( i > n )
{
ok = 1;
return;
}
v[i].clear();
int sop = 0;
for( int bit = 0; bit < m; bit ++ )
{
if( s[bit] == '1' ) sop += ( 1 << bit );
}
mask( 0, sop, a[i], i );
for( auto mas : v[i] )
{
string sss = "";
int sam = mas;
for( int bit = 0; bit < m; bit ++ )
{
if(( sam >> bit ) & 1 ) sss += '1';
else sss += '0';
}
//cout << sss << ' ' << i << '\n';
rec( i + 1, sss );
if( ok ) return;
}
}
void solve()
{
cin >> n >> m;
string nw = "";
for( int i = 1; i <= n; i ++ ) cin >> a[i];
for( int i = 1; i <= m; i ++ ) cin >> b[i], nw += '0';
rec( 1, nw );
if( ok ) cout << "YES\n";
else cout << "NO\n";
}
int main()
{
//freopen("rmq.in", "r", stdin);
//freopen("rmq.out", "w", stdout);
Shrek_Crush228
int test = 1;
if( !test ) cin >> test;
while( test -- )
{
solve();
}
}
// solved by KluydQ
# | 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... |