This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
{
while(!sk.empty()&&a[sk.top()]<=a[i])
{
f[i][j]=min(f[i][j],f[sk.top()][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()][j-1]+a[i]);
f[i][j]=min(f[i][j],f[sk.top()][j]);
}
sk.push(i);
}
}
cout << f[n][k];
}
Compilation message (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |