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 ll long long
#define int long long
#define el "\n"
#define FILE "test1"
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define bit(mask,i) ((mask>>i) & 1)
const int N=1e5+2;
const ll oo=1e18;
const int mod=1e9+7;
const int base=31;
const int dx[4]={0, 0, 1, -1};
const int dy[4]={1, -1, 0, 0};
using namespace std;
int n,k,a[N],dp[N][102],rmq[N][25],lg2[N];
void kt()
{
for (int i=2;i<=n;i++)
lg2[i]=lg2[i>>1]+1;
for (int i=1;i<=n;i++)
rmq[i][0]=a[i];
for (int k=1;(1<<k)<=n;k++)
for (int i=1;i<=n-(1<<k)+1;i++)
rmq[i][k]=max(rmq[i][k-1],rmq[i+(1<<(k-1))][k-1]);
}
ll get(int l,int r)
{
int k=lg2[r-l+1];
return max(rmq[l][k],rmq[r-(1<<k)+1][k]);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
if (fopen(FILE".inp","r"))
{
freopen(FILE".inp","r",stdin);
freopen(FILE".out","w",stdout);
}
cin>>n>>k;
for (int i=1;i<=n;i++)
cin>>a[i];
kt();
memset(dp,0x3f,sizeof dp);
dp[0][0]=0;
for (int i=1;i<=k;i++)
for (int j=i;j<=n;j++)
for (int l=max(0ll,j-100);l<j;l++)
dp[j][i]=min(dp[j][i],dp[l][i-1]+get(l+1,j));
cout<<dp[n][k];
}
/** /\_/\
* (= ._.)
* / >AC \>AC
**/
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:44:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen(FILE".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:45:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen(FILE".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... |