#include <bits/stdc++.h>
#define N 100010
#define K 110
#define inf 200000000000000000LL
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
ll n, k, v[N], pref[N], dp[N][K], best[N][K];
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
for(int i = 1; i <= n; i++) cin>>v[i];
for(int i = 0; i < N; i++)
for(int j = 0; j < K; j++)
dp[i][j] = inf;
dp[0][1] = 0;
for(int i = 1; i <= n; i++)
{
dp[i][1] = max(dp[i - 1][1], v[i]);
best[i][1] = dp[i][1];
pref[i] = pref[i - 1] + v[i];
}
dp[0][0] = 0;
dp[0][1] = inf;
for(int q = 2; q <= k; q++)
{
stack < pii > W;
for(int i = 1; i <= n; i++)
{
ll opt = dp[i - 1][q - 1];
while(!W.empty() and W.top().f < v[i])
{
opt = min(opt, W.top().s);
W.pop();
}
if(W.empty() or opt + v[i] < W.top().f + W.top().s) W.push({v[i], opt});
if(i >= k) dp[i][k] = W.top().f + W.top().s;
}
}
cout<<dp[n][k]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
86520 KB |
Output is correct |
2 |
Correct |
62 ms |
86520 KB |
Output is correct |
3 |
Correct |
72 ms |
86572 KB |
Output is correct |
4 |
Correct |
63 ms |
86704 KB |
Output is correct |
5 |
Correct |
66 ms |
86792 KB |
Output is correct |
6 |
Correct |
66 ms |
86792 KB |
Output is correct |
7 |
Correct |
65 ms |
86792 KB |
Output is correct |
8 |
Correct |
64 ms |
86868 KB |
Output is correct |
9 |
Incorrect |
67 ms |
86868 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
86868 KB |
Output is correct |
2 |
Correct |
63 ms |
86868 KB |
Output is correct |
3 |
Correct |
62 ms |
86868 KB |
Output is correct |
4 |
Correct |
62 ms |
86868 KB |
Output is correct |
5 |
Correct |
64 ms |
86868 KB |
Output is correct |
6 |
Correct |
62 ms |
86868 KB |
Output is correct |
7 |
Correct |
62 ms |
86868 KB |
Output is correct |
8 |
Correct |
64 ms |
86868 KB |
Output is correct |
9 |
Incorrect |
59 ms |
86868 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
86908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
76 ms |
95484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |