parent
3b4883793f
commit
f9c188f1d8
@ -0,0 +1,24 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
const int N = 100;
|
||||||
|
int a[N];
|
||||||
|
int n, k, res;
|
||||||
|
|
||||||
|
void dfs(int u, int k, int last, int sum) {
|
||||||
|
if (k == 0) {
|
||||||
|
res = max(res, sum);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (u == n + 1) return;
|
||||||
|
|
||||||
|
if (a[u] >= last)
|
||||||
|
dfs(u + 1, k - 1, a[u], sum + a[u]);
|
||||||
|
dfs(u + 1, k, last, sum);
|
||||||
|
}
|
||||||
|
int main() {
|
||||||
|
cin >> n >> k;
|
||||||
|
for (int i = 1; i <= n; i++) cin >> a[i];
|
||||||
|
dfs(1, k, 0, 0);
|
||||||
|
cout << res;
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,27 +1,24 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
const int INF = 0x3f3f3f3f;
|
const int N = 100;
|
||||||
const int N = 110;
|
|
||||||
int n;
|
|
||||||
int d;
|
|
||||||
int res = INF;
|
|
||||||
int a[N];
|
int a[N];
|
||||||
void dfs(int u, int r, int c) {
|
int n, k, res;
|
||||||
if (u == n + 1) {
|
|
||||||
res = min(res, c);
|
void dfs(int u, int k, int last, int sum) {
|
||||||
|
if (k == 0) {
|
||||||
|
res = max(res, sum);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (r + d >= a[u + 1]) dfs(u + 1, r + d - a[u + 1], c + 1);
|
if (u == n + 1) return;
|
||||||
if (r >= a[u + 1]) dfs(u + 1, r - a[u + 1], c);
|
|
||||||
|
if (a[u] >= last)
|
||||||
|
dfs(u + 1, k - 1, a[u], sum + a[u]);
|
||||||
|
dfs(u + 1, k, last, sum);
|
||||||
}
|
}
|
||||||
int main() {
|
int main() {
|
||||||
cin >> n >> d;
|
cin >> n >> k;
|
||||||
for (int i = 1; i <= n; i++) cin >> a[i];
|
for (int i = 1; i <= n; i++) cin >> a[i];
|
||||||
dfs(0, 0, 0);
|
dfs(1, k, 0, 0);
|
||||||
if (res == INF)
|
cout << res;
|
||||||
printf("-1");
|
|
||||||
else
|
|
||||||
printf("%d", res);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in new issue