#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
using namespace std;
deque <pair <ll,ll> > v;
ll dp[101][100001],a[100001];
int main()
{
ll n,k,i,j;
cin>>n>>k;
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
for(j=1;j<=k;j++)
dp[j][i]=1e9;
for(i=1;i<=k;i++)
{
v.clear();
for(j=1;j<=n;j++)
{
ll s=dp[i-1][j-1];
if (j<i)
{
dp[i][j]=1e9;
continue;
}
while(!v.empty() && v.front().fr<=a[j])
{
//cout<<v.front().first<<" delete "<<v.front().sc<<endl;
s=min(s,v.front().sc);
v.pop_front();
}
if (!v.empty() && v.front().fr+v.front().sc<=s+a[j])
dp[i][j]=v.front().fr+v.front().sc;
else
{
dp[i][j]=s+a[j];
v.push_front({a[j],s});
//cout<<v.front().first<<" add "<<v.front().sc<<endl;
}
//cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
}
}
cout<<dp[k][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
464 KB |
Output is correct |
2 |
Incorrect |
3 ms |
464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
2844 KB |
Output is correct |
2 |
Correct |
26 ms |
2844 KB |
Output is correct |
3 |
Correct |
26 ms |
4056 KB |
Output is correct |
4 |
Correct |
82 ms |
28680 KB |
Output is correct |
5 |
Correct |
245 ms |
81444 KB |
Output is correct |
6 |
Incorrect |
51 ms |
81444 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |