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 time_run cerr << 1.0*clock()/CLOCKS_PER_SEC << "\n"
#define fdto(i, a, b) for(int i = (int)a; i >= (int)b; --i)
#define fto(i, a, b) for(int i = (int)a; i <= (int)b; ++i)
#define all(x) x.begin(), x.end()
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
using namespace std;
using db = double;
using ll = long long;
using ii = pair<int, int>;
using llll = pair<ll, ll>;
const long long ooo = (ll)1e18 + 7;
const int mod = (int)1e9 + 7, oo = (int)1e9 + 7;
#define name "cpp"
#define maxM (int)1e + 5
#define maxN (int)2e1 + 5
int n, m, a[maxN], b[maxN];
ii f[(1 << 20) + 5];
bool cmp(int a, int b){
return (a > b);
}
int main(){
// freopen(name".in", "r", stdin); freopen(name".out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(nullptr);
// Like Elevator Rides
cin >> n >> m;
fto(i, 1, n) cin >> a[i];
fto(i, 0, m-1) cin >> b[i];
sort(a + 1, a + 1 + n, cmp);
f[0] = {1, a[1]};
fto(mask, 1, (1 << m)-1){
f[mask] = {oo, oo};
fto(bit, 0, m-1){
if (mask >> bit & 1){
int pre = mask ^ (1 << bit);
if (f[pre].ss - b[bit] >= 0){
if (f[pre].ff < f[mask].ff){
f[mask] = {f[pre].ff, f[pre].ss - b[bit]};
} else
if (f[pre].ff == f[mask].ff){
f[mask] = {f[mask].ff, max(f[mask].ss, f[pre].ss - b[bit])};
}
} else
if (a[f[pre].ff+1] - b[bit] >= 0){
if (f[pre].ff + 1 < f[mask].ff){
f[mask] = {f[pre].ff + 1, max(f[pre].ss, a[f[pre].ff+1] - b[bit])};
}
}
}
}
if (f[mask].ff == n && f[mask].ss == 0){
// int msk = mask;
// fto(mask, 0, msk){
// cout << mask << " "; fto(bit, 0, m-1) cout << (mask >> bit & 1);
// cout << endl;
// cout << f[mask].ff << " " << f[mask].ss << endl;
// }
cout << "YES" << endl;
return 0;
}
}
// fto(mask, 0, (1 << m)-1){
// cout << mask << " "; fto(bit, 0, m-1) cout << (mask >> bit & 1);
// cout << endl;
// cout << f[mask].ff << " " << f[mask].ss << endl;
// }
cout << "NO" << endl;
return (0-0); // winhonepunch <3s
/*
bài thang máy nhưng mà thang máy này mỗi lần đi là sẽ thày đổi giá trị cân nặng
random_shuffle(v.begin(), v.end()): shuffles the order of the elements
bitset<10^9 + 1> vẫn hợp lệ do mỗi bit chỉ chiếm 1 bit => 128 MB
struct cmp{ // cmp for set
bool operator()(const type& x, const type& y) const {
return x. < y.;
}
};
__builtin_popcount(x) đếm số bit 1 trong x O(1)
bitset.to_ulong() chuyển từ bitset sang số
map should use for(const auto& it: map)
a += b better than a = a + b;
1 hour -> tags -> each hint -> code
*/
}
# | 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... |