#include <bits/stdc++.h>
using namespace std;
#define taskname "MAXK"
#define forinc(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define fordec(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define foreach(i, x) for (auto &i : x)
#define ms(x, n) memset(x, n, sizeof(x))
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define fi first
#define se second
#define pb push_back
#define pf push_front
template<typename TH>
void _dbg(const char* sdbg, TH h)
{
cerr << sdbg << " = " << h << "\n";
}
template<typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t)
{
while (*sdbg != ',') cerr << *sdbg++;
cerr << " = " << h << ",";
_dbg(sdbg + 1, t...);
}
#define db(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define chkpt cerr << "--- Checkpoint here ---\n";
const int N=1e5+5,K=1e2+2;
const int64_t LINF=1e18;
#define left Left
int n,k,a[N],left[N];
int64_t dp[K][N];
void ReadInput()
{
cin>>n>>k;
forinc(i,1,n) cin>>a[i];
}
int GetMax(int l,int r)
{
int res=a[l];
forinc(i,l+1,r) res=max(res,a[i]);
return res;
}
void Init()
{
stack<int> s;
forinc(i,1,n)
{
while(sz(s)&&a[s.top()]<=a[i]) s.pop();
left[i]=s.empty()?0:s.top();
s.push(i);
}
}
void Solve()
{
Init();
forinc(nGroup,1,k) forinc(i,1,n) dp[nGroup][i]=LINF;
int curMax=a[1];
forinc(i,1,n)
{
curMax=max(curMax,a[i]);
dp[1][i]=curMax;
}
forinc(nGroup,2,k)
{
forinc(i,nGroup,n)
{
// int curMax=a[i];
// fordec(j,i-1,1)
// {
// curMax=max(curMax,a[j+1]);
// if(dp[nGroup-1][j]==LINF) continue;
// dp[nGroup][i]=min(dp[nGroup][i],dp[nGroup-1][j]+curMax);
// }
// forinc(j,1,left[i])
// {
// if(nGroup==k&&i==n&&dp[nGroup][j]==5)
// {
// db(j);
// }
// dp[nGroup][i]=min(dp[nGroup][i],dp[nGroup][j]);
// }
if(left[i]) dp[nGroup][i]=dp[nGroup][left[i]];
int64_t tmp=LINF;
forinc(j,left[i]+1,i-1) tmp=min(tmp,dp[nGroup-1][j]);
if(tmp!=LINF)
{
dp[nGroup][i]=min(dp[nGroup][i],tmp+a[i]);
}
}
}
cout<<dp[k][n];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen(taskname".INP","r",stdin);
#endif // ONLINE_JUDGE
ReadInput();
Solve();
return 0;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:114:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP","r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |