#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];
int64_t st[4*N];
void ReadInput()
{
cin>>n>>k;
forinc(i,1,n) cin>>a[i];
}
void Update(int id,int l,int r,int i,int64_t val)
{
if(r<i||l>i) return;
if(l==r)
{
st[id]=val;
return;
}
Update(2*id,l,(l+r)/2,i,val);
Update(2*id+1,(l+r)/2+1,r,i,val);
st[id]=min(st[2*id],st[2*id+1]);
}
int64_t Query(int id,int l,int r,int i,int j)
{
if(r<i||l>j) return LINF;
if(i<=l&&r<=j) return st[id];
return min(Query(2*id,l,(l+r)/2,i,j),Query(2*id+1,(l+r)/2+1,r,i,j));
}
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;
Update(1,1,n,i,dp[1][i]);
}
forinc(nGroup,2,k)
{
forinc(i,nGroup,n)
{
if(left[i]) dp[nGroup][i]=dp[nGroup][left[i]];
if(max(1,left[i])<=i-1) dp[nGroup][i]=min(dp[nGroup][i],Query(1,1,n,max(1,left[i]),i-1)+a[i]);
}
forinc(i,1,n) Update(1,1,n,i,dp[nGroup][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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
296 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
0 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
1016 KB |
Output is correct |
16 |
Correct |
3 ms |
1016 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
1016 KB |
Output is correct |
19 |
Correct |
3 ms |
1020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
2808 KB |
Output is correct |
2 |
Correct |
16 ms |
2424 KB |
Output is correct |
3 |
Correct |
104 ms |
4600 KB |
Output is correct |
4 |
Execution timed out |
1092 ms |
28792 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |