#include<bits/stdc++.h>
using namespace std;
int N, mid;
long long M;
long long cost[40];
void smallM(){
vector<long long int> dp(M+1, 0);
dp[0] = 1;
for(int match = 0; match < N; ++match){
for(long long val = M; val > 0; --val){
if(cost[match] <= val) dp[val] += dp[val - cost[match]];
}
}
long long ans = 0;
for(int i = 0; i <= M; ++i) ans += dp[i];
cout << ans << endl;
}
int binarySearch(long long key, vector<long long>& V){
//maximo i tal que V[i] <= key
int lo = 0;
int hi = (int)V.size()-1;
int ans = -1;
while(lo <= hi){
int m = lo + (hi - lo)/2;
if(V[m] <= key){
ans = m;
lo = m+1;
}else hi = m-1;
}
return ans;
}
void print(vector<long long> & V){
for(int x: V) cout << x << " ";
cout << endl;
}
int main(){
cin >> N >> M;
for(int i = 0; i < N; ++i) cin >> cost[i];
mid = N/2;
vector<long long int>nums;
for(int mask = 0; mask < (1 << mid); ++mask){
long long sum = 0;
for(int i = 0; i < mid; ++i){
if(mask & (1 << i))sum += cost[i];
}
nums.push_back(sum);
}
sort(nums.begin(), nums.end());
//print(nums);
long long ans = 0;
for(int mask = 0; mask < (1 << (N - mid)); ++mask){
long long sum = 0;
for(int i = 0; i < (N - mid); ++i){
if(mask & (1 << i))sum += cost[i + mid];
}
long long pos = binarySearch(M - sum, nums);
//cout << sum << " " << pos << endl;
ans += pos + 1;
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
932 KB |
Output is correct |
2 |
Correct |
90 ms |
2592 KB |
Output is correct |
3 |
Correct |
389 ms |
8532 KB |
Output is correct |
4 |
Correct |
83 ms |
2376 KB |
Output is correct |
5 |
Correct |
14 ms |
960 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
13 ms |
956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
1372 KB |
Output is correct |
2 |
Correct |
28 ms |
848 KB |
Output is correct |
3 |
Correct |
167 ms |
4532 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
8 ms |
644 KB |
Output is correct |
6 |
Correct |
13 ms |
848 KB |
Output is correct |
7 |
Correct |
13 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
1436 KB |
Output is correct |
2 |
Correct |
136 ms |
2476 KB |
Output is correct |
3 |
Correct |
132 ms |
2480 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
72 ms |
2480 KB |
Output is correct |
6 |
Correct |
232 ms |
8632 KB |
Output is correct |
7 |
Correct |
78 ms |
2476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
4532 KB |
Output is correct |
2 |
Correct |
28 ms |
936 KB |
Output is correct |
3 |
Correct |
9 ms |
700 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
672 KB |
Output is correct |
6 |
Correct |
164 ms |
4528 KB |
Output is correct |
7 |
Correct |
13 ms |
936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
916 KB |
Output is correct |
2 |
Correct |
80 ms |
2492 KB |
Output is correct |
3 |
Correct |
8 ms |
688 KB |
Output is correct |
4 |
Correct |
8 ms |
596 KB |
Output is correct |
5 |
Correct |
93 ms |
2460 KB |
Output is correct |
6 |
Correct |
20 ms |
848 KB |
Output is correct |
7 |
Correct |
236 ms |
8628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
332 ms |
8636 KB |
Output is correct |
2 |
Correct |
29 ms |
888 KB |
Output is correct |
3 |
Correct |
9 ms |
596 KB |
Output is correct |
4 |
Correct |
406 ms |
8584 KB |
Output is correct |
5 |
Correct |
112 ms |
4528 KB |
Output is correct |
6 |
Correct |
13 ms |
848 KB |
Output is correct |
7 |
Correct |
27 ms |
1404 KB |
Output is correct |