이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
#endif
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl '\n'
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < (int) n; i++)
#define pb push_back
#define fi first
#define se second
int mod = 1e9;
int inf = 1e16;
const int N = 3e5+5;
void solve() {
int n, m;
cin >> n >> m;
vi a(n), b(m);
F(i, 0, n) cin >> a[i];
F(i, 0, m) cin >> b[i];
vb dp(1<<m, 0);
dp[0] = 1;
F(mask, 0, 1<<m) {
if(!dp[mask]) continue;
int paidSoFar = 0;
F(i, 0, m) {
if(mask >> i & 1) paidSoFar += b[i];
}
int nextPerson = 0;
while(nextPerson < n && paidSoFar > a[nextPerson]) paidSoFar -= a[nextPerson++];
if(nextPerson == n) continue;
int gap = a[nextPerson] - paidSoFar;
F(i, 0, m) {
if(mask >> i & 1) continue;
if(b[i] <= gap) {
dp[mask | (1 << i)] = 1;
}
}
}
int toBePaid = 0;
F(i, 0, n) toBePaid += a[i];
F(i, 0, 1 << m) {
if(!dp[i]) continue;
int paidSoFar = 0;
F(j, 0, m) {
if(i >> j & 1) paidSoFar += b[j];
}
if(paidSoFar == toBePaid) {
cout << "YES" << endl;
exit(0);
}
}
cout << "NO" << endl;
}
void setIO() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef Local
freopen("local.in", "r", stdin);
freopen("local.out", "w", stdout);
#else
// freopen("friendcross.in","r",stdin);
// freopen("friendcross.out","w",stdout);
#endif
}
signed main() {
setIO();
int t = 1;
//cin >> t;
while(t--) 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... |