#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define ll long long
#define pb push_back
#define nl '\n'
#define popb pop_back()
#define sz size()
#define ld long double
#define ull unsigned long long
#define F first
#define S second
#define fix fixed<<setprecision
#define pii pair<int,int>
#define E exit (0)
#define int long long
const int inf=1e9;
int c[100001],mn=inf,n;
void r(int pos,int k,int sum=0,int mx=0) {
if (pos==n+1&&k>0) {
return;
}
if (k==0&&pos==n+1) {
mn=min(mn,sum);
return;
}
if (k<0) {
return;
}
r(pos+1,k-1,sum+c[pos],c[pos]);
r(pos+1,k,sum-mx+max(mx,c[pos]),max(mx,c[pos]));
}
signed main() {
//freopen("planting.in","r",stdin);
//freopen("planting.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k;
cin>>n>>k;
int s[n+2];
s[n+1]=0;
for (int i=1;i<=n;i++) {
cin>>c[i];
}
for (int i=n;i>=1;i--) {
s[i]=max(s[i+1],c[i]);
}
if (k<=5) {
int mn=inf;
if (k==1) {
mn=s[1];
}
for (int i=1,mx1=0;i<n;i++) {
mx1=max(mx1,c[i]);
if (k>2) {
for (int j=i+1,mx2=0;j<n;j++) {
mx2=max(mx2,c[j]);
if (k>3) {
for (int d=j+1,mx3=0;d<n;d++) {
mx3=max(mx3,c[d]);
if (k>4) {
for (int f=d+1,mx4=0;f<n;f++) {
mx4=max(mx4,c[f]);
mn=min(mn,mx4+mx3+mx2+mx1+s[f+1]);
}
}else {
mn=min(mn,mx3+mx2+mx1+s[d+1]);
}
}
}else {
mn=min(mn,mx1+mx2+s[j+1]);
}
}
}else {
mn=min(mn,mx1+s[i+1]);
}
}
cout<<mn;
return 0;
}
r(1,k);
cout<<mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
496 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
8 ms |
400 KB |
Output is correct |
14 |
Correct |
8 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
10 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
8 ms |
364 KB |
Output is correct |
15 |
Correct |
6 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
512 KB |
Output is correct |
17 |
Correct |
8 ms |
400 KB |
Output is correct |
18 |
Correct |
9 ms |
364 KB |
Output is correct |
19 |
Correct |
12 ms |
364 KB |
Output is correct |
20 |
Correct |
8 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
496 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
8 ms |
400 KB |
Output is correct |
14 |
Correct |
8 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
10 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
8 ms |
364 KB |
Output is correct |
33 |
Correct |
6 ms |
364 KB |
Output is correct |
34 |
Correct |
8 ms |
512 KB |
Output is correct |
35 |
Correct |
8 ms |
400 KB |
Output is correct |
36 |
Correct |
9 ms |
364 KB |
Output is correct |
37 |
Correct |
12 ms |
364 KB |
Output is correct |
38 |
Correct |
8 ms |
364 KB |
Output is correct |
39 |
Execution timed out |
1037 ms |
364 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
496 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
8 ms |
400 KB |
Output is correct |
14 |
Correct |
8 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
10 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
8 ms |
364 KB |
Output is correct |
33 |
Correct |
6 ms |
364 KB |
Output is correct |
34 |
Correct |
8 ms |
512 KB |
Output is correct |
35 |
Correct |
8 ms |
400 KB |
Output is correct |
36 |
Correct |
9 ms |
364 KB |
Output is correct |
37 |
Correct |
12 ms |
364 KB |
Output is correct |
38 |
Correct |
8 ms |
364 KB |
Output is correct |
39 |
Execution timed out |
1037 ms |
364 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |