#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 1e5 + 10, MAXK = 105;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n, k, a[MAXN];
void read()
{
cin >> n >> k;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
}
int dp[MAXN][MAXK];
void solve()
{
int inf = 1e9;
for (int j = 1; j <= k; ++ j)
{
stack < pair <int, int > > t;
for (int i = j; i <= n; ++ i)
{
dp[i][j] = inf;
dp[i][j] = dp[i-1][j-1] + a[i];
int init = dp[i][j];
while(!t.empty())
{
int sz = t.top().first;
int dpp = t.top().second;
if(sz < a[i])
{
if(dpp + a[i] - sz < init)dp[i][j] = dpp + a[i] - sz;
t.pop();
}
else break;
}
if(t.size() == 0 || t.top().second > dp[i][j])t.push(make_pair(a[i], dp[i][j]));
else dp[i][j] = min(dp[i][j], t.top().second);
//cout << i << " " << j << " --> " << dp[i][j] << endl;
}
}
cout << dp[n][k] << endl;
}
int main()
{
speed();
read();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |