#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod=1e9+7;
const int M=1e6+10;
const int N=1e3+10;
int n,k,f[100010][110],a[100010];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task "1"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k;
fori(i,1,n) cin >> a[i];
memset(f,61,sizeof(f));
f[0][1]=0;
fori(i,1,n) f[i][1]=max(f[i-1][1],a[i]);
f[0][1]=1e16;
fori(j,2,k)
{
stack<int> sk;
fori(i,1,n)
{
f[i][j]=f[i-1][j-1]+a[i];//cout << f[i][j] << " ";
while(!sk.empty()&&a[sk.top()]<=a[i])
{
f[i][j]=min(f[i][j],f[sk.top()+1][j-1]+a[i]);
f[i][j]=min(f[i][j],f[sk.top()][j]+a[i]-a[sk.top()]);
sk.pop();
}
if(sk.size())
{
f[i][j]=min(f[i][j],f[sk.top()+1][j-1]+a[i]);
f[i][j]=min(f[i][j],f[sk.top()][j]);
}
sk.push(i);
}
}
cout << f[n][k];
}
Compilation message
blocks.cpp: In function 'int32_t main()':
blocks.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
86352 KB |
Output is correct |
2 |
Correct |
45 ms |
86352 KB |
Output is correct |
3 |
Correct |
42 ms |
86536 KB |
Output is correct |
4 |
Correct |
43 ms |
86436 KB |
Output is correct |
5 |
Correct |
43 ms |
86356 KB |
Output is correct |
6 |
Correct |
42 ms |
86516 KB |
Output is correct |
7 |
Correct |
42 ms |
86416 KB |
Output is correct |
8 |
Correct |
47 ms |
86356 KB |
Output is correct |
9 |
Correct |
48 ms |
86404 KB |
Output is correct |
10 |
Incorrect |
42 ms |
86352 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
86356 KB |
Output is correct |
2 |
Correct |
40 ms |
86356 KB |
Output is correct |
3 |
Correct |
42 ms |
86356 KB |
Output is correct |
4 |
Correct |
42 ms |
86364 KB |
Output is correct |
5 |
Correct |
46 ms |
86340 KB |
Output is correct |
6 |
Correct |
43 ms |
86368 KB |
Output is correct |
7 |
Correct |
41 ms |
86352 KB |
Output is correct |
8 |
Correct |
43 ms |
86472 KB |
Output is correct |
9 |
Correct |
41 ms |
86560 KB |
Output is correct |
10 |
Incorrect |
40 ms |
86352 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
86352 KB |
Output is correct |
2 |
Correct |
45 ms |
86352 KB |
Output is correct |
3 |
Correct |
42 ms |
86536 KB |
Output is correct |
4 |
Correct |
43 ms |
86436 KB |
Output is correct |
5 |
Correct |
43 ms |
86356 KB |
Output is correct |
6 |
Correct |
42 ms |
86516 KB |
Output is correct |
7 |
Correct |
42 ms |
86416 KB |
Output is correct |
8 |
Correct |
47 ms |
86356 KB |
Output is correct |
9 |
Correct |
48 ms |
86404 KB |
Output is correct |
10 |
Incorrect |
42 ms |
86352 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
86352 KB |
Output is correct |
2 |
Correct |
45 ms |
86352 KB |
Output is correct |
3 |
Correct |
42 ms |
86536 KB |
Output is correct |
4 |
Correct |
43 ms |
86436 KB |
Output is correct |
5 |
Correct |
43 ms |
86356 KB |
Output is correct |
6 |
Correct |
42 ms |
86516 KB |
Output is correct |
7 |
Correct |
42 ms |
86416 KB |
Output is correct |
8 |
Correct |
47 ms |
86356 KB |
Output is correct |
9 |
Correct |
48 ms |
86404 KB |
Output is correct |
10 |
Incorrect |
42 ms |
86352 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |