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>
#include <vector>
#include <set>
#include <iostream>
//#pragma GCC optimize("Ofast")
#define ld long double
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
using namespace std;
//mt19937 gen(time(0));
const ll inf = 1e18 + 18;
vector <int> a, b;
int fre(int n)
{
return rand() % n;
}
main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("divide.in", "r", stdin);
// freopen("divide.out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
if (n > m)
{
cout << "NO";
return 0;
}
a.resize(n, 0);
b.resize(m, 0);
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < m; i++)
cin >> b[i];
srand(time(0));
random_shuffle(a.begin(), a.end(), fre);
random_shuffle(b.begin(), b.end(), fre);
int t = 1;
for(int i = 2; i <= m; i++)
t *= i;
if (n == 1)
{
//O(2 ^ m * m)
for(int i = 0; i < int(1 << m); i++)
{
int sum = 0;
for(int j = 0; j < m; j++)
{
if ((i & (1 << j)) > 0) sum += b[j];
}
if (a[0] == sum)
{
cout << "YES";
return 0;
}
}
}
else
{
// O(m! * (n + m))
while(t-- && (clock() / CLOCKS_PER_SEC) < 0.860)
{
vector <int> c = a;
int i = 0;
int j = 0;
bool f = true;
while(i < n && j < m)
{
if (c[i] > 0)
{
c[i] -= b[j];
j++;
}
if (c[i] == 0)
{
i++;
}
if (c[i] < 0)
{
f = false;
break;
}
}
if (f && i == n)
{
cout << "YES";
return 0;
}
next_permutation(b.begin(), b.end());
}
}
cout << "NO";
return 0;
}
Compilation message (stderr)
bank.cpp:25:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
# | 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... |