-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1653.cpp
More file actions
47 lines (44 loc) · 1.27 KB
/
cses_1653.cpp
File metadata and controls
47 lines (44 loc) · 1.27 KB
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
37
38
39
40
41
42
43
44
45
46
47
#include <bits/extc++.h>
#define int long long
using namespace std;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, tar;
cin >> n >> tar;
vector<int> a(n);
vector<pair<int, int>> dp(1 << n, {1e9 + 87, 1e9 + 87});
for (auto &x : a)
cin >> x;
sort(a.rbegin(), a.rend());
dp[0] = {1, 0};
for (int i = 1; i < (1 << n); i++)
{
for (int j = 0; j < n; j++)
{
if (i & (1 << j))
{
int prev = i ^ (1 << j);
if (dp[prev].second + a[j] <= tar)
{
if ((dp[prev].first < dp[i].first) ||
((dp[prev].first == dp[i].first) && dp[prev].second + a[j] < dp[i].second))
{
dp[i] = {dp[prev].first, dp[prev].second + a[j]};
}
}
else
{
if ((dp[prev].first + 1 < dp[i].first) ||
((dp[prev].first + 1 == dp[i].first) && dp[prev].second + a[j] < dp[i].second))
{
dp[i] = {dp[prev].first + 1, a[j]};
}
}
}
}
}
cout << dp[(1 << n) - 1].first << '\n';
return 0;
}