#include <bits/stdc++.h>
using namespace std;
#define ll long long
//14:20
int n;
ll m;
ll c[45];
ll way[1050000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) {
cin >> c[i];
}
if(n <= 21) {
ll ways = 0;
for(int mask = 0; mask < (1<<n); mask++) {
ll money = m;
for(int i = 0; i < n; i++) {
if(mask&(1<<i)) {
money-= c[i];
}
}
if(money >= 0) {
ways++;
}
}
cout << ways;
} else {
ll ways = 0;
vector<ll> did;
unordered_map<ll,int> compr;
for(int mask = 0; mask < (1<<20); mask++) {
ll money = m;
for(int i = 0; i < 20; i++) {
if(mask&(1<<i)) {
money-= c[i];
}
}
if(money >= 0) {
did.push_back(m-money);
}
}
sort(did.begin(),did.end());
int in = 1;
for(ll el : did) {
compr[el] = in++;
}
for(int mask = 0; mask < (1<<20); mask++) {
ll money = m;
for(int i = 0; i < 20; i++) {
if(mask&(1<<i)) {
money-= c[i];
}
}
if(money >= 0) {
way[compr[(m-money)]]++;
}
}
for(int i = 1; i <= in; i++) {
way[i]+=way[i-1];
}
for(int mask = 0; mask < (1<<(n-20)); mask++) {
ll money = m;
for(int i = 20; i < n; i++) {
if(mask&(1<<(i-20))) {
money-= c[i];
}
}
if(money >= 0) {
auto it = upper_bound(did.begin(),did.end(),money);
if(it != did.begin()) {
it--;
ways+= way[compr[*it]];
}
}
}
cout << ways;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
484 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
664 KB |
Output is correct |
6 |
Correct |
2 ms |
664 KB |
Output is correct |
7 |
Correct |
2 ms |
664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
720 KB |
Output is correct |
2 |
Correct |
6 ms |
720 KB |
Output is correct |
3 |
Correct |
11 ms |
720 KB |
Output is correct |
4 |
Correct |
2 ms |
720 KB |
Output is correct |
5 |
Correct |
6 ms |
720 KB |
Output is correct |
6 |
Correct |
81 ms |
720 KB |
Output is correct |
7 |
Correct |
12 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
732 KB |
Output is correct |
2 |
Correct |
20 ms |
732 KB |
Output is correct |
3 |
Correct |
6 ms |
732 KB |
Output is correct |
4 |
Correct |
8 ms |
732 KB |
Output is correct |
5 |
Correct |
81 ms |
732 KB |
Output is correct |
6 |
Correct |
11 ms |
732 KB |
Output is correct |
7 |
Correct |
12 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
321 ms |
17788 KB |
Output is correct |
2 |
Correct |
873 ms |
30916 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
41632 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
360 ms |
41632 KB |
Output is correct |
2 |
Correct |
684 ms |
41632 KB |
Output is correct |
3 |
Correct |
488 ms |
41632 KB |
Output is correct |
4 |
Correct |
2 ms |
41632 KB |
Output is correct |
5 |
Correct |
262 ms |
41632 KB |
Output is correct |
6 |
Correct |
455 ms |
41632 KB |
Output is correct |
7 |
Correct |
158 ms |
41632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
725 ms |
41632 KB |
Output is correct |
2 |
Correct |
889 ms |
41632 KB |
Output is correct |
3 |
Correct |
921 ms |
41632 KB |
Output is correct |
4 |
Correct |
22 ms |
41632 KB |
Output is correct |
5 |
Correct |
298 ms |
41632 KB |
Output is correct |
6 |
Correct |
502 ms |
41632 KB |
Output is correct |
7 |
Correct |
169 ms |
41632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1087 ms |
64212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
64212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
64224 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |