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 ff first
#define ss second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ones(x) __builtin_popcount(x)
#define remove(v)  v.erase(unique(all(v)), v.end())
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
 
#ifdef local
#include "C:\debug.h"
#else
#define dbg(x...) 42
#endif
#define int long long
 
const int N = 2e6 + 2 ;
const int MOD = 1e9 + 7 ;
const int P = 1007 ;
 
int n , m , a[21] , b[21] , pref[21] ;
int dp[(1 << 21)] ;
 
bool go(int i , int mask , int cur) {
    if(cur == pref[i]) i++ ;
    if(i > n) return 1 ;
    if(~dp[mask]) return dp[mask] ;
    bool current = 0 ;
    rep(j , 0 , m - 1) {
        if(!(mask >> j & 1) && cur + b[j + 1] <= pref[i]) {
            current |= go(i , (mask | (1 << j)) , cur + b[j + 1]) ;    
        }
    }
    dp[mask] = current ;
    return dp[mask] ;
}
 
void solve(){
    cin >> n >> m ;
    rep(i , 0 , (1 << 21) - 1) dp[i] = -1 ;
    rep(i , 1 , n) cin >> a[i] ;
    rep(i , 1 , m) cin >> b[i] ;
    rep(i , 1 , n) pref[i] = pref[i - 1] + a[i] ;
    bool ok = go(1 , 0 , 0) ;
    if(ok) cout << "YES\n" ;
    else cout << "NO\n" ;
}
 
signed main () {
	// ios::sync_with_stdio(false) ;
	// cin.tie(0) ;
	int test = 1 ;
	// cin >> test ; 
	for(int i = 1 ; i <= test ; i++) {
		solve() ;
	}
}
| # | 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... |