#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define FORD(i,a,b) for(ll i=a;i>=b;i--)
#define MAXN 100005
#define MAXK 102
#define pb push_back
#define TASK "name"
#define ff first
#define ss second
#define oo 4557430888798830399
#define ll long long
#define pii pair<long long,long long>
using namespace std;
bool minimize(ll &a,ll b)
{
if (a>b)
{
a=b;
return true;
}
return false;
}
bool maximize (ll &a,ll b)
{
if (a<b)
{
a=b;
return true;
}
return false;
}
ll dp[MAXK+1][MAXN+1];
ll n,k;
ll a[MAXN+1];
void nhap()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen (TASK".inp","r",stdin);
//freopen (TASK".out","w",stdout);
cin >> n >> k;
FOR(i,1,n) cin >> a[i];
}
void solve()
{
ll res=0;
FOR(i,1,n)
{
res=max(res,a[i]);
dp[1][i]=res;
}
FOR(i,2,k)
{
stack<ll> s1,s2;
FOR(j,1,n)
{
ll last_dp=dp[i-1][j-1];
while (!s1.empty()&&s1.top()<a[j])
{
last_dp=min(last_dp,s2.top());
s1.pop();
s2.pop();
}
if (s1.empty()||last_dp+a[j]<s1.top()+s2.top())
{
s1.push(a[j]);
s2.push(last_dp);
}
if (j>=i) dp[i][j]=s1.top()+s2.top();
}
}
cout << dp[k][n];
}
int main()
{
nhap();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
328 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |