You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
569 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
int n;
int d;
int res = INF;
int a[N];
void dfs(int u, int r, int c) {
if (u == n + 1) {
res = min(res, c);
return;
}
if (r + d >= a[u + 1]) dfs(u + 1, r + d - a[u + 1], c + 1);
if (r >= a[u + 1]) dfs(u + 1, r - a[u + 1], c);
}
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
dfs(0, 0, 0);
if (res == INF)
printf("-1");
else
printf("%d", res);
return 0;
}