#include<bits/stdc++.h>
#define STIZE(x) fprintf(stderr, "STIZE%lld\n", x);
#define PRINT(x) fprintf(stderr, "%s = %lld\n", #x, x);
#define NL(x) printf("%c", " \n"[(x)]);
#define lld long long
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 100010
#define MAXK 110
stack<pii> s;
stack<int> mx;
int dp[MAXN][MAXK];
int a[MAXN];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int N, K; cin >> N >> K;
for(int i = 1; i <= N; i++) dp[i][0] = INF;
//for(int i = 1; i <= K; i++) {
// for(int j = 0; j < i; j++) dp[j][i] = INF;
//}
for(int i = 1; i <= N; i++) cin >> a[i];
for(int k = 1; k <= K; k++) {
while(!s.empty()) s.pop();
while(!mx.empty()) mx.pop();
for(int n = 1; n <= N; n++) {
int dpmin = dp[n-1][k-1];
while(!s.empty() && a[n] >= s.top().fi) {
dpmin = min(dpmin, s.top().se);
s.pop();
mx.pop();
}
s.push({a[n], dpmin});
mx.push(min((!mx.empty() ? mx.top():INF), s.top().se+s.top().fi));
dp[n][k] = mx.top();
//PRINT(dp[n][k]);
}
}
cout << dp[N][K];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
252 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
568 KB |
Output is correct |
4 |
Incorrect |
2 ms |
568 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
568 KB |
Output is correct |
2 |
Correct |
2 ms |
584 KB |
Output is correct |
3 |
Correct |
2 ms |
584 KB |
Output is correct |
4 |
Incorrect |
2 ms |
584 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
4936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |