forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevator Rides.cpp
More file actions
36 lines (31 loc) · 808 Bytes
/
Elevator Rides.cpp
File metadata and controls
36 lines (31 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxN = 20;
const int INF = maxN+1;
int N;
pii dp[1<<maxN];
ll X, w[maxN];
int main(){
scanf("%d %lld", &N, &X);
for(int i = 0; i < N; i++)
scanf("%lld", &w[i]);
dp[0] = {1, 0};
for(int mask = 1; mask < (1<<N); mask++){
dp[mask] = {INF, 0};
for(int i = 0; i < N; i++){
if(mask&(1<<i)){
pii can = dp[mask^(1<<i)];
if(can.second + w[i] <= X){
can.second += w[i];
} else {
can.first++;
can.second = w[i];
}
dp[mask] = min(dp[mask], can);
}
}
}
printf("%d\n", dp[(1<<N)-1].first);
}