#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define show(a) cerr << #a <<" -> "<< a <<"\n"
#define fo(a, b, c, d) for(int (a) = (b); (a) <= (c); (a) += (d))
#define foo(a, b, c ,d) for(int (a) = (b); (a) >= (c); (a) -= (d))
//#define int ll
const int N = 205;
const int INF = 2e9 + 5;
int n, k, a[N], ans = INF, sufmx[N];
bool End[N];
void rec(int pos, int cnt, int sum, int mx) {
if(pos == n) {
ans = min(ans, sum + max(mx, a[pos]));
return;
}
if(cnt + n - pos >= k) {
rec(pos + 1, cnt, sum, max(mx, a[pos]));
}
if(cnt + 1 + n - pos >= k) {
End[pos] = 1;
rec(pos + 1, cnt + 1, sum + max(mx, a[pos]), mx = 0);
End[pos] = 0;
}
}
int mx = 0;
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
mx = max(mx, a[i]);
}
if(k <= 5) {
sufmx[n] = a[n];
for(int i = n - 1; i >= 1; --i) {
sufmx[i] = max(sufmx[i + 1], a[i]);
}
if(k == 1) {
cout << mx;
return 0;
}
int mxi = 0;
for(int i = 1; i <= n - k + 1; ++i) {
mxi = max(mxi, a[i]);
if(k == 2) {
ans = min(ans, mxi + sufmx[i + 1]);
continue;
}
int mxj = 0;
for(int j = i + 1; j <= n - k + 2; ++j) {
mxj = max(mxj, a[j]);
if(k == 3) {
ans = min(ans, mxi + mxj + sufmx[j + 1]);
continue;
}
int mxl = 0;
for(int l = j + 1; l <= n - k + 3; ++l) {
mxl = max(mxl, a[l]);
if(k == 4) {
ans = min(ans, mxi + mxj + mxl + sufmx[l + 1]);
continue;
}
int mxr = 0;
for(int r = l + 1; r <= n - k + 4; ++r) {
mxr = max(mxr, a[r]);
ans = min(ans, mxi + mxj + mxl + mxr + sufmx[r + 1]);
}
}
}
}
cout << ans <<'\n';
/*if(k == 1) {
cout << mx;
} else if(k == 2) {
int mxi = 0;
for(int i = 1; i < n; ++i) {
mxi = max(mxi, a[i]);
ans = min(ans, mxi + sufmx[i + 1]);
}
cout << ans;
} else if(k == 3) {
int mxi = 0;
for(int i = 1; i <= n - k + 1; ++i) {
mxi = max(mxi, a[i]);
int mxj = 0;
for(int j = i + 1; j <= n - k + 2; ++j) {
mxj = max(mxj, a[j]);
int mxl = 0;
for(int l = j + 1; l <= n - k + 3; ++l) {
mxl = max(mxl, a[l]);
int mxr = 0;
for(int r = l + 1; r <= n - k + 4; ++r) {
mxr = max(mxr, a[r]);
}
}
}
}
}*/
return 0;
}
End[n] = 1;
rec(1, 0, 0, 0);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
8 ms |
376 KB |
Output is correct |
14 |
Correct |
8 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
8 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1081 ms |
256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
508 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |