이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define ll int
#define pb push_back
#define ar array
#define vi vector
#define bit(i,x) ((x >> i) & 1ll)
#define all(x) x.begin(),x.end()
#define low(l,r,x) x.begin()+l,x.begin()+r
#define len(l,r) (r-l+1)
#define PI acos(-1.0)
const long long mod = 1e9+7, N = 2e5, INF = 1e18, Log = 19, MASK = 1 << 19;
template <class T> void setmin(T &a, T b) { if(b < a) a = b; }
template <class T> void setmax(T &a, T b) { if(b > a) a = b; }
template <class T> void add(T &a, T b) {a += b; if(a >= mod) a -= mod;}
using namespace std;
#define int long long
ll dp[21][1 << 21];
ll fac[N+5];
void kazuha() {
ll n , m;
cin >> n >> m;
ll a[n+5] , b[m+5];
ll pfs[n+5];
pfs[0] = 0;
for(int i = 1; i <= n;i++) {
cin >> a[i];
pfs[i] = pfs[i-1] + a[i];
}
for(int j =0; j < m;j++) {
cin >> b[j];
}
ll s = (1 << m);
for(int i = 0; i < s;i++) {
ll cur = 0;
for(int j = 0; j < m;j++) {
if(bit(j, i)) cur += b[j];
}
fac[i] = cur;
}
dp[0][0] = 1;
for(int i = 1; i <= n;i++) {
for(int msk = 1; msk < s;msk++) {
if(fac[msk] != pfs[i]) continue;
for(int sub = msk; ; sub = (sub -1) & msk) {
if(fac[sub] < a[i]) break;
if(fac[sub] == a[i] && dp[i-1][msk ^ sub]) {
dp[i][msk] = 1; break;
}
if(!sub) break;
}
}
}
ll ans = 0;
for(int i = 0; i < s;i++) {
ans |= dp[n][i];
}
cout << ((ans == 1) ? "YES" : "NO") << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("permutation.cpp", "r", stdin);
// freopen("permutation.cpp", "w", stdout);
long long mitsu = 1;
// cin >> mitsu;
for(long long seele = 0; seele < mitsu;seele++) {
// cout << "Case " << seele << " :";
kazuha();
}
}
# | 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... |