/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author win11905
*/
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define iii tuple<int, int, int>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 1e5+5;
const int K = 1e2+5;
class blocks {
private:
int n, k;
int dp[K][N];
int A[N];
public:
void solve(istream& cin, ostream& cout) {
cin >> n >> k;
for(int i = 1; i <= n; ++i) cin >> A[i];
for(int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i-1], A[i]);
for(int i = 2; i <= k; ++i) {
stack<pii> stk;
for(int j = i; j <= n; ++j) {
int mnpos = j;
while(!stk.empty() && A[stk.top().x] <= A[j]) mnpos = stk.top().x, stk.pop();
stk.emplace(mnpos, min(dp[i-1][mnpos-1] + A[j], stk.empty() ? INF : stk.top().y));
dp[i][j] = stk.top().y;
}
}
cout << dp[k][n] << endl;
}
};
class Solver {
public:
void solve(std::istream& in, std::ostream& out) {
blocks *obj = new blocks();
obj->solve(in, out);
}
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solver solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
41848 KB |
Output is correct |
2 |
Correct |
29 ms |
41848 KB |
Output is correct |
3 |
Correct |
29 ms |
41848 KB |
Output is correct |
4 |
Correct |
66 ms |
41848 KB |
Output is correct |
5 |
Correct |
29 ms |
41848 KB |
Output is correct |
6 |
Correct |
29 ms |
41848 KB |
Output is correct |
7 |
Correct |
29 ms |
41976 KB |
Output is correct |
8 |
Correct |
29 ms |
41848 KB |
Output is correct |
9 |
Incorrect |
32 ms |
41796 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
41976 KB |
Output is correct |
2 |
Correct |
29 ms |
41848 KB |
Output is correct |
3 |
Correct |
28 ms |
41848 KB |
Output is correct |
4 |
Correct |
29 ms |
41976 KB |
Output is correct |
5 |
Correct |
30 ms |
41848 KB |
Output is correct |
6 |
Correct |
29 ms |
41848 KB |
Output is correct |
7 |
Correct |
29 ms |
41848 KB |
Output is correct |
8 |
Correct |
29 ms |
41848 KB |
Output is correct |
9 |
Incorrect |
29 ms |
41856 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
41848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
41976 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |